From 21725c00917f0fa8b587f4c7b996f26fcc7eb274 Mon Sep 17 00:00:00 2001 From: Georgios Katsikas Date: Wed, 20 Sep 2017 13:10:14 +0200 Subject: [PATCH] Method for trimming a string's spaces from start --- include/click/string.hh | 1 + lib/string.cc | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/include/click/string.hh b/include/click/string.hh index 969a82059a..d2a0a56749 100644 --- a/include/click/string.hh +++ b/include/click/string.hh @@ -100,6 +100,7 @@ class String { public: String substring(int pos, int len) const; inline String substring(int pos) const; String trim_space() const; + String trim_space_left() const; inline bool equals(const String &x) const; inline bool equals(const char *s, int len) const; diff --git a/lib/string.cc b/lib/string.cc index 0dd6985760..aa394fa8bd 100644 --- a/lib/string.cc +++ b/lib/string.cc @@ -819,6 +819,16 @@ String::trim_space() const return String(); } +/** @brief Return a substring with spaces trimmed from the start. */ +String +String::trim_space_left() const +{ + for (int i = 0 ; i <= _r.length - 1; i++) + if (!isspace((unsigned char) _r.data[i])) + return substring(i); + return String(); +} + /** @brief Return a hex-quoted version of the string. For example, the string "Abcd" would convert to "\<41626364>". */