2012-05-29 07:19:50 +02:00
|
|
|
#include "string.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2013-03-29 19:31:06 +01:00
|
|
|
#include "exception.hh"
|
2014-12-23 14:34:21 +01:00
|
|
|
#include "containers.hh"
|
2014-04-28 20:49:00 +02:00
|
|
|
#include "utf8_iterator.hh"
|
2012-05-29 07:19:50 +02:00
|
|
|
|
2015-01-08 20:31:28 +01:00
|
|
|
#include <cstdio>
|
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<String> split(StringView str, char separator, char escape)
|
2012-05-29 07:19:50 +02:00
|
|
|
{
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<String> res;
|
2014-08-03 11:02:17 +02:00
|
|
|
auto it = str.begin();
|
|
|
|
while (it != str.end())
|
2012-05-29 07:19:50 +02:00
|
|
|
{
|
2013-07-24 22:37:17 +02:00
|
|
|
res.emplace_back();
|
|
|
|
String& element = res.back();
|
2014-08-03 11:02:17 +02:00
|
|
|
while (it != str.end())
|
2013-07-24 22:37:17 +02:00
|
|
|
{
|
2014-08-03 11:02:17 +02:00
|
|
|
auto c = *it;
|
|
|
|
if (c == escape and it + 1 != str.end() and *(it+1) == separator)
|
2013-07-24 22:37:17 +02:00
|
|
|
{
|
|
|
|
element += separator;
|
2014-08-03 11:02:17 +02:00
|
|
|
it += 2;
|
2013-07-24 22:37:17 +02:00
|
|
|
}
|
|
|
|
else if (c == separator)
|
|
|
|
{
|
2014-08-03 11:02:17 +02:00
|
|
|
++it;
|
2013-07-24 22:37:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
element += c;
|
2014-08-03 11:02:17 +02:00
|
|
|
++it;
|
2013-07-24 22:37:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<StringView> split(StringView str, char separator)
|
2014-10-19 17:27:36 +02:00
|
|
|
{
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<StringView> res;
|
2014-10-19 17:27:36 +02: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 14:31:15 +01:00
|
|
|
String escape(StringView str, StringView characters, char escape)
|
2013-07-24 22:37:17 +02:00
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 12:46:53 +01:00
|
|
|
res.reserve(str.length());
|
2013-07-24 22:37:17 +02:00
|
|
|
for (auto& c : str)
|
|
|
|
{
|
2014-11-04 14:31:15 +01:00
|
|
|
if (contains(characters, c))
|
2013-07-24 22:37:17 +02:00
|
|
|
res += escape;
|
|
|
|
res += c;
|
2012-05-29 07:19:50 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-11-04 14:31:15 +01:00
|
|
|
String unescape(StringView str, StringView characters, char escape)
|
2014-08-03 11:02:17 +02:00
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 12:46:53 +01:00
|
|
|
res.reserve(str.length());
|
2014-08-03 11:02:17 +02:00
|
|
|
for (auto& c : str)
|
|
|
|
{
|
2014-11-04 14:31:15 +01:00
|
|
|
if (contains(characters, c) and not res.empty() and res.back() == escape)
|
|
|
|
res.back() = c;
|
|
|
|
else
|
|
|
|
res += c;
|
2014-08-03 11:02:17 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-02-19 14:54:03 +01:00
|
|
|
String indent(StringView str, StringView indent)
|
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 12:46:53 +01:00
|
|
|
res.reserve(str.length());
|
2015-02-19 14:54:03 +01: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;
|
|
|
|
}
|
|
|
|
|
2014-08-03 11:02:17 +02:00
|
|
|
int str_to_int(StringView str)
|
2013-05-17 14:09:42 +02:00
|
|
|
{
|
2015-03-31 00:34:08 +02: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
|
|
|
|
throw runtime_error(str + "is not a number");
|
|
|
|
}
|
|
|
|
return negative ? -(int)res : (int)res;
|
2013-06-18 22:11:44 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 14:53:40 +02:00
|
|
|
InplaceString<16> to_string(int val)
|
2013-06-18 22:11:44 +02:00
|
|
|
{
|
2015-03-31 14:53:40 +02:00
|
|
|
InplaceString<16> res;
|
|
|
|
res.m_length = sprintf(res.m_data, "%i", val);
|
|
|
|
return res;
|
2013-05-17 14:09:42 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 14:53:40 +02:00
|
|
|
InplaceString<24> to_string(size_t val)
|
2015-01-13 14:47:46 +01:00
|
|
|
{
|
2015-03-31 14:53:40 +02:00
|
|
|
InplaceString<24> res;
|
|
|
|
res.m_length = sprintf(res.m_data, "%zu", val);
|
|
|
|
return res;
|
2015-01-13 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 14:53:40 +02:00
|
|
|
InplaceString<24> to_string(float val)
|
2015-01-13 14:47:46 +01:00
|
|
|
{
|
2015-03-31 14:53:40 +02:00
|
|
|
InplaceString<24> res;
|
|
|
|
res.m_length = sprintf(res.m_data, "%f", val);
|
|
|
|
return res;
|
2015-01-13 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
2014-04-18 14:45:33 +02:00
|
|
|
bool subsequence_match(StringView str, StringView subseq)
|
2013-09-23 21:16:57 +02: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 20:49:00 +02:00
|
|
|
String expand_tabs(StringView line, CharCount tabstop, CharCount col)
|
|
|
|
{
|
|
|
|
String res;
|
2015-03-14 12:46:53 +01:00
|
|
|
res.reserve(line.length());
|
2015-03-30 14:33:46 +02:00
|
|
|
for (auto it = line.begin(), end = line.end(); it != end; )
|
2014-04-28 20:49:00 +02:00
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
{
|
|
|
|
CharCount end_col = (col / tabstop + 1) * tabstop;
|
|
|
|
res += String{' ', end_col - col};
|
2015-03-30 14:33:46 +02:00
|
|
|
col = end_col;
|
|
|
|
++it;
|
2014-04-28 20:49:00 +02:00
|
|
|
}
|
|
|
|
else
|
2015-03-30 14:33:46 +02:00
|
|
|
{
|
|
|
|
auto char_end = utf8::next(it, end);
|
|
|
|
res += {it, char_end};
|
|
|
|
++col;
|
|
|
|
it = char_end;
|
|
|
|
}
|
2014-04-28 20:49:00 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<StringView> wrap_lines(StringView text, CharCount max_width)
|
2014-11-19 20:42:15 +01:00
|
|
|
{
|
|
|
|
using Utf8It = utf8::iterator<const char*>;
|
|
|
|
Utf8It word_begin{text.begin()};
|
|
|
|
Utf8It word_end{word_begin};
|
|
|
|
Utf8It end{text.end()};
|
|
|
|
CharCount col = 0;
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<StringView> lines;
|
2014-11-20 19:45:10 +01:00
|
|
|
Utf8It line_begin = text.begin();
|
|
|
|
Utf8It line_end = line_begin;
|
2014-11-19 20:42:15 +01:00
|
|
|
while (word_begin != end)
|
|
|
|
{
|
2014-11-20 14:55:07 +01:00
|
|
|
const CharCategories cat = categorize(*word_begin);
|
2014-11-19 20:42:15 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
++word_end;
|
|
|
|
} while (word_end != end and categorize(*word_end) == cat);
|
|
|
|
|
|
|
|
col += word_end - word_begin;
|
2014-11-20 19:45:10 +01:00
|
|
|
if ((word_begin != line_begin and col > max_width) or
|
|
|
|
cat == CharCategories::EndOfLine)
|
2014-11-19 20:42:15 +01:00
|
|
|
{
|
2014-11-20 19:45:10 +01:00
|
|
|
lines.emplace_back(line_begin.base(), line_end.base());
|
2014-11-20 14:55:07 +01:00
|
|
|
line_begin = (cat == CharCategories::EndOfLine or
|
2014-11-20 19:45:10 +01:00
|
|
|
cat == CharCategories::Blank) ? word_end : word_begin;
|
|
|
|
col = word_end - line_begin;
|
2014-11-19 20:42:15 +01:00
|
|
|
}
|
2014-11-20 14:55:07 +01:00
|
|
|
if (cat == CharCategories::Word or cat == CharCategories::Punctuation)
|
2014-11-20 19:45:10 +01:00
|
|
|
line_end = word_end;
|
2014-11-20 14:55:07 +01:00
|
|
|
|
2014-11-19 20:42:15 +01:00
|
|
|
word_begin = word_end;
|
|
|
|
}
|
2014-11-20 19:45:10 +01:00
|
|
|
if (line_begin != word_begin)
|
|
|
|
lines.emplace_back(line_begin.base(), word_begin.base());
|
2014-11-19 20:42:15 +01:00
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2015-03-31 00:05:24 +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);
|
|
|
|
|
|
|
|
int implicitIndex = 0;
|
|
|
|
for (auto it = fmt.begin(), end = fmt.end(); it != end;)
|
|
|
|
{
|
|
|
|
auto opening = std::find(it, end, '{');
|
|
|
|
res += StringView{it, opening};
|
|
|
|
if (opening == end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (opening != it && res.back() == '\\')
|
|
|
|
{
|
|
|
|
res.back() = '{';
|
|
|
|
it = opening + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto closing = std::find(it, end, '}');
|
|
|
|
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");
|
|
|
|
|
|
|
|
res += params[index];
|
|
|
|
implicitIndex = index+1;
|
|
|
|
it = closing+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
}
|