format.cc (4468B)
1 #include "format.hh" 2 3 #include "exception.hh" 4 #include "string_utils.hh" 5 6 #include <algorithm> 7 #include <charconv> 8 #include <cstdio> 9 10 namespace Kakoune 11 { 12 13 14 template<size_t N> 15 InplaceString<N> to_string_impl(auto val, auto format) 16 { 17 InplaceString<N> res; 18 auto [end, errc] = std::to_chars(res.m_data, res.m_data + N, val, format); 19 if (errc != std::errc{}) 20 throw runtime_error("to_string error"); 21 res.m_length = end - res.m_data; 22 *end = '\0'; 23 return res; 24 } 25 26 template<size_t N> 27 InplaceString<N> to_string_impl(auto val) 28 { 29 return to_string_impl<N>(val, 10); 30 } 31 32 InplaceString<15> to_string(int val) 33 { 34 return to_string_impl<15>(val); 35 } 36 37 InplaceString<15> to_string(unsigned val) 38 { 39 return to_string_impl<15>(val); 40 } 41 42 InplaceString<23> to_string(long int val) 43 { 44 return to_string_impl<23>(val); 45 } 46 47 InplaceString<23> to_string(long long int val) 48 { 49 return to_string_impl<23>(val); 50 } 51 52 InplaceString<23> to_string(unsigned long val) 53 { 54 return to_string_impl<23>(val); 55 } 56 57 InplaceString<23> to_string(Hex val) 58 { 59 return to_string_impl<23>(val.val, 16); 60 } 61 62 InplaceString<23> to_string(Grouped val) 63 { 64 auto ungrouped = to_string_impl<23>(val.val); 65 66 InplaceString<23> res; 67 for (int pos = 0, len = ungrouped.m_length; pos != len; ++pos) 68 { 69 if (res.m_length and ((len - pos) % 3) == 0) 70 res.m_data[res.m_length++] = ','; 71 res.m_data[res.m_length++] = ungrouped.m_data[pos]; 72 } 73 return res; 74 } 75 76 InplaceString<23> to_string(float val) 77 { 78 #if defined(__cpp_lib_to_chars) 79 return to_string_impl<23>(val, std::chars_format::general); 80 #else 81 InplaceString<23> res; 82 res.m_length = snprintf(res.m_data, 23, "%f", val); 83 return res; 84 #endif 85 } 86 87 InplaceString<7> to_string(Codepoint c) 88 { 89 InplaceString<7> res; 90 char* ptr = res.m_data; 91 utf8::dump(ptr, c); 92 res.m_length = (int)(ptr - res.m_data); 93 return res; 94 } 95 96 template<typename AppendFunc> 97 void format_impl(StringView fmt, ArrayView<const StringView> params, AppendFunc append) 98 { 99 int implicitIndex = 0; 100 for (auto it = fmt.begin(), end = fmt.end(); it != end;) 101 { 102 auto opening = std::find(it, end, '{'); 103 if (opening == end) 104 { 105 append(StringView{it, opening}); 106 break; 107 } 108 else if (opening != it and *(opening-1) == '\\') 109 { 110 append(StringView{it, opening-1}); 111 append('{'); 112 it = opening + 1; 113 } 114 else 115 { 116 append(StringView{it, opening}); 117 auto closing = std::find(opening, end, '}'); 118 if (closing == end) 119 throw runtime_error("format string error, unclosed '{'"); 120 121 auto format = std::find(opening+1, closing, ':'); 122 const int index = opening+1 == format ? implicitIndex : str_to_int({opening+1, format}); 123 124 if (index >= params.size()) 125 throw runtime_error("format string parameter index too big"); 126 127 if (format != closing) 128 { 129 char padding = ' '; 130 if (*(++format) == '0') 131 { 132 padding = '0'; 133 ++format; 134 } 135 for (ColumnCount width = str_to_int({format, closing}), len = params[index].column_length(); 136 width > len; --width) 137 append(padding); 138 } 139 140 append(params[index]); 141 implicitIndex = index+1; 142 it = closing+1; 143 } 144 } 145 } 146 147 StringView format_to(ArrayView<char> buffer, StringView fmt, ArrayView<const StringView> params) 148 { 149 char* ptr = buffer.begin(); 150 const char* end = buffer.end(); 151 format_impl(fmt, params, [&](StringView s) mutable { 152 for (auto c : s) 153 { 154 if (ptr == end) 155 throw runtime_error("buffer is too small"); 156 *ptr++ = c; 157 } 158 }); 159 if (ptr == end) 160 throw runtime_error("buffer is too small"); 161 *ptr = 0; 162 163 return { buffer.begin(), ptr }; 164 } 165 166 void format_with(FunctionRef<void (StringView)> append, StringView fmt, ArrayView<const StringView> params) 167 { 168 format_impl(fmt, params, append); 169 } 170 171 String format(StringView fmt, ArrayView<const StringView> params) 172 { 173 ByteCount size = fmt.length(); 174 for (auto& s : params) size += s.length(); 175 String res; 176 res.reserve(size); 177 178 format_impl(fmt, params, [&](StringView s) { res += s; }); 179 return res; 180 } 181 182 }