aboutsummaryrefslogtreecommitdiff
path: root/libbutl/utility.ixx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-04-28 16:34:57 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-04-28 16:34:57 +0200
commiteba3042910f063ae638a7e0134b79175978e2fca (patch)
treec17863f5f87f578cdb80736154e22a4f1d187205 /libbutl/utility.ixx
parentb32579bad74e1b7f3a719fa49aad8566d20a6d2b (diff)
Move trim(), next_word() basic string utilities from build2
Diffstat (limited to 'libbutl/utility.ixx')
-rw-r--r--libbutl/utility.ixx56
1 files changed, 44 insertions, 12 deletions
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 ("");
+ }
}