Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 14, 2008, 3:42:49 AM (16 years ago)
Author:
landauf
Message:

merged core2 back to trunk
there might be some errors, wasn't able to test it yet due to some strange g++ and linker behaviour.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/util/String.cc

    r871 r1052  
    2626 */
    2727
     28#include <cctype>
     29#include <iostream>
    2830#include "String.h"
    2931
     
    5456
    5557/**
     58    @brief Returns a copy of a string without trailing whitespaces.
     59    @param str The string
     60    @return The modified copy
     61*/
     62std::string removeTrailingWhitespaces(const std::string& str)
     63{
     64    unsigned int pos1 = 0;
     65    unsigned int pos2 = str.size() - 1;
     66    for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++);
     67    for (; pos2 >= 0         && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--);
     68    return str.substr(pos1, pos2 - pos1 + 1);
     69}
     70
     71/**
     72    @brief Returns the position of the next quote in the string, starting with start.
     73    @param str The string
     74    @param start The startposition
     75    @return The position of the next quote (std::string::npos if there is no next quote)
     76*/
     77unsigned int getNextQuote(const std::string& str, unsigned int start)
     78{
     79    unsigned int quote = start - 1;
     80
     81    while ((quote = str.find('\"', quote + 1)) != std::string::npos)
     82    {
     83        unsigned int backslash = quote;
     84        unsigned int numbackslashes = 0;
     85        for (; backslash > 0; backslash--, numbackslashes++)
     86            if (str[backslash - 1] != '\\')
     87                break;
     88
     89        if (numbackslashes % 2 == 0)
     90            break;
     91    }
     92
     93    return quote;
     94}
     95
     96/**
     97    @brief Returns true if pos is between two quotes.
     98    @param str The string
     99    @param pos The position to check
     100    @return True if pos is between two quotes
     101*/
     102bool isBetweenQuotes(const std::string& str, unsigned int pos)
     103{
     104    if (pos == std::string::npos)
     105        return false;
     106
     107    unsigned int quotecount = 0;
     108    unsigned int quote = 0;
     109    while ((quote = getNextQuote(str, quote)) < pos)
     110    {
     111        quotecount++;
     112    }
     113
     114    if (quote == std::string::npos)
     115        return false;
     116
     117    return ((quotecount % 2) == 1);
     118}
     119
     120/**
     121    @brief Returns true if the string contains something like '..."between quotes"...'
     122    @param The string
     123    @return True if there is something between quotes
     124*/
     125bool hasStringBetweenQuotes(const std::string& str)
     126{
     127    unsigned int pos1 = getNextQuote(str, 0);
     128    unsigned int pos2 = getNextQuote(str, pos1 + 1);
     129    return (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1);
     130}
     131
     132/**
     133    @brief If the string contains something like '..."between quotes"...' then 'between quotes' gets returned (without quotes).
     134    @param The string
     135    @param The string between the quotes
     136*/
     137std::string getStringBetweenQuotes(const std::string& str)
     138{
     139    unsigned int pos1 = getNextQuote(str, 0);
     140    unsigned int pos2 = getNextQuote(str, pos1 + 1);
     141    if (pos1 != std::string::npos && pos2 != std::string::npos)
     142        return str.substr(pos1, pos2 - pos1 + 1);
     143    else
     144        return "";
     145}
     146
     147/**
     148    @brief Removes enclosing quotes if available.
     149    @brief str The string to strip
     150    @return The string with removed quotes
     151*/
     152std::string stripEnclosingQuotes(const std::string& str)
     153{
     154    unsigned int start = std::string::npos;
     155    unsigned int end = 0;
     156
     157    for (unsigned int pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++)
     158    {
     159        if (str[pos] == '"')
     160        {
     161            start = pos;
     162            break;
     163        }
     164
     165        if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
     166            return str;
     167    }
     168
     169    for (unsigned int pos = str.size() - 1; pos < std::string::npos; pos--)
     170    {
     171        if (str[pos] == '"')
     172        {
     173            end = pos;
     174            break;
     175        }
     176
     177        if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n'))
     178            return str;
     179    }
     180
     181    if ((start != std::string::npos) && (end != 0))
     182        return str.substr(start + 1, end - start - 1);
     183    else
     184        return str;
     185}
     186
     187/**
    56188    @brief Determines if a string in is a comment.
    57189    @param str The string to check
     
    70202    //  3) ;comment in unreal tournament config-file style
    71203    //  4) //comment in code style
    72     if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/'))
    73         return true;
     204    if (teststring.size() >= 2)
     205    {
     206        if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/'))
     207            return true;
     208    }
     209    else if (teststring.size() == 1)
     210    {
     211        if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';')
     212            return true;
     213    }
    74214
    75215    return false;
     
    83223bool isEmpty(const std::string& str)
    84224{
    85     return getStripped(str) == "";
     225    std::string temp = getStripped(str);
     226    return ((temp == "") || (temp.size() == 0));
    86227}
    87228
     
    109250}
    110251
     252std::string addSlashes(const std::string& str)
     253{
     254    std::string output = str;
     255
     256    for (unsigned int pos = 0; (pos = output.find('\\', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\\"); }
     257    for (unsigned int pos = 0; (pos = output.find('\n', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\n"); }
     258    for (unsigned int pos = 0; (pos = output.find('\t', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\t"); }
     259    for (unsigned int pos = 0; (pos = output.find('\v', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\v"); }
     260    for (unsigned int pos = 0; (pos = output.find('\b', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\b"); }
     261    for (unsigned int pos = 0; (pos = output.find('\r', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\r"); }
     262    for (unsigned int pos = 0; (pos = output.find('\f', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\f"); }
     263    for (unsigned int pos = 0; (pos = output.find('\a', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\a"); }
     264    for (unsigned int pos = 0; (pos = output.find('"', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\\""); }
     265    for (unsigned int pos = 0; (pos = output.find('\0', pos)) < std::string::npos; pos += 2) { output.replace(pos, 1, "\\0"); }
     266
     267    return output;
     268}
     269
     270std::string removeSlashes(const std::string& str)
     271{
     272    if (str.size() == 0)
     273        return str;
     274
     275    std::string output = "";
     276    for (unsigned int pos = 0; pos < str.size() - 1; )
     277    {
     278        if (str[pos] == '\\')
     279        {
     280            if (str[pos + 1] == '\\') { output += '\\'; pos += 2; continue; }
     281            else if (str[pos + 1] == 'n') { output += '\n'; pos += 2; continue; }
     282            else if (str[pos + 1] == 't') { output += '\t'; pos += 2; continue; }
     283            else if (str[pos + 1] == 'v') { output += '\v'; pos += 2; continue; }
     284            else if (str[pos + 1] == 'b') { output += '\b'; pos += 2; continue; }
     285            else if (str[pos + 1] == 'r') { output += '\r'; pos += 2; continue; }
     286            else if (str[pos + 1] == 'f') { output += '\f'; pos += 2; continue; }
     287            else if (str[pos + 1] == 'a') { output += '\a'; pos += 2; continue; }
     288            else if (str[pos + 1] == '"') { output += '"'; pos += 2; continue; }
     289            else if (str[pos + 1] == '0') { output += '\0'; pos += 2; continue; }
     290        }
     291        output += str[pos];
     292        pos++;
     293        if (pos == str.size() - 1)
     294            output += str[pos];
     295    }
     296
     297    return output;
     298}
     299
    111300/**
    112301    @brief Replaces each char between A and Z with its lowercase equivalent.
     
    115304void lowercase(std::string* str)
    116305{
    117     static unsigned const char difference_between_A_and_a = 'A' - 'a';
    118 
    119     for (std::string::iterator it = (*str).begin(); it != (*str).end(); ++it)
    120         if ((*it) >= 'A' && (*it) <= 'Z')
    121             (*it) -= difference_between_A_and_a;
     306    for (unsigned int i = 0; i < str->size(); ++i)
     307    {
     308        (*str)[i] = tolower((*str)[i]);
     309    }
    122310}
    123311
     
    140328void uppercase(std::string* str)
    141329{
    142     static unsigned const char difference_between_A_and_a = 'A' - 'a';
    143 
    144     for (std::string::iterator it = (*str).begin(); it != (*str).end(); ++it)
    145         if ((*it) >= 'a' && (*it) <= 'z')
    146             (*it) += difference_between_A_and_a;
     330    for (unsigned int i = 0; i < str->size(); ++i)
     331    {
     332        (*str)[i] = toupper((*str)[i]);
     333    }
    147334}
    148335
     
    160347
    161348/**
    162  * @brief compares two strings without ignoring the case
    163  * @param s1 first string
    164  * @param s2 second string
    165  */
     349   @brief compares two strings without ignoring the case
     350   @param s1 first string
     351   @param s2 second string
     352*/
    166353int nocaseCmp(const std::string& s1, const std::string& s2)
    167354{
     
    188375
    189376/**
    190  * @brief compares two strings without ignoring the case
    191  * @param s1 first string
    192  * @param s2 second string
    193  * @param len how far from the beginning to start.
    194  */
     377   @brief compares two strings without ignoring the case
     378   @param s1 first string
     379   @param s2 second string
     380   @param len how far from the beginning to start.
     381*/
    195382int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len)
    196383{
     
    212399    return 0;
    213400}
     401
     402/**
     403    @brief Returns true if the string contains a comment, introduced by #, %, ; or //.
     404    @param str The string
     405    @return True if the string contains a comment
     406*/
     407bool hasComment(const std::string& str)
     408{
     409    return (getCommentPosition(str) != std::string::npos);
     410}
     411
     412/**
     413    @brief If the string contains a comment, the comment gets returned (including the comment symbol), an empty string otherwise.
     414    @param str The string
     415    @return The comment
     416*/
     417std::string getComment(const std::string& str)
     418{
     419    return str.substr(getCommentPosition(str));
     420}
     421
     422/**
     423    @brief If the string contains a comment, the position of the comment-symbol gets returned, std::string::npos otherwise.
     424    @param str The string
     425    @return The position
     426*/
     427unsigned int getCommentPosition(const std::string& str)
     428{
     429    return getNextCommentPosition(str, 0);
     430}
     431
     432/**
     433    @brief Returns the position of the next comment-symbol, starting with start.
     434    @param str The string
     435    @param start The startposition
     436    @return The position
     437*/
     438unsigned int getNextCommentPosition(const std::string& str, unsigned int start)
     439{
     440    for (unsigned int i = start; i < str.size(); i++)
     441        if (isComment(str.substr(i)))
     442            return i;
     443
     444    return std::string::npos;
     445}
Note: See TracChangeset for help on using the changeset viewer.