std::string environment_expand(std::string_view s) { std::string r; size_t start_pos = 0; size_t dollar_pos; while ((dollar_pos = s.find('$', start_pos)) != s.npos) { r += s.substr(start_pos, dollar_pos - start_pos); std::string varname; if (dollar_pos + 1 < s.length() && s[dollar_pos + 1] == '{') { size_t dollar_end = s.find('}', dollar_pos + 2); if (dollar_end == s.npos) { break; } varname = s.substr(dollar_pos + 2, dollar_end - (dollar_pos + 2)); start_pos = dollar_end + 1; } else { size_t dollar_end = s.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"sv, dollar_pos + 1); varname = s.substr(dollar_pos + 1, dollar_end - (dollar_pos + 1)); start_pos = dollar_end; } if (const char *varval = getenv (varname.c_str ())) { r += varval; } } if (start_pos < s.length()) { r += s.substr(start_pos); } return r; }
inline std::pair<std::string_view, std::string_view> splitAt( std::string_view str, char splitter ){ auto pos = str.find( splitter ); if( pos == std::string_view::npos ) return { str, {} }; else return { str.substr( 0, pos ), str.substr( pos+1 ) }; }
std::string quote_this(std::string_view arg) { if (arg.size() && arg[0] != '"' && arg.find(' ') != std::string::npos) { return '"' + std::string(arg) + '"'; } return std::string(arg); }
std::pair<std::string_view, std::string_view> splitStringIn2( const std::string_view str, char delimiter) { auto pos = str.find(delimiter, 0); if (pos != std::string::npos) { return std::make_pair(str.substr(0, pos), str.substr(pos + 1, str.size() - pos)); } return std::make_pair(str, ""); }
inline std::vector<std::string_view> splitAllOn( std::string_view str, char splitter ){ std::vector<std::string_view> splits; size_t last=0, pos = 0; while( (pos = str.find(splitter, last)) != std::string_view::npos ){ splits.push_back( str.substr( last, pos-last ) ); last = pos+1; } splits.push_back( str.substr( last ) ); return splits; }
void message_headers::add(std::string_view name, std::string_view value) { std::unordered_map<std::string, std::string>::iterator iter (fields_.find(name.data())); // if the field name was found previously if (iter != fields_.end()) { char separator((name.find(COOKIE) != std::string::npos) ? ';' : ','); iter->second.append({separator}); iter->second.append(value); } else fields_.insert(std::unordered_map<std::string, std::string>::value_type (name, value)); }
std::vector<std::string> splitString(const std::string_view str, char delimiter) { std::vector<std::string> strings; std::string_view::size_type pos = 0; std::string_view::size_type prev = 0; while ((pos = str.find(delimiter, prev)) != std::string::npos) { strings.push_back(std::string(str.substr(prev, pos - prev))); prev = pos + 1; } strings.push_back(std::string(str.substr(prev))); return strings; }