From d4a41cc98cede1e692d9f8e8c4e121338519bd4b Mon Sep 17 00:00:00 2001 From: codereader Date: Wed, 18 Oct 2017 20:32:00 +0200 Subject: [PATCH] Add string trim function replacements. --- libs/string/trim.h | 128 ++++++++++++++++++++++++++++++++ tools/msvc/libs.vcxproj | 1 + tools/msvc/libs.vcxproj.filters | 3 + 3 files changed, 132 insertions(+) create mode 100644 libs/string/trim.h diff --git a/libs/string/trim.h b/libs/string/trim.h new file mode 100644 index 0000000000..f8fb6d4e8a --- /dev/null +++ b/libs/string/trim.h @@ -0,0 +1,128 @@ +#pragma once + +#include + +namespace string +{ + +/** +* Removes all characters from the beginning of the string matching the given predicate, +* modifying the given string in-place. +*/ +template +inline void trim_left_if(std::string& subject, Predicate predicate) +{ + // Erase everything from the beginning up to the first character + // that is not matching the predicate (e.g. is not a space) + subject.erase(subject.begin(), + std::find_if(subject.begin(), subject.end(), [&](int ch) { return !predicate(ch); })); +} + +/** +* Removes all characters from the end of the string matching the given predicate, +* modifying the given string in-place. +*/ +template +inline void trim_right_if(std::string& subject, Predicate predicate) +{ + // Erase everything from the end up to the nearest-to-last character + // that is not matching the predicate (e.g. is not a space) + subject.erase( + std::find_if(subject.rbegin(), subject.rend(), [&](int ch) { return !predicate(ch); }).base(), + subject.end()); +} + +/** +* Removes all characters from the beginning and the end of the string +* matching the given predicate, returning a new string containing of the result. +*/ +template +inline std::string trim_copy_if(std::string subject, Predicate predicate) +{ + trim_left_if(subject, predicate); + trim_right_if(subject, predicate); + + return subject; +} + +/** +* Removes all space characters from the beginning and the end of the string, in-place. +*/ +inline void trim(std::string& subject) +{ + trim_left_if(subject, ::isspace); + trim_right_if(subject, ::isspace); +} + +/** +* Removes all space characters from the beginning and the end of the string +* and returns a new string containing of the result. +*/ +inline std::string trim_copy(std::string subject) +{ + trim_left_if(subject, ::isspace); + trim_right_if(subject, ::isspace); + + return subject; +} + +/** +* Removes all of the given characters from the beginning and the end of the subject, in-place. +*/ +inline void trim(std::string& subject, const std::string& charsToBeRemoved) +{ + trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); + trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); +} + +/** +* Removes all of the given characters from the beginning and the end of the subject, +* returning a new string containing the result. +*/ +inline std::string trim_copy(std::string subject, const std::string& charsToBeRemoved) +{ + trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); + trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); + + return subject; +} + +/** +* Removes all of the given characters from the beginning of the subject, in-place +*/ +inline void trim_left(std::string& subject, const std::string& charsToBeRemoved) +{ + trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); +} + +/** +* Removes all of the given characters from the beginning of the subject, +* returning a new string containing the result. +*/ +inline std::string trim_left_copy(std::string subject, const std::string& charsToBeRemoved) +{ + trim_left_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); + + return subject; +} + +/** +* Removes all of the given characters from the end of the subject, in-place +*/ +inline void trim_right(std::string& subject, const std::string& charsToBeRemoved) +{ + trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); +} + +/** +* Removes all of the given characters from the end of the subject, +* returning a new string containing the result. +*/ +inline std::string trim_right_copy(std::string subject, const std::string& charsToBeRemoved) +{ + trim_right_if(subject, [&](int ch) { return charsToBeRemoved.find(ch) != std::string::npos; }); + + return subject; +} + +} diff --git a/tools/msvc/libs.vcxproj b/tools/msvc/libs.vcxproj index 3d1f6fbc96..c8a53038c6 100644 --- a/tools/msvc/libs.vcxproj +++ b/tools/msvc/libs.vcxproj @@ -204,6 +204,7 @@ + diff --git a/tools/msvc/libs.vcxproj.filters b/tools/msvc/libs.vcxproj.filters index 9d948393c2..8601be31b6 100644 --- a/tools/msvc/libs.vcxproj.filters +++ b/tools/msvc/libs.vcxproj.filters @@ -169,6 +169,9 @@ string + + string +