Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_HS18/SuperOrxoBros_HS18/src/libraries/util/StringUtils.cc @ 12175

Last change on this file since 12175 was 12175, checked in by siramesh, 5 years ago

Super Orxo Bros (Sidharth Ramesh, Nisa Balta, Jeff Ren)

File size: 17.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Benjamin Grauer
26 *
27 */
28
29/**
30    @file
31    @brief Implementation of several string manipulation functions.
32*/
33
34#include "StringUtils.h"
35
36#include <algorithm>
37#include <cctype>
38#include <ctime>
39#include <memory>
40#include "Convert.h"
41#include "Math.h"
42
43namespace orxonox
44{
45    /// A blank string (""). Used to return a blank string by reference.
46    std::string BLANKSTRING;
47
48    /// Returns a string of a unique number. This function is guaranteed to never return the same string twice.
49    std::string getUniqueNumberString()
50    {
51        return multi_cast<std::string>(getUniqueNumber());
52    }
53
54    /// Removes all whitespaces from a string.
55    void strip(std::string* str)
56    {
57        str->erase(std::remove_if(str->begin(), str->end(), [](char val) { return std::isspace(val) != 0; }), str->end());
58    }
59
60    /// Returns a copy of a string without whitespaces.
61    std::string getStripped(const std::string& str)
62    {
63        std::string output(str);
64        strip(&output);
65        return output;
66    }
67
68    /// Returns a copy of a string without trailing whitespaces.
69    std::string removeTrailingWhitespaces(const std::string& str)
70    {
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());
79    }
80
81    /// Splits a given string by a delimiter and stores it in an output vector. See @ref SubString for a more sophisticated implementation.
82    void vectorize(const std::string& str, char delimiter, std::vector<std::string>* output)
83    {
84        output->clear();
85        for (size_t start = 0, end = 0; end != std::string::npos; start = end + 1)
86        {
87            end = str.find_first_of(delimiter, start);
88            output->push_back(str.substr(start, end - start));
89        }
90    }
91
92    /**
93        @brief Returns the position of the next quotation mark in the string, starting with start. Escaped quotation marks (with \ in front) are not considered.
94        @param str The string
95        @param start The first position to look at
96        @return The position of the next quotation mark (@c std::string::npos if there is none)
97    */
98    size_t getNextQuote(const std::string& str, size_t start)
99    {
100        size_t quote = start - 1;
101
102        while ((quote = str.find('"', quote + 1)) != std::string::npos)
103        {
104            size_t backslash = quote;
105            size_t numbackslashes = 0;
106            for (; backslash > 0; backslash--, numbackslashes++)
107                if (str[backslash - 1] != '\\')
108                    break;
109
110            if (numbackslashes % 2 == 0)
111                break;
112        }
113
114        return quote;
115    }
116
117    /**
118        @brief Returns true if pos is between two quotation marks.
119        @param str The string
120        @param pos The position to check
121        @return True if pos is between two quotation marks
122    */
123    bool isBetweenQuotes(const std::string& str, size_t pos)
124    {
125        if (pos == std::string::npos)
126            return false;
127
128        size_t quotecount = 0;
129        size_t quote = static_cast<size_t>(-1);
130        while ((quote = getNextQuote(str, quote + 1)) < pos)
131            quotecount++;
132
133        if (quote == pos)
134            return false;
135        if (quote == std::string::npos)
136            return false;
137
138        return ((quotecount % 2) == 1);
139    }
140
141    /// Returns true if the string contains something like '..."between quotaton marks"...'.
142    bool hasStringBetweenQuotes(const std::string& str)
143    {
144        size_t pos1 = getNextQuote(str, 0);
145        size_t pos2 = getNextQuote(str, pos1 + 1);
146        return (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1);
147    }
148
149    /// If the string contains something like '..."between quotaton marks"...' then 'between quotaton marks' gets returned, otherwise "".
150    std::string getStringBetweenQuotes(const std::string& str)
151    {
152        size_t pos1 = getNextQuote(str, 0);
153        size_t pos2 = getNextQuote(str, pos1 + 1);
154        if (pos1 != std::string::npos && pos2 != std::string::npos)
155            return str.substr(pos1 + 1, pos2 - pos1 - 1);
156        else
157            return "";
158    }
159
160    /**
161        @brief Removes enclosing quotation marks if available (including whitespaces at the outside of the quotation marks).
162        @return The stripped string without quotation marks
163    */
164    std::string stripEnclosingQuotes(const std::string& str)
165    {
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)
172                return str;
173        }
174
175        for (; end != str.rend() && *end != '"'; ++end)
176        {
177            if (std::isspace(*end) == 0)
178                return str;
179        }
180
181        if (start != str.end() && end != str.rend())
182        {
183            ++start; // Skip "
184            ++end; // Skip "
185            return std::string(start, end.base());
186        }
187        else
188            return str;
189    }
190
191    /**
192        @brief Removes enclosing braces '{' and '}' (the braces must be exactly on the beginning and the end of the string).
193        @return The stripped string without braces
194    */
195    std::string stripEnclosingBraces(const std::string& str)
196    {
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());
212    }
213
214    /**
215        @brief Determines if a string is a comment (starts with a comment-symbol).
216
217        A comment is defined by a leading '#', '%', ';' or '//'.
218    */
219    bool isComment(const std::string& str)
220    {
221        // Strip the line, whitespaces are disturbing
222        const std::string& teststring = getStripped(str);
223
224        // There are four possible comment-symbols:
225        //  1) #comment in script-language style
226        //  2) %comment in matlab style
227        //  3) ;comment in unreal tournament config-file style
228        //  4) //comment in code style
229        if (teststring.size() >= 2)
230        {
231            if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/'))
232                return true;
233        }
234        else if (teststring.size() == 1)
235        {
236            if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';')
237                return true;
238        }
239
240        return false;
241    }
242
243    /// Determines if a string is empty (contains only whitespaces).
244    bool isEmpty(const std::string& str)
245    {
246        return std::all_of(str.begin(), str.end(), [](char val) { return std::isspace(val) != 0; });
247    }
248
249    /**
250        @brief Adds backslashes to the given string which makes special chars visible. Existing slashes will be doubled.
251
252        This function converts all special chars like line breaks, tabs, quotation marks etc. into
253        a human readable format by adding a backslash. So for example "\n" will be converted to
254        "\\" + "n".
255
256        This is usually used when a string is written to a file.
257
258        @see removeSlashes
259    */
260    std::string addSlashes(const std::string& str)
261    {
262        std::string output(str.size() * 2, ' ');
263        size_t i = 0;
264        for (const char& character : str)
265        {
266            switch (character)
267            {
268            case '\\': output[i] = '\\'; output[i + 1] = '\\'; break;
269            case '\n': output[i] = '\\'; output[i + 1] =  'n'; break;
270            case '\t': output[i] = '\\'; output[i + 1] =  't'; break;
271            case '\v': output[i] = '\\'; output[i + 1] =  'v'; break;
272            case '\b': output[i] = '\\'; output[i + 1] =  'b'; break;
273            case '\r': output[i] = '\\'; output[i + 1] =  'r'; break;
274            case '\f': output[i] = '\\'; output[i + 1] =  'f'; break;
275            case '\a': output[i] = '\\'; output[i + 1] =  'a'; break;
276            case  '"': output[i] = '\\'; output[i + 1] =  '"'; break;
277            case '\0': output[i] = '\\'; output[i + 1] =  '0'; break;
278            default  : output[i] = character; ++i; continue;
279            }
280            i += 2;
281        }
282        output.resize(i);
283
284        return output;
285    }
286
287    /**
288        @brief Removes backslashes from the given string. Double backslashes are interpreted as one backslash.
289
290        This function removes all backslashes and converts the human readable equivalents of
291        special chars like "\\" + "n" into their real meaning (in this case a line break or "\n").
292
293        This is usually used when reading a string from a file.
294
295        @see addSlashes
296    */
297    std::string removeSlashes(const std::string& str)
298    {
299        if (str.size() <= 1)
300            return str;
301
302        std::string output(str.size(), ' ');
303        size_t i = 0;
304        size_t pos = 0;
305        while (pos < str.size() - 1)
306        {
307            if (str[pos] == '\\')
308            {
309                switch (str[pos + 1])
310                {
311                case '\\': output[i] = '\\'; break;
312                case  'n': output[i] = '\n'; break;
313                case  't': output[i] = '\t'; break;
314                case  'v': output[i] = '\v'; break;
315                case  'b': output[i] = '\b'; break;
316                case  'r': output[i] = '\r'; break;
317                case  'f': output[i] = '\f'; break;
318                case  'a': output[i] = '\a'; break;
319                case  '"': output[i] =  '"'; break;
320                case  '0': output[i] = '\0'; break;
321                default: ++pos; continue;
322                }
323                pos += 2; ++i;
324            }
325            else
326                output[i++] = str[pos++];
327        }
328        if (pos < str.size())
329            output[i++] = str[pos];
330        output.resize(i);
331
332        return output;
333    }
334
335    /// Replaces each char between A and Z with its lowercase equivalent.
336    void lowercase(std::string* str)
337    {
338        std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::tolower(val); });
339    }
340
341    /// Returns a copy of the given string where all chars are converted to lowercase.
342    std::string getLowercase(const std::string& str)
343    {
344        std::string output(str);
345        lowercase(&output);
346        return output;
347    }
348
349    /// Replaces each char between a and z with its uppercase equivalent.
350    void uppercase(std::string* str)
351    {
352        std::transform(str->begin(), str->end(), str->begin(), [](char val) { return std::toupper(val); });
353    }
354
355    /// Returns a copy of the given string where all chars are converted to uppercase.
356    std::string getUppercase(const std::string& str)
357    {
358        std::string output(str);
359        uppercase(&output);
360        return output;
361    }
362
363    /**
364        @brief Compares two strings ignoring different casing.
365        @return s1 == s2 -> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1
366    */
367    int nocaseCmp(const std::string& s1, const std::string& s2)
368    {
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
378        //return -1,0 or 1 according to strings' lengths
379        if (size1 == size2)
380            return 0;
381        return (size1 < size2) ? -1 : 1;
382
383    }
384
385
386    /**
387        @brief Compares the first @a len chars of two strings ignoring different casing.
388        @param s1 First string
389        @param s2 Second string
390        @param len Maximal number of chars to compare
391    */
392    int nocaseCmp(const std::string& s1, const std::string& s2, size_t len)
393    {
394        if (len == 0)
395            return 0;
396        std::string::const_iterator it1 = s1.begin();
397        std::string::const_iterator it2 = s2.begin();
398
399        //stop when either string's end has been reached
400        while ( (it1 != s1.end()) && (it2 != s2.end()) && len-- > 0)
401        {
402            if(std::toupper(*it1) != std::toupper(*it2)) //letters differ?
403                // return -1 to indicate smaller than, 1 otherwise
404                return (std::toupper(*it1)  < std::toupper(*it2)) ? -1 : 1;
405            //proceed to the next character in each string
406            ++it1;
407            ++it2;
408        }
409        return 0;
410    }
411
412    /// Returns true if the string contains a comment, introduced by #, %, ; or //.
413    bool hasComment(const std::string& str)
414    {
415        return (getNextCommentPosition(str) != std::string::npos);
416    }
417
418    /// If the string contains a comment, the comment gets returned (including the comment symbol and white spaces in front of it), an empty string otherwise.
419    std::string getComment(const std::string& str)
420    {
421        size_t pos = getNextCommentPosition(str);
422        if (pos == std::string::npos)
423            return "";
424        else
425            return str.substr(pos);
426    }
427
428    /**
429        @brief Returns the beginning of the next comment including whitespaces in front of the comment symbol.
430        @param str The string
431        @param start The first position to look at
432    */
433    size_t getNextCommentPosition(const std::string& str, size_t start)
434    {
435        for (size_t i = start; i < str.size(); i++)
436            if (isComment(str.substr(i)))
437                return i;
438
439        return std::string::npos;
440    }
441
442    /**
443        @brief Replaces individual charaters
444        @param str String to be manipulated
445        @param target Character to be replaced
446        @param replacement Replacement character
447        @return Number of replacements
448    */
449    size_t replaceCharacters(std::string& str, char target, char replacement)
450    {
451        size_t j = 0;
452        for (char& character : str)
453        {
454            if (character == target)
455            {
456                character = replacement;
457                ++j;
458            }
459        }
460        return j;
461    }
462
463    /**
464        @brief Calculates the Levenshtein distance between two strings.
465
466        The Levenshtein distance is defined by the number of transformations needed to convert str1
467        into str2. Possible transformations are substituted, added, or removed characters.
468    */
469    unsigned int getLevenshteinDistance(const std::string& str1, const std::string& str2)
470    {
471        size_t cols = str1.size() + 1;
472        size_t rows = str2.size() + 1;
473        const std::unique_ptr<int[]> matrix(new int[rows * cols]);
474
475        for (size_t r = 0; r < rows; ++r)
476            for (size_t c = 0; c < cols; ++c)
477                matrix[r*cols + c] = 0;
478
479        for (size_t i = 1; i < cols; ++i)
480            matrix[0*cols + i] = i;
481        for (size_t i = 1; i < rows; ++i)
482            matrix[i*cols + 0] = i;
483
484        for (size_t r = 1; r < rows; ++r)
485            for (size_t c = 1; c < cols; ++c)
486                matrix[r*cols + c] = (str1[c-1] != str2[r-1]);
487
488        for (size_t r = 1; r < rows; ++r)
489            for (size_t c = 1; c < cols; ++c)
490                matrix[r*cols + c] = std::min(std::min(matrix[(r-1)*cols + c] + 1,
491                                                       matrix[r*cols + c-1] + 1),
492                                              matrix[(r-1)*cols + c-1] + (str1[c-1] != str2[r-1]));
493
494        return matrix[(rows-1)*cols + cols-1];
495    }
496
497    /**
498    @brief
499        Get a timestamp for the current time instant.
500    @return
501        Returns a string with the timestamp.
502    */
503    std::string getTimestamp(void)
504    {
505        time_t ctTime; std::time(&ctTime);
506        tm* pTime = std::localtime(&ctTime);
507        std::ostringstream oss;
508//        oss << std::put_time(pTime, "%m%d%Y_%H%M%S"); // <-- std::put_time is only available in GCC 5+
509        oss << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1)
510            << std::setw(2) << std::setfill('0') << pTime->tm_mday
511            << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900)
512            << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour
513            << std::setw(2) << std::setfill('0') << pTime->tm_min
514            << std::setw(2) << std::setfill('0') << pTime->tm_sec;
515        return oss.str();
516    }
517}
Note: See TracBrowser for help on using the repository browser.