]> git.sesse.net Git - freerainbowtables/commitdiff
Fix O(n^2) behavior in ReadLinesFromFile().
authorSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Mon, 21 Feb 2011 21:44:18 +0000 (22:44 +0100)
committerSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Mon, 21 Feb 2011 21:44:18 +0000 (22:44 +0100)
Client Applications/rcracki_mt/Public.cpp
Client Applications/rcracki_mt/Public.h

index 6e02af61d23d0d2fe2647b10c9b07fa3982445ef..5e041cf02bad8aad2249f2d9fdb19573f4709732 100644 (file)
@@ -153,8 +153,9 @@ string TrimString(string s)
 \r
        while (s.size() > 0)\r
        {\r
-               if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')\r
-                       s = s.substr(0, s.size() - 1);\r
+               if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t' ||\r
+                   s[s.size() - 1] == '\r' || s[s.size() - 1] == '\n')\r
+                       s.resize(s.size() - 1);\r
                else\r
                        break;\r
        }\r
@@ -250,21 +251,20 @@ bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
                content += "\n";\r
                delete [] data;\r
 \r
-               unsigned int i;\r
-               for (i = 0; i < content.size(); i++)\r
-               {\r
-                       if (content[i] == '\r')\r
-                               content[i] = '\n';\r
-               }\r
+               string::iterator n = content.begin();\r
+               string::iterator line_end;\r
+               for ( ;; ) {\r
+                       line_end = find(n, content.end(), '\n');\r
 \r
-               string::size_type n;\r
-               while ((n = content.find("\n", 0)) != string::npos)\r
-               {\r
-                       string line = content.substr(0, n);\r
+                       string line(n, line_end);\r
                        line = TrimString(line);\r
                        if (line != "")\r
                                vLine.push_back(line);\r
-                       content = content.substr(n + 1);\r
+\r
+                       if (line_end == content.end()) {\r
+                               break;\r
+                       }\r
+                       n = line_end + 1;\r
                }\r
 \r
                fclose(file);\r
index 08943d4b836228c417201e3c802c7b07f52e932f..2241cbf41358d634a50b813a3e56ee9db77e7b84 100644 (file)
 #include <stdlib.h>\r
 #include <string.h>\r
 \r
+#include <algorithm>\r
 #include <string>\r
 #include <vector>\r
 #include <list>\r
+#include <map>\r
 \r
 #include "global.h"\r
 \r