Remove duplicate code detecting known kinds and move it to ResKind::explicitBuiltin
authorMichael Andres <ma@suse.de>
Tue, 25 Feb 2014 10:23:58 +0000 (11:23 +0100)
committerMichael Andres <ma@suse.de>
Tue, 25 Feb 2014 10:23:58 +0000 (11:23 +0100)
zypp/ResKind.cc
zypp/ResKind.h
zypp/sat/Solvable.cc
zypp/sat/Solvable.h

index d7b8d39..ad0fb7e 100644 (file)
@@ -43,6 +43,28 @@ namespace zypp
   template<>
     const ResKind ResTraits<Application>::kind( ResKind::application );
 
+  ResKind ResKind::explicitBuiltin( const char * str_r )
+  {
+    if ( str_r && str_r[0] && str_r[1] && str_r[2] )
+    {
+      switch ( str_r[3] )
+      {
+       // NOTE: it needs to be assertd that the separating ':' is present
+       // if a known kind is retuirned. Dependent code relies on this!
+       #define OUTS(K,S) if ( !::strncmp( str_r, ResKind::K.c_str(), S ) && str_r[S] == ':' ) return ResKind::K
+       //             ----v
+       case 'c': OUTS( patch, 5 );       break;
+       case 'd': OUTS( product, 7 );     break;
+       case 'k': OUTS( package, 7 );     break;
+       case 'l': OUTS( application, 11 );break;
+       case 'p': OUTS( srcpackage, 10 ); break;
+       case 't': OUTS( pattern, 7 );     break;
+       #undef OUTS
+      }
+    }
+    return nokind;
+  }
+
   std::string ResKind::satIdent( const ResKind & refers_r, const std::string & name_r )
   {
     if ( ! refers_r || refers_r == package || refers_r == srcpackage )
index f75e63a..c47498a 100644 (file)
@@ -45,6 +45,19 @@ namespace zypp
       static const ResKind application;
       //@}
 
+      /** Return the builtin kind if \a str_r explicitly prefixed.
+       * \a str_r must start with a builtin kind followed by a \c ':'.
+       * If no builtin kind is detected, \ref nokind is returned,
+       * which usually indicates a \ref package or \ref srcpackage.
+       */
+      static ResKind explicitBuiltin( const char * str_r );
+      /** \overload */
+      static ResKind explicitBuiltin( const std::string & str_r )
+      { return explicitBuiltin( str_r.c_str() ); }
+      /** \overload */
+      static ResKind explicitBuiltin( const IdString & str_r )
+      { return explicitBuiltin( str_r.c_str() ); }
+
     public:
       /** Default ctor: \ref nokind */
       ResKind() {}
index cfdc974..0e84a0b 100644 (file)
@@ -43,7 +43,7 @@ namespace zypp
         if ( ! _ident )
           return;
 
-       ResKind explicitKind = Solvable::SplitIdent::explicitKind( _ident.c_str() );
+       ResKind explicitKind = ResKind::explicitBuiltin( _ident.c_str() );
        // NOTE: kind package and srcpackage do not have namespaced ident!
        if ( ! explicitKind  )
        {
@@ -51,7 +51,7 @@ namespace zypp
          // No kind defaults to package
          if ( !_kind )
            _kind = ResKind::package;
-         if ( ! ( _kind == ResKind::package || _kind == ResKind::srcpackage ) )
+         else if ( ! ( _kind == ResKind::package || _kind == ResKind::srcpackage ) )
            _ident = IdString( str::form( "%s:%s", _kind.c_str(), _ident.c_str() ) );
        }
        else
@@ -88,33 +88,6 @@ namespace zypp
     , _kind( kind_r )
     { _doSplit( _ident, _kind, _name ); }
 
-    ResKind Solvable::SplitIdent::explicitKind( const char * ident_r )
-    {
-      if ( ! ident_r )
-       return ResKind();
-
-      const char * sep = ::strchr( ident_r, ':' );
-      if ( ! sep )
-       return ResKind();
-
-      ResKind ret;
-      if ( sep-ident_r >= 4 )
-      {
-       switch ( ident_r[3] )
-       {
-         #define OUTS(K,S) if ( !::strncmp( ident_r, ResKind::K.c_str(), S ) && ident_r[S] == ':' ) ret = ResKind::K
-         //             ----v
-         case 'c': OUTS( patch, 5 );       break;
-         case 'd': OUTS( product, 7 );     break;
-         case 'k': OUTS( package, 7 );     break;
-         case 'p': OUTS( srcpackage, 10 ); break;
-         case 't': OUTS( pattern, 7 );     break;
-         #undef OUTS
-       }
-      }
-      return ret;
-    }
-
     /////////////////////////////////////////////////////////////////
 
     const Solvable Solvable::noSolvable;
@@ -307,30 +280,18 @@ namespace zypp
           break;
       }
 
+      // either explicitly prefixed...
       const char * ident = IdString( _solvable->name ).c_str();
-      const char * sep = ::strchr( ident, ':' );
+      ResKind knownKind( ResKind::explicitBuiltin( ident ) );
+      if ( knownKind )
+       return knownKind;
 
-      // no ':' in package names (hopefully)
+      // ...or no ':' in package names (hopefully)...
+      const char * sep = ::strchr( ident, ':' );
       if ( ! sep )
-        return ResKind::package;
-
-      // quick check for well known kinds
-      if ( sep-ident >= 4 )
-      {
-        switch ( ident[3] )
-        {
-#define OUTS(K,S) if ( !::strncmp( ident, ResKind::K.c_str(), S ) ) return ResKind::K
-          //             ----v
-          case 'c': OUTS( patch, 5 );       break;
-          case 'd': OUTS( product, 7 );     break;
-          case 'k': OUTS( package, 7 );     break;
-          case 'p': OUTS( srcpackage, 10 ); break;
-          case 't': OUTS( pattern, 7 );     break;
-#undef OUTS
-        }
-      }
+       return ResKind::package;
 
-      // an unknown kind
+      // ...or something unknown.
       return ResKind( std::string( ident, sep-ident ) );
     }
 
index 7a5a4ee..ffefa01 100644 (file)
@@ -275,14 +275,6 @@ namespace zypp
             ResKind  kind()  const { return _kind; }
             IdString name()  const { return _name; }
 
-           /** Return an idents explicit kind prefix, or \ref ResKind() if none.
-            * Mainly to detect whether a given ident string is explicitly prefixed
-            * by a known kind (e.g \c pattern:foo or \c package:foo).
-            */
-           static ResKind explicitKind( IdString ident_r )             { return explicitKind( ident_r.c_str() );  }
-           static ResKind explicitKind( const char * ident_r );
-           static ResKind explicitKind( const std::string & ident_r )  { return explicitKind( ident_r.c_str() );  }
-
           private:
             IdString  _ident;
             ResKind   _kind;