Skip to content

Instantly share code, notes, and snippets.

@gnzlbg
Last active April 3, 2017 15:29
Show Gist options
  • Save gnzlbg/f03ce2649c97ee9c81bed27d26fa4498 to your computer and use it in GitHub Desktop.
Save gnzlbg/f03ce2649c97ee9c81bed27d26fa4498 to your computer and use it in GitHub Desktop.
char ranges:
/// Is \p c an end-of-line character?
///
/// On Linux `\n` and on Windows `\r\n`.
template <typename Char>
constexpr bool is_eol(Char c, Char n = Char{}) noexcept {
return c == '\n' || (c == '\r' && n == '\n');
}
/// Remove EOL characters from the \p line.
template <typename CharStr,
CONCEPT_REQUIRES_(!std::is_rvalue_reference<CharStr&&>())>
constexpr auto strip_eol(CharStr&& chars) noexcept {
auto eol_it = ranges::adjacent_find(chars, [](auto&& c, auto&& n) { return is_eol(c, n); });
return ranges::make_iterator_range(ranges::begin(chars), eol_it);
}
{ // tests:
test::check_equal(strip_eol(" a b \n"), view::c_str(" a b "));
test::check_equal(strip_eol("a b c \r\n"), view::c_str("a b c "));
test::check_equal(strip_eol(" c d\n"), view::c_str(" c d"));
test::check_equal(strip_eol("c d e\r\n"), view::c_str("c d e"));
test::check_equal(strip_eol(" d e "), view::c_str(" d e "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment