aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-06-20 07:20:32 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-06-20 07:20:32 +0200
commitcb4b8c02c68aa18170e74a96f840e3613970af22 (patch)
treee968eb62113bc3dd96c297958c65a80973954aca
parent06e915be138b0638e30083f84cecda0eb1bfc895 (diff)
Extend trim() to treat newline as whitespace
-rw-r--r--libbutl/utility.cxx15
-rw-r--r--libbutl/utility.mxx2
2 files changed, 9 insertions, 8 deletions
diff --git a/libbutl/utility.cxx b/libbutl/utility.cxx
index 502586c..1212aa1 100644
--- a/libbutl/utility.cxx
+++ b/libbutl/utility.cxx
@@ -104,17 +104,18 @@ namespace butl
assert (trim (r = "a \r") == "a");
assert (trim (r = " a ") == "a");
assert (trim (r = " \ta \r") == "a");
+ assert (trim (r = "\na\n") == "a");
*/
- size_t i (0), n (l.size ());
+ auto ws = [] (char c )
+ {
+ return c == ' ' || c == '\t' || c == '\n' || c == '\r';
+ };
- for (char c;
- i != n && ((c = l[i]) == ' ' || c == '\t' || c == '\r');
- ++i) ;
+ size_t i (0), n (l.size ());
- for (char c;
- n != i && ((c = l[n - 1]) == ' ' || c == '\t' || c == '\r');
- --n) ;
+ for (; i != n && ws (l[i]); ++i) ;
+ for (; n != i && ws (l[n - 1]); --n) ;
if (i != 0)
{
diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx
index 252a4b4..ee2aed7 100644
--- a/libbutl/utility.mxx
+++ b/libbutl/utility.mxx
@@ -140,7 +140,7 @@ LIBBUTL_MODEXPORT namespace butl
// Basic string utilities.
//
- // Trim leading/trailing whitespacec, including '\r'.
+ // Trim leading/trailing whitespaces, including '\n' and '\r'.
//
LIBBUTL_SYMEXPORT std::string&
trim (std::string&);