1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/String.cc
17 #include "zypp/base/String.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 /******************************************************************
30 ** FUNCTION NAME : form
31 ** FUNCTION TYPE : std::string
33 std::string form( const char * format, ... )
38 va_start( ap, format );
39 vasprintf( &safe._buf, format, ap );
42 return safe.asString();
45 /******************************************************************
47 ** FUNCTION NAME : strerror
48 ** FUNCTION TYPE : std::string
50 std::string strerror( int errno_r )
52 return form( "(%d)%s", errno_r, ::strerror( errno_r ) );
55 /******************************************************************
57 ** FUNCTION NAME : strToTrue
58 ** FUNCTION TYPE : bool
60 bool strToTrue( const C_Str & str )
62 std::string t( toLower( str ) );
67 || strtonum<long long>( str )
71 /******************************************************************
73 ** FUNCTION NAME : strToFalse
74 ** FUNCTION TYPE : bool
76 bool strToFalse( const C_Str & str )
78 std::string t( toLower( str ) );
86 ///////////////////////////////////////////////////////////////////
88 ///////////////////////////////////////////////////////////////////
90 /** What's not decoded. */
91 inline bool heIsAlNum( char ch )
93 return ( ( 'a' <= ch && ch <= 'z' )
94 ||( 'A' <= ch && ch <= 'Z' )
95 ||( '0' <= ch && ch <= '9' ) );
97 /** Hex-digit to number or -1. */
98 inline int heDecodeCh( char ch )
100 if ( '0' <= ch && ch <= '9' )
102 if ( 'A' <= ch && ch <= 'Z' )
103 return( ch - 'A' + 10 );
104 if ( 'a' <= ch && ch <= 'z' )
105 return( ch - 'A' + 10 );
110 std::string hexencode( const C_Str & str_r )
112 static const char *const hdig = "0123456789ABCDEF";
114 res.reserve( str_r.size() );
115 for ( const char * it = str_r.c_str(); *it; ++it )
117 if ( heIsAlNum( *it ) )
124 res += hdig[(unsigned char)(*it)/16];
125 res += hdig[(unsigned char)(*it)%16];
131 std::string hexdecode( const C_Str & str_r )
134 res.reserve( str_r.size() );
135 for_( it, str_r.c_str(), str_r.c_str()+str_r.size() )
139 int d1 = heDecodeCh( *(it+1) );
142 int d2 = heDecodeCh( *(it+2) );
151 // verbatim if no %XX:
156 ///////////////////////////////////////////////////////////////////
158 /******************************************************************
160 ** FUNCTION NAME : toLower
161 ** FUNCTION TYPE : std::string
163 std::string toLower( const std::string & s )
168 std::string ret( s );
169 for ( std::string::size_type i = 0; i < ret.length(); ++i )
171 if ( isupper( ret[i] ) )
172 ret[i] = static_cast<char>(tolower( ret[i] ));
177 /******************************************************************
179 ** FUNCTION NAME : toUpper
180 ** FUNCTION TYPE : std::string
182 std::string toUpper( const std::string & s )
187 std::string ret( s );
188 for ( std::string::size_type i = 0; i < ret.length(); ++i )
190 if ( islower( ret[i] ) )
191 ret[i] = static_cast<char>(toupper( ret[i] ));
196 /******************************************************************
198 ** FUNCTION NAME : trim
199 ** FUNCTION TYPE : std::string
201 std::string trim( const std::string & s, const Trim trim_r )
203 if ( s.empty() || trim_r == NO_TRIM )
206 std::string ret( s );
208 if ( trim_r & L_TRIM )
210 std::string::size_type p = ret.find_first_not_of( " \t\n" );
211 if ( p == std::string::npos )
212 return std::string();
214 ret = ret.substr( p );
217 if ( trim_r & R_TRIM )
219 std::string::size_type p = ret.find_last_not_of( " \t\n" );
220 if ( p == std::string::npos )
221 return std::string();
223 ret = ret.substr( 0, p+1 );
229 /******************************************************************
231 ** FUNCTION NAME : stripFirstWord
232 ** FUNCTION TYPE : std::string
234 std::string stripFirstWord( std::string & line, const bool ltrim_first )
237 line = ltrim( line );
243 std::string::size_type p = line.find_first_of( " \t" );
245 if ( p == std::string::npos ) {
249 } else if ( p == 0 ) {
252 line = ltrim( line );
255 // strip word and ltim line
256 ret = line.substr( 0, p );
257 line = ltrim( line.erase( 0, p ) );
262 /******************************************************************
264 ** FUNCTION NAME : stripLastWord
265 ** FUNCTION TYPE : std::string
267 std::string stripLastWord( std::string & line, const bool rtrim_first )
270 line = rtrim( line );
276 std::string::size_type p = line.find_last_of( " \t" );
278 if ( p == std::string::npos ) {
282 } else if ( p == line.size()-1 ) {
285 line = rtrim( line );
288 // strip word and rtim line
289 ret = line.substr( p+1 );
290 line = rtrim( line.erase( p ) );
295 string gsub(const string& sData, const string& sFrom, const string& sTo)
298 sNew.reserve(sData.size());
302 string::size_type frLen = sFrom.length();
303 string::size_type loc = 0;
304 string::size_type oldLoc = 0;
306 while (string::npos != (loc = sData.find(sFrom, loc)))
308 sNew.append(sData,oldLoc,loc-oldLoc);
312 if (loc >= sData.length())
315 if (oldLoc!=sData.size())
316 sNew.append(sData,oldLoc,sData.size()-oldLoc);
322 string & replaceAll(string & str, const string & from, const string & to)
324 string::size_type pos = 0;
325 while((pos = str.find(from, pos)) != string::npos)
327 str.replace(pos, from.size(), to);
330 if (pos >= str.length())
337 std::string escape( const std::string & str_r, const char sep_r )
339 std::vector<char> buf;
340 for_( s, str_r.begin(), str_r.end() )
347 buf.push_back( '\\' );
352 buf.push_back( '\\' );
356 return std::string( buf.begin(), buf.end() );
361 /******************************************************************
364 ** FUNCTION NAME : getline
365 ** FUNCTION TYPE : std::string
369 static inline std::string _getline( std::istream & str, const Trim trim_r )
371 const unsigned tmpBuffLen = 1024;
372 char tmpBuff[tmpBuffLen];
377 str.getline( tmpBuff, tmpBuffLen ); // always writes '\0' terminated
379 } while( str.rdstate() == std::ios::failbit );
381 return trim( ret, trim_r );
384 std::string getline( std::istream & str, const Trim trim_r )
386 return _getline(str, trim_r);
389 std::string getline( std::istream & str, bool trim )
391 return _getline(str, trim?TRIM:NO_TRIM);
394 /////////////////////////////////////////////////////////////////
396 ///////////////////////////////////////////////////////////////////
397 ////////////////////////////////////////////////////////////////
399 //////////////////////////////////////////////////////////////////