UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
string_view_utils.cpp File Reference
Include dependency graph for string_view_utils.cpp:

Go to the source code of this file.

Functions

std::string_view tokenize (std::string_view &str, char delim, char quot)
 Tokenizer for string_view.
 

Function Documentation

◆ tokenize()

std::string_view tokenize ( std::string_view & str,
char delim,
char quot = '\0' )

Tokenizer for string_view.

Useful for non-destructive tokenization of strings. Skips empty tokens. str is modified to view the not yet processed remainder.

Typical usage (prints lines "Hello", "World", "!"):

std::string_view sv = ":::Hello:World::!::";
while(!sv.empty()){
cout << tokenize(sv, ':') << "\n";
}
std::string_view tokenize(std::string_view &str, char delim, char quot)
Tokenizer for string_view.

The 'quot' param allows for a basic quotation based deilimiter escape, however the whole token must be escaped. i.e. sync:"opts=a=1:b=2":fs is fine, but sync:opts="a=1:b=2":fs is not.

Definition at line 3 of file string_view_utils.cpp.

3 {
4 if(str.empty())
5 return {};
6
7 bool escaped = false;
8
9 auto token_begin = str.begin();
10 while(token_begin != str.end()){
11 if(*token_begin == quot)
12 escaped = !escaped;
13 else if(*token_begin != delim)
14 break;
15
16 token_begin++;
17 }
18
19 auto token_end = token_begin;
20 while(token_end != str.end()){
21 if(*token_end == quot){
22 str = std::string_view(token_end, str.end() - token_end);
23 str.remove_prefix(1); //remove the end quote
24 return std::string_view(token_begin, token_end - token_begin);
25 }
26 else if(*token_end == delim && !escaped)
27 break;
28
29 token_end++;
30 }
31
32 str = std::string_view(token_end, str.end() - token_end);
33
34 return std::string_view(token_begin, token_end - token_begin);
35}

Referenced by audio_frame2_resampler::audio_frame2_resampler(), image_pattern_diagonal::image_pattern_diagonal(), image_pattern_text::image_pattern_text(), audio_cap_pipewire_init(), audio_init(), audio_play_pw_init(), display_conference_init(), display_conference_worker(), display_drm_init(), display_multiplier_init(), display_pw_init(), display_unix_sock_init(), Filter_chain::emplace_new(), init(), parse_cfg(), parse_cfg(), parse_holepunch_conf(), BandwidthWidget::parseLine(), RecvLossWidget::parseLine(), VuMeterWidget::parseLine(), print_capabilities(), and AvailableSettings::queryProcessLine().