Dual CANbus monitor and instrumentation cluster
Dependencies: SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed
Fork of CANary by
Util/stringUtils.cpp@125:f58b7ab2abee, 2013-07-21 (annotated)
- Committer:
- TickTock
- Date:
- Sun Jul 21 11:08:38 2013 +0000
- Branch:
- Metric
- Revision:
- 125:f58b7ab2abee
- Parent:
- 1:9dcd70c32180
Re-centered efficiency screen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TickTock | 1:9dcd70c32180 | 1 | |
TickTock | 1:9dcd70c32180 | 2 | |
TickTock | 1:9dcd70c32180 | 3 | #include "stringUtils.h" |
TickTock | 1:9dcd70c32180 | 4 | |
TickTock | 1:9dcd70c32180 | 5 | using namespace std; |
TickTock | 1:9dcd70c32180 | 6 | |
TickTock | 1:9dcd70c32180 | 7 | string trim(const string& src, const string& c) |
TickTock | 1:9dcd70c32180 | 8 | { |
TickTock | 1:9dcd70c32180 | 9 | int p2 = src.find_last_not_of(c); |
TickTock | 1:9dcd70c32180 | 10 | if (p2 == string::npos) |
TickTock | 1:9dcd70c32180 | 11 | { |
TickTock | 1:9dcd70c32180 | 12 | return string(); |
TickTock | 1:9dcd70c32180 | 13 | } |
TickTock | 1:9dcd70c32180 | 14 | |
TickTock | 1:9dcd70c32180 | 15 | int p1 = src.find_first_not_of(c); |
TickTock | 1:9dcd70c32180 | 16 | if (p1 == string::npos) |
TickTock | 1:9dcd70c32180 | 17 | { |
TickTock | 1:9dcd70c32180 | 18 | p1 = 0; |
TickTock | 1:9dcd70c32180 | 19 | } |
TickTock | 1:9dcd70c32180 | 20 | |
TickTock | 1:9dcd70c32180 | 21 | return src.substr(p1, (p2-p1)+1); |
TickTock | 1:9dcd70c32180 | 22 | } |
TickTock | 1:9dcd70c32180 | 23 | |
TickTock | 1:9dcd70c32180 | 24 | vector<string> tokenize(const string& str, const string& delimiters) |
TickTock | 1:9dcd70c32180 | 25 | { |
TickTock | 1:9dcd70c32180 | 26 | vector<string> tokens; |
TickTock | 1:9dcd70c32180 | 27 | |
TickTock | 1:9dcd70c32180 | 28 | // Skip delimiters at beginning. |
TickTock | 1:9dcd70c32180 | 29 | string::size_type lastPos = str.find_first_not_of(delimiters, 0); |
TickTock | 1:9dcd70c32180 | 30 | // Find first "non-delimiter". |
TickTock | 1:9dcd70c32180 | 31 | string::size_type pos = str.find_first_of(delimiters, lastPos); |
TickTock | 1:9dcd70c32180 | 32 | |
TickTock | 1:9dcd70c32180 | 33 | while (string::npos != pos || string::npos != lastPos) |
TickTock | 1:9dcd70c32180 | 34 | { |
TickTock | 1:9dcd70c32180 | 35 | // Found a token, add it to the vector. |
TickTock | 1:9dcd70c32180 | 36 | tokens.push_back(str.substr(lastPos, pos - lastPos)); |
TickTock | 1:9dcd70c32180 | 37 | // Skip delimiters. Note the "not_of" |
TickTock | 1:9dcd70c32180 | 38 | lastPos = str.find_first_not_of(delimiters, pos); |
TickTock | 1:9dcd70c32180 | 39 | // Find next "non-delimiter" |
TickTock | 1:9dcd70c32180 | 40 | pos = str.find_first_of(delimiters, lastPos); |
TickTock | 1:9dcd70c32180 | 41 | } |
TickTock | 1:9dcd70c32180 | 42 | |
TickTock | 1:9dcd70c32180 | 43 | return tokens; |
TickTock | 1:9dcd70c32180 | 44 | } |