summaryrefslogtreecommitdiff
path: root/base/str.cc
diff options
context:
space:
mode:
authorNathan Binkert <binkertn@umich.edu>2005-02-09 13:46:23 -0500
committerNathan Binkert <binkertn@umich.edu>2005-02-09 13:46:23 -0500
commit26ef1f56c8b62019e45f008f2abb2f8dcca6f24b (patch)
treec5a5897f9847423a9fc17d3fe3d14c0104155677 /base/str.cc
parent89ba024b9843719bf06a9c3efaaf1b137dee2a12 (diff)
downloadgem5-26ef1f56c8b62019e45f008f2abb2f8dcca6f24b.tar.xz
Add the split_first and split_last functions on strings.
base/str.cc: base/str.hh: Add a couple functions that allow you to split a string at the first or last instance of a delimiter. --HG-- extra : convert_revision : 2af22639e1b67ac61577c00475a555841a56f902
Diffstat (limited to 'base/str.cc')
-rw-r--r--base/str.cc30
1 files changed, 30 insertions, 0 deletions
diff --git a/base/str.cc b/base/str.cc
index dd8d80043..5357ba79f 100644
--- a/base/str.cc
+++ b/base/str.cc
@@ -39,6 +39,36 @@
using namespace std;
+bool
+split_first(const string &s, string &lhs, string &rhs, char c)
+{
+ string::size_type offset = s.find(c);
+ if (offset == string::npos) {
+ lhs = s;
+ rhs = "";
+ return false;
+ }
+
+ lhs = s.substr(0, offset);
+ rhs = s.substr(offset + 1);
+ return true;
+}
+
+bool
+split_last(const string &s, string &lhs, string &rhs, char c)
+{
+ string::size_type offset = s.rfind(c);
+ if (offset == string::npos) {
+ lhs = s;
+ rhs = "";
+ return false;
+ }
+
+ lhs = s.substr(0, offset);
+ rhs = s.substr(offset + 1);
+ return true;
+}
+
void
tokenize(vector<string>& v, const string &s, char token, bool ignore)
{