Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / base / String.cc
index f332d3f..be0bb64 100644 (file)
@@ -17,6 +17,8 @@
 #include "zypp/base/String.h"
 #include "zypp/base/LogTools.h"
 
+#include "zypp/TriBool.h"
+
 using std::string;
 
 ///////////////////////////////////////////////////////////////////
@@ -86,6 +88,13 @@ namespace zypp
                );
     }
 
+    TriBool strToTriBool( const C_Str & str )  // from TriBool.h
+    {
+      if ( strToTrue( str ) )  return true;
+      if ( !strToFalse( str ) )        return false;
+      return indeterminate;
+    }
+
     ///////////////////////////////////////////////////////////////////
     // Hexencode
     ///////////////////////////////////////////////////////////////////
@@ -102,10 +111,10 @@ namespace zypp
       {
         if ( '0' <= ch && ch <= '9' )
           return( ch - '0' );
-        if ( 'A' <= ch && ch <= 'Z' )
-          return( ch - 'A' + 10 );
-        if ( 'a' <= ch && ch <= 'z' )
+        if ( 'A' <= ch && ch <= 'F' )
           return( ch - 'A' + 10 );
+        if ( 'a' <= ch && ch <= 'f' )
+          return( ch - 'a' + 10 );
         return -1;
       }
     }
@@ -164,11 +173,15 @@ namespace zypp
      **      FUNCTION TYPE : std::string
     */
     std::string toLower( const std::string & s )
+    { return toLower( std::string(s) ); }
+
+    std::string toLower( std::string && s )
     {
-      if ( s.empty() )
-        return s;
+      std::string ret( std::move(s) );
+
+      if ( ret.empty() )
+        return ret;
 
-      std::string ret( s );
       for ( std::string::size_type i = 0; i < ret.length(); ++i )
         {
           if ( isupper( ret[i] ) )
@@ -183,11 +196,15 @@ namespace zypp
      **      FUNCTION TYPE : std::string
     */
     std::string toUpper( const std::string & s )
+    { return toUpper( std::string(s) ); }
+
+    std::string toUpper( std::string && s )
     {
-      if ( s.empty() )
-        return s;
+      std::string ret( std::move(s) );
+
+      if ( ret.empty() )
+        return ret;
 
-      std::string ret( s );
       for ( std::string::size_type i = 0; i < ret.length(); ++i )
         {
           if ( islower( ret[i] ) )
@@ -202,29 +219,36 @@ namespace zypp
      **      FUNCTION TYPE : std::string
     */
     std::string trim( const std::string & s, const Trim trim_r )
+    { return trim( std::string(s), trim_r ); }
+
+    std::string trim( std::string && s, const Trim trim_r )
     {
-      if ( s.empty() || trim_r == NO_TRIM )
-        return s;
+      std::string ret( std::move(s) );
 
-      std::string ret( s );
+      if ( ret.empty() || trim_r == NO_TRIM )
+        return ret;
 
       if ( trim_r & L_TRIM )
-        {
-          std::string::size_type p = ret.find_first_not_of( " \t\n" );
-          if ( p == std::string::npos )
-            return std::string();
-
-          ret = ret.substr( p );
-        }
+      {
+       std::string::size_type p = ret.find_first_not_of( " \t\n" );
+       if ( p == std::string::npos )
+       {
+         ret.clear();
+         return ret;
+       }
+       ret.erase( 0, p );
+      }
 
       if ( trim_r & R_TRIM )
-        {
-          std::string::size_type p = ret.find_last_not_of( " \t\n" );
-          if ( p == std::string::npos )
-            return std::string();
-
-          ret = ret.substr( 0, p+1 );
-        }
+      {
+       std::string::size_type p = ret.find_last_not_of( " \t\n" );
+       if ( p == std::string::npos )
+       {
+         ret.clear();
+         return ret;
+       }
+       ret = ret.erase( p+1 );
+      }
 
       return ret;
     }
@@ -303,14 +327,17 @@ namespace zypp
 
     std::string & replaceAll( std::string & str_r, const std::string & from_r, const std::string & to_r )
     {
-      std::string::size_type pos = 0;
-      while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
+      if ( ! from_r.empty() )
       {
-        str_r.replace( pos, from_r.size(), to_r );
-        pos += to_r.size();
+       std::string::size_type pos = 0;
+       while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
+       {
+         str_r.replace( pos, from_r.size(), to_r );
+         pos += to_r.size();
 
-        if ( pos >= str_r.length() )
-          break;
+         if ( pos >= str_r.length() )
+           break;
+       }
       }
       return str_r;
     }
@@ -323,15 +350,18 @@ namespace zypp
 
     std::string & replaceAllFun( std::string & str_r, const std::string & from_r, function<std::string()> to_r )
     {
-      std::string::size_type pos = 0;
-      while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
+      if ( ! from_r.empty() )
       {
-       std::string to( to_r() );
-        str_r.replace( pos, from_r.size(), to );
-        pos += to.size();
+       std::string::size_type pos = 0;
+       while ( (pos = str_r.find( from_r, pos )) != std::string::npos )
+       {
+         std::string to( to_r() );
+         str_r.replace( pos, from_r.size(), to );
+         pos += to.size();
 
-        if ( pos >= str_r.length() )
-          break;
+         if ( pos >= str_r.length() )
+           break;
+       }
       }
       return str_r;
     }