2012-05-29 09:19:50 +04:00
|
|
|
#include "string.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
#include "exception.hh"
|
2014-12-23 16:34:21 +03:00
|
|
|
#include "containers.hh"
|
2014-04-28 22:49:00 +04:00
|
|
|
#include "utf8_iterator.hh"
|
2015-05-22 15:58:56 +03:00
|
|
|
#include "unit_tests.hh"
|
2012-05-29 09:19:50 +04:00
|
|
|
|
2015-01-08 22:31:28 +03:00
|
|
|
#include <cstdio>
|
|
|
|
|
2012-05-29 09:19:50 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2016-02-05 12:27:22 +03:00
|
|
|
String::Data::Data(const char* data, size_t size, size_t capacity)
|
|
|
|
{
|
|
|
|
if (capacity > Short::capacity)
|
|
|
|
{
|
|
|
|
if (capacity & 1)
|
|
|
|
++capacity;
|
|
|
|
|
|
|
|
l.ptr = Alloc{}.allocate(capacity+1);
|
|
|
|
l.size = size;
|
|
|
|
l.capacity = capacity;
|
|
|
|
|
|
|
|
memcpy(l.ptr, data, size);
|
|
|
|
l.ptr[size] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
set_short(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
String::Data::Data(Data&& other) noexcept
|
|
|
|
{
|
|
|
|
if (other.is_long())
|
|
|
|
{
|
|
|
|
l = other.l;
|
|
|
|
other.s.size = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s = other.s;
|
|
|
|
}
|
|
|
|
|
|
|
|
String::Data& String::Data::operator=(const Data& other)
|
|
|
|
{
|
|
|
|
const size_t new_size = other.size();
|
|
|
|
reserve<false>(new_size);
|
|
|
|
memcpy(data(), other.data(), new_size+1);
|
|
|
|
set_size(new_size);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String::Data& String::Data::operator=(Data&& other) noexcept
|
|
|
|
{
|
|
|
|
if (other.is_long())
|
|
|
|
{
|
|
|
|
l = other.l;
|
|
|
|
other.set_empty();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s = other.s;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool copy>
|
|
|
|
void String::Data::reserve(size_t new_capacity)
|
|
|
|
{
|
|
|
|
if (new_capacity <= capacity())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (is_long())
|
|
|
|
new_capacity = std::max(l.capacity * 2, new_capacity);
|
|
|
|
|
|
|
|
char* new_ptr = Alloc{}.allocate(new_capacity+1);
|
|
|
|
if (copy)
|
|
|
|
{
|
|
|
|
memcpy(new_ptr, data(), size()+1);
|
|
|
|
l.size = size();
|
|
|
|
}
|
|
|
|
release();
|
|
|
|
|
|
|
|
l.ptr = new_ptr;
|
|
|
|
l.capacity = new_capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
template void String::Data::reserve<true>(size_t);
|
|
|
|
template void String::Data::reserve<false>(size_t);
|
|
|
|
|
|
|
|
void String::Data::force_size(size_t new_size)
|
|
|
|
{
|
|
|
|
reserve<false>(new_size);
|
|
|
|
set_size(new_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::append(const char* str, size_t len)
|
|
|
|
{
|
|
|
|
const size_t new_size = size() + len;
|
|
|
|
reserve(new_size);
|
|
|
|
|
|
|
|
memcpy(data() + size(), str, len);
|
|
|
|
set_size(new_size);
|
|
|
|
data()[new_size] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::clear()
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
set_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::release()
|
|
|
|
{
|
|
|
|
if (is_long())
|
|
|
|
Alloc{}.deallocate(l.ptr, l.capacity+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::set_size(size_t size)
|
|
|
|
{
|
|
|
|
if (is_long())
|
|
|
|
l.size = size;
|
|
|
|
else
|
|
|
|
s.size = (size << 1) | 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::Data::set_short(const char* data, size_t size)
|
|
|
|
{
|
|
|
|
s.size = (size << 1) | 1;
|
|
|
|
memcpy(s.string, data, size);
|
|
|
|
s.string[size] = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:07:41 +03:00
|
|
|
const String String::ms_empty;
|
|
|
|
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<String> split(StringView str, char separator, char escape)
|
2012-05-29 09:19:50 +04:00
|
|
|
{
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<String> res;
|
2014-08-03 13:02:17 +04:00
|
|
|
auto it = str.begin();
|
2016-03-25 02:25:58 +03:00
|
|
|
auto start = it;
|
2014-08-03 13:02:17 +04:00
|
|
|
while (it != str.end())
|
2012-05-29 09:19:50 +04:00
|
|
|
{
|
2013-07-25 00:37:17 +04:00
|
|
|
res.emplace_back();
|
|
|
|
String& element = res.back();
|
2014-08-03 13:02:17 +04:00
|
|
|
while (it != str.end())
|
2013-07-25 00:37:17 +04:00
|
|
|
{
|
2014-08-03 13:02:17 +04:00
|
|
|
auto c = *it;
|
|
|
|
if (c == escape and it + 1 != str.end() and *(it+1) == separator)
|
2013-07-25 00:37:17 +04:00
|
|
|
{
|
2016-03-25 02:25:58 +03:00
|
|
|
element += StringView{start, it+1};
|
|
|
|
element.back() = separator;
|
2014-08-03 13:02:17 +04:00
|
|
|
it += 2;
|
2016-03-25 02:25:58 +03:00
|
|
|
start = it;
|
2013-07-25 00:37:17 +04:00
|
|
|
}
|
|
|
|
else if (c == separator)
|
|
|
|
{
|
2016-03-25 02:25:58 +03:00
|
|
|
element += StringView{start, it};
|
2014-08-03 13:02:17 +04:00
|
|
|
++it;
|
2016-03-25 02:25:58 +03:00
|
|
|
start = it;
|
2013-07-25 00:37:17 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2014-08-03 13:02:17 +04:00
|
|
|
++it;
|
2013-07-25 00:37:17 +04:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 02:25:58 +03:00
|
|
|
if (start != str.end())
|
|
|
|
res.back() += StringView{start, str.end()};
|
2013-07-25 00:37:17 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<StringView> split(StringView str, char separator)
|
2014-10-19 19:27:36 +04:00
|
|
|
{
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<StringView> res;
|
2015-11-19 03:17:18 +03:00
|
|
|
if (str.empty())
|
|
|
|
return res;
|
|
|
|
|
2014-10-19 19:27:36 +04:00
|
|
|
auto beg = str.begin();
|
|
|
|
for (auto it = beg; it != str.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == separator)
|
|
|
|
{
|
|
|
|
res.emplace_back(beg, it);
|
|
|
|
beg = it + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.emplace_back(beg, str.end());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-04 16:31:15 +03:00
|
|
|
String escape(StringView str, StringView characters, char escape)
|
2013-07-25 00:37:17 +04:00
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 14:46:53 +03:00
|
|
|
res.reserve(str.length());
|
2015-08-14 00:04:21 +03:00
|
|
|
for (auto it = str.begin(), end = str.end(); it != end; )
|
2013-07-25 00:37:17 +04:00
|
|
|
{
|
2015-08-14 00:04:21 +03:00
|
|
|
auto next = std::find_if(it, end, [&characters](char c) { return contains(characters, c); });
|
|
|
|
if (next != end)
|
|
|
|
{
|
|
|
|
res += StringView{it, next+1};
|
|
|
|
res.back() = escape;
|
|
|
|
res += *next;
|
|
|
|
it = next+1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res += StringView{it, next};
|
|
|
|
break;
|
|
|
|
}
|
2012-05-29 09:19:50 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-04 16:31:15 +03:00
|
|
|
String unescape(StringView str, StringView characters, char escape)
|
2014-08-03 13:02:17 +04:00
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 14:46:53 +03:00
|
|
|
res.reserve(str.length());
|
2015-08-14 00:04:21 +03:00
|
|
|
for (auto it = str.begin(), end = str.end(); it != end; )
|
2014-08-03 13:02:17 +04:00
|
|
|
{
|
2015-08-14 00:04:21 +03:00
|
|
|
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);
|
2015-09-20 13:47:20 +03:00
|
|
|
it = next + 2;
|
2015-08-14 00:04:21 +03:00
|
|
|
}
|
2014-11-04 16:31:15 +03:00
|
|
|
else
|
2015-09-20 13:47:20 +03:00
|
|
|
{
|
2015-08-14 00:04:21 +03:00
|
|
|
res += StringView{it, next == end ? next : next + 1};
|
2015-09-20 13:47:20 +03:00
|
|
|
it = next == end ? next : next + 1;
|
|
|
|
}
|
2014-08-03 13:02:17 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-02-19 16:54:03 +03:00
|
|
|
String indent(StringView str, StringView indent)
|
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 14:46:53 +03:00
|
|
|
res.reserve(str.length());
|
2015-02-19 16:54:03 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-13 15:59:27 +03:00
|
|
|
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;
|
|
|
|
it = match + (int)substr.length();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:47:22 +03:00
|
|
|
Optional<int> str_to_int_ifp(StringView str)
|
2013-05-17 16:09:42 +04:00
|
|
|
{
|
2015-03-31 01:34:08 +03:00
|
|
|
unsigned int res = 0;
|
|
|
|
bool negative = false;
|
|
|
|
for (auto it = str.begin(), end = str.end(); it != end; ++it)
|
|
|
|
{
|
|
|
|
const char c = *it;
|
|
|
|
if (it == str.begin() and c == '-')
|
|
|
|
negative = true;
|
|
|
|
else if (c >= '0' and c <= '9')
|
|
|
|
res = res * 10 + c - '0';
|
|
|
|
else
|
2015-05-01 20:47:22 +03:00
|
|
|
return {};
|
2015-03-31 01:34:08 +03:00
|
|
|
}
|
|
|
|
return negative ? -(int)res : (int)res;
|
2013-06-19 00:11:44 +04:00
|
|
|
}
|
|
|
|
|
2015-05-01 20:47:22 +03:00
|
|
|
int str_to_int(StringView str)
|
|
|
|
{
|
|
|
|
if (auto val = str_to_int_ifp(str))
|
|
|
|
return *val;
|
2015-06-19 20:29:58 +03:00
|
|
|
throw runtime_error{str + " is not a number"};
|
2015-05-01 20:47:22 +03:00
|
|
|
}
|
|
|
|
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<15> to_string(int val)
|
2013-06-19 00:11:44 +04:00
|
|
|
{
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<15> res;
|
2015-03-31 15:53:40 +03:00
|
|
|
res.m_length = sprintf(res.m_data, "%i", val);
|
|
|
|
return res;
|
2013-05-17 16:09:42 +04:00
|
|
|
}
|
|
|
|
|
2015-09-25 01:36:29 +03:00
|
|
|
InplaceString<23> to_string(long int val)
|
|
|
|
{
|
|
|
|
InplaceString<23> res;
|
|
|
|
res.m_length = sprintf(res.m_data, "%li", val);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<23> to_string(size_t val)
|
2015-01-13 16:47:46 +03:00
|
|
|
{
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<23> res;
|
2015-03-31 15:53:40 +03:00
|
|
|
res.m_length = sprintf(res.m_data, "%zu", val);
|
|
|
|
return res;
|
2015-01-13 16:47:46 +03:00
|
|
|
}
|
|
|
|
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<23> to_string(Hex val)
|
2015-06-22 15:56:00 +03:00
|
|
|
{
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<23> res;
|
2015-06-22 15:56:00 +03:00
|
|
|
res.m_length = sprintf(res.m_data, "%zx", val.val);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<23> to_string(float val)
|
2015-01-13 16:47:46 +03:00
|
|
|
{
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<23> res;
|
2015-03-31 15:53:40 +03:00
|
|
|
res.m_length = sprintf(res.m_data, "%f", val);
|
|
|
|
return res;
|
2015-01-13 16:47:46 +03:00
|
|
|
}
|
|
|
|
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<7> to_string(Codepoint c)
|
2015-07-14 15:47:51 +03:00
|
|
|
{
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<7> res;
|
2015-07-14 15:47:51 +03:00
|
|
|
char* ptr = res.m_data;
|
|
|
|
utf8::dump(ptr, c);
|
|
|
|
res.m_length = (int)(ptr - res.m_data);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
bool subsequence_match(StringView str, StringView subseq)
|
2013-09-23 23:16:57 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 22:49:00 +04:00
|
|
|
String expand_tabs(StringView line, CharCount tabstop, CharCount col)
|
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 14:46:53 +03:00
|
|
|
res.reserve(line.length());
|
2015-03-30 15:33:46 +03:00
|
|
|
for (auto it = line.begin(), end = line.end(); it != end; )
|
2014-04-28 22:49:00 +04:00
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
{
|
|
|
|
CharCount end_col = (col / tabstop + 1) * tabstop;
|
|
|
|
res += String{' ', end_col - col};
|
2015-03-30 15:33:46 +03:00
|
|
|
col = end_col;
|
|
|
|
++it;
|
2014-04-28 22:49:00 +04:00
|
|
|
}
|
|
|
|
else
|
2015-03-30 15:33:46 +03:00
|
|
|
{
|
|
|
|
auto char_end = utf8::next(it, end);
|
|
|
|
res += {it, char_end};
|
|
|
|
++col;
|
|
|
|
it = char_end;
|
|
|
|
}
|
2014-04-28 22:49:00 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<StringView> wrap_lines(StringView text, CharCount max_width)
|
2014-11-19 22:42:15 +03:00
|
|
|
{
|
2015-09-08 01:29:01 +03:00
|
|
|
if (max_width <= 0)
|
|
|
|
throw runtime_error("Invalid max width");
|
|
|
|
|
2014-11-19 22:42:15 +03:00
|
|
|
using Utf8It = utf8::iterator<const char*>;
|
2015-09-23 21:39:21 +03:00
|
|
|
Utf8It word_begin{text.begin(), text};
|
2014-11-19 22:42:15 +03:00
|
|
|
Utf8It word_end{word_begin};
|
2015-09-23 21:39:21 +03:00
|
|
|
Utf8It end{text.end(), text};
|
2014-11-19 22:42:15 +03:00
|
|
|
CharCount col = 0;
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<StringView> lines;
|
2015-09-23 21:39:21 +03:00
|
|
|
Utf8It line_begin{text.begin(), text};
|
2014-11-20 21:45:10 +03:00
|
|
|
Utf8It line_end = line_begin;
|
2014-11-19 22:42:15 +03:00
|
|
|
while (word_begin != end)
|
|
|
|
{
|
2014-11-20 16:55:07 +03:00
|
|
|
const CharCategories cat = categorize(*word_begin);
|
2014-11-19 22:42:15 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
++word_end;
|
|
|
|
} while (word_end != end and categorize(*word_end) == cat);
|
|
|
|
|
|
|
|
col += word_end - word_begin;
|
2014-11-20 21:45:10 +03:00
|
|
|
if ((word_begin != line_begin and col > max_width) or
|
|
|
|
cat == CharCategories::EndOfLine)
|
2014-11-19 22:42:15 +03:00
|
|
|
{
|
2014-11-20 21:45:10 +03:00
|
|
|
lines.emplace_back(line_begin.base(), line_end.base());
|
2014-11-20 16:55:07 +03:00
|
|
|
line_begin = (cat == CharCategories::EndOfLine or
|
2014-11-20 21:45:10 +03:00
|
|
|
cat == CharCategories::Blank) ? word_end : word_begin;
|
|
|
|
col = word_end - line_begin;
|
2014-11-19 22:42:15 +03:00
|
|
|
}
|
2014-11-20 16:55:07 +03:00
|
|
|
if (cat == CharCategories::Word or cat == CharCategories::Punctuation)
|
2014-11-20 21:45:10 +03:00
|
|
|
line_end = word_end;
|
2014-11-20 16:55:07 +03:00
|
|
|
|
2014-11-19 22:42:15 +03:00
|
|
|
word_begin = word_end;
|
|
|
|
}
|
2014-11-20 21:45:10 +03:00
|
|
|
if (line_begin != word_begin)
|
|
|
|
lines.emplace_back(line_begin.base(), word_begin.base());
|
2014-11-19 22:42:15 +03:00
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2015-04-22 15:19:46 +03:00
|
|
|
template<typename AppendFunc>
|
|
|
|
void format_impl(StringView fmt, ArrayView<const StringView> params, AppendFunc append)
|
2015-03-31 01:05:24 +03:00
|
|
|
{
|
|
|
|
int implicitIndex = 0;
|
|
|
|
for (auto it = fmt.begin(), end = fmt.end(); it != end;)
|
|
|
|
{
|
|
|
|
auto opening = std::find(it, end, '{');
|
|
|
|
if (opening == end)
|
2015-04-22 15:19:46 +03:00
|
|
|
{
|
|
|
|
append(StringView{it, opening});
|
2015-03-31 01:05:24 +03:00
|
|
|
break;
|
2015-04-22 15:19:46 +03:00
|
|
|
}
|
|
|
|
else if (opening != it and *(opening-1) == '\\')
|
2015-03-31 01:05:24 +03:00
|
|
|
{
|
2015-04-22 15:19:46 +03:00
|
|
|
append(StringView{it, opening-1});
|
|
|
|
append('{');
|
2015-03-31 01:05:24 +03:00
|
|
|
it = opening + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-22 15:19:46 +03:00
|
|
|
append(StringView{it, opening});
|
|
|
|
auto closing = std::find(opening, end, '}');
|
2015-03-31 01:05:24 +03:00
|
|
|
if (closing == end)
|
|
|
|
throw runtime_error("Format string error, unclosed '{'");
|
|
|
|
int index;
|
|
|
|
if (closing == opening + 1)
|
|
|
|
index = implicitIndex;
|
|
|
|
else
|
|
|
|
index = str_to_int({opening+1, closing});
|
|
|
|
|
|
|
|
if (index >= params.size())
|
|
|
|
throw runtime_error("Format string parameter index too big");
|
|
|
|
|
2015-04-22 15:19:46 +03:00
|
|
|
append(params[index]);
|
2015-03-31 01:05:24 +03:00
|
|
|
implicitIndex = index+1;
|
|
|
|
it = closing+1;
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 15:19:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
|
|
|
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; });
|
2015-03-31 01:05:24 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:58:56 +03:00
|
|
|
UnitTest test_string{[]()
|
|
|
|
{
|
|
|
|
kak_assert(String("youpi ") + "matin" == "youpi matin");
|
|
|
|
|
|
|
|
Vector<String> splited = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':', '\\');
|
|
|
|
kak_assert(splited[0] == "youpi");
|
|
|
|
kak_assert(splited[1] == "matin");
|
|
|
|
kak_assert(splited[2] == "");
|
|
|
|
kak_assert(splited[3] == "tchou:kanaky");
|
|
|
|
kak_assert(splited[4] == "hihi:");
|
|
|
|
|
|
|
|
Vector<StringView> splitedview = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':');
|
|
|
|
kak_assert(splitedview[0] == "youpi");
|
|
|
|
kak_assert(splitedview[1] == "matin");
|
|
|
|
kak_assert(splitedview[2] == "");
|
|
|
|
kak_assert(splitedview[3] == "tchou\\");
|
|
|
|
kak_assert(splitedview[4] == "kanaky");
|
|
|
|
kak_assert(splitedview[5] == "hihi\\");
|
|
|
|
kak_assert(splitedview[6] == "");
|
|
|
|
|
2015-09-20 19:18:43 +03:00
|
|
|
kak_assert(escape("youpi:matin:tchou:", ':', '\\') == "youpi\\:matin\\:tchou\\:");
|
|
|
|
kak_assert(unescape("youpi\\:matin\\:tchou\\:", ':', '\\') == "youpi:matin:tchou:");
|
2015-05-22 15:58:56 +03: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"));
|
|
|
|
|
|
|
|
kak_assert(format("Youhou {1} {} {0} \\{}", 10, "hehe", 5) == "Youhou hehe 5 10 {}");
|
|
|
|
|
|
|
|
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);
|
2016-02-13 15:59:27 +03:00
|
|
|
|
|
|
|
kak_assert(replace("tchou/tcha/tchi", "/", "!!") == "tchou!!tcha!!tchi");
|
2015-05-22 15:58:56 +03:00
|
|
|
}};
|
|
|
|
|
2012-05-29 09:19:50 +04:00
|
|
|
}
|