File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # String Split implementation in C++
2+
3+ ### Source Code
4+
5+ ``` c++
6+ auto split (std::vector< std::string > &result, std::string_view s, std::string_view t) -> std::vector< std::string > &
7+ {
8+
9+ if (t.empty())
10+ {
11+ result.emplace_back(s);
12+ return result;
13+ }
14+
15+ result.reserve(s.size());
16+
17+ if (!s.empty())
18+ {
19+ std::size_t first = 0;
20+ while (first < s.size())
21+ {
22+ const auto foundIndex = s.find(t, first);
23+ const auto last = foundIndex < s.size() ? foundIndex : s.size();
24+ result.emplace_back(s.begin() + first, s.begin() + last);
25+ first = last + t.size();
26+ }
27+ }
28+ return result;
29+ }
30+ ```
31+
32+ benchmark results between
33+
34+ > - Boost string split
35+ > - Stackoverflow solution for split
36+ > - Split function that I wrote
37+
38+ 
You can’t perform that action at this time.
0 commit comments