"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/stringtools.cpp" (12 May 2020, 3980 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "stringtools.cpp": 3.55_vs_3.56.

    1 /***************************************************************************
    2                           stringtools.cpp  -  description
    3                              -------------------
    4     begin                : Mon Dec 10 2001
    5     copyright            : (C) 2001-2010 by Andre Simon
    6     email                : a.simon@mailbox.org
    7  ***************************************************************************/
    8 
    9 
   10 /*
   11 This file is part of Highlight.
   12 
   13 Highlight is free software: you can redistribute it and/or modify
   14 it under the terms of the GNU General Public License as published by
   15 the Free Software Foundation, either version 3 of the License, or
   16 (at your option) any later version.
   17 
   18 Highlight is distributed in the hope that it will be useful,
   19 but WITHOUT ANY WARRANTY; without even the implied warranty of
   20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   21 GNU General Public License for more details.
   22 
   23 You should have received a copy of the GNU General Public License
   24 along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
   25 */
   26 
   27 #include "stringtools.h"
   28 
   29 namespace StringTools
   30 {
   31 
   32 string change_case ( const string & s, const KeywordCase kcase ) throw()
   33 {
   34     string r ( s );
   35     switch ( kcase ) {
   36     case CASE_UNCHANGED:
   37         break;
   38     case CASE_CAPITALIZE:
   39     case CASE_LOWER:
   40         for ( unsigned int i = 0; i < r.size(); ++i )
   41             r[i] = tolower ( r[i] );
   42         if ( kcase == CASE_CAPITALIZE && r.size() )
   43             r[0] = toupper ( r[0] );
   44         break;
   45     case CASE_UPPER:
   46         for ( unsigned int i = 0; i < r.size(); ++i )
   47             r[i] = toupper ( r[i] );
   48         break;
   49     }
   50     return r;
   51 }
   52 
   53 
   54 string trimRight ( const string &value )
   55 {
   56     string::size_type where = value.find_last_not_of ( " \t\r" );
   57 
   58     if ( where == string::npos )
   59         // string has nothing but space
   60         return string();
   61 
   62     if ( where == ( value.length() - 1 ) )
   63         // string has no trailing space, don't copy its contents
   64         return value;
   65 
   66     return value.substr ( 0, where + 1 );
   67 }
   68 
   69 string trim(const string& s)
   70 {
   71     string str(s);
   72     string::size_type pos = str.find_last_not_of(' ');
   73     if(pos != string::npos) {
   74         str.erase(pos + 1);
   75         pos = str.find_first_not_of(' ');
   76         if(pos != string::npos) str.erase(0, pos);
   77     } else str.erase(str.begin(), str.end());
   78     return str;
   79 }
   80 
   81 vector<string> splitString ( const string& s, unsigned char delim )
   82 {
   83     string::size_type pos=s.find ( delim ), oldPos=0;
   84     vector <string> results;
   85 
   86     if ( pos ==string::npos ) {
   87         if ( !s.empty() ) results.push_back ( s );
   88         return results;
   89     }
   90 
   91     do {
   92         if ( oldPos-pos ) results.push_back ( s.substr ( oldPos, pos-oldPos ) );
   93         oldPos=pos+1;
   94         pos=s.find ( delim, pos+1 );
   95     } while ( pos!=string::npos );
   96     results.push_back ( s.substr ( oldPos ) );
   97 
   98     return results;
   99 }
  100 
  101 int calcWeight(const string&s)
  102 {
  103     int sum=0;
  104     for (unsigned int i=0; i<s.size(); i++) {
  105         sum+=(i%2)? int(s[i])*3 : int(s[i]);
  106     }
  107     return sum;
  108 }
  109 
  110 string getPathAcronym(const string&path, char delim)
  111 {
  112     string acronym;
  113     size_t pos=0;
  114     while ((pos=path.find(delim, pos))!=string::npos) {
  115         pos++;
  116         if (pos<path.size() && path[pos]!=delim) {
  117             acronym+= path[pos];
  118         }
  119     }
  120     if(!acronym.empty()) acronym.replace(acronym.end()-1, acronym.end(), "_");
  121     return acronym;
  122 }
  123 
  124 
  125 bool endsWith(std::string const & value, std::string const & ending)
  126 {
  127     if (ending.size() > value.size()) return false;
  128     return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
  129 }
  130 
  131 int utf8_strlen(const string& str)
  132 {
  133     int c,i,ix,q;
  134     for (q=0, i=0, ix=str.length(); i < ix; i++, q++)
  135     {
  136         c = (unsigned char) str[i];
  137         if      (c>=0   && c<=127) i+=0;
  138         else if ((c & 0xE0) == 0xC0) i+=1;
  139         else if ((c & 0xF0) == 0xE0) i+=2;
  140         else if ((c & 0xF8) == 0xF0) i+=3;
  141         else return 0;
  142     }
  143     return q;
  144 }
  145 
  146 }