Loading...
Searching...
No Matches
string_util.h
Go to the documentation of this file.
1#ifndef _BBM_STRING_UTIL_
2#define _BBM_STRING_UTIL_
3
4#include <string>
5
6/************************************************************************/
7/*! \file string_util.h
8
9 \brief Helper method for processing strings
10
11*************************************************************************/
12
13namespace bbm {
14 namespace string {
15
16 /**********************************************************************/
17 /*! \brief remove the white space at the front and back of a string
18 **********************************************************************/
19 std::string remove_whitespace(const std::string& str)
20 {
21 // trivial case: empty
22 if(str.empty()) return str;
23
24 // otherwise, search for first non-white space chars
25 const char* whiteSpace = " \r\n\t\v";
26
27 size_t s = str.find_first_not_of(whiteSpace);
28 size_t e = str.find_last_not_of(whiteSpace);
29
30 if(s > e) return std::string();
31 else return str.substr(s, e-s+1);
32 }
33
34 /**********************************************************************/
35 /*! \brief remove surrounding backets
36 **********************************************************************/
37 std::string remove_brackets(const std::string& str)
38 {
39 auto newStr = remove_whitespace(str);
40
41 const std::string openbracket = "[{(";
42 const std::string closebracket = "]})";
43
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);
46
47 return newStr.substr(1, newStr.size() - 2);
48 }
49
50 /**********************************************************************/
51 /*! \brief Remove comments from string
52 **********************************************************************/
53 std::string remove_comment(const std::string& str, const std::string& comment_marker)
54 {
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);
58 else return str;
59 }
60
61 /**********************************************************************/
62 /*! \brief Return the keyword substring appearing an open bracket, and the
63 arguments appearing in the brackets: e.g., keyword(arguments)
64 **********************************************************************/
65 std::pair<std::string, std::string> get_keyword(const std::string& str)
66 {
67 size_t b = str.find_first_of('(');
68 if(b == std::string::npos) throw std::runtime_error("Expected open bracket in expression: " + str);
69
70 return std::pair( b!=0 ? remove_whitespace( str.substr(0, b) ) : "", remove_whitespace( str.substr(b) ) );
71 }
72
73 /**********************************************************************/
74 /*! \brief split a string of the form "key = val" in key and value. If no
75 '=', then return an empty key.
76 ***********************************************************************/
77 std::pair<std::string, std::string> split_eq(const std::string& str)
78 {
79 // search for '='; if none found return str (without whitespace)
80 size_t eqpos = str.find_first_of('=');
81 if(eqpos == std::string::npos) return std::pair("", remove_whitespace(str));
82
83 // else extract key and value
84 std::string key = str.substr(0, eqpos);
85 std::string value = str.substr(eqpos+1);
86
87 // return without whitespace
88 return std::pair( remove_whitespace(key), remove_whitespace(value) );
89 }
90
91 /**********************************************************************/
92 /*! \brief Split a string based on comma's if not surrounded by brackets
93 *********************************************************************/
94 std::vector<std::string> split_args(const std::string& str)
95 {
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;
101
102 for(auto c : str)
103 {
104 // check if bracket is being opened
105 auto opos = openbracket.find(c);
106 if(opos != std::string::npos)
107 {
108 bracket_stack.push_back(opos);
109 }
110
111 // check if corresponding bracket is closed
112 auto cpos = closebracket.find(c);
113 if(cpos != std::string::npos)
114 {
115 if(bracket_stack.empty() || bracket_stack.back() != cpos) throw std::runtime_error("Mismatched brackets in expression: " + str);
116 bracket_stack.pop_back();
117 }
118
119 // otherwise did we encounter a comma and there is no bracket open
120 if(c == ',' && bracket_stack.empty())
121 {
122 result.push_back(remove_whitespace(current_word));
123 current_word = "";
124 }
125
126 // else grow current word
127 else current_word += c;
128 }
129
130 // process last word
131 if(!bracket_stack.empty()) throw std::runtime_error("Bracket not closed in expression:" + str);
132 else if(current_word != "") result.push_back(remove_whitespace(current_word));
133
134 // Done.
135 return result;
136 }
137
138 } // end string namespace
139} // end bbm namespace
140
141#endif /* _BBM_STRING_UTIL_ */
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