Ignore:
Timestamp:
07/20/20 14:15:14 (4 years ago)
Author:
Maciej Komosinski
Message:

Added whitespace-trimming functions for std::string

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/util-string.cpp

    r913 r1016  
    7979bool str_starts_with(const char *str, const char *prefix)
    8080{
    81         return strncmp(str,prefix,strlen(prefix))==0;
     81        return strncmp(str, prefix, strlen(prefix)) == 0;
    8282}
    8383
     
    132132        return filename.substr(0, slash);
    133133}
     134
     135//trimming functions, https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
     136
     137void ltrim_inplace(string &s)
     138{
     139        s.erase(s.begin(), find_if(s.begin(), s.end(), [](int ch) {
     140                return !isspace(ch);
     141                }));
     142}
     143
     144void rtrim_inplace(string &s)
     145{
     146        s.erase(find_if(s.rbegin(), s.rend(), [](int ch) {
     147                return !isspace(ch);
     148                }).base(), s.end());
     149}
     150
     151void trim_inplace(string &s)
     152{
     153        ltrim_inplace(s);
     154        rtrim_inplace(s);
     155}
     156
     157string ltrim(string s)
     158{
     159        ltrim_inplace(s);
     160        return s;
     161}
     162
     163string rtrim(string s)
     164{
     165        rtrim_inplace(s);
     166        return s;
     167}
     168
     169string trim(string s)
     170{
     171        trim_inplace(s);
     172        return s;
     173}
Note: See TracChangeset for help on using the changeset viewer.