mirror of
https://github.com/outbackdingo/UltraGrid.git
synced 2026-04-07 00:05:24 +00:00
Move tokenize and sv_parse_num into string_view_utils
This commit is contained in:
35
src/utils/string_view_utils.cpp
Normal file
35
src/utils/string_view_utils.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "string_view_utils.hpp"
|
||||
|
||||
std::string_view tokenize(std::string_view& str, char delim, char quot){
|
||||
if(str.empty())
|
||||
return {};
|
||||
|
||||
bool escaped = false;
|
||||
|
||||
auto token_begin = str.begin();
|
||||
while(token_begin != str.end()){
|
||||
if(*token_begin == quot)
|
||||
escaped = !escaped;
|
||||
else if(*token_begin != delim)
|
||||
break;
|
||||
|
||||
token_begin++;
|
||||
}
|
||||
|
||||
auto token_end = token_begin;
|
||||
while(token_end != str.end()){
|
||||
if(*token_end == quot){
|
||||
str = std::string_view(token_end, str.end() - token_end);
|
||||
str.remove_prefix(1); //remove the end quote
|
||||
return std::string_view(token_begin, token_end - token_begin);
|
||||
}
|
||||
else if(*token_end == delim && !escaped)
|
||||
break;
|
||||
|
||||
token_end++;
|
||||
}
|
||||
|
||||
str = std::string_view(token_end, str.end() - token_end);
|
||||
|
||||
return std::string_view(token_begin, token_end - token_begin);
|
||||
}
|
||||
Reference in New Issue
Block a user