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.ixx | 56 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'libbutl/utility.ixx') diff --git a/libbutl/utility.ixx b/libbutl/utility.ixx index fcb8789..24271e4 100644 --- a/libbutl/utility.ixx +++ b/libbutl/utility.ixx @@ -4,18 +4,6 @@ namespace butl { - inline bool - eof (std::istream& is) - { - if (!is.fail ()) - return false; - - if (is.eof ()) - return true; - - throw std::istream::failure (""); - } - inline char ucase (char c) { @@ -167,4 +155,48 @@ namespace butl { return std::iswxdigit (c); } + + inline std::size_t + next_word (const std::string& s, std::size_t& b, std::size_t& e, + char d1, char d2) + { + return next_word (s, s.size (), b, e, d1, d2); + } + + inline size_t + next_word (const std::string& s, + std::size_t n, std::size_t& b, std::size_t& e, + char d1, char d2) + { + if (b != e) + b = e; + + // Skip leading delimiters. + // + for (; b != n && (s[b] == d1 || s[b] == d2); ++b) ; + + if (b == n) + { + e = n; + return 0; + } + + // Find first trailing delimiter. + // + for (e = b + 1; e != n && s[e] != d1 && s[e] != d2; ++e) ; + + return e - b; + } + + inline bool + eof (std::istream& is) + { + if (!is.fail ()) + return false; + + if (is.eof ()) + return true; + + throw std::istream::failure (""); + } } -- cgit v1.1