Skip to content

Instantly share code, notes, and snippets.

@XuhuaHuang
Created March 5, 2022 15:29
Show Gist options
  • Save XuhuaHuang/072149f2ceff9ea4ddd58d0e9526216f to your computer and use it in GitHub Desktop.
Save XuhuaHuang/072149f2ceff9ea4ddd58d0e9526216f to your computer and use it in GitHub Desktop.
String trim operation with C++20 views.
inline constexpr auto trim_front = views::drop_while(::isspace);
inline constexpr auto trim_back = views::reverse
| views::drop_while(::isspace)
| views::reverse;
inline constexpr auto trim = trim_front | trim_back;
std::string trim_str(const std::string& str) {
return str | trim | rangesnext::to<std::string>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment