From eba3042910f063ae638a7e0134b79175978e2fca Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 28 Apr 2018 16:34:57 +0200 Subject: Move trim(), next_word() basic string utilities from build2 --- libbutl/utility.mxx | 64 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 15 deletions(-) (limited to 'libbutl/utility.mxx') diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx index 4c83827..b957282 100644 --- a/libbutl/utility.mxx +++ b/libbutl/utility.mxx @@ -57,21 +57,6 @@ LIBBUTL_MODEXPORT namespace butl [[noreturn]] LIBBUTL_SYMEXPORT void throw_system_error (int system_code, int fallback_errno_code = 0); - // If an input stream is in a failed state, then return true if this is - // because of the eof and throw istream::failure otherwise. If the stream - // is not in a failed state, return false. This helper function is normally - // used like this: - // - // is.exceptions (istream::badbit); - // - // for (string l; !eof (getline (is, l)); ) - // { - // ... - // } - // - bool - eof (std::istream&); - // Convert ASCII character/string case. If there is no upper/lower case // counterpart, leave the character unchanged. The POSIX locale (also known // as C locale) must be the current application locale. Otherwise the @@ -149,6 +134,55 @@ LIBBUTL_MODEXPORT namespace butl bool alnum (wchar_t); bool xdigit (wchar_t); + // Basic string utilities. + // + + // Trim leading/trailing whitespacec, including '\r'. + // + std::string& + trim (std::string&); + + // Find the beginning and end poistions of the next word. Return the size + // of the word or 0 and set b = e = n if there are no more words. For + // example: + // + // for (size_t b (0), e (0); next_word (s, b, e); ) + // { + // string w (s, b, e - b); + // } + // + // Or: + // + // for (size_t b (0), e (0), n; n = next_word (s, b, e, ' ', ','); ) + // { + // string w (s, b, n); + // } + // + // The second version examines up to the n'th character in the string. + // + std::size_t + next_word (const std::string&, std::size_t& b, std::size_t& e, + char d1 = ' ', char d2 = '\0'); + + std::size_t + next_word (const std::string&, std::size_t n, std::size_t& b, std::size_t& e, + char d1 = ' ', char d2 = '\0'); + + // If an input stream is in a failed state, then return true if this is + // because of the eof and throw istream::failure otherwise. If the stream + // is not in a failed state, return false. This helper function is normally + // used like this: + // + // is.exceptions (istream::badbit); + // + // for (string l; !eof (getline (is, l)); ) + // { + // ... + // } + // + bool + eof (std::istream&); + // Key comparators (i.e., to be used in sets, maps, etc). // struct compare_c_string -- cgit v1.1