LCOV - code coverage report
Current view: top level - Utils - str_utils.cpp (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 31.6 % 79 25
Test Date: 2026-11-19 04:56:22 Functions: 29.0 % 31 9
Legend: Lines: hit not hit

            Line data    Source code
       1              : #include "str_utils.h"
       2              : #include <iostream>
       3              : #include <string>
       4              : #include <algorithm>
       5              : #include <vector>
       6              : 
       7              : 
       8              : namespace Utils
       9              : {
      10              : 
      11              :     static bool case_insensitive_compare(const std::string& s1, const std::string& s2);
      12              : 
      13              :     //! std::isspace(char) can only handle chars in the range [0,255] 
      14              :     //! so we need to cast the it's argument to unsigned char
      15              :     // trim from start (in place)
      16     45442895 :     std::string& str_ltrim(std::string& s)
      17              :     {
      18     45442895 :         auto it = std::find_if(s.begin(), s.end(),
      19     45730627 :             [](unsigned char c) {
      20     45730627 :                 return !std::isspace(c);
      21              :             });
      22     45442895 :         s.erase(s.begin(), it);
      23     45442895 :         return s;
      24              :     }
      25              : 
      26              :     // trim from end (in place)
      27     45442895 :     std::string& str_rtrim(std::string& s)
      28              :     {
      29     45442895 :         auto it = std::find_if(s.rbegin(), s.rend(),
      30     47637467 :             [](unsigned char c) {
      31     47637467 :                 return !std::isspace(c);
      32     45442895 :             });
      33     45442895 :         s.erase(it.base(), s.end());
      34     45442895 :         return s;
      35              :     }
      36              : 
      37              :     // trim from both ends (in place)
      38     45442895 :     std::string& str_trim(std::string& s)
      39              :     {
      40     45442895 :         return str_ltrim(str_rtrim(s));
      41              :     }
      42              : 
      43           10 :     std::string& str_ltrim(std::string& s, /*unsigned*/ char toRemove)
      44              :     {
      45           10 :         auto it = std::find_if(s.begin(), s.end(),
      46           20 :             [toRemove](/*unsigned*/ char c) {
      47           20 :                 return c != toRemove;
      48              :             });
      49           10 :         s.erase(s.begin(), it);
      50           10 :         return s;
      51              :     }
      52              : 
      53              :     // trim from end (in place)
      54            0 :     std::string& str_rtrim(std::string& s, /*unsigned*/ char toRemove)
      55              :     {
      56            0 :         auto it = std::find_if(s.rbegin(), s.rend(),
      57            0 :             [toRemove](/*unsigned*/ char c) {
      58            0 :                 return c != toRemove;
      59            0 :             });
      60            0 :         s.erase(it.base(), s.end());
      61            0 :         return s;
      62              :     }
      63              : 
      64              :     // trim from both ends (in place)
      65            0 :     std::string& str_trim(std::string& s, /*unsigned*/ char toRemove)
      66              :     {
      67            0 :         return str_ltrim(str_rtrim(s, toRemove), toRemove);
      68              :     }
      69              : 
      70              :     // trim from start (copying)
      71            0 :     std::string str_ltrim_copy(const std::string& s)
      72              :     {
      73            0 :         auto copy(s);
      74            0 :         return str_ltrim(copy);
      75            0 :     }
      76              : 
      77              :     // trim from end (copying)
      78            0 :     std::string str_rtrim_copy(const std::string& s)
      79              :     {
      80            0 :         auto copy(s);
      81            0 :         return str_rtrim(copy);
      82            0 :     }
      83              : 
      84              :     // trim from both ends (copying)
      85            0 :     std::string str_trim_copy(const std::string& s)
      86              :     {
      87            0 :         auto copy(s);
      88            0 :         return str_trim(copy);
      89            0 :     }
      90              : 
      91            0 :     std::string str_ltrim_copy(const std::string& s, /*unsigned*/ char toRemove)
      92              :     {
      93            0 :         auto copy(s);
      94            0 :         return str_ltrim(copy, toRemove);
      95            0 :     }
      96              : 
      97              :     // trim from end (copying)
      98            0 :     std::string str_rtrim_copy(const std::string& s, /*unsigned*/ char toRemove)
      99              :     {
     100            0 :         auto copy(s);
     101            0 :         return str_rtrim(copy, toRemove);
     102            0 :     }
     103              : 
     104              :     // trim from both ends (copying)
     105            0 :     std::string str_trim_copy(const std::string& s, /*unsigned*/ char toRemove)
     106              :     {
     107            0 :         auto copy(s);
     108            0 :         return str_trim(copy, toRemove);
     109            0 :     }
     110              : 
     111            0 :     void str_to_lower(std::string& s)
     112              :     {
     113            0 :         std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
     114            0 :     }
     115              : 
     116            0 :     void str_to_upper(std::string& s)
     117              :     {
     118            0 :                 std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
     119            0 :         }   
     120              : 
     121          752 :     std::string str_to_lower_copy(const std::string& s)
     122              :     {
     123          752 :         std::string copy = s;
     124         3760 :         std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c) { return std::tolower(c); });
     125          752 :         return copy;
     126              :     }
     127              : 
     128            0 :     std::string str_to_upper_copy(const std::string& s)
     129              :     {
     130            0 :         std::string copy;
     131            0 :         std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c) { return std::toupper(c); });
     132            0 :         return copy;
     133              :     }   
     134              : 
     135            0 :     bool str_iequals(const std::string& s1, const std::string& s2)
     136              :     {
     137            0 :         return case_insensitive_compare(s1, s2);
     138              :     }
     139              : 
     140            0 :     std::vector<std::string>::iterator find_str_icmp(std::vector<std::string>::iterator first, std::vector<std::string>::iterator last, const std::string& val)
     141              :     {
     142            0 :         return std::find_if(first, last, [&val](const std::string& s) { return case_insensitive_compare(s, val); });
     143              :     }
     144              : 
     145            0 :     std::vector<std::string>::const_iterator find_str_icmp(const std::vector<std::string>::const_iterator first, const std::vector<std::string>::const_iterator last, const std::string& val)
     146              :     {
     147            0 :         return std::find_if(first, last, [&val](const std::string& s) { return case_insensitive_compare(s, val); });
     148              :     }
     149              : 
     150            0 :     static bool case_insensitive_compare(const std::string& s1, const std::string& s2)
     151              :     {
     152            0 :         return std::equal(
     153              :             s1.begin(), s1.end(),
     154              :             s2.begin(), s2.end(),
     155            0 :             [](char c1, char c2)
     156              :             {
     157            0 :                 return std::tolower(c1) == std::tolower(c2);
     158            0 :             });
     159              :     }
     160              : 
     161              :   //  UTILS_EXPORT std::string Utils::str_replace(const std::string& s, const std::string& from, const std::string& to)
     162              :   //  {
     163              :   //      std::string result;
     164              :                 //result.reserve(s.length()); // avoids a few memory allocations
     165              : 
     166              :                 //std::string::size_type lastPos = 0;
     167              :                 //std::string::size_type findPos;
     168              : 
     169              :   //      while (std::string::npos != (findPos = s.find(from, lastPos)))
     170              :   //      {
     171              :                 //      result.append(s, lastPos, findPos - lastPos);
     172              :                 //      result += to;
     173              :                 //      lastPos = findPos + from.length();
     174              :                 //}
     175              : 
     176              :                 //// Care for the rest after last occurrence
     177              :                 //result += s.substr(lastPos);
     178              : 
     179              :                 //return result;
     180              :   //  }
     181              : }
        

Generated by: LCOV version 2.0-1