1 /*
2 * Copyright (C) 2006 Universitat d'Alacant / Universidad de Alicante
3 * author: Felipe S�nchez-Mart�nez
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <apertium/string_utils.h>
20 #include <lttoolbox/xml_parse_util.h>
21 #include <iostream>
22 #include <cstring>
23
24 #ifdef _MSC_VER
25 #define snprintf _snprintf
26 #endif
27
28 //Delete white spaces from the end and the begining of the string
29 wstring
trim(wstring const & str)30 StringUtils::trim(wstring const &str)
31 {
32 if(str == L"")
33 {
34 return L"";
35 }
36
37 int begin = 0, end = str.size() - 1;
38
39 while(begin < end && iswspace(str[begin]))
40 {
41 begin++;
42 }
43
44 while(end > begin && iswspace(str[end]))
45 {
46 end--;
47 }
48
49 if(!iswspace(str[end]))
50 {
51 end++;
52 }
53
54 return str.substr(begin, end-begin);
55 }
56
57 vector<wstring>
split_wstring(wstring const & input,wstring const & delimiter)58 StringUtils::split_wstring(wstring const &input, wstring const &delimiter)
59 {
60 unsigned pos;
61 int new_pos;
62 vector<wstring> result;
63 wstring s = L"";
64 pos=0;
65
66 while(pos<input.size())
67 {
68 new_pos=input.find(delimiter, pos);
69 if(new_pos<0)
70 new_pos=input.size();
71 s=input.substr(pos, new_pos-pos);
72 if (s.length()==0) {
73 wcerr<<L"Warning in StringUtils::split_wstring: After splitting there is an empty string\n";
74 wcerr<<L"Skipping this empty string\n";
75 } else
76 result.push_back(s);
77 pos=new_pos+delimiter.size();
78 }
79
80 return result;
81 }
82
83 wstring
vector2wstring(vector<wstring> const & v)84 StringUtils::vector2wstring(vector<wstring> const &v)
85 {
86 wstring s = L"";
87 for(unsigned i=0; i<v.size(); i++)
88 {
89 if (i>0)
90 s+=L' ';
91 s.append(v[i]);
92 }
93 return s;
94 }
95
96 wstring
substitute(wstring const & source,wstring const & olds,wstring const & news)97 StringUtils::substitute(wstring const &source, wstring const &olds, wstring const &news) {
98 wstring s = source;
99
100 unsigned int p=s.find(olds , 0);
101 while (p!=static_cast<unsigned int>(wstring::npos))
102 {
103 s.replace(p, olds.length(), news);
104 p+=news.length();
105 p=s.find(olds,p);
106 }
107
108 return s;
109 }
110
111 wstring
itoa(int n)112 StringUtils::itoa(int n)
113 {
114 return XMLParseUtil::stows(itoa_string(n));
115 }
116
117 string
itoa_string(int n)118 StringUtils::itoa_string(int n)
119 {
120 char str[256];
121 snprintf(str, 256, "%d", n);
122 return str;
123 }
124
125 wstring
ftoa(double f)126 StringUtils::ftoa(double f)
127 {
128 char str[256];
129 sprintf(str, "%f",f);
130 return XMLParseUtil::stows(str);
131 }
132
133 wstring
tolower(wstring const & s)134 StringUtils::tolower(wstring const &s)
135 {
136 wstring l=s;
137 for(unsigned i=0; i<s.length(); i++)
138 {
139 l[i] = (wchar_t) towlower(s[i]);
140 }
141 return l;
142 }
143
144 wstring
toupper(wstring const & s)145 StringUtils::toupper(wstring const &s) {
146 wstring l=s;
147 for(unsigned i=0; i<s.length(); i++)
148 {
149 l[i] = (wchar_t) towupper(s[i]);
150 }
151
152 return l;
153 }
154
operator ==(string const & s1,string const & s2)155 bool Apertium::operator==(string const &s1, string const &s2)
156 {
157 return strcmp(s1.c_str(), s2.c_str()) == 0;
158 }
159
operator ==(string const & s1,char const * s2)160 bool Apertium::operator==(string const &s1, char const *s2)
161 {
162 return strcmp(s1.c_str(), s2) == 0;
163 }
164
operator ==(char const * s1,string const & s2)165 bool Apertium::operator==(char const *s1, string const &s2)
166 {
167 return strcmp(s1, s2.c_str()) == 0;
168 }
169
operator !=(string const & s1,string const & s2)170 bool Apertium::operator!=(string const &s1, string const &s2)
171 {
172 return strcmp(s1.c_str(), s2.c_str()) != 0;
173 }
174
operator !=(string const & s1,char const * s2)175 bool Apertium::operator!=(string const &s1, char const *s2)
176 {
177 return strcmp(s1.c_str(), s2) != 0;
178 }
179
operator !=(char const * s1,string const & s2)180 bool Apertium::operator!=(char const *s1, string const &s2)
181 {
182 return strcmp(s1, s2.c_str()) != 0;
183 }
184
185 #include "string_to_wostream.h"
186