utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.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     bool atend(){
69         if(p>=tek.size()) return true;
70         return false;
71     }
72     Strfnd(std::string s){
73         start(s);
74     }
75 };
76
77 class WStrfnd{
78     std::wstring tek;
79     unsigned int p;
80 public:
81     void start(std::wstring niinq){
82         tek = niinq;
83         p=0;
84     }
85     unsigned int where(){
86         return p;
87     }
88     void to(unsigned int i){
89         p = i;
90     }
91     std::wstring what(){
92         return tek;
93     }
94     std::wstring next(std::wstring plop){
95         //std::cout<<"tek=\""<<tek<<"\" plop=\""<<plop<<"\""<<std::endl;
96         size_t n;
97         std::wstring palautus;
98         if (p < tek.size())
99         {  
100             //std::cout<<"\tp<tek.size()"<<std::endl;
101             if ((n = tek.find(plop, p)) == std::wstring::npos || plop == L"")
102             {  
103                 //std::cout<<"\t\tn == string::npos || plop == \"\""<<std::endl;
104                 n = tek.size();
105             }
106             else
107             {  
108                 //std::cout<<"\t\tn != string::npos"<<std::endl;
109             }
110             palautus = tek.substr(p, n-p);
111             p = n + plop.length();
112         }
113         //else
114             //std::cout<<"\tp>=tek.size()"<<std::endl;
115                 //std::cout<<"palautus=\""<<palautus<<"\""<<std::endl;
116         return palautus;
117     }
118     bool atend(){
119         if(p>=tek.size()) return true;
120         return false;
121     }
122     WStrfnd(std::wstring s){
123         start(s);
124     }
125 };
126
127 inline std::string trim(const std::string &s)
128 {
129         std::string str = s;
130     while( 
131             str.length()>0
132             &&
133             (
134              str.substr(0,               1)==" "     ||
135              str.substr(0,               1)=="\t"    ||
136              str.substr(0,               1)=="\r"    ||
137              str.substr(0,               1)=="\n"    ||
138              str.substr(str.length()-1,  1)==" "     ||
139              str.substr(str.length()-1,  1)=="\t"    ||
140              str.substr(str.length()-1,  1)=="\r"    ||
141              str.substr(str.length()-1,  1)=="\n"
142             )
143          )
144     {  
145         if      (str.substr(0,              1)==" ")
146                         str = str.substr(1,str.length()-1);
147         else if (str.substr(0,              1)=="\t")
148                         str = str.substr(1,str.length()-1);
149         else if (str.substr(0,              1)=="\r")
150                         str = str.substr(1,str.length()-1);
151         else if (str.substr(0,              1)=="\n")
152                         str = str.substr(1,str.length()-1);
153         else if (str.substr(str.length()-1, 1)==" ")
154                         str = str.substr(0,str.length()-1);
155         else if (str.substr(str.length()-1, 1)=="\t")
156                         str = str.substr(0,str.length()-1);
157         else if (str.substr(str.length()-1, 1)=="\r")
158                         str = str.substr(0,str.length()-1);
159         else if (str.substr(str.length()-1, 1)=="\n")
160                         str = str.substr(0,str.length()-1);
161     }
162     return str;
163 }
164
165 #endif
166