Пример #1
0
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;
}
Пример #2
0
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 );
}
Пример #3
0
std::string_view trim(std::string_view str)
{
    str.remove_prefix(str.find_first_not_of(' '));
    str.remove_suffix(str.size() - (str.find_last_not_of(' ') + 1));
    return str;
}