3 // *****************************************************************************
4 // *****************************************************************************
6 // global string functions
8 // *****************************************************************************
9 // *****************************************************************************
11 // The following functions should be language specific
12 bool is_text_char ( chart ch )
14 if ( ch >= 'a' && ch <= 'z' ) return true ;
15 if ( ch >= 'A' && ch <= 'Z' ) return true ;
22 string left ( string &s , size_t num )
24 if ( num <= 0 ) return "" ;
25 if ( num >= s.length() ) return s ;
26 return s.substr ( 0 , num ) ;
29 string right ( string &s , int num )
31 if ( num <= 0 ) return "" ;
32 int from = s.length() - num ;
34 if ( from <= 0 ) ret = s ;
35 else ret = s.substr ( from , s.length() ) ;
39 string upper ( string s ) // For internal purposes, will do...
42 for ( a = 0 ; a < s.length() ; a++ )
44 if ( s[a] >= 'a' && s[a] <= 'z' ) s[a] = s[a] - 'a' + 'A' ;
49 void explode ( chart ch , string &l , vector <string> &parts )
53 for ( a = b = 0 ; a < l.length() ; a++ )
57 parts.push_back ( l.substr ( b , a - b ) ) ;
61 parts.push_back ( l.substr ( b , a - b ) ) ;
63 if ( debug ) cout << "Explode : " << l << endl ;
64 for ( a = 0 ; a < parts.size() ; a++ )
65 if ( debug ) cout << a << " " << parts[a] << endl ;
66 if ( debug ) cout << endl ;
69 string implode ( string mid , vector <string> &parts )
71 if ( parts.size() == 0 ) return "" ;
72 if ( parts.size() == 1 ) return parts[0] ;
73 string ret = parts[0] ;
74 for ( size_t a = 1 ; a < parts.size() ; a++ )
75 ret += mid + parts[a] ;
79 string unquote ( chart quote , string &s )
82 for ( a = 0 ; a < s.length() ; a++ )
84 if ( s[a] == quote && ( a == 0 || ( a > 0 && s[a-1] != '\\' ) ) )
86 s.insert ( a , "\\" ) ;
93 bool submatch ( string &main , string &sub , int from )
95 if ( from + sub.length() > main.length() ) return false ;
97 for ( a = 0 ; a < sub.length() ; a++ )
99 if ( sub[a] != main[a+from] ) return false ;
104 int find_first ( chart c , string &s )
107 for ( a = 0 ; a < s.length() && s[a] != c ; a++ ) ;
108 if ( a == s.length() ) return -1 ;
112 int find_last ( chart c , string &s )
116 for ( a = 0 ; a < s.length() ; a++ )
118 if ( s[a] == c ) b = a ;
123 string before_first ( chart c , string s )
125 int pos = find_first ( c , s ) ;
126 if ( pos == -1 ) return s ;
127 return s.substr ( 0 , pos ) ;
130 string before_last ( chart c , string s )
132 int pos = find_last ( c , s ) ;
133 if ( pos == -1 ) return "" ;
134 return s.substr ( 0 , pos ) ;
137 string after_first ( chart c , string s )
139 int pos = find_first ( c , s ) ;
140 if ( pos == -1 ) return "" ;
141 return s.substr ( pos+1 , s.length() ) ;
144 string after_last ( chart c , string s )
146 int pos = find_last ( c , s ) ;
147 if ( pos == -1 ) return s ;
148 return s.substr ( pos+1 , s.length() ) ;
151 string trim ( string &s )
153 if ( s.length() == 0 ) return s ;
154 if ( s[0] != ' ' && s[s.length()-1] != ' ' ) return s ;
157 for ( a = 0 ; a < s.length() && s[a] == ' ' ; a++ ) ;
158 for ( b = s.length()-1 ; b >= 0 && s[b] == ' ' ; b-- ) ;
159 return s.substr ( a , b - a + 1 ) ;
162 int find_next_unquoted ( chart c , string &s , int start )
165 chart lastquote = ' ' ;
166 for ( a = start ; a < s.length() ; a++ )
168 if ( s[a] == c && lastquote == ' ' ) return a ; // Success!
169 if ( s[a] != SINGLE_QUOTE && s[a] != DOUBLE_QUOTE ) continue ; // No quotes, next
170 if ( a > 0 && s[a-1] == '\\' ) continue ; // Ignore \' and \"
171 if ( lastquote == ' ' ) lastquote = s[a] ; // Remember opening quote, text now quoted
172 else if ( lastquote == s[a] ) lastquote = ' ' ; // Close quote, not quoted anymore
180 sprintf ( t , "%d" , a ) ;
181 return string ( t ) ;
184 string xml_embed ( string inside , string tag , string param )
188 if ( param != "" ) ret += " " + param ;
189 if ( inside == "" ) return ret + "/>" ;
190 return ret + ">" + trim ( inside ) + "</" + tag + ">" ;
193 string xml_params ( string l ) // Yes, this function is thin...
196 vector <string> params ;
199 int p = find_next_unquoted ( ' ' , l ) ;
208 first = left ( l , p ) ;
209 l = l.substr ( p , l.length() - p ) ;
211 first = trim ( first ) ;
213 if ( first == "" ) continue ;
215 p = find_next_unquoted ( '=' , first ) ;
216 if ( p == -1 ) first = xml_embed ( first , "value" ) ;
219 first = xml_embed ( left ( first , p ) , "key" ) +
220 xml_embed ( first.substr ( p + 1 , first.length() - p ) , "value" ) ;
222 first = xml_embed ( first , "wikiparameter" ) ;