Utility library. Some string and mbed memory utilities.
Dependents: 19E042PIM_T3_2020_0639
stringUtils.cpp@0:3d93ff64fdee, 2011-06-19 (annotated)
- Committer:
- Phlaphead
- Date:
- Sun Jun 19 13:13:00 2011 +0000
- Revision:
- 0:3d93ff64fdee
Initial Revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Phlaphead | 0:3d93ff64fdee | 1 | |
Phlaphead | 0:3d93ff64fdee | 2 | |
Phlaphead | 0:3d93ff64fdee | 3 | #include "stringUtils.h" |
Phlaphead | 0:3d93ff64fdee | 4 | |
Phlaphead | 0:3d93ff64fdee | 5 | using namespace std; |
Phlaphead | 0:3d93ff64fdee | 6 | |
Phlaphead | 0:3d93ff64fdee | 7 | string trim(const string& src, const string& c) |
Phlaphead | 0:3d93ff64fdee | 8 | { |
Phlaphead | 0:3d93ff64fdee | 9 | int p2 = src.find_last_not_of(c); |
Phlaphead | 0:3d93ff64fdee | 10 | if (p2 == string::npos) |
Phlaphead | 0:3d93ff64fdee | 11 | { |
Phlaphead | 0:3d93ff64fdee | 12 | return string(); |
Phlaphead | 0:3d93ff64fdee | 13 | } |
Phlaphead | 0:3d93ff64fdee | 14 | |
Phlaphead | 0:3d93ff64fdee | 15 | int p1 = src.find_first_not_of(c); |
Phlaphead | 0:3d93ff64fdee | 16 | if (p1 == string::npos) |
Phlaphead | 0:3d93ff64fdee | 17 | { |
Phlaphead | 0:3d93ff64fdee | 18 | p1 = 0; |
Phlaphead | 0:3d93ff64fdee | 19 | } |
Phlaphead | 0:3d93ff64fdee | 20 | |
Phlaphead | 0:3d93ff64fdee | 21 | return src.substr(p1, (p2-p1)+1); |
Phlaphead | 0:3d93ff64fdee | 22 | } |
Phlaphead | 0:3d93ff64fdee | 23 | |
Phlaphead | 0:3d93ff64fdee | 24 | vector<string> tokenize(const string& str, const string& delimiters) |
Phlaphead | 0:3d93ff64fdee | 25 | { |
Phlaphead | 0:3d93ff64fdee | 26 | vector<string> tokens; |
Phlaphead | 0:3d93ff64fdee | 27 | |
Phlaphead | 0:3d93ff64fdee | 28 | // Skip delimiters at beginning. |
Phlaphead | 0:3d93ff64fdee | 29 | string::size_type lastPos = str.find_first_not_of(delimiters, 0); |
Phlaphead | 0:3d93ff64fdee | 30 | // Find first "non-delimiter". |
Phlaphead | 0:3d93ff64fdee | 31 | string::size_type pos = str.find_first_of(delimiters, lastPos); |
Phlaphead | 0:3d93ff64fdee | 32 | |
Phlaphead | 0:3d93ff64fdee | 33 | while (string::npos != pos || string::npos != lastPos) |
Phlaphead | 0:3d93ff64fdee | 34 | { |
Phlaphead | 0:3d93ff64fdee | 35 | // Found a token, add it to the vector. |
Phlaphead | 0:3d93ff64fdee | 36 | tokens.push_back(str.substr(lastPos, pos - lastPos)); |
Phlaphead | 0:3d93ff64fdee | 37 | // Skip delimiters. Note the "not_of" |
Phlaphead | 0:3d93ff64fdee | 38 | lastPos = str.find_first_not_of(delimiters, pos); |
Phlaphead | 0:3d93ff64fdee | 39 | // Find next "non-delimiter" |
Phlaphead | 0:3d93ff64fdee | 40 | pos = str.find_first_of(delimiters, lastPos); |
Phlaphead | 0:3d93ff64fdee | 41 | } |
Phlaphead | 0:3d93ff64fdee | 42 | |
Phlaphead | 0:3d93ff64fdee | 43 | return tokens; |
Phlaphead | 0:3d93ff64fdee | 44 | } |