diff --git a/src/utils.cpp b/src/utils.cpp index 3b1426c..2c7ee56 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -21,6 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +#include +#include #include "utils.h" std::vector strsplit(const std::string& str, const std::string& delim) { @@ -41,3 +43,22 @@ void strtolower(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); }); } + +std::string& ltrim(std::string& str) +{ + auto it = std::find_if(str.begin(), str.end(), [](char ch){ return !std::isspace(ch); }); + str.erase(str.begin(), it); + return str; +} + +std::string& rtrim(std::string& str) +{ + auto it = std::find_if(str.rbegin(), str.rend(), [](char ch){ return !std::isspace(ch); }); + str.erase(it.base(), str.end()); + return str; +} + + +std::string& trim(std::string& str) { + return ltrim(rtrim(str)); +} diff --git a/src/utils.h b/src/utils.h index 120bf7c..df5aac6 100644 --- a/src/utils.h +++ b/src/utils.h @@ -31,4 +31,8 @@ std::vector strsplit(const std::string& str, const std::string& del void strtolower(std::string& str); +std::string& rtrim(std::string& str); +std::string& ltrim(std::string& str); +std::string& trim(std::string& str); + #endif /* UTILS_H */