UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
string_view_utils.hpp File Reference
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <stdlib.h>
#include <ctype.h>
Include dependency graph for string_view_utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T , std::enable_if_t<!from_chars_available_v< T >, bool > = true, std::enable_if_t< std::is_same_v< T, double >, bool > = true>
bool parse_num (std::string_view sv, T &res)
 
template<typename T , std::enable_if_t<!from_chars_available_v< T >, bool > = true, std::enable_if_t< std::is_integral_v< T >, bool > = true>
bool parse_num (std::string_view sv, T &res, int base=10)
 
template<typename T , auto convert, typename... Args>
bool parse_num_fallback_impl (std::string_view sv, T &res, Args... args)
 
bool sv_contains (std::string_view haystack, std::string_view needle)
 
bool sv_is_prefix (std::string_view haystack, std::string_view needle)
 
std::string_view sv_trim_whitespace (std::string_view sv)
 
std::string_view sv_trim_whitespace_back (std::string_view sv)
 
std::string_view sv_trim_whitespace_front (std::string_view sv)
 
std::string_view tokenize (std::string_view &str, char delim, char quot='\0')
 Tokenizer for string_view.
 

Variables

template<typename T >
constexpr bool from_chars_available_v = false
 

Detailed Description

Author
Martin Piatka piatk.nosp@m.a@ce.nosp@m.snet..nosp@m.cz

Definition in file string_view_utils.hpp.

Function Documentation

◆ parse_num() [1/2]

template<typename T , std::enable_if_t<!from_chars_available_v< T >, bool > = true, std::enable_if_t< std::is_same_v< T, double >, bool > = true>
bool parse_num ( std::string_view sv,
T & res )

Definition at line 164 of file string_view_utils.hpp.

164 {
166}
bool parse_num_fallback_impl(std::string_view sv, T &res, Args... args)

References parse_num_fallback_impl().

Here is the call graph for this function:

◆ parse_num() [2/2]

template<typename T , std::enable_if_t<!from_chars_available_v< T >, bool > = true, std::enable_if_t< std::is_integral_v< T >, bool > = true>
bool parse_num ( std::string_view sv,
T & res,
int base = 10 )

◆ parse_num_fallback_impl()

template<typename T , auto convert, typename... Args>
bool parse_num_fallback_impl ( std::string_view sv,
T & res,
Args... args )

Definition at line 136 of file string_view_utils.hpp.

136 {
137 if(sv.empty())
138 return false;
139
140 std::string tmp(sv);
141 const char *c_str_ptr = tmp.c_str();
142 char *endptr = nullptr;
143 T num;
144
145 num = convert(c_str_ptr, &endptr, args...);
146
147 if(c_str_ptr == endptr)
148 return false;
149
150 res = num;
151 return true;
152}
convert_t convert

References convert.

Referenced by parse_num(), and parse_num().

◆ sv_contains()

bool sv_contains ( std::string_view haystack,
std::string_view needle )
inline

Definition at line 68 of file string_view_utils.hpp.

68 {
69 return haystack.find(needle) != std::string_view::npos;
70}

Referenced by parse_log_cfg().

◆ sv_is_prefix()

bool sv_is_prefix ( std::string_view haystack,
std::string_view needle )
inline

◆ sv_trim_whitespace()

std::string_view sv_trim_whitespace ( std::string_view sv)
inline

Definition at line 90 of file string_view_utils.hpp.

90 {
92}
std::string_view sv_trim_whitespace_back(std::string_view sv)
std::string_view sv_trim_whitespace_front(std::string_view sv)

References sv_trim_whitespace_back(), and sv_trim_whitespace_front().

Referenced by AvailableSettings::queryProcessLine().

Here is the call graph for this function:

◆ sv_trim_whitespace_back()

std::string_view sv_trim_whitespace_back ( std::string_view sv)
inline

Definition at line 83 of file string_view_utils.hpp.

83 {
84 size_t i = sv.size();
85 while(i > 0 && isspace(sv[i - 1])) i--;
86
87 return sv.substr(0, i);
88}

Referenced by sv_trim_whitespace().

◆ sv_trim_whitespace_front()

std::string_view sv_trim_whitespace_front ( std::string_view sv)
inline

Definition at line 76 of file string_view_utils.hpp.

76 {
77 size_t i = 0;
78 while(i < sv.size() && isspace(sv[i])) i++;
79
80 return sv.substr(i);
81}

Referenced by sv_trim_whitespace().

◆ 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().

Variable Documentation

◆ from_chars_available_v

template<typename T >
bool from_chars_available_v = false
inlineconstexpr

Definition at line 131 of file string_view_utils.hpp.