00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00034 #include "pinapa-parser-all.h"
00035
00036 #include <string>
00037 #include "pinapa-hash.h"
00038 #include <sstream>
00039
00040 #ifndef PINAPA_STRING_H
00041 #define PINAPA_STRING_H
00042
00043 namespace pinapa {
00044
00045 inline void replace( string& Source, const string& Find, const string& Replacement)
00046 {
00047 int Length = Find.length();
00048 string::size_type Pos = 0;
00049 while( (Pos = Source.find( Find, Pos )) != string::npos ) {
00050 Source.replace( Pos, Length, Replacement );
00051 Pos += 1;
00052 }
00053 }
00054
00055 inline bool end_with( string& Source, const string& End)
00056 {
00057 int sl = Source.length();
00058 int el = End.length();
00059 if(0==sl) return 0==el;
00060 else return sl >= el
00061 && Source.substr( sl-el, el) == End;
00062 }
00063
00064 inline void end_with__test(){
00065 string a,b; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00066 a = "aab"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00067 b = "b"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00068 b = "ab"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00069 b = "aab"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00070 b = "aaab"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00071 b = "a"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00072 a = ""; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00073 a = "aa"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00074 b = "aaa"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00075 b = "aa"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00076 b = "a"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00077 a = "baab"; cout << "\"" << a << "\" ends with \"" << b << "\" : " << end_with( a, b) << endl;
00078 }
00079
00080
00081 inline void push_split( const string &str, const string &sep, vector<string> &dest) {
00082 string::size_type pos = 0;
00083 string::size_type old_pos = 0;
00084 while( (pos = str.find( sep, pos )) != string::npos ) {
00085 dest.push_back( str.substr( old_pos, pos - old_pos));
00086 pos +=1;
00087 old_pos = pos;
00088 }
00089 dest.push_back( str.substr( old_pos, pos - old_pos));
00090 }
00091
00092 inline void push_split__test() {
00093 vector<string> res;
00094 string text;
00095 text = "a"; res.push_back( text); push_split( text, ",", res);
00096 text = "aaaaa"; res.push_back( text); push_split( text, ",", res);
00097 text = "a,b"; res.push_back( text); push_split( text, ",", res);
00098 text = "aaaaa,bbbbb"; res.push_back( text); push_split( text, ",", res);
00099 text = "a,b,c"; res.push_back( text); push_split( text, ",", res);
00100 text = "a,b,c,d"; res.push_back( text); push_split( text, ",", res);
00101 text = ""; res.push_back( text); push_split( text, ",", res);
00102 text = ","; res.push_back( text); push_split( text, ",", res);
00103 text = ",a,,"; res.push_back( text); push_split( text, ",", res);
00104 text = ",a,,b"; res.push_back( text); push_split( text, ",", res);
00105 text = " , "; res.push_back( text); push_split( text, ",", res);
00106 text = " , , "; res.push_back( text); push_split( text, ",", res);
00107 for( vector<string>::iterator it = res.begin();
00108 it != res.end();
00109 it++) {
00110 cout << "|" << *it << "|" << endl;
00111 }
00112 }
00113
00114
00116 template <class T>
00117 string string_from(const T &val,
00118 ios_base & (*f)(ios_base&) = dec)
00119 {
00120 ostringstream oss(ostringstream::out);
00121 oss<<f<<val;
00122 return oss.str();
00123 }
00124
00126 template <class T>
00127 T to(const std::string &s,
00128 std::ios_base & (*f)(std::ios_base&) = std::dec)
00129 {
00130 T t;
00131 std::istringstream iss(s);
00132 iss>>f>>t;
00133 if( iss.fail()) throw 333;
00134 return t;
00135 }
00136
00137
00138
00141 inline string defensive_getenv(const char * variable) {
00142 char * val = getenv(variable);
00143 PINAPA_ASSERT(val != NULL, "The environment variable " << variable <<
00144 " is supposed to be defined, but is not");
00145 return string(val);
00146 }
00147
00148 }
00149
00150 #endif // PINAPA_STRING_H
00151