-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_processing.cpp
More file actions
40 lines (35 loc) · 901 Bytes
/
Copy pathstring_processing.cpp
File metadata and controls
40 lines (35 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "string_processing.h"
using namespace std;
vector<string> SplitIntoWords(const string& text) {
vector<string> words;
string word;
for (const char c : text) {
if (c == ' ') {
if (!word.empty()) {
words.push_back(word);
word.clear();
}
} else {
word += c;
}
}
if (!word.empty()) {
words.push_back(word);
}
return words;
}
vector<string_view> SplitIntoWordsView(string_view str) {
vector<string_view> result;
int64_t pos = 0;
const int64_t pos_end = str.npos;
while (true) {
int64_t space = str.find(' ', pos);
result.push_back(space == pos_end ? str.substr(pos) : str.substr(pos, space - pos));
if (space == pos_end) {
break;
} else {
pos = space + 1;
}
}
return result;
}