2015-11-05 07:45:03 +08:00
|
|
|
#include <algorithm>
|
2015-11-08 07:37:23 +08:00
|
|
|
#include <array>
|
2015-11-05 07:45:03 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
2015-11-08 07:37:23 +08:00
|
|
|
//TODO: make this conditional on XLNT_STD_STRING once std::sto* functions are replaced
|
|
|
|
#include <string>
|
2015-11-05 07:45:03 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <utf8.h>
|
|
|
|
|
|
|
|
#include <xlnt/utils/string.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template<typename T>
|
2015-11-08 07:37:23 +08:00
|
|
|
std::size_t string_length(const T *arr)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
std::size_t i = 0;
|
|
|
|
|
|
|
|
while (arr[i] != T(0))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
xlnt::string::code_point to_upper(xlnt::string::code_point p)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(p >= U'a' && p <= U'z')
|
|
|
|
{
|
|
|
|
return U'A' + p - U'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
xlnt::string::code_point to_lower(xlnt::string::code_point p)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(p >= U'A' && p <= U'Z')
|
|
|
|
{
|
|
|
|
return U'a' + p - U'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
namespace xlnt {
|
2015-11-05 07:45:03 +08:00
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::int8_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::int16_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::int32_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::int64_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::uint8_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::uint16_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::uint32_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(std::uint64_t i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(float i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(double i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
2015-11-05 07:45:03 +08:00
|
|
|
string string::from(long double i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
xlnt::string::iterator::iterator(xlnt::string *parent, size_type index)
|
|
|
|
: parent_(parent),
|
|
|
|
index_(index)
|
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
xlnt::string::iterator::iterator(const xlnt::string::iterator &other)
|
|
|
|
: parent_(other.parent_),
|
|
|
|
index_(other.index_)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::code_point xlnt::string::iterator::operator*()
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return parent_->at(index_);
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
bool xlnt::string::iterator::operator==(const iterator &other) const
|
|
|
|
{
|
|
|
|
return parent_ == other.parent_ && index_ == other.index_;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::difference_type string::iterator::operator-(const iterator &other) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return index_ - other.index_;
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
xlnt::string::const_iterator::const_iterator(const xlnt::string *parent, size_type index)
|
|
|
|
: parent_(parent),
|
|
|
|
index_(index)
|
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
xlnt::string::const_iterator::const_iterator(const xlnt::string::iterator &other)
|
|
|
|
: parent_(other.parent_),
|
|
|
|
index_(other.index_)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
xlnt::string::const_iterator::const_iterator(const xlnt::string::const_iterator &other)
|
|
|
|
: parent_(other.parent_),
|
|
|
|
index_(other.index_)
|
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::difference_type string::const_iterator::operator-(const const_iterator &other) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return index_ - other.index_;
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
const string::code_point xlnt::string::const_iterator::operator*() const
|
|
|
|
{
|
|
|
|
return parent_->at(index_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool xlnt::string::const_iterator::operator==(const const_iterator &other) const
|
|
|
|
{
|
|
|
|
return parent_ == other.parent_ && index_ == other.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlnt::string::iterator &xlnt::string::iterator::operator--()
|
|
|
|
{
|
|
|
|
return *this -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlnt::string::const_iterator &xlnt::string::const_iterator::operator--()
|
|
|
|
{
|
|
|
|
return *this -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlnt::string::iterator &xlnt::string::iterator::operator++()
|
|
|
|
{
|
|
|
|
return *this += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlnt::string::const_iterator &xlnt::string::const_iterator::operator++()
|
|
|
|
{
|
|
|
|
return *this += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
string::const_iterator &string::const_iterator::operator+=(int offset)
|
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
auto new_index = static_cast<int>(index_) + offset;
|
|
|
|
new_index = std::max<int>(0, std::min<int>(static_cast<int>(parent_->length()), new_index));
|
2015-11-08 07:37:23 +08:00
|
|
|
index_ = static_cast<std::size_t>(new_index);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlnt::string::iterator xlnt::string::iterator::operator+(int offset)
|
|
|
|
{
|
|
|
|
iterator copy = *this;
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
auto new_index = static_cast<int>(index_) + offset;
|
|
|
|
new_index = std::max<int>(0, std::min<int>(static_cast<int>(parent_->length()), new_index));
|
2015-11-08 07:37:23 +08:00
|
|
|
copy.index_ = static_cast<std::size_t>(new_index);
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlnt::string::const_iterator xlnt::string::const_iterator::operator+(int offset)
|
|
|
|
{
|
|
|
|
const_iterator copy = *this;
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
auto new_index = static_cast<int>(index_) + offset;
|
|
|
|
new_index = std::max<int>(0, std::min<int>(static_cast<int>(parent_->length()), new_index));
|
2015-11-08 07:37:23 +08:00
|
|
|
copy.index_ = static_cast<std::size_t>(new_index);
|
|
|
|
|
|
|
|
return copy;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::string(size_type initial_size)
|
2015-11-08 07:37:23 +08:00
|
|
|
: data_(new std::vector<char>),
|
|
|
|
code_point_byte_offsets_(new std::unordered_map<size_type, size_type>)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
data_->resize(initial_size + 1);
|
|
|
|
data_->back() = '\0';
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
code_point_byte_offsets_->insert({0, 0});
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::string() : string(size_type(0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
string::string(string &&str) : string()
|
|
|
|
{
|
|
|
|
swap(*this, str);
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::string(const string &str)
|
|
|
|
: data_(new std::vector<char>(*str.data_)),
|
|
|
|
code_point_byte_offsets_(new std::unordered_map<size_type, size_type>(*str.code_point_byte_offsets_))
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
string::string(const string &str, size_type offset) : string(str, offset, str.length() - offset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
string::string(const string &str, size_type offset, size_type len) : string()
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
auto part = str.substr(offset, len);
|
|
|
|
|
|
|
|
*data_ = *part.data_;
|
|
|
|
*code_point_byte_offsets_ = *part.code_point_byte_offsets_;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::string(const utf_mb_wide_string str) : string()
|
|
|
|
{
|
|
|
|
auto iter = str;
|
|
|
|
|
|
|
|
while(*iter != '\0')
|
|
|
|
{
|
|
|
|
append(*iter);
|
|
|
|
iter++;
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::string(const utf8_string str) : string()
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
auto start = str;
|
|
|
|
auto end = str;
|
|
|
|
|
|
|
|
while(*end != '\0')
|
|
|
|
{
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = start;
|
|
|
|
|
|
|
|
while(iter != end)
|
|
|
|
{
|
|
|
|
append((utf32_char)utf8::next(iter, end));
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::string(const utf16_string str) : string()
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
auto iter = str;
|
|
|
|
|
|
|
|
while(*iter != '\0')
|
|
|
|
{
|
|
|
|
append(*iter);
|
|
|
|
iter++;
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::string(const utf32_string str) : string()
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
auto iter = str;
|
|
|
|
|
|
|
|
while(*iter != '\0')
|
|
|
|
{
|
|
|
|
append(*iter);
|
|
|
|
iter++;
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::~string()
|
|
|
|
{
|
|
|
|
delete data_;
|
2015-11-08 07:37:23 +08:00
|
|
|
data_ = nullptr;
|
|
|
|
delete code_point_byte_offsets_;
|
|
|
|
code_point_byte_offsets_ = nullptr;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string string::to_upper() const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
string upper;
|
|
|
|
|
|
|
|
for(auto c : *this)
|
|
|
|
{
|
|
|
|
upper.append(::to_upper(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
return upper;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string string::to_lower() const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
string lower;
|
|
|
|
|
|
|
|
for(auto c : *this)
|
|
|
|
{
|
|
|
|
lower.append(::to_lower(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
return lower;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string string::substr(size_type offset) const
|
|
|
|
{
|
|
|
|
return substr(offset, length() - offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
string string::substr(size_type offset, size_type len) const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(len != npos && offset + len < length())
|
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
return string(begin() + static_cast<int>(offset), begin() + static_cast<int>(offset + len));
|
2015-11-08 07:37:23 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
return string(begin() + static_cast<int>(offset), end());
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::code_point string::back() const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return at(length() - 1);
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::code_point string::front() const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return at(0);
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type string::find(code_point c) const
|
|
|
|
{
|
|
|
|
return find(c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type string::find(char c) const
|
|
|
|
{
|
|
|
|
return find(c, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find(const string &str) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return find(str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type string::find(code_point c, size_type offset) const
|
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
auto iter = begin() + static_cast<int>(offset);
|
2015-11-05 07:45:03 +08:00
|
|
|
|
|
|
|
while (iter != end())
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if (*iter == c)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type string::find(char c, size_type offset) const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return find(static_cast<code_point>(c), offset);
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find(const string &str, size_type offset) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
while (offset < length() - str.length())
|
|
|
|
{
|
|
|
|
if (substr(offset, str.length()) == str)
|
|
|
|
{
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return npos;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type string::find_last_of(code_point c) const
|
|
|
|
{
|
|
|
|
return find_last_of(c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type string::find_last_of(char c) const
|
|
|
|
{
|
|
|
|
return find_last_of(c, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_last_of(const string &str) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return find_last_of(str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type string::find_last_of(code_point c, size_type offset) const
|
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
auto stop = begin() + static_cast<int>(offset);
|
2015-11-08 07:37:23 +08:00
|
|
|
auto iter = end() - 1;
|
|
|
|
|
|
|
|
while (iter != stop)
|
|
|
|
{
|
|
|
|
if (*iter == c)
|
|
|
|
{
|
|
|
|
return iter - begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
--iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *stop == c ? offset : npos;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
string::size_type string::find_last_of(char c, size_type offset) const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return find_last_of(static_cast<code_point>(c), offset);
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_last_of(const string &str, size_type offset) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
auto stop = begin() + static_cast<int>(offset);
|
2015-11-08 07:37:23 +08:00
|
|
|
auto iter = end() - 1;
|
|
|
|
|
|
|
|
while (iter != stop)
|
|
|
|
{
|
|
|
|
if(str.find(*iter) != npos)
|
|
|
|
{
|
|
|
|
return iter - begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
--iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return npos;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_first_of(const string &str) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return find_first_of(str, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_first_of(const string &str, size_type offset) const
|
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
auto iter = begin() + static_cast<int>(offset);
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
while (iter != end())
|
|
|
|
{
|
|
|
|
if(str.find(*iter) != npos)
|
|
|
|
{
|
|
|
|
return iter - begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return npos;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_first_not_of(const string &str) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return find_first_not_of(str, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_first_not_of(const string &str, size_type offset) const
|
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
auto iter = begin() + static_cast<int>(offset);
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
while (iter != end())
|
|
|
|
{
|
|
|
|
if(str.find(*iter) == npos)
|
|
|
|
{
|
|
|
|
return iter - begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return npos;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_last_not_of(const string &str) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return find_last_not_of(str, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::find_last_not_of(const string &str, size_type offset) const
|
|
|
|
{
|
2015-11-08 09:00:54 +08:00
|
|
|
auto stop = begin() + static_cast<int>(offset);
|
2015-11-08 07:37:23 +08:00
|
|
|
auto iter = end() - 1;
|
|
|
|
|
|
|
|
while (iter != stop)
|
|
|
|
{
|
|
|
|
if(str.find(*iter) == npos)
|
|
|
|
{
|
|
|
|
return iter - begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
--iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return npos;
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void string::clear()
|
|
|
|
{
|
|
|
|
data_->clear();
|
|
|
|
data_->push_back('\0');
|
2015-11-08 07:37:23 +08:00
|
|
|
code_point_byte_offsets_->clear();
|
|
|
|
code_point_byte_offsets_->insert({0, 0});
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::size_t string::to() const
|
|
|
|
{
|
|
|
|
return std::stoull(std::string(data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::uint32_t string::to() const
|
|
|
|
{
|
|
|
|
return std::stoul(std::string(data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::int32_t string::to() const
|
|
|
|
{
|
|
|
|
return std::stoi(std::string(data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
long double string::to() const
|
|
|
|
{
|
|
|
|
return std::stold(std::string(data()));
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
|
|
|
double string::to() const
|
|
|
|
{
|
|
|
|
return std::stod(std::string(data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XLNT_STD_STRING
|
2015-11-08 09:20:36 +08:00
|
|
|
string::string(const std::string &str) : string(str.data())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
template<>
|
|
|
|
std::string string::to() const
|
|
|
|
{
|
|
|
|
return std::string(data());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
int string::to_hex() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
void string::erase(size_type index)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
auto start = code_point_byte_offsets_->at(index);
|
|
|
|
auto next_start = code_point_byte_offsets_->at(index + 1);
|
|
|
|
|
|
|
|
data_->erase(data_->begin() + start, data_->begin() + next_start);
|
|
|
|
|
|
|
|
auto code_point_bytes = next_start - start;
|
|
|
|
|
|
|
|
for(size_type i = index + 1; i < length(); i++)
|
|
|
|
{
|
|
|
|
code_point_byte_offsets_->at(i) = code_point_byte_offsets_->at(i + 1) - code_point_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
code_point_byte_offsets_->erase(code_point_byte_offsets_->find(length()));
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string &string::operator=(string rhs)
|
|
|
|
{
|
|
|
|
swap(*this, rhs);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
string::iterator string::begin()
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return iterator(this, 0);
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::const_iterator string::cbegin() const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return const_iterator(this, 0);
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::iterator string::end()
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return iterator(this, length());
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::const_iterator string::cend() const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
return const_iterator(this, length());
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::byte_pointer string::data()
|
|
|
|
{
|
|
|
|
return &data_->front();
|
|
|
|
}
|
|
|
|
|
|
|
|
string::const_byte_pointer string::data() const
|
|
|
|
{
|
|
|
|
return &data_->front();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t string::hash() const
|
|
|
|
{
|
|
|
|
static std::hash<std::string> hasher;
|
|
|
|
return hasher(std::string(data()));
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
std::size_t string::num_bytes() const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return data_->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void string::append(char c)
|
|
|
|
{
|
|
|
|
data_->back() = c;
|
|
|
|
data_->push_back('\0');
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
code_point_byte_offsets_->insert({length() + 1, num_bytes() - 1});
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void string::append(wchar_t c)
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(c < 128)
|
|
|
|
{
|
|
|
|
append(static_cast<char>(c));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
data_->pop_back();
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
std::array<char, 4> utf8_encoded {{0}};
|
|
|
|
auto end = utf8::utf16to8(&c, &c + 1, utf8_encoded.begin());
|
|
|
|
std::copy(utf8_encoded.begin(), end, std::back_inserter(*data_));
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
data_->push_back('\0');
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
code_point_byte_offsets_->insert({length() + 1, num_bytes() - 1});
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void string::append(char16_t c)
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(c < 128)
|
|
|
|
{
|
|
|
|
append(static_cast<char>(c));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
data_->pop_back();
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
std::array<char, 4> utf8_encoded {{0}};
|
|
|
|
auto end = utf8::utf16to8(&c, &c + 1, utf8_encoded.begin());
|
|
|
|
std::copy(utf8_encoded.begin(), end, std::back_inserter(*data_));
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
data_->push_back('\0');
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
code_point_byte_offsets_->insert({length() + 1, num_bytes() - 1});
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
void string::append(code_point c)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(c < 128)
|
|
|
|
{
|
|
|
|
append(static_cast<char>(c));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
data_->pop_back();
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
std::array<char, 4> utf8_encoded {{0}};
|
|
|
|
auto end = utf8::utf32to8(&c, &c + 1, utf8_encoded.begin());
|
|
|
|
std::copy(utf8_encoded.begin(), end, std::back_inserter(*data_));
|
2015-11-05 07:45:03 +08:00
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
data_->push_back('\0');
|
|
|
|
|
|
|
|
code_point_byte_offsets_->insert({length() + 1, num_bytes() - 1});
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
void string::append(const string &str)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
for (auto c : str)
|
|
|
|
{
|
|
|
|
append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
void string::replace(size_type index, utf32_char value)
|
|
|
|
{
|
|
|
|
std::array<byte, 4> encoded = {{0}};
|
|
|
|
auto encoded_end = utf8::utf32to8(&value, &value + 1, encoded.begin());
|
|
|
|
auto encoded_len = encoded_end - encoded.begin();
|
|
|
|
|
|
|
|
auto data_start = code_point_byte_offsets_->at(index);
|
|
|
|
auto data_end = code_point_byte_offsets_->at(index + 1);
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
auto previous_len = static_cast<int>(data_end - data_start);
|
|
|
|
int difference = static_cast<int>(encoded_len) - previous_len;
|
2015-11-08 07:37:23 +08:00
|
|
|
|
|
|
|
if(difference < 0)
|
|
|
|
{
|
|
|
|
data_->erase(data_->begin() + data_end + difference, data_->begin() + data_end);
|
|
|
|
}
|
|
|
|
else if(difference > 0)
|
|
|
|
{
|
|
|
|
data_->insert(data_->begin() + data_start, difference, '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
for(std::size_t i = index + 1; i < code_point_byte_offsets_->size(); i++)
|
|
|
|
{
|
|
|
|
code_point_byte_offsets_->at(i) += difference;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = encoded.begin();
|
|
|
|
auto data_iter = data_->begin() + data_start;
|
|
|
|
|
|
|
|
while(iter != encoded_end)
|
|
|
|
{
|
|
|
|
*data_iter = *iter;
|
|
|
|
++data_iter;
|
|
|
|
++iter;
|
|
|
|
}
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
string::code_point string::at(size_type index)
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(index == length())
|
|
|
|
{
|
|
|
|
return U'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return utf8::peek_next(data_->begin() + code_point_byte_offsets_->at(index), data_->end());
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const string::code_point string::at(size_type index) const
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if(index == length())
|
|
|
|
{
|
|
|
|
return U'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return utf8::peek_next(data_->begin() + code_point_byte_offsets_->at(index), data_->end());
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool string::operator==(const_byte_pointer str) const
|
|
|
|
{
|
|
|
|
return *this == string(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string::operator==(const string &str) const
|
|
|
|
{
|
|
|
|
if (length() != str.length()) return false;
|
|
|
|
|
|
|
|
auto left_iter = begin();
|
|
|
|
auto right_iter = str.begin();
|
|
|
|
|
|
|
|
while (left_iter != end() && right_iter != str.end())
|
|
|
|
{
|
2015-11-08 07:37:23 +08:00
|
|
|
if (*left_iter != *right_iter)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++left_iter;
|
|
|
|
++right_iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (left_iter == end()) != (right_iter == end());
|
|
|
|
}
|
|
|
|
|
|
|
|
string::code_point string::operator[](size_type index)
|
|
|
|
{
|
|
|
|
return at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
const string::code_point string::operator[](size_type index) const
|
|
|
|
{
|
|
|
|
return at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
string string::operator+(const string &rhs) const
|
|
|
|
{
|
|
|
|
string copy(*this);
|
|
|
|
copy.append(rhs);
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
string &string::operator+=(const string &rhs)
|
|
|
|
{
|
|
|
|
append(rhs);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
XLNT_FUNCTION void swap(string &left, string &right)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
|
|
|
|
swap(left.data_, right.data_);
|
2015-11-08 07:37:23 +08:00
|
|
|
swap(left.code_point_byte_offsets_, right.code_point_byte_offsets_);
|
2015-11-05 07:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
XLNT_FUNCTION std::ostream &operator<<(std::ostream &left, string &right)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
auto d = right.data();
|
|
|
|
std::size_t i = 0;
|
|
|
|
|
|
|
|
while (d[i] != '\0')
|
|
|
|
{
|
|
|
|
left << d[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
XLNT_FUNCTION string operator+(const char *left, const string &right)
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return string(left) + right;
|
|
|
|
}
|
|
|
|
|
2015-11-08 09:00:54 +08:00
|
|
|
XLNT_FUNCTION bool string::operator<(const string &other) const
|
2015-11-05 07:45:03 +08:00
|
|
|
{
|
|
|
|
return std::string(data()) < std::string(other.data());
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:37:23 +08:00
|
|
|
string::size_type string::length() const
|
|
|
|
{
|
|
|
|
return code_point_byte_offsets_->size() - 1;
|
|
|
|
}
|
|
|
|
|
2015-11-08 09:20:36 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
template<>
|
|
|
|
string string::from(unsigned long i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
unsigned long string::to() const
|
|
|
|
{
|
|
|
|
return std::stoul(std::string(data()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __linux
|
|
|
|
template<>
|
|
|
|
string string::from(long long int i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
string string::from(unsigned long long int i)
|
|
|
|
{
|
|
|
|
return string(std::to_string(i).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
long long int string::to() const
|
|
|
|
{
|
|
|
|
return std::stoll(std::string(data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
unsigned long long int string::to() const
|
|
|
|
{
|
|
|
|
return std::stoull(std::string(data()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
} // namespace xlnt
|