mail[Wesnoth-commits] r20190 - /branches/1.2/src/serialization/string_utils.cpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by patrick_X99 on September 08, 2007 - 05:23:
Author: sapient
Date: Sat Sep  8 05:23:07 2007
New Revision: 20190

URL: http://svn.gna.org/viewcvs/wesnoth?rev=20190&view=rev
Log:
backport r20188 to the 1.2 branch

Modified:
    branches/1.2/src/serialization/string_utils.cpp

Modified: branches/1.2/src/serialization/string_utils.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/1.2/src/serialization/string_utils.cpp?rev=20190&r1=20189&r2=20190&view=diff
==============================================================================
--- branches/1.2/src/serialization/string_utils.cpp (original)
+++ branches/1.2/src/serialization/string_utils.cpp Sat Sep  8 05:23:07 2007
@@ -670,56 +670,47 @@
        return s;
 }
 
+// match using '*' as any number of characters (including none), and '?' as 
any one character
 bool wildcard_string_match(const std::string& str, const std::string& match)
 {
-       // match using '*' as any number of characters (including none), and 
'?' as any one character
-       std::string::const_iterator c = str.begin();
-       std::string::const_iterator m = match.begin();
-       while(c != str.end() && m != match.end()) {
-               if(*m == '?') {
-                       ++c;
-                       ++m;
-               } else if(*m == '*') {
-                       while(c != str.end()) {
-                               if(*c == *(m+1)) {
-                                       std::string ns,nm;
-                                       ns.assign(c+1,str.end());
-                                       nm.assign(m,match.end());
-                                       if(ns.find(*(m+1)) != 
std::string::npos) {
-                                               
if(wildcard_string_match(ns,nm)) {
-                                                       return true;
-                                               }
-                                       } else {
-                                               break;
-                                       }
+       bool matches = false;
+       for(std::string::size_type current = 0; current <= str.length() && 
!matches; ++current) {
+               std::string test_str = str.substr(current);
+               bool wild_matching = (!match.empty() && match[0] == '*');
+               std::string::size_type solid_begin = 
match.find_first_not_of('*');
+               bool have_solids = (solid_begin != std::string::npos);
+               if(test_str.empty()) {
+                       matches = match.empty() || (wild_matching && 
!have_solids);
+               } else if(!have_solids) {
+                       matches = wild_matching;
+               } else {
+                       //try to place the first char of str into the solid 
space
+                       matches = true;
+                       const std::string::size_type solid_end = 
match.find_first_of('*', solid_begin);
+                       const std::string::size_type solid_len = (solid_end 
== std::string::npos)
+                               ? match.length() - solid_begin : solid_end - 
solid_begin;
+                       for(std::string::size_type i=0; i < solid_len; ++i) {
+                               if(i > test_str.length()) {
+                                       matches = false;
+                                       break;
                                }
-                               ++c;
-                       }
-                       ++m;
-               } else {
-                       if(*c != *m) {
-                               return false;
-                       }
-                       ++c;
-                       ++m;
-               }
-       }
-       if(m == match.end()) {
-               if(c == str.end()) {
-                       return true;
-               } else {
-                       return false;
-               }
-       } else {
-               if(c == str.end()) {
-                       if(*m == '*') {
-                               return true;
-                       }
-                       return false;
-               } else {
-                       return false;
-               }
-       }
+                               char solid_c = match[solid_begin + i];
+                               if(solid_c != '?' && solid_c != test_str[i]) {
+                                       matches = false;
+                                       break;
+                               }
+                       }
+                       if(matches) {
+                               //the solid space matched, now consume it and 
attempt to find more
+                               std::string consumed_match = 
(solid_begin+solid_len < match.length())
+                                       ? match.substr(solid_end) : "";
+                               std::string consumed_str = (solid_len < 
test_str.length())
+                                       ? test_str.substr(solid_len) : "";
+                               matches = wildcard_string_match(consumed_str, 
consumed_match);
+                       }
+               }
+       }
+       return matches;
 }
 
 




Related Messages


Powered by MHonArc, Updated Sat Sep 08 05:40:11 2007