2017-10-09 16:12:42 +02:00
|
|
|
#include "string_utils.hh"
|
|
|
|
|
|
|
|
#include "exception.hh"
|
|
|
|
#include "utf8_iterator.hh"
|
|
|
|
#include "unit_tests.hh"
|
|
|
|
|
2022-05-05 00:41:48 +02:00
|
|
|
#include <charconv>
|
Fix build on FreeBSD
file.cc:390:21: error: use of undeclared identifier 'rename'; did you mean 'devname'?
if (replace and rename(temp_filename, zfilename) != 0)
^~~~~~
devname
/usr/include/stdlib.h:277:7: note: 'devname' declared here
char *devname(__dev_t, __mode_t);
^
file.cc:390:28: error: cannot initialize a parameter of type '__dev_t' (aka 'unsigned long') with an lvalue of type 'char [1024]'
if (replace and rename(temp_filename, zfilename) != 0)
^~~~~~~~~~~~~
/usr/include/stdlib.h:277:22: note: passing argument to parameter here
char *devname(__dev_t, __mode_t);
^
2 errors generated.
---
highlighters.cc:1110:13: error: use of undeclared identifier 'snprintf'; did you mean 'vswprintf'?
snprintf(buffer, 16, format, std::abs(line_to_format));
^~~~~~~~
vswprintf
/usr/include/wchar.h:139:5: note: 'vswprintf' declared here
int vswprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
highlighters.cc:1110:22: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [16]'
snprintf(buffer, 16, format, std::abs(line_to_format));
^~~~~~
/usr/include/wchar.h:139:35: note: passing argument to parameter here
int vswprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
2 errors generated.
---
json_ui.cc:60:13: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
sprintf(buf, "\\u%04x", *next);
^~~~~~~
swprintf
/usr/include/wchar.h:133:5: note: 'swprintf' declared here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
json_ui.cc:60:21: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [7]'
sprintf(buf, "\\u%04x", *next);
^~~
/usr/include/wchar.h:133:34: note: passing argument to parameter here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
json_ui.cc:74:9: error: use of undeclared identifier 'sprintf'
sprintf(buffer, R"("#%02x%02x%02x")", color.r, color.g, color.b);
^
3 errors generated.
---
regex_impl.cc:1039:9: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
sprintf(buf, " %03d ", count++);
^~~~~~~
swprintf
/usr/include/wchar.h:133:5: note: 'swprintf' declared here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
regex_impl.cc:1039:17: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [20]'
sprintf(buf, " %03d ", count++);
^~~
/usr/include/wchar.h:133:34: note: passing argument to parameter here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
regex_impl.cc:1197:17: error: use of undeclared identifier 'puts'
{ if (dump) puts(dump_regex(*this).c_str()); }
^
regex_impl.cc:1208:18: note: in instantiation of member function 'Kakoune::(anonymous namespace)::TestVM<Kakoune::RegexMode::Forward>::TestVM' requested here
TestVM<> vm{R"(a*b)"};
^
regex_impl.cc:1197:17: error: use of undeclared identifier 'puts'
{ if (dump) puts(dump_regex(*this).c_str()); }
^
regex_impl.cc:1283:56: note: in instantiation of member function 'Kakoune::(anonymous namespace)::TestVM<5>::TestVM' requested here
TestVM<RegexMode::Forward | RegexMode::Search> vm{R"(f.*a(.*o))"};
^
regex_impl.cc:1197:17: error: use of undeclared identifier 'puts'
{ if (dump) puts(dump_regex(*this).c_str()); }
^
regex_impl.cc:1423:57: note: in instantiation of member function 'Kakoune::(anonymous namespace)::TestVM<6>::TestVM' requested here
TestVM<RegexMode::Backward | RegexMode::Search> vm{R"(fo{1,})"};
^
5 errors generated.
---
remote.cc:829:9: error: use of undeclared identifier 'rename'; did you mean 'devname'?
if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
^~~~~~
devname
/usr/include/stdlib.h:277:7: note: 'devname' declared here
char *devname(__dev_t, __mode_t);
^
remote.cc:829:16: error: cannot initialize a parameter of type '__dev_t' (aka 'unsigned long') with an rvalue of type 'const char *'
if (rename(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/stdlib.h:277:22: note: passing argument to parameter here
char *devname(__dev_t, __mode_t);
^
2 errors generated.
---
string_utils.cc:126:20: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
res.m_length = sprintf(res.m_data, "%i", val);
^~~~~~~
swprintf
/usr/include/wchar.h:133:5: note: 'swprintf' declared here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
string_utils.cc:126:28: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'char [15]'
res.m_length = sprintf(res.m_data, "%i", val);
^~~~~~~~~~
/usr/include/wchar.h:133:34: note: passing argument to parameter here
int swprintf(wchar_t * __restrict, size_t n, const wchar_t * __restrict,
^
string_utils.cc:133:20: error: use of undeclared identifier 'sprintf'; did you mean 'swprintf'?
res.m_length = sprintf(res.m_data, "%u", val);
^~~~~~~
swprintf
[...]
2019-07-01 16:32:14 +02:00
|
|
|
#include <cstdio>
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2019-07-23 12:09:41 +02:00
|
|
|
String trim_indent(StringView str)
|
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
return {};
|
|
|
|
|
2019-09-17 13:48:00 +02:00
|
|
|
if (str[0_byte] == '\n')
|
|
|
|
str = str.substr(1_byte);
|
|
|
|
while (not str.empty() and is_blank(str.back()))
|
|
|
|
str = str.substr(0, str.length() - 1);
|
|
|
|
|
|
|
|
utf8::iterator it{str.begin(), str};
|
|
|
|
while (it != str.end() and is_horizontal_blank(*it))
|
2021-09-10 13:16:51 +02:00
|
|
|
++it;
|
2019-09-17 13:48:00 +02:00
|
|
|
|
|
|
|
const StringView indent{str.begin(), it.base()};
|
|
|
|
return accumulate(str | split_after<StringView>('\n') | transform([&](auto&& line) {
|
|
|
|
if (line == "\n")
|
|
|
|
return line;
|
|
|
|
else if (not prefix_match(line, indent))
|
|
|
|
throw runtime_error("inconsistent indentation in the string");
|
|
|
|
|
|
|
|
return line.substr(indent.length());
|
2020-07-25 06:07:57 +02:00
|
|
|
}), String{}, [](String s, StringView l) { return s += l; });
|
2019-07-23 12:09:41 +02:00
|
|
|
}
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
String escape(StringView str, StringView characters, char escape)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
res.reserve(str.length());
|
|
|
|
auto cbeg = characters.begin(), cend = characters.end();
|
|
|
|
for (auto it = str.begin(), end = str.end(); it != end; )
|
|
|
|
{
|
|
|
|
auto next = std::find_first_of(it, end, cbeg, cend);
|
|
|
|
if (next != end)
|
|
|
|
{
|
|
|
|
res += StringView{it, next+1};
|
|
|
|
res.back() = escape;
|
|
|
|
res += *next;
|
|
|
|
it = next+1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res += StringView{it, next};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String unescape(StringView str, StringView characters, char escape)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
res.reserve(str.length());
|
|
|
|
for (auto it = str.begin(), end = str.end(); it != end; )
|
|
|
|
{
|
|
|
|
auto next = std::find(it, end, escape);
|
|
|
|
if (next != end and next+1 != end and contains(characters, *(next+1)))
|
|
|
|
{
|
|
|
|
res += StringView{it, next+1};
|
|
|
|
res.back() = *(next+1);
|
|
|
|
it = next + 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res += StringView{it, next == end ? next : next + 1};
|
|
|
|
it = next == end ? next : next + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String indent(StringView str, StringView indent)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
res.reserve(str.length());
|
|
|
|
bool was_eol = true;
|
|
|
|
for (ByteCount i = 0; i < str.length(); ++i)
|
|
|
|
{
|
|
|
|
if (was_eol)
|
|
|
|
res += indent;
|
|
|
|
res += str[i];
|
|
|
|
was_eol = is_eol(str[i]);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String replace(StringView str, StringView substr, StringView replacement)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (auto it = str.begin(); it != str.end(); )
|
|
|
|
{
|
|
|
|
auto match = std::search(it, str.end(), substr.begin(), substr.end());
|
|
|
|
res += StringView{it, match};
|
|
|
|
if (match == str.end())
|
|
|
|
break;
|
|
|
|
|
|
|
|
res += replacement;
|
2022-12-15 03:29:45 +01:00
|
|
|
it = match + substr.length();
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-05-19 09:16:37 +02:00
|
|
|
String left_pad(StringView str, ColumnCount size, Codepoint c)
|
|
|
|
{
|
|
|
|
return String(c, std::max(0_col, size - str.column_length())) + str.substr(0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
String right_pad(StringView str, ColumnCount size, Codepoint c)
|
|
|
|
{
|
|
|
|
return str.substr(0, size) + String(c, std::max(0_col, size - str.column_length()));
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
Optional<int> str_to_int_ifp(StringView str)
|
|
|
|
{
|
|
|
|
bool negative = not str.empty() and str[0] == '-';
|
|
|
|
if (negative)
|
|
|
|
str = str.substr(1_byte);
|
2018-08-04 12:10:19 +02:00
|
|
|
if (str.empty())
|
|
|
|
return {};
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
unsigned int res = 0;
|
|
|
|
for (auto c : str)
|
|
|
|
{
|
|
|
|
if (c < '0' or c > '9')
|
|
|
|
return {};
|
|
|
|
res = res * 10 + c - '0';
|
|
|
|
}
|
|
|
|
return negative ? -res : res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_to_int(StringView str)
|
|
|
|
{
|
|
|
|
if (auto val = str_to_int_ifp(str))
|
|
|
|
return *val;
|
|
|
|
throw runtime_error{str + " is not a number"};
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:41:48 +02:00
|
|
|
template<size_t N>
|
|
|
|
InplaceString<N> to_string_impl(auto val, auto format)
|
2017-10-09 16:12:42 +02:00
|
|
|
{
|
2022-05-05 00:41:48 +02:00
|
|
|
InplaceString<N> res;
|
|
|
|
auto [end, errc] = std::to_chars(res.m_data, res.m_data + N, val, format);
|
|
|
|
if (errc != std::errc{})
|
|
|
|
throw runtime_error("to_string error");
|
|
|
|
res.m_length = end - res.m_data;
|
|
|
|
*end = '\0';
|
2017-10-09 16:12:42 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:41:48 +02:00
|
|
|
template<size_t N>
|
|
|
|
InplaceString<N> to_string_impl(auto val)
|
|
|
|
{
|
|
|
|
return to_string_impl<N>(val, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
InplaceString<15> to_string(int val)
|
|
|
|
{
|
|
|
|
return to_string_impl<15>(val);
|
|
|
|
}
|
|
|
|
|
2018-04-27 00:34:49 +02:00
|
|
|
InplaceString<15> to_string(unsigned val)
|
|
|
|
{
|
2022-05-05 00:41:48 +02:00
|
|
|
return to_string_impl<15>(val);
|
2018-04-27 00:34:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
InplaceString<23> to_string(long int val)
|
|
|
|
{
|
2022-05-05 00:41:48 +02:00
|
|
|
return to_string_impl<23>(val);
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InplaceString<23> to_string(long long int val)
|
|
|
|
{
|
2022-05-05 00:41:48 +02:00
|
|
|
return to_string_impl<23>(val);
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-21 12:01:19 +02:00
|
|
|
InplaceString<23> to_string(unsigned long val)
|
2017-10-09 16:12:42 +02:00
|
|
|
{
|
2022-05-05 00:41:48 +02:00
|
|
|
return to_string_impl<23>(val);
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InplaceString<23> to_string(Hex val)
|
|
|
|
{
|
2022-05-05 00:41:48 +02:00
|
|
|
return to_string_impl<23>(val.val, 16);
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
2022-08-13 19:53:27 +02:00
|
|
|
InplaceString<23> to_string(Grouped val)
|
|
|
|
{
|
|
|
|
auto ungrouped = to_string_impl<23>(val.val);
|
|
|
|
|
|
|
|
InplaceString<23> res;
|
|
|
|
for (int pos = 0, len = ungrouped.m_length; pos != len; ++pos)
|
|
|
|
{
|
|
|
|
if (res.m_length and ((len - pos) % 3) == 0)
|
|
|
|
res.m_data[res.m_length++] = ',';
|
|
|
|
res.m_data[res.m_length++] = ungrouped.m_data[pos];
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
InplaceString<23> to_string(float val)
|
|
|
|
{
|
2022-05-08 13:41:30 +02:00
|
|
|
#if defined(__cpp_lib_to_chars)
|
2022-05-05 00:41:48 +02:00
|
|
|
return to_string_impl<23>(val, std::chars_format::general);
|
2022-05-08 13:41:30 +02:00
|
|
|
#else
|
|
|
|
InplaceString<23> res;
|
|
|
|
res.m_length = sprintf(res.m_data, "%f", val);
|
|
|
|
return res;
|
|
|
|
#endif
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InplaceString<7> to_string(Codepoint c)
|
|
|
|
{
|
|
|
|
InplaceString<7> res;
|
|
|
|
char* ptr = res.m_data;
|
|
|
|
utf8::dump(ptr, c);
|
|
|
|
res.m_length = (int)(ptr - res.m_data);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subsequence_match(StringView str, StringView subseq)
|
|
|
|
{
|
|
|
|
auto it = str.begin();
|
|
|
|
for (auto& c : subseq)
|
|
|
|
{
|
|
|
|
if (it == str.end())
|
|
|
|
return false;
|
|
|
|
while (*it != c)
|
|
|
|
{
|
|
|
|
if (++it == str.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
String expand_tabs(StringView line, ColumnCount tabstop, ColumnCount col)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
res.reserve(line.length());
|
|
|
|
for (auto it = line.begin(), end = line.end(); it != end; )
|
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
{
|
|
|
|
ColumnCount end_col = (col / tabstop + 1) * tabstop;
|
|
|
|
res += String{' ', end_col - col};
|
|
|
|
col = end_col;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto char_beg = it;
|
|
|
|
auto cp = utf8::read_codepoint(it, end);
|
|
|
|
res += {char_beg, it};
|
|
|
|
col += codepoint_width(cp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
WrapView::Iterator::Iterator(StringView text, ColumnCount max_width)
|
|
|
|
: m_remaining{text}, m_max_width{max_width}
|
2017-10-09 16:12:42 +02:00
|
|
|
{
|
|
|
|
if (max_width <= 0)
|
|
|
|
throw runtime_error("Invalid max width");
|
2019-11-23 11:50:58 +01:00
|
|
|
++*this;
|
|
|
|
}
|
2017-10-09 16:12:42 +02:00
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
WrapView::Iterator& WrapView::Iterator::operator++()
|
|
|
|
{
|
2017-10-09 16:12:42 +02:00
|
|
|
using Utf8It = utf8::iterator<const char*>;
|
2019-11-23 11:50:58 +01:00
|
|
|
Utf8It it{m_remaining.begin(), m_remaining};
|
2017-10-09 16:12:42 +02:00
|
|
|
Utf8It last_word_end = it;
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
while (it != m_remaining.end())
|
2017-10-09 16:12:42 +02:00
|
|
|
{
|
2018-11-27 08:13:29 +01:00
|
|
|
const CharCategories cat = categorize(*it, {'_'});
|
2017-10-09 16:12:42 +02:00
|
|
|
if (cat == CharCategories::EndOfLine)
|
|
|
|
{
|
2019-11-23 11:50:58 +01:00
|
|
|
m_current = StringView{m_remaining.begin(), it.base()};
|
|
|
|
m_remaining = StringView{(it+1).base(), m_remaining.end()};
|
|
|
|
return *this;
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Utf8It word_end = it+1;
|
2019-11-23 11:50:58 +01:00
|
|
|
while (word_end != m_remaining.end() and categorize(*word_end, {'_'}) == cat)
|
2017-10-09 16:12:42 +02:00
|
|
|
++word_end;
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
if (word_end > m_remaining.begin() and
|
|
|
|
utf8::column_distance(m_remaining.begin(), word_end.base()) >= m_max_width)
|
2017-10-09 16:12:42 +02:00
|
|
|
{
|
2019-11-23 11:50:58 +01:00
|
|
|
auto line_end = last_word_end <= m_remaining.begin() ?
|
|
|
|
Utf8It{utf8::advance(m_remaining.begin(), m_remaining.end(), m_max_width), m_remaining}
|
2017-10-09 16:12:42 +02:00
|
|
|
: last_word_end;
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
m_current = StringView{m_remaining.begin(), line_end.base()};
|
2017-10-09 16:12:42 +02:00
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
while (line_end != m_remaining.end() and is_horizontal_blank(*line_end))
|
2017-10-09 16:12:42 +02:00
|
|
|
++line_end;
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
if (line_end != m_remaining.end() and *line_end == '\n')
|
2017-10-09 16:12:42 +02:00
|
|
|
++line_end;
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
m_remaining = StringView{line_end.base(), m_remaining.end()};
|
|
|
|
return *this;
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
if (cat == CharCategories::Word or cat == CharCategories::Punctuation)
|
|
|
|
last_word_end = word_end;
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
if (word_end > m_remaining.begin())
|
2017-10-09 16:12:42 +02:00
|
|
|
it = word_end;
|
|
|
|
}
|
2019-11-23 11:50:58 +01:00
|
|
|
m_current = m_remaining;
|
|
|
|
m_remaining = StringView{};
|
|
|
|
return *this;
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename AppendFunc>
|
|
|
|
void format_impl(StringView fmt, ArrayView<const StringView> params, AppendFunc append)
|
|
|
|
{
|
|
|
|
int implicitIndex = 0;
|
|
|
|
for (auto it = fmt.begin(), end = fmt.end(); it != end;)
|
|
|
|
{
|
|
|
|
auto opening = std::find(it, end, '{');
|
|
|
|
if (opening == end)
|
|
|
|
{
|
|
|
|
append(StringView{it, opening});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (opening != it and *(opening-1) == '\\')
|
|
|
|
{
|
|
|
|
append(StringView{it, opening-1});
|
|
|
|
append('{');
|
|
|
|
it = opening + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
append(StringView{it, opening});
|
|
|
|
auto closing = std::find(opening, end, '}');
|
|
|
|
if (closing == end)
|
2018-04-06 16:56:53 +02:00
|
|
|
throw runtime_error("format string error, unclosed '{'");
|
2017-10-09 16:12:42 +02:00
|
|
|
|
2022-08-13 19:53:27 +02:00
|
|
|
auto format = std::find(opening+1, closing, ':');
|
|
|
|
const int index = opening+1 == format ? implicitIndex : str_to_int({opening+1, format});
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
if (index >= params.size())
|
2018-04-06 16:56:53 +02:00
|
|
|
throw runtime_error("format string parameter index too big");
|
2017-10-09 16:12:42 +02:00
|
|
|
|
2022-08-13 19:53:27 +02:00
|
|
|
if (format != closing)
|
|
|
|
{
|
|
|
|
for (ColumnCount width = str_to_int({format+1, closing}), len = params[index].column_length();
|
|
|
|
width > len; --width)
|
|
|
|
append(' ');
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
append(params[index]);
|
|
|
|
implicitIndex = index+1;
|
|
|
|
it = closing+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView format_to(ArrayView<char> buffer, StringView fmt, ArrayView<const StringView> params)
|
|
|
|
{
|
|
|
|
char* ptr = buffer.begin();
|
|
|
|
const char* end = buffer.end();
|
|
|
|
format_impl(fmt, params, [&](StringView s) mutable {
|
|
|
|
for (auto c : s)
|
|
|
|
{
|
|
|
|
if (ptr == end)
|
|
|
|
throw runtime_error("buffer is too small");
|
|
|
|
*ptr++ = c;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (ptr == end)
|
|
|
|
throw runtime_error("buffer is too small");
|
|
|
|
*ptr = 0;
|
|
|
|
|
|
|
|
return { buffer.begin(), ptr };
|
|
|
|
}
|
|
|
|
|
2021-07-31 01:45:05 +02:00
|
|
|
void format_with(FunctionRef<void (StringView)> append, StringView fmt, ArrayView<const StringView> params)
|
|
|
|
{
|
|
|
|
format_impl(fmt, params, append);
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
String format(StringView fmt, ArrayView<const StringView> params)
|
|
|
|
{
|
|
|
|
ByteCount size = fmt.length();
|
|
|
|
for (auto& s : params) size += s.length();
|
|
|
|
String res;
|
|
|
|
res.reserve(size);
|
|
|
|
|
|
|
|
format_impl(fmt, params, [&](StringView s) { res += s; });
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-06-23 04:43:39 +02:00
|
|
|
String double_up(StringView s, StringView characters)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
auto pos = s.begin();
|
|
|
|
for (auto it = s.begin(), end = s.end(); it != end; ++it)
|
|
|
|
{
|
|
|
|
if (contains(characters, *it))
|
|
|
|
{
|
|
|
|
res += StringView{pos, it+1};
|
|
|
|
res += *it;
|
|
|
|
pos = it+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res += StringView{pos, s.end()};
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
UnitTest test_string{[]()
|
|
|
|
{
|
|
|
|
kak_assert(String("youpi ") + "matin" == "youpi matin");
|
|
|
|
|
2022-02-22 10:14:47 +01:00
|
|
|
kak_assert(StringView{"youpi"}.starts_with(""));
|
|
|
|
kak_assert(StringView{"youpi"}.starts_with("you"));
|
|
|
|
kak_assert(StringView{"youpi"}.starts_with("youpi"));
|
|
|
|
kak_assert(not StringView{"youpi"}.starts_with("youpi!"));
|
|
|
|
|
|
|
|
kak_assert(StringView{"youpi"}.ends_with(""));
|
|
|
|
kak_assert(StringView{"youpi"}.ends_with("pi"));
|
|
|
|
kak_assert(StringView{"youpi"}.ends_with("youpi"));
|
|
|
|
kak_assert(not StringView{"youpi"}.ends_with("oup"));
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
auto wrapped = "wrap this paragraph\n respecting whitespaces and much_too_long_words" | wrap_at(16) | gather<Vector>();
|
2017-10-09 16:12:42 +02:00
|
|
|
kak_assert(wrapped.size() == 6);
|
|
|
|
kak_assert(wrapped[0] == "wrap this");
|
|
|
|
kak_assert(wrapped[1] == "paragraph");
|
|
|
|
kak_assert(wrapped[2] == " respecting");
|
|
|
|
kak_assert(wrapped[3] == "whitespaces and");
|
|
|
|
kak_assert(wrapped[4] == "much_too_long_wo");
|
|
|
|
kak_assert(wrapped[5] == "rds");
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
auto wrapped2 = "error: unknown type" | wrap_at(7) | gather<Vector>();
|
2017-10-09 16:12:42 +02:00
|
|
|
kak_assert(wrapped2.size() == 3);
|
|
|
|
kak_assert(wrapped2[0] == "error:");
|
|
|
|
kak_assert(wrapped2[1] == "unknown");
|
|
|
|
kak_assert(wrapped2[2] == "type");
|
|
|
|
|
2019-07-23 12:09:41 +02:00
|
|
|
kak_assert(trim_indent(" ") == "");
|
|
|
|
kak_assert(trim_indent("no-indent") == "no-indent");
|
|
|
|
kak_assert(trim_indent("\nno-indent") == "no-indent");
|
|
|
|
kak_assert(trim_indent("\n indent\n indent") == "indent\nindent");
|
|
|
|
kak_assert(trim_indent("\n indent\n indent") == "indent\n indent");
|
2019-09-17 13:48:00 +02:00
|
|
|
kak_assert(trim_indent("\n indent\n indent\n ") == "indent\nindent");
|
2019-07-23 12:09:41 +02:00
|
|
|
|
|
|
|
kak_expect_throw(runtime_error, trim_indent("\n indent\nno-indent"));
|
|
|
|
|
2017-12-06 18:56:02 +01:00
|
|
|
kak_assert(escape(R"(\youpi:matin:tchou\:)", ":\\", '\\') == R"(\\youpi\:matin\:tchou\\\:)");
|
|
|
|
kak_assert(unescape(R"(\\youpi\:matin\:tchou\\\:)", ":\\", '\\') == R"(\youpi:matin:tchou\:)");
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
kak_assert(prefix_match("tchou kanaky", "tchou"));
|
|
|
|
kak_assert(prefix_match("tchou kanaky", "tchou kanaky"));
|
|
|
|
kak_assert(prefix_match("tchou kanaky", "t"));
|
|
|
|
kak_assert(not prefix_match("tchou kanaky", "c"));
|
|
|
|
|
|
|
|
kak_assert(subsequence_match("tchou kanaky", "tknky"));
|
|
|
|
kak_assert(subsequence_match("tchou kanaky", "knk"));
|
|
|
|
kak_assert(subsequence_match("tchou kanaky", "tchou kanaky"));
|
|
|
|
kak_assert(not subsequence_match("tchou kanaky", "tchou kanaky"));
|
|
|
|
|
2022-08-13 19:53:27 +02:00
|
|
|
kak_assert(format("Youhou {1} {} '{0:4}' \\{}", 10, "hehe", 5) == "Youhou hehe 5 ' 10' {}");
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
char buffer[20];
|
|
|
|
kak_assert(format_to(buffer, "Hey {}", 15) == "Hey 15");
|
|
|
|
|
|
|
|
kak_assert(str_to_int("5") == 5);
|
|
|
|
kak_assert(str_to_int(to_string(INT_MAX)) == INT_MAX);
|
|
|
|
kak_assert(str_to_int(to_string(INT_MIN)) == INT_MIN);
|
|
|
|
kak_assert(str_to_int("00") == 0);
|
|
|
|
kak_assert(str_to_int("-0") == 0);
|
|
|
|
|
2018-06-23 04:43:39 +02:00
|
|
|
kak_assert(double_up(R"('foo%"bar"')", "'\"%") == R"(''foo%%""bar""'')");
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
kak_assert(replace("tchou/tcha/tchi", "/", "!!") == "tchou!!tcha!!tchi");
|
|
|
|
}};
|
|
|
|
|
|
|
|
}
|