1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Header: string_utilities.h
7 ------------- Copyright (C) 2009 Jon S. Berndt (jon@jsbsim.org) -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA.
23 Further information about the GNU Lesser General Public License can also be found on
24 the world wide web at http://www.gnu.org.
27 --------------------------------------------------------------------------------
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 #define ID_STRINGUTILS "$Id: string_utilities.h,v 1.18 2014/01/13 10:46:03 ehofman Exp $"
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 extern std::string& trim_left(std::string& str);
69 extern std::string& trim_right(std::string& str);
70 extern std::string& trim(std::string& str);
71 extern std::string& trim_all_space(std::string& str);
72 extern std::string& to_upper(std::string& str);
73 extern std::string& to_lower(std::string& str);
74 extern bool is_number(const std::string& str);
75 std::vector <std::string> split(std::string str, char d);
77 // libc++ has these as built-ins for all C++ language versions
78 #if !defined(_LIBCPP_VERSION)
79 extern std::string to_string(int);
80 extern std::string to_string(double);
81 extern std::string to_string(float);
84 extern std::string replace(std::string str, const std::string& old, const std::string& newstr);
90 string& trim_left(string& str)
92 while (str.size() && isspace((unsigned char)str[0])) {
98 string& trim_right(string& str)
100 while (str.size() && isspace((unsigned char)str[str.size()-1])) {
101 str = str.erase(str.size()-1,1);
106 string& trim(string& str)
108 if (str.size() == 0) return str;
109 string temp_str = trim_right(str);
110 return str = trim_left(temp_str);
113 string& trim_all_space(string& str)
115 for (size_t i=0; i<str.size(); i++) {
116 if (isspace((unsigned char)str[i])) {
117 str = str.erase(i,1);
124 string& to_upper(string& str)
126 for (size_t i=0; i<str.size(); i++) str[i] = toupper(str[i]);
130 string& to_lower(string& str)
132 for (size_t i=0; i<str.size(); i++) str[i] = tolower(str[i]);
136 bool is_number(const string& str)
139 return (str.find_first_not_of("+-.0123456789Ee") == string::npos);
144 vector <string> split(string str, char d)
146 vector <string> str_array;
152 while (index != string::npos) {
153 temp = str.substr(0,index);
155 if (temp.size() > 0) str_array.push_back(temp);
156 str = str.erase(0,index+1);
159 if (str.size() > 0) {
161 if (temp.size() > 0) str_array.push_back(temp);
166 /* Comment out to_string functions when they are defined already - C++ 11 defines these */
167 string to_string(int i)
170 sprintf(buffer, "%d", i);
171 return string(buffer);
174 string to_string(float x)
176 std::ostringstream o;
177 if (!(o << x)) cerr << "Bad float to string conversion" << endl;
181 string to_string(double x)
183 std::ostringstream o;
184 if (!(o << x)) cerr << "Bad double to string conversion" << endl;
188 string replace(string str, const string& oldstr, const string& newstr)
192 old_idx = str.find(oldstr);
194 temp = str.replace(old_idx, 1, newstr);
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%