2 * Copyright (C) 2006 Universitat d'Alacant / Universidad de Alicante
3 * author: Felipe Sánchez-Martínez
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.
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.
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
21 #include <apertium/StringUtils.H>
25 //Delete white spaces from the end and the begining of the string
27 StringUtils::trim(string str) {
30 while ((str.length()>0)&&((*(it=str.begin()))==' ')) {
34 while ((str.length()>0)&&((*(it=(str.end()-1)))==' ')) {
42 StringUtils::split_string(const string& input, const string& delimiter) {
45 vector<string> result;
49 while (pos<input.size()) {
50 new_pos=input.find(delimiter, pos);
53 s=input.substr(pos, new_pos-pos);
55 cerr<<"Warning in StringUtils::split_string: After splitting there is an empty string\n";
56 cerr<<"Skipping this empty string\n";
59 pos=new_pos+delimiter.size();
66 StringUtils::vector2string(const vector<string>& v) {
68 for(unsigned i=0; i<v.size(); i++) {
77 StringUtils::substitute(const string& source, const string& olds, const string& news) {
80 unsigned int p=s.find(olds,0);
81 while (p!=static_cast<unsigned int>(string::npos)) {
82 s.replace(p, olds.length(), news);
91 StringUtils::itoa(int n) {
98 StringUtils::ftoa(double f) {
100 sprintf(str, "%f",f);
105 StringUtils::tolower(const string& s) {
107 for(unsigned i=0; i<s.length(); i++)
108 l+=(char)::tolower(s[i]);
113 StringUtils::toupper(const string& s) {
115 for(unsigned i=0; i<s.length(); i++)
116 l+=(char)::toupper(s[i]);