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::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; }
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; }
static std::string_view hash_from_url( std::string_view url ){ size_t start = url.find_last_of( '/' ) + 1; size_t end = url.find_last_of( '.' ); //Validate results if( start == string::npos || end == string::npos || start >= url.size() ) return ""; else return url.substr( start, end-start ); }
inline std::string StringFromHex(const std::string_view& input) { assert((input.size() & 1) == 0); std::string result; result.reserve(input.size() / 2); for (size_t i = 0; i < input.size(); i += 2) { auto val = std::stoi(std::string(input.substr(i, 2)), 0, 16); result.push_back(val); } return result; }
double Calculator::parseFunction(std::string_view &ref) { try { std::string_view partStr = ref.substr(0, 3); if (partStr == std::string_view("sin")) { ref.remove_prefix(3); m_printStrategy->printFunctionName("sin"); return sin(ParseArguments(ref) * M_PI / 180); } else if (partStr == std::string_view("cos")) { ref.remove_prefix(3); m_printStrategy->printFunctionName("cos"); return cos(ParseArguments(ref) * M_PI / 180); } else { std::string_view partStr = ref.substr(0, 4); if (partStr == std::string_view("sqrt")) { ref.remove_prefix(4); m_printStrategy->printFunctionName("sqrt"); return sqrt(ParseArguments(ref)); } } } catch(const std::out_of_range&) { return std::numeric_limits<double>::quiet_NaN(); } return std::numeric_limits<double>::quiet_NaN(); }
inline std::string_view trim( std::string_view str ){ auto first = str.find_first_not_of( ' ' ); auto last = str.find_last_not_of( ' ' ); auto chars_removed = first + (str.size()-last-1); return str.substr( first, str.size() - chars_removed ); }
inline std::string_view removePostfix( std::string_view str, std::string_view postfix ){ if( !ends_with( str, postfix ) ) throw std::logic_error( std::string(str) + " does not end with " + std::string(postfix) ); return str.substr( str.size() - postfix.size() ); }
inline std::string_view removePrefix( std::string_view str, std::string_view prefix ){ if( !starts_with( str, prefix ) ) throw std::logic_error( std::string(str) + " does not start with " + std::string(prefix) ); return str.substr( prefix.size() );; }
inline bool ends_with( std::string_view input, std::string_view match ){ if( input.length() >= match.length() ) return input.substr( input.size() - match.length() ) == match; return false; }
inline bool starts_with( std::string_view input, std::string_view match ){ if( input.length() >= match.length() ) return input.substr( 0, match.length() ) == match; return false; }
std::string_view removeSuffix(std::string_view a, std::string_view b) { ASSERT(endsWith(a, b)); return a.substr(0, a.size() - b.size()); }
bool endsWith(std::string_view a, std::string_view b) { return a.size() >= b.size() && a.substr(a.size() - b.size()) == b; }
bool startsWith(std::string_view a, std::string_view b) { return a.size() >= b.size() && a.substr(0, b.size()) == b; }