]> git.lizzy.rs Git - minetest-m13.git/blob - src/strfnd.h
Update code style to C++11
[minetest-m13.git] / src / strfnd.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef STRFND_HEADER
21 #define STRFND_HEADER
22
23 #include <string>
24
25 std::string trim(const std::string &str);
26
27 class Strfnd{
28     std::string tek;
29     unsigned int p;
30 public:
31     void start(std::string niinq){
32         tek = niinq;
33         p=0;
34     }
35     unsigned int where(){
36         return p;
37     }
38     void to(unsigned int i){
39         p = i;
40     }
41     std::string what(){
42         return tek;
43     }
44     std::string next(std::string plop){
45         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
46         size_t n;
47         std::string palautus;
48         if (p < tek.size())
49         {  
50             //std::cout<<"\tp<tek.size()"<<std::endl;
51             if ((n = tek.find(plop, p)) == std::string::npos || plop == "")
52             {  
53                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
54                 n = tek.size();
55             }
56             else
57             {  
58                 //std::cout<<"\t\tn != string::npos"<<std::endl;
59             }
60             palautus = tek.substr(p, n-p);
61             p = n + plop.length();
62         }
63         //else
64             //std::cout<<"\tp>=tek.size()"<<std::endl;
65                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
66         return palautus;
67     }
68         void skip_over(std::string chars){
69                 while(p < tek.size()){
70                         bool is = false;
71                         for(unsigned int i=0; i<chars.size(); i++){
72                                 if(chars[i] == tek[p]){
73                                         is = true;
74                                         break;
75                                 }
76                         }
77                         if(!is) break;
78                         p++;
79                 }
80         }
81     bool atend(){
82         if(p>=tek.size()) return true;
83         return false;
84     }
85     Strfnd(std::string s){
86         start(s);
87     }
88 };
89
90 class WStrfnd{
91     std::wstring tek;
92     unsigned int p;
93 public:
94     void start(std::wstring niinq){
95         tek = niinq;
96         p=0;
97     }
98     unsigned int where(){
99         return p;
100     }
101     void to(unsigned int i){
102         p = i;
103     }
104     std::wstring what(){
105         return tek;
106     }
107     std::wstring next(std::wstring plop){
108         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
109         size_t n;
110         std::wstring palautus;
111         if (p < tek.size())
112         {  
113             //std::cout<<"\tp<tek.size()"<<std::endl;
114             if ((n = tek.find(plop, p)) == std::wstring::npos || plop == L"")
115             {  
116                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
117                 n = tek.size();
118             }
119             else
120             {  
121                 //std::cout<<"\t\tn != string::npos"<<std::endl;
122             }
123             palautus = tek.substr(p, n-p);
124             p = n + plop.length();
125         }
126         //else
127             //std::cout<<"\tp>=tek.size()"<<std::endl;
128                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
129         return palautus;
130     }
131     bool atend(){
132         if(p>=tek.size()) return true;
133         return false;
134     }
135     WStrfnd(std::wstring s){
136         start(s);
137     }
138 };
139
140 inline std::string trim(const std::string &s)
141 {
142         std::string str = s;
143     while( 
144             str.length()>0
145             &&
146             (
147              str.substr(0,               1)==" "     ||
148              str.substr(0,               1)=="\t"    ||
149              str.substr(0,               1)=="\r"    ||
150              str.substr(0,               1)=="\n"    ||
151              str.substr(str.length()-1,  1)==" "     ||
152              str.substr(str.length()-1,  1)=="\t"    ||
153              str.substr(str.length()-1,  1)=="\r"    ||
154              str.substr(str.length()-1,  1)=="\n"
155             )
156          )
157     {  
158         if      (str.substr(0,              1)==" ")
159                         str = str.substr(1,str.length()-1);
160         else if (str.substr(0,              1)=="\t")
161                         str = str.substr(1,str.length()-1);
162         else if (str.substr(0,              1)=="\r")
163                         str = str.substr(1,str.length()-1);
164         else if (str.substr(0,              1)=="\n")
165                         str = str.substr(1,str.length()-1);
166         else if (str.substr(str.length()-1, 1)==" ")
167                         str = str.substr(0,str.length()-1);
168         else if (str.substr(str.length()-1, 1)=="\t")
169                         str = str.substr(0,str.length()-1);
170         else if (str.substr(str.length()-1, 1)=="\r")
171                         str = str.substr(0,str.length()-1);
172         else if (str.substr(str.length()-1, 1)=="\n")
173                         str = str.substr(0,str.length()-1);
174     }
175     return str;
176 }
177
178 #endif
179