Changeset 1016 for cpp/common


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

Added whitespace-trimming functions for std::string

Location:
cpp/common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd.h

    r936 r1016  
    8080
    8181
     82
     83#if defined(__ANDROID__) && !defined(PRIuPTR)
     84#define PRIuPTR "u" //w ktorejs z wersji kompilacji 32 albo 64 bit pewnie brakuje tej definicji w naglowku <inttypes.h>, a w tej drugiej wersji jest. Nie wiem czy "u" jest wlasciwe ale i tak prawdopodobnie naprawili w nowszym ndk...
    8285#endif
     86
     87
     88#endif
  • 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}
  • cpp/common/util-string.h

    r913 r1016  
    2121string stripFileDir(const string& filename); // strip path component from filename
    2222
     23void ltrim_inplace(string &s);
     24void rtrim_inplace(string &s);
     25void trim_inplace(string &s);
     26string ltrim(string s);
     27string rtrim(string s);
     28string trim(string s);
     29
    2330#endif
Note: See TracChangeset for help on using the changeset viewer.