diff options
| author | Alexander Karatarakis <alkarata@microsoft.com> | 2016-12-15 17:09:14 -0800 |
|---|---|---|
| committer | Alexander Karatarakis <alkarata@microsoft.com> | 2016-12-15 17:09:14 -0800 |
| commit | 8f397bb8d1bd05a2e1f6d5de808322364100ae5d (patch) | |
| tree | 10fbe68aa55b5ad681cc183c002cacbb8870fb49 | |
| parent | b2859c65121c2c2593287be33ee87750fdd7d40e (diff) | |
| download | vcpkg-8f397bb8d1bd05a2e1f6d5de808322364100ae5d.tar.gz vcpkg-8f397bb8d1bd05a2e1f6d5de808322364100ae5d.zip | |
Add Strings::trim() function
| -rw-r--r-- | toolsrc/include/vcpkg_Strings.h | 4 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg_Strings.cpp | 20 |
2 files changed, 24 insertions, 0 deletions
diff --git a/toolsrc/include/vcpkg_Strings.h b/toolsrc/include/vcpkg_Strings.h index 859da5658..92c24298c 100644 --- a/toolsrc/include/vcpkg_Strings.h +++ b/toolsrc/include/vcpkg_Strings.h @@ -69,4 +69,8 @@ namespace vcpkg {namespace Strings std::string ascii_to_lowercase(const std::string& input); std::string join(const std::vector<std::string>& v, const std::string& delimiter); + + void trim(std::string* s); + + std::string trimmed(const std::string& s); }} diff --git a/toolsrc/src/vcpkg_Strings.cpp b/toolsrc/src/vcpkg_Strings.cpp index 19ba8595f..4859a480b 100644 --- a/toolsrc/src/vcpkg_Strings.cpp +++ b/toolsrc/src/vcpkg_Strings.cpp @@ -4,9 +4,16 @@ #include <algorithm> #include <codecvt> #include <iterator> +#include <functional> +#include <cctype> namespace vcpkg {namespace Strings {namespace details { + static const auto isspace = [](const char c) + { + return std::isspace(c); + }; + std::string format_internal(const char* fmtstr, ...) { va_list lst; @@ -85,4 +92,17 @@ namespace vcpkg {namespace Strings return output; } + + void trim(std::string* s) + { + s->erase(s->begin(), std::find_if_not(s->begin(), s->end(), details::isspace)); + s->erase(std::find_if_not(s->rbegin(), s->rend(), details::isspace).base(), s->end()); + } + + std::string trimmed(const std::string& s) + { + auto whitespace_front = std::find_if_not(s.begin(), s.end(), details::isspace); + auto whitespace_back = std::find_if_not(s.rbegin(), s.rend(), details::isspace).base(); + return (whitespace_back <= whitespace_front ? std::string() : std::string(whitespace_front, whitespace_back)); + } }} |
