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-08-03 11:02:17 +02:00
|
|
|
#include "utils.hh"
|
2014-04-28 20:49:00 +02:00
|
|
|
#include "utf8_iterator.hh"
|
2012-05-29 07:19:50 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-10-01 01:20:12 +02:00
|
|
|
bool operator<(StringView lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
int cmp = strncmp(lhs.data(), rhs.data(), (int)std::min(lhs.length(), rhs.length()));
|
|
|
|
if (cmp == 0)
|
|
|
|
return lhs.length() < rhs.length();
|
|
|
|
return cmp < 0;
|
|
|
|
}
|
|
|
|
|
2014-08-03 11:02:17 +02:00
|
|
|
std::vector<String> split(StringView str, char separator, char escape)
|
2012-05-29 07:19:50 +02:00
|
|
|
{
|
|
|
|
std::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;
|
|
|
|
}
|
|
|
|
|
2014-10-19 17:27:36 +02:00
|
|
|
std::vector<StringView> split(StringView str, char separator)
|
|
|
|
{
|
|
|
|
std::vector<StringView> res;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_to_int(StringView str)
|
2013-05-17 14:09:42 +02:00
|
|
|
{
|
2013-06-18 22:11:44 +02:00
|
|
|
int res = 0;
|
2014-08-03 11:02:17 +02:00
|
|
|
if (sscanf(str.zstr(), "%i", &res) != 1)
|
2013-05-17 14:09:42 +02:00
|
|
|
throw runtime_error(str + "is not a number");
|
2013-06-18 22:11:44 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String to_string(int val)
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
sprintf(buf, "%i", val);
|
|
|
|
return buf;
|
2013-05-17 14:09:42 +02:00
|
|
|
}
|
|
|
|
|
2014-04-18 14:45:33 +02:00
|
|
|
bool prefix_match(StringView str, StringView prefix)
|
2013-09-23 21:16:25 +02:00
|
|
|
{
|
|
|
|
auto it = str.begin();
|
|
|
|
for (auto& c : prefix)
|
|
|
|
{
|
|
|
|
if (it ==str.end() or *it++ != c)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
2014-06-24 20:10:57 +02:00
|
|
|
using Utf8It = utf8::iterator<const char*>;
|
2014-04-28 20:49:00 +02:00
|
|
|
for (Utf8It it = line.begin(); it.base() < line.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == '\t')
|
|
|
|
{
|
|
|
|
CharCount end_col = (col / tabstop + 1) * tabstop;
|
|
|
|
res += String{' ', end_col - col};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res += *it;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:20:12 +02:00
|
|
|
[[gnu::always_inline]]
|
|
|
|
static inline uint32_t rotl(uint32_t x, int8_t r)
|
|
|
|
{
|
|
|
|
return (x << r) | (x >> (32 - r));
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
static inline uint32_t fmix(uint32_t h)
|
|
|
|
{
|
|
|
|
h ^= h >> 16;
|
|
|
|
h *= 0x85ebca6b;
|
|
|
|
h ^= h >> 13;
|
|
|
|
h *= 0xc2b2ae35;
|
|
|
|
h ^= h >> 16;
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// murmur3 hash, based on https://github.com/PeterScott/murmur3
|
|
|
|
size_t hash_data(const char* input, size_t len)
|
|
|
|
{
|
|
|
|
const uint8_t* data = reinterpret_cast<const uint8_t*>(input);
|
|
|
|
uint32_t hash = 0x1235678;
|
|
|
|
constexpr uint32_t c1 = 0xcc9e2d51;
|
|
|
|
constexpr uint32_t c2 = 0x1b873593;
|
|
|
|
|
|
|
|
const int nblocks = len / 4;
|
|
|
|
const uint32_t* blocks = reinterpret_cast<const uint32_t*>(data + nblocks*4);
|
|
|
|
|
|
|
|
for (int i = -nblocks; i; ++i)
|
|
|
|
{
|
|
|
|
uint32_t key = blocks[i];
|
|
|
|
key *= c1;
|
|
|
|
key = rotl(key, 15);
|
|
|
|
key *= c2;
|
|
|
|
|
|
|
|
hash ^= key;
|
|
|
|
hash = rotl(hash, 13);
|
|
|
|
hash = hash * 5 + 0xe6546b64;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* tail = data + nblocks * 4;
|
|
|
|
uint32_t key = 0;
|
|
|
|
switch (len & 3)
|
|
|
|
{
|
|
|
|
case 3: key ^= tail[2] << 16;
|
|
|
|
case 2: key ^= tail[1] << 8;
|
|
|
|
case 1: key ^= tail[0];
|
|
|
|
key *= c1;
|
|
|
|
key = rotl(key,15);
|
|
|
|
key *= c2;
|
|
|
|
hash ^= key;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash ^= len;
|
|
|
|
hash = fmix(hash);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
}
|