search: * and ? wildcard support added
authorJan Kupec <jkupec@suse.cz>
Fri, 13 Oct 2006 16:29:15 +0000 (16:29 +0000)
committerJan Kupec <jkupec@suse.cz>
Fri, 13 Oct 2006 16:29:15 +0000 (16:29 +0000)
tools/zmart/zypper-search.cc
tools/zmart/zypper-search.h

index b7af7a9..f97a6d0 100644 (file)
@@ -75,29 +75,46 @@ Table ZyppSearch::doSearch() {
   return otable;
 }
 
-// macro for word boundary tags for regexes
+//! macro for word boundary tags for regexes
 #define WB (_options.matchWords() ? string("\\b") : string())
 
+/**
+ * Creates a regex for searching in resolvable names.
+ * 
+ * The regex is created according to given search strings and search options.
+ * 
+ * Examples:
+ *   - no search string: .*
+ *   - one search string: .*searchstring.*
+ *     with --match-words: .*\bsearchstring\b.*
+ *   - more search strings:
+ *     --match-all (default):
+ *       (?=.*searchstring1)(?=.*searchstring2).*
+ *       with --match-words: (?=.*\bsearchstring1\b)(?=.*\bsearchstring2\b).*
+ *     --match-any:
+ *       .*(searchstring1|searchstring2).*
+ *       with --match-words: .*\b(searchstring1|searchstring2)\b.*
+ */
 void ZyppSearch::setupRegexp() {
   string regstr;
 
   if (_qstrings.size() == 0) regstr = ".*";
-  else if (_qstrings.size() == 1) regstr = ".*" + WB + _qstrings[0] + WB + ".*";
+  else if (_qstrings.size() == 1) regstr = ".*" + WB + wildcards2regex(_qstrings[0]) + WB + ".*";
   else {
     vector<string>::const_iterator it = _qstrings.begin();
-    
+
     if (_options.matchAll())
-      regstr = "(?=.*" + WB + *it + WB + ")";
+      regstr = "(?=.*" + WB + wildcards2regex(*it) + WB + ")";
     else
-      regstr = ".*" + WB + "(" + *it;
+      regstr = ".*" + WB + "(" + wildcards2regex(*it);
 
     ++it;
 
     for (; it != _qstrings.end(); ++it) {
       if (_options.matchAll())
-        regstr += "(?=.*" + WB + *it + WB + ")";
+        regstr += "(?=.*" + WB + wildcards2regex(*it) + WB + ")";
       else
-        regstr += "|" + *it;
+        regstr += "|" + wildcards2regex(*it);
     }
 
     if (_options.matchAll())
@@ -121,6 +138,28 @@ void ZyppSearch::setupRegexp() {
   }
 }
 
+/**
+ * Converts '*' and '?' wildcards within str into their regex equivalents.
+ */
+string ZyppSearch::wildcards2regex(const string & str) const {
+  string regexed;
+
+  regex all("\\*"); // regex to search for '*'
+  regex one("\\?"); // regex to search for '?'
+  string r_all(".*"); // regex equivalent of '*'
+  string r_one(".");  // regex equivalent of '?'
+
+  // replace all "*" in input with ".*"
+  regexed = regex_replace(str, all, r_all);
+  cerr_vv << "wildcards2regex: " << str << " -> " << regexed;
+
+  // replace all "?" in input with "."
+  regexed = regex_replace(regexed, one, r_one);
+  cerr_vv << " -> " << regexed << endl;
+
+  return regexed;
+}
+
 bool ZyppSearch::match(const PoolItem & pool_item) {
   // TODO search in descriptions and summaries
   return regex_match(pool_item.resolvable()->name(), _reg); 
index 19149f4..2f7eadf 100644 (file)
@@ -68,9 +68,9 @@ private:
 
   bool init() const;
   void setupRegexp();
+  string wildcards2regex(const string & str) const;
   bool match(const zypp::PoolItem & pool_item);
   TableRow createRow(const zypp::PoolItem & pool_item);
 };
 
-
 #endif /*ZYPPERSEARCH_H_*/