1#ifndef _BBM_STRING_UTIL_
2#define _BBM_STRING_UTIL_
22 if(str.empty())
return str;
25 const char* whiteSpace =
" \r\n\t\v";
27 size_t s = str.find_first_not_of(whiteSpace);
28 size_t e = str.find_last_not_of(whiteSpace);
30 if(s > e)
return std::string();
31 else return str.substr(s, e-s+1);
41 const std::string openbracket =
"[{(";
42 const std::string closebracket =
"]})";
44 bool matching_brackets = !newStr.empty() && (openbracket.find(newStr.front()) != std::string::npos) && (openbracket.find(newStr.front()) == closebracket.find(newStr.back()));
45 if(!matching_brackets)
throw std::runtime_error(
"Mismatch brackets in expression: " + str);
47 return newStr.substr(1, newStr.size() - 2);
53 std::string
remove_comment(
const std::string& str,
const std::string& comment_marker)
55 size_t pos = str.find(comment_marker);
56 if(pos == 0 || str.empty())
return std::string();
57 else if(pos != std::string::npos)
return str.substr(0, pos);
65 std::pair<std::string, std::string>
get_keyword(
const std::string& str)
67 size_t b = str.find_first_of(
'(');
68 if(b == std::string::npos)
throw std::runtime_error(
"Expected open bracket in expression: " + str);
77 std::pair<std::string, std::string>
split_eq(
const std::string& str)
80 size_t eqpos = str.find_first_of(
'=');
84 std::string key = str.substr(0, eqpos);
85 std::string
value = str.substr(eqpos+1);
94 std::vector<std::string>
split_args(
const std::string& str)
96 const std::string openbracket =
"[{(";
97 const std::string closebracket =
"]})";
98 std::vector<size_t> bracket_stack;
99 std::string current_word;
100 std::vector<std::string> result;
105 auto opos = openbracket.find(c);
106 if(opos != std::string::npos)
108 bracket_stack.push_back(opos);
112 auto cpos = closebracket.find(c);
113 if(cpos != std::string::npos)
115 if(bracket_stack.empty() || bracket_stack.back() != cpos)
throw std::runtime_error(
"Mismatched brackets in expression: " + str);
116 bracket_stack.pop_back();
120 if(c ==
',' && bracket_stack.empty())
127 else current_word += c;
131 if(!bracket_stack.empty())
throw std::runtime_error(
"Bracket not closed in expression:" + str);
std::pair< std::string, std::string > split_eq(const std::string &str)
split a string of the form "key = val" in key and value. If no '=', then return an empty key.
Definition: string_util.h:77
std::pair< std::string, std::string > get_keyword(const std::string &str)
Return the keyword substring appearing an open bracket, and the arguments appearing in the brackets: ...
Definition: string_util.h:65
std::string remove_whitespace(const std::string &str)
remove the white space at the front and back of a string
Definition: string_util.h:19
std::string remove_comment(const std::string &str, const std::string &comment_marker)
Remove comments from string.
Definition: string_util.h:53
std::string remove_brackets(const std::string &str)
remove surrounding backets
Definition: string_util.h:37
std::vector< std::string > split_args(const std::string &str)
Split a string based on comma's if not surrounded by brackets.
Definition: string_util.h:94
Definition: aggregatebsdf.h:29
decltype(auto) value(T &&t)
return the value of an attribute, or if not an attribute the object
Definition: attribute_value.h:20