string_utils.hh (3003B)
1 #ifndef string_utils_hh_INCLUDED 2 #define string_utils_hh_INCLUDED 3 4 #include "string.hh" 5 #include "enum.hh" 6 #include "vector.hh" 7 #include "optional.hh" 8 #include "format.hh" 9 #include "array.hh" 10 11 namespace Kakoune 12 { 13 14 String trim_indent(StringView str); 15 16 String escape(StringView str, StringView characters, char escape); 17 String unescape(StringView str, StringView characters, char escape); 18 19 template<char character, char escape> 20 String unescape(StringView str) 21 { 22 const char to_escape[2] = { character, escape }; 23 return unescape(str, {to_escape, 2}, escape); 24 } 25 26 String indent(StringView str, StringView indent = " "); 27 28 String replace(StringView str, StringView substr, StringView replacement); 29 30 String left_pad(StringView str, ColumnCount size, Codepoint c = ' '); 31 String right_pad(StringView str, ColumnCount size, Codepoint c = ' '); 32 33 template<typename Container> 34 String join(const Container& container, char joiner, bool esc_joiner = true) 35 { 36 const char to_escape[2] = { joiner, '\\' }; 37 String res; 38 for (const auto& str : container) 39 { 40 if (not res.empty()) 41 res += joiner; 42 res += esc_joiner ? escape(str, {to_escape, 2}, '\\') : str; 43 } 44 return res; 45 } 46 47 template<typename Container> 48 String join(const Container& container, StringView joiner) 49 { 50 String res; 51 for (const auto& str : container) 52 { 53 if (not res.empty()) 54 res += joiner; 55 res += str; 56 } 57 return res; 58 } 59 60 inline bool prefix_match(StringView str, StringView prefix) 61 { 62 return str.substr(0_byte, prefix.length()) == prefix; 63 } 64 65 bool subsequence_match(StringView str, StringView subseq); 66 67 String expand_tabs(StringView line, ColumnCount tabstop, ColumnCount col = 0); 68 69 int str_to_int(StringView str); // throws on error 70 Optional<int> str_to_int_ifp(StringView str); 71 72 String double_up(StringView s, StringView characters); 73 74 inline String quote(StringView s) 75 { 76 return format("'{}'", double_up(s, "'")); 77 } 78 79 inline String shell_quote(StringView s) 80 { 81 return format("'{}'", replace(s, "'", R"('\'')")); 82 } 83 84 enum class Quoting 85 { 86 Raw, 87 Kakoune, 88 Shell 89 }; 90 91 constexpr auto enum_desc(Meta::Type<Quoting>) 92 { 93 return make_array<EnumDesc<Quoting>>({ 94 { Quoting::Raw, "raw" }, 95 { Quoting::Kakoune, "kakoune" }, 96 { Quoting::Shell, "shell" } 97 }); 98 } 99 100 inline auto quoter(Quoting quoting) 101 { 102 switch (quoting) 103 { 104 case Quoting::Kakoune: return "e; 105 case Quoting::Shell: return &shell_quote; 106 case Quoting::Raw: 107 default: 108 return +[](StringView s) { return s.str(); }; 109 } 110 } 111 112 inline String option_to_string(StringView opt, Quoting quoting) { return quoter(quoting)(opt); } 113 inline Vector<String> option_to_strings(StringView opt) { return {opt.str()}; } 114 inline String option_from_string(Meta::Type<String>, StringView str) { return str.str(); } 115 inline bool option_add(String& opt, StringView val) { opt += val; return not val.empty(); } 116 117 } 118 119 #endif // string_utils_hh_INCLUDED