Add verb
[apertium.git] / apertium / apertium / StringUtils.C
blob3bc55fab9314effa344ae6ecc0c4140ec711b1f2
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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
21 #include <apertium/StringUtils.H>
23 #include <iostream>
25 //Delete white spaces from the end and the begining of the string
26 string 
27 StringUtils::trim(string str) { 
28   string::iterator it;
29   
30   while ((str.length()>0)&&((*(it=str.begin()))==' ')) {
31      str.erase(it);
32   }
33   
34   while ((str.length()>0)&&((*(it=(str.end()-1)))==' ')) {
35      str.erase(it);
36   }
38   return str;
41 vector<string>
42 StringUtils::split_string(const string& input, const string& delimiter) {
43   unsigned pos;
44   int new_pos;
45   vector<string> result;
46   string s="";
47   pos=0;
49   while (pos<input.size()) {
50     new_pos=input.find(delimiter, pos);
51     if(new_pos<0)
52       new_pos=input.size();
53     s=input.substr(pos, new_pos-pos);
54     if (s.length()==0) {
55       cerr<<"Warning in StringUtils::split_string: After splitting there is an empty string\n";
56       cerr<<"Skipping this empty string\n";
57     } else
58       result.push_back(s);
59     pos=new_pos+delimiter.size();
60   }
62   return result;
65 string 
66 StringUtils::vector2string(const vector<string>& v) {
67   string s="";
68   for(unsigned i=0; i<v.size(); i++) {
69     if (i>0)
70       s+=" ";
71     s+=v[i];
72   }
73   return s;
76 string 
77 StringUtils::substitute(const string& source, const string& olds, const string& news) {
78   string s=source;
80   unsigned int p=s.find(olds,0);
81   while (p!=static_cast<unsigned int>(string::npos)) {
82     s.replace(p, olds.length(), news);
83     p+=news.length();
84     p=s.find(olds,p);
85   }
87   return s;
90 string
91 StringUtils::itoa(int n) {
92   char str[256];
93   sprintf(str, "%d",n);
94   return str;
97 string
98 StringUtils::ftoa(double f) {
99   char str[256];
100   sprintf(str, "%f",f);
101   return str;
104 string
105 StringUtils::tolower(const string& s) {
106   string l="";
107   for(unsigned i=0; i<s.length(); i++)
108     l+=(char)::tolower(s[i]);
109   return l;
112 string
113 StringUtils::toupper(const string& s) {
114   string l="";
115   for(unsigned i=0; i<s.length(); i++)
116     l+=(char)::toupper(s[i]);
117   return l;