Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 11111


Ignore:
Timestamp:
Feb 12, 2016, 1:05:41 PM (8 years ago)
Author:
muemart
Message:

Some simplifications in StringUtils.cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/libraries/util/StringUtils.cc

    r11097 r11111  
    3434#include "StringUtils.h"
    3535
     36#include <algorithm>
    3637#include <cctype>
    3738#include <ctime>
     
    5455    void strip(std::string* str)
    5556    {
    56         size_t pos;
    57         while ((pos = str->find(' ')) < str->length())
    58             str->erase(pos, 1);
    59         while ((pos = str->find('\t')) < str->length())
    60             str->erase(pos, 1);
    61         while ((pos = str->find('\n')) < str->length())
    62             str->erase(pos, 1);
     57        str->erase(std::remove_if(str->begin(), str->end(), [](char val) { return std::isspace(val) != 0; }), str->end());
    6358    }
    6459
     
    7469    std::string removeTrailingWhitespaces(const std::string& str)
    7570    {
    76         size_t pos1 = 0;
    77         int pos2 = static_cast<int>(str.size() - 1);
    78         for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++);
    79         for (; pos2 > 0         && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--);
    80         return str.substr(pos1, pos2 - pos1 + 1);
     71        auto pos1 = std::find_if(str.begin(), str.end(), [](char val) { return std::isspace(val) == 0; });
     72        auto pos2 = std::find_if(str.rbegin(), str.rend(), [](char val) { return std::isspace(val) == 0; });
     73        if (pos1 == str.end() && pos2 == str.rend())
     74        {
     75            // String doesn't have non-whitespace characters
     76            return "";
     77        }
     78        return std::string(pos1, pos2.base());
    8179    }
    8280
     
    162160    /**
    163161        @brief Removes enclosing quotation marks if available (including whitespaces at the outside of the quotation marks).
    164         @return The striped string without quotation marks
     162        @return The stripped string without quotation marks
    165163    */
    166164    std::string stripEnclosingQuotes(const std::string& str)
    167165    {
    168         size_t start = std::string::npos;
    169         size_t end = 0;
    170 
    171         for (size_t pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++)
    172         {
    173             if (str[pos] == '"')
    174             {
    175                 start = pos;
    176                 break;
    177             }
    178 
    179             if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
     166        auto start = str.begin();
     167        auto end = str.rbegin();
     168
     169        for (; start != str.end() && *start != '"'; ++start)
     170        {
     171            if (std::isspace(*start) == 0)
    180172                return str;
    181173        }
    182174
    183         for (size_t pos = str.size() - 1; pos < std::string::npos; pos--)
    184         {
    185             if (str[pos] == '"')
    186             {
    187                 end = pos;
    188                 break;
    189             }
    190 
    191             if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
     175        for (; end != str.rend() && *end != '"'; ++end)
     176        {
     177            if (std::isspace(*end) == 0)
    192178                return str;
    193179        }
    194180
    195         if ((start != std::string::npos) && (end != 0))
    196             return str.substr(start + 1, end - start - 1);
     181        if (start != str.end() && end != str.rend())
     182        {
     183            ++start; // Skip "
     184            ++end; // Skip "
     185            return std::string(start, end.base());
     186        }
    197187        else
    198188            return str;
     
    201191    /**
    202192        @brief Removes enclosing braces '{' and '}' (the braces must be exactly on the beginning and the end of the string).
    203         @return The striped string without braces
     193        @return The stripped string without braces
    204194    */
    205195    std::string stripEnclosingBraces(const std::string& str)
    206196    {
    207         std::string output = str;
    208 
    209         while (output.size() >= 2 && output[0] == '{' && output[output.size() - 1] == '}')
    210             output = output.substr(1, output.size() - 2);
    211 
    212         return output;
     197        if (str.empty())
     198        {
     199            return "";
     200        }
     201
     202        auto start = str.begin();
     203        auto end = str.rbegin();
     204
     205        while (*start == '{' && *end == '}')
     206        {
     207            ++start;
     208            ++end;
     209        }
     210
     211        return std::string(start, end.base());
    213212    }
    214213
     
    245244    bool isEmpty(const std::string& str)
    246245    {
    247         return getStripped(str).empty();
     246        return std::all_of(str.begin(), str.end(), [](char val) { return std::isspace(val) != 0; });
    248247    }
    249248
     
    337336    void lowercase(std::string* str)
    338337    {
    339         for (char& character : *str)
    340         {
    341             character = static_cast<char>(tolower(character));
    342         }
     338        std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::tolower(val); });
    343339    }
    344340
     
    354350    void uppercase(std::string* str)
    355351    {
    356         for (char& character : *str)
    357         {
    358             character = static_cast<char>(toupper(character));
    359         }
     352        std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::toupper(val); });
    360353    }
    361354
     
    370363    /**
    371364        @brief Compares two strings ignoring different casing.
    372         @return s1 == s1 -> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1
     365        @return s1 == s2 -> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1
    373366    */
    374367    int nocaseCmp(const std::string& s1, const std::string& s2)
    375368    {
    376         std::string::const_iterator it1=s1.begin();
    377         std::string::const_iterator it2=s2.begin();
    378 
    379         //stop when either string's end has been reached
    380         while ( (it1!=s1.end()) && (it2!=s2.end()) )
    381         {
    382             if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
    383                 // return -1 to indicate smaller than, 1 otherwise
    384                 return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
    385             //proceed to the next character in each string
    386             ++it1;
    387             ++it2;
    388         }
    389         size_t size1=s1.size(), size2=s2.size();// cache lengths
     369        size_t size1 = s1.size(), size2 = s2.size(); // cache lengths
     370
     371        int res = nocaseCmp(s1, s2, std::min(size1, size2));
     372
     373        if (res != 0)
     374        {
     375            return res;
     376        }
     377
    390378        //return -1,0 or 1 according to strings' lengths
    391         if (size1==size2)
     379        if (size1 == size2)
    392380            return 0;
    393         return (size1<size2) ? -1 : 1;
     381        return (size1 < size2) ? -1 : 1;
     382
    394383    }
    395384
     
    405394        if (len == 0)
    406395            return 0;
    407         std::string::const_iterator it1=s1.begin();
    408         std::string::const_iterator it2=s2.begin();
     396        std::string::const_iterator it1 = s1.begin();
     397        std::string::const_iterator it2 = s2.begin();
    409398
    410399        //stop when either string's end has been reached
    411         while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0)
    412         {
    413             if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
     400        while ( (it1 != s1.end()) && (it2 != s2.end()) && len-- > 0)
     401        {
     402            if(std::toupper(*it1) != std::toupper(*it2)) //letters differ?
    414403                // return -1 to indicate smaller than, 1 otherwise
    415                 return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
     404                return (std::toupper(*it1)  < std::toupper(*it2)) ? -1 : 1;
    416405            //proceed to the next character in each string
    417406            ++it1;
     
    508497    /**
    509498    @brief
    510         Get a timestamp for the curent time instant.
     499        Get a timestamp for the current time instant.
    511500    @return
    512501        Returns a string with the timestamp.
     
    514503    std::string getTimestamp(void)
    515504    {
    516         struct tm *pTime;
    517505        time_t ctTime; std::time(&ctTime);
    518         pTime = std::localtime( &ctTime );
     506        tm* pTime = std::localtime(&ctTime);
    519507        std::ostringstream oss;
    520         oss << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1)
    521             << std::setw(2) << std::setfill('0') << pTime->tm_mday
    522             << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900)
    523             << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour
    524             << std::setw(2) << std::setfill('0') << pTime->tm_min
    525             << std::setw(2) << std::setfill('0') << pTime->tm_sec;
     508        oss << std::put_time(pTime, "%m%d%Y_%H%M%S");
    526509        return oss.str();
    527510    }
Note: See TracChangeset for help on using the changeset viewer.