Rudimentary approach at water splash effect for badguys
[supertux.git] / src / util / string_util.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include <algorithm>
18
19 #include "string_util.hpp"
20
21 bool
22 StringUtil::has_suffix(const std::string& data, const std::string& suffix)
23 {
24   if (data.length() >= suffix.length())
25   {
26     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
27   }
28   else
29   {
30     return false;
31   }
32 }
33
34 bool
35 StringUtil::numeric_less(const std::string& lhs, const std::string& rhs)
36 {
37   std::string::size_type i = 0;
38   std::string::size_type min_len = std::min(lhs.size(), rhs.size());
39
40   while(i < min_len)
41   {
42     if (isdigit(lhs[i]) && isdigit(rhs[i]))
43     {
44       // have two digits, so check which number is smaller
45       std::string::size_type li = i+1;
46       std::string::size_type ri = i+1;
47
48       // find the end of the number in both strings
49       while(li < lhs.size() && isdigit(lhs[li])) { li += 1; }
50       while(ri < rhs.size() && isdigit(rhs[ri])) { ri += 1; }
51
52       if (li == ri)
53       {
54         // end is at the same point in both strings, so do a detaile
55         // comparism of the numbers
56         for(std::string::size_type j = i; j < li; ++j)
57         {
58           if (lhs[j] != rhs[j])
59           {
60             return lhs[j] < rhs[j];
61           }
62         }
63
64         // numbers are the same, so jump to the end of the number and compare
65         i = li;
66       }
67       else
68       {
69         // numbers have different numbers of digits, so the number
70         // with the least digits wins
71         return li < ri;
72       }
73     }
74     else
75     {
76       // do normal character comparism
77       if (lhs[i] != rhs[i])
78       {
79         return lhs[i] < rhs[i];
80       }
81       else
82       {
83         // strings are the same so far, so continue
84         i += 1;
85       }
86     }
87   }
88
89   return lhs.size() < rhs.size();
90 }
91
92 /* EOF */