https://github.com/goto-bus-stop/cpp-split-string
A simple C++17 class to iterate over pieces of a string using ranged for loops.
https://github.com/goto-bus-stop/cpp-split-string
cpp lines ranged-for split string
Last synced: 12 months ago
JSON representation
A simple C++17 class to iterate over pieces of a string using ranged for loops.
- Host: GitHub
- URL: https://github.com/goto-bus-stop/cpp-split-string
- Owner: goto-bus-stop
- License: other
- Created: 2019-10-22T11:27:15.000Z (over 6 years ago)
- Default Branch: default
- Last Pushed: 2020-01-07T13:05:35.000Z (over 6 years ago)
- Last Synced: 2025-03-27T23:44:31.385Z (over 1 year ago)
- Topics: cpp, lines, ranged-for, split, string
- Language: C++
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-Apache.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# split-string
A simple C++17 class to iterate over pieces of a string using ranged for loops.
It iterates over `string_view` containers only. It could be more generic by accepting iterator pairs, but I don't need it to be, and I don't like dealing with iterator pairs. 🤷🏻
```cpp
#include
int main () {
for (auto line : split_string("some\nsource\ntext")) {
std::cout << "line: " << line << '\n';
}
// line: some
// line: source
// line: text
for (auto entry : split_string(L"a, b, c", L", ")) {
std::wcout << "value: " << entry << '\n';
}
// value: a
// value: b
// value: c
}
```
## Install
Copy [`include/split_string.hpp`](./include/split_string.hpp) into your project or add the repository as a submodule and add `$THIS_REPO/include` to your include path.
Or with CMake do:
```cmake
add_subdirectory(path/to/cpp-split-string)
target_link_directories(your-target PRIVATE split-string)
```
## API
### `auto splitter = split_string(string_view input, string_view separator)`
Create an iterable value for the given input string. `separator` can be anything that `std::is_convertible<>` to a `string_view.`
This is a templated function that will accept any kind of `std::basic_string_view`. The `separator` and `input` must use the same character type. Use `auto` to avoid naming the return type. The full signature is:
```cpp
template
split_string(std::basic_string_view input, std::basic_string_view separator);
template>::type>
split_string(std::basic_string_view input, SepT separator);
```
### `split_string::iterator begin = splitter.begin()`
Get the iterator for the first character in the string.
### `split_string::iterator end = splitter.end()`
Get the iterator for the end of the string.
### `split_string::reverse_iterator begin = splitter.rbegin()`
Get the reverse iterator for the entire remaining string.
### `split_string::reverse_iterator end = splitter.rend()`
Get the reverse iterator for the start of the string.
### `auto reverse_splitter = rsplit_string(string_view input, string_view separator)`
Create an iterable value that proxies its `begin()` and `end()` calls to the reverse iterator of `split_string`.
This way you can do reverse iteration in a ranged for loop, and not fiddle around with `rbegin()` and `rend()`:
```cpp
for (auto line : rsplit_string("some\nsource\ntext")) {
std::cout << "line: " << line << '\n';
}
// line: text
// line: source
// line: some
```
### `split_string::reverse_iterator begin = reverse_splitter.begin()`
Get the reverse iterator for the entire remaining string.
### `split_string::reverse_iterator end = reverse_splitter.end()`
Get the reverse iterator for the start of the string.
## License
[MIT](./LICENSE-MIT.md) or [Apache-2.0](./LICENSE-Apache.md) at your option.