summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorNathan Binkert <binkertn@umich.edu>2005-06-22 09:52:02 -0400
committerNathan Binkert <binkertn@umich.edu>2005-06-22 09:52:02 -0400
commitc95e1281fc5ecfe3f65a59fa426f16a54b2a6a50 (patch)
treebe85ce2bbaba3f2589f24debacc6a173950cb7e4 /base
parent11894d3b4b85160e1cc0c0a20157f89dcd3bae6c (diff)
downloadgem5-c95e1281fc5ecfe3f65a59fa426f16a54b2a6a50.tar.xz
fix tokenize
base/str.cc: Fix tokenize so that it doesn't behave incorrectly when there are empty strings. test/tokentest.cc: Clean up the test function so it's easier to see what's going on --HG-- extra : convert_revision : c7a3db7bc516d3575b1cc4ab7afbd0f1fbe1ec6f
Diffstat (limited to 'base')
-rw-r--r--base/str.cc23
1 files changed, 12 insertions, 11 deletions
diff --git a/base/str.cc b/base/str.cc
index 3a11bb17d..15f44dad2 100644
--- a/base/str.cc
+++ b/base/str.cc
@@ -26,11 +26,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <iostream>
-
-#include <string.h>
#include <ctype.h>
+#include <cstring>
+#include <iostream>
#include <string>
#include <vector>
@@ -75,15 +74,17 @@ tokenize(vector<string>& v, const string &s, char token, bool ignore)
string::size_type first = 0;
string::size_type last = s.find_first_of(token);
- if (ignore) {
- if (last == first) {
- while (last == first)
- last = s.find_first_of(token, ++first);
+ if (s.empty())
+ return;
- if (last == string::npos) {
- v.push_back(s);
- return;
- }
+ if (ignore && last == first) {
+ while (last == first)
+ last = s.find_first_of(token, ++first);
+
+ if (last == string::npos) {
+ if (first != s.size())
+ v.push_back(s.substr(first));
+ return;
}
}