-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added splitting at multiple whitespace characters #36
Changes from 1 commit
6f1fee9
3d52d0f
7ddcfe9
7b8f6a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include <stdlib.h> | ||
#include <assert.h> | ||
#include <iostream> | ||
#include <cctype> | ||
#include <clocale> | ||
#include <cstring> | ||
#include <cwchar> | ||
|
@@ -108,6 +109,9 @@ inline string rstrip(const string& text, const char *s); | |
//! Splits a string at the separator, kinda like python. | ||
inline vector<string> split(const string& orig, const char sep); | ||
|
||
//! Splits a string at any maximum length sequence of whitespace | ||
inline vector<string> splitWs(const string &orig); | ||
|
||
//! Splits a string a any character inside the seps string. | ||
inline vector<string> splitAny(const string& orig, const char *seps); | ||
|
||
|
@@ -381,6 +385,33 @@ vector<string> split(const string& orig, const char sep) { | |
return result; | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
vector<string> splitWs(const string &orig) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a weird mix of |
||
vector<string> result; | ||
if (orig.size() > 0) { | ||
size_t start = 0; | ||
size_t pos = 0; | ||
while (pos < orig.size()) { | ||
if (isspace(orig[pos])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said, both functions have an additional pitfall in taking ints that represent unsigned |
||
if (start != pos) { | ||
result.emplace_back(orig.substr(start, pos - start)); | ||
} | ||
// skip any whitespace | ||
while (pos < orig.size() && isspace(orig[pos])) { | ||
pos++; | ||
} | ||
start = pos; | ||
} | ||
pos++; | ||
} | ||
// avoid adding whitespace at the back of the string | ||
if (!isspace(orig[orig.size() - 1])) { | ||
result.emplace_back(orig.substr(start, pos - start)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
vector<string> splitAny(const string& orig, const char *seps) { | ||
return splitAny(orig, string(seps)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since splitWs removes the whitespace from the splits shouldn't the strip() be redudant?