Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud2/src/util/String.cc @ 1180

Last change on this file since 1180 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

File size: 6.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      Benjamin Grauer
25 *
26 */
27
28#include "String.h"
29
30/**
31    @brief Removes all whitespaces from a string.
32    @param str The string to strip
33*/
34void strip(std::string* str)
35{
36    unsigned int pos;
37    while ((pos = (*str).find(" ")) < (*str).length())
38        (*str).erase(pos, 1);
39    while ((pos = (*str).find("\t")) < (*str).length())
40        (*str).erase(pos, 1);
41}
42
43/**
44    @brief Returns a copy of a string without whitespaces.
45    @param str The string to strip
46    @return The stripped line
47*/
48std::string getStripped(const std::string& str)
49{
50    std::string output = std::string(str);
51    strip(&output);
52    return output;
53}
54
55/**
56    @brief Determines if a string in is a comment.
57    @param str The string to check
58    @return True = it's a comment
59
60    A comment is defined by a leading '#', '%', ';' or '//'.
61*/
62bool isComment(const std::string& str)
63{
64    // Strip the line, whitespaces are disturbing
65    std::string teststring = getStripped(str);
66
67    // There are four possible comment-symbols:
68    //  1) #comment in script-language style
69    //  2) %comment in matlab style
70    //  3) ;comment in unreal tournament config-file style
71    //  4) //comment in code style
72    if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[0] == '/'))
73        return true;
74
75    return false;
76}
77
78/**
79    @brief Determines if a string is empty (contains only whitespaces).
80    @param str The string to check
81    @return True = it's empty
82*/
83bool isEmpty(const std::string& str)
84{
85    return getStripped(str) == "";
86}
87
88/**
89    @brief Determines if a string contains only numbers and maximal one '.'.
90    @param str The string to check
91    @return True = it's a number
92*/
93bool isNumeric(const std::string& str)
94{
95    bool foundPoint = false;
96
97    for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
98    {
99        if (((*it) < '0' || (*it) > '9'))
100        {
101            if ((*it) != '.' && !foundPoint)
102                foundPoint = true;
103            else
104                return false;
105        }
106    }
107
108    return true;
109}
110
111/**
112    @brief Replaces each char between A and Z with its lowercase equivalent.
113    @param str The string to convert
114*/
115void lowercase(std::string* str)
116{
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;
122}
123
124/**
125    @brief Returns a copy of the given string without uppercase chars.
126    @param str The string
127    @return The copy
128*/
129std::string getLowercase(const std::string& str)
130{
131    std::string output = std::string(str);
132    lowercase(&output);
133    return output;
134}
135
136/**
137    @brief Replaces each char between a and z with its uppercase equivalent.
138    @param str The string to convert
139*/
140void uppercase(std::string* str)
141{
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;
147}
148
149/**
150    @brief Returns a copy of the given string without lowercase chars.
151    @param str The string
152    @return The copy
153*/
154std::string getUppercase(const std::string& str)
155{
156    std::string output = std::string(str);
157    uppercase(&output);
158    return output;
159}
160
161/**
162 * @brief compares two strings without ignoring the case
163 * @param s1 first string
164 * @param s2 second string
165 */
166int nocaseCmp(const std::string& s1, const std::string& s2)
167{
168    std::string::const_iterator it1=s1.begin();
169    std::string::const_iterator it2=s2.begin();
170
171    //stop when either string's end has been reached
172    while ( (it1!=s1.end()) && (it2!=s2.end()) )
173    {
174        if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
175            // return -1 to indicate smaller than, 1 otherwise
176            return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
177        //proceed to the next character in each string
178        ++it1;
179        ++it2;
180    }
181    size_t size1=s1.size(), size2=s2.size();// cache lengths
182    //return -1,0 or 1 according to strings' lengths
183    if (size1==size2)
184        return 0;
185    return (size1<size2) ? -1 : 1;
186}
187
188
189/**
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 */
195int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len)
196{
197    if (len == 0)
198        return 0;
199    std::string::const_iterator it1=s1.begin();
200    std::string::const_iterator it2=s2.begin();
201
202    //stop when either string's end has been reached
203    while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0)
204    {
205        if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
206            // return -1 to indicate smaller than, 1 otherwise
207            return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
208        //proceed to the next character in each string
209        ++it1;
210        ++it2;
211    }
212    return 0;
213}
Note: See TracBrowser for help on using the repository browser.