Author: sayer
Date: 2009-10-22 17:06:01 +0200 (Thu, 22 Oct 2009)
New Revision: 1556

Modified:
   trunk/core/AmUtils.cpp
   trunk/core/AmUtils.h
Log:
replaced explode() function with a more efficient version



Modified: trunk/core/AmUtils.cpp
===================================================================
--- trunk/core/AmUtils.cpp      2009-10-22 01:22:47 UTC (rev 1555)
+++ trunk/core/AmUtils.cpp      2009-10-22 15:06:01 UTC (rev 1556)
@@ -972,23 +972,33 @@
     
   return r;
 }
+
 // Explode string by a separator to a vector
-vector <string> explode(string s, string e) {
-  vector <string> ret;
-  int iPos = s.find(e, 0);
-  int iLen = e.length();
-  while (iPos > -1) {
-    if (iPos != 0)
-      ret.push_back(s.substr(0, iPos));
-    s.erase(0, iPos+iLen);
-    iPos = s.find(e, 0);
+// see http://stackoverflow.com/questions/236129/c-how-to-split-a-string
+std::vector<string> explode(const string& s, const string& delim, 
+                           const bool keep_empty) {
+  vector<string> result;
+  if (delim.empty()) {
+    result.push_back(s);
+    return result;
   }
-  if (s != "")
-    ret.push_back(s);
-  return ret;
+  string::const_iterator substart = s.begin(), subend;
+  while (true) {
+    subend = search(substart, s.end(), delim.begin(), delim.end());
+    string temp(substart, subend);
+    if (keep_empty || !temp.empty()) {
+      result.push_back(temp);
+    }
+    if (subend == s.end()) {
+      break;
+    }
+    substart = subend + delim.size();
+  }
+  return result;
 }
 
 
+
 // Warning: static var is not mutexed
 // Call this func only in init code.
 //

Modified: trunk/core/AmUtils.h
===================================================================
--- trunk/core/AmUtils.h        2009-10-22 01:22:47 UTC (rev 1555)
+++ trunk/core/AmUtils.h        2009-10-22 15:06:01 UTC (rev 1556)
@@ -286,7 +286,7 @@
 unsigned int get_random();
 
 /** Explode string by a separator to a vector */
-std::vector <string> explode(string s, string e);
+std::vector<string> explode(const string& s, const string& delim, const bool 
keep_empty = false);
 
 /** add a directory to an environement variable */
 void add_env_path(const char* name, const string& path);

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to