String: inherit from std::string rather than using it as a backend

This commit is contained in:
Maxime Coste 2013-03-29 14:21:55 +01:00
parent 1f4072dc72
commit 01968cb96e
7 changed files with 42 additions and 71 deletions

View File

@ -128,7 +128,7 @@ HighlighterAndId colorize_regex_factory(const HighlighterParameters params, cons
ColorSpec colors; ColorSpec colors;
for (auto it = params.begin() + 1; it != params.end(); ++it) for (auto it = params.begin() + 1; it != params.end(); ++it)
{ {
boost::match_results<String::iterator> res; boost::smatch res;
if (not boost::regex_match(it->begin(), it->end(), res, color_spec_ex)) if (not boost::regex_match(it->begin(), it->end(), res, color_spec_ex))
throw runtime_error("wrong colorspec: '" + *it + throw runtime_error("wrong colorspec: '" + *it +
"' expected <capture>:<fgcolor>[,<bgcolor>]"); "' expected <capture>:<fgcolor>[,<bgcolor>]");

View File

@ -500,7 +500,7 @@ static BufferCompletion complete_opt(const BufferIterator& pos, OptionManager& o
auto& desc = opt[0]; auto& desc = opt[0];
Regex re(R"((\d+):(\d+)@(\d+))"); Regex re(R"((\d+):(\d+)@(\d+))");
boost::match_results<String::iterator> match; boost::smatch match;
if (boost::regex_match(desc.begin(), desc.end(), match, re)) if (boost::regex_match(desc.begin(), desc.end(), match, re))
{ {
LineCount line = str_to_int(String(match[1].first, match[1].second)) - 1; LineCount line = str_to_int(String(match[1].first, match[1].second)) - 1;

View File

@ -128,7 +128,7 @@ void NCursesUI::redraw()
doupdate(); doupdate();
} }
using Utf8Policy = utf8::InvalidBytePolicy::Pass; using Utf8Policy = utf8::InvalidBytePolicy::Pass;
using Utf8Iterator = utf8::utf8_iterator<String::iterator, Utf8Policy>; using Utf8Iterator = utf8::utf8_iterator<String::const_iterator, Utf8Policy>;
void addutf8str(WINDOW* win, Utf8Iterator begin, Utf8Iterator end) void addutf8str(WINDOW* win, Utf8Iterator begin, Utf8Iterator end)
{ {
waddstr(win, std::string(begin.underlying_iterator(), end.underlying_iterator()).c_str()); waddstr(win, std::string(begin.underlying_iterator(), end.underlying_iterator()).c_str());
@ -290,7 +290,7 @@ void NCursesUI::draw_status()
move((int)m_dimensions.line, 0); move((int)m_dimensions.line, 0);
clrtoeol(); clrtoeol();
if (m_status_cursor == -1) if (m_status_cursor == -1)
addutf8str(stdscr, m_status_line.begin(), m_status_line.end()); addutf8str(stdscr, m_status_line.cbegin(), m_status_line.cend());
else else
{ {
Utf8Iterator begin{m_status_line.begin()}; Utf8Iterator begin{m_status_line.begin()};
@ -298,7 +298,7 @@ void NCursesUI::draw_status()
Utf8Iterator cursor_it{begin}; Utf8Iterator cursor_it{begin};
cursor_it.advance(m_status_cursor, end); cursor_it.advance(m_status_cursor, end);
addutf8str(stdscr, m_status_line.begin(), cursor_it); addutf8str(stdscr, m_status_line.cbegin(), cursor_it);
set_attribute(A_REVERSE, 1); set_attribute(A_REVERSE, 1);
if (cursor_it == end) if (cursor_it == end)
addch(' '); addch(' ');
@ -345,8 +345,8 @@ void NCursesUI::draw_menu()
wattron(m_menu_win, COLOR_PAIR(menu_fg)); wattron(m_menu_win, COLOR_PAIR(menu_fg));
auto& choice = m_choices[choice_idx]; auto& choice = m_choices[choice_idx];
auto begin = choice.begin(); auto begin = choice.cbegin();
auto end = utf8::advance(begin, choice.end(), column_width); auto end = utf8::advance(begin, choice.cend(), column_width);
addutf8str(m_menu_win, begin, end); addutf8str(m_menu_win, begin, end);
for (auto pad = column_width - utf8::distance(begin, end); pad > 0; --pad) for (auto pad = column_width - utf8::distance(begin, end); pad > 0; --pad)
waddch(m_menu_win, ' '); waddch(m_menu_win, ' ');

View File

@ -31,7 +31,7 @@ void option_from_string(const String& str, LineAndFlag& opt)
{ {
static Regex re{R"((\d+):(\w+):(.+))"}; static Regex re{R"((\d+):(\w+):(.+))"};
boost::match_results<String::iterator> res; boost::smatch res;
if (not boost::regex_match(str.begin(), str.end(), res, re)) if (not boost::regex_match(str.begin(), str.end(), res, re))
throw runtime_error("wrong syntax, expected <line>:<color>:<flag>"); throw runtime_error("wrong syntax, expected <line>:<color>:<flag>");

View File

@ -79,8 +79,8 @@ String ShellManager::pipe(const String& input,
dup2(error_pipe[1], 2); close(error_pipe[1]); dup2(error_pipe[1], 2); close(error_pipe[1]);
dup2(write_pipe[0], 0); close(write_pipe[0]); dup2(write_pipe[0], 0); close(write_pipe[0]);
boost::regex_iterator<String::iterator> it(cmdline.begin(), cmdline.end(), m_regex); boost::regex_iterator<String::const_iterator> it(cmdline.begin(), cmdline.end(), m_regex);
boost::regex_iterator<String::iterator> end; boost::regex_iterator<String::const_iterator> end;
while (it != end) while (it != end)
{ {

View File

@ -51,8 +51,8 @@ std::vector<String> split(const String& str, char separator)
String String::replace(const String& expression, String String::replace(const String& expression,
const String& replacement) const const String& replacement) const
{ {
boost::regex re(expression.m_content); boost::regex re(expression);
return String(boost::regex_replace(m_content, re, replacement.m_content)); return String(boost::regex_replace(*this, re, replacement));
} }
} }

View File

@ -13,49 +13,39 @@ namespace Kakoune
typedef boost::regex Regex; typedef boost::regex Regex;
class String class String : public std::string
{ {
public: public:
String() {} String() {}
String(const char* content) : m_content(content) {} String(const char* content) : std::string(content) {}
String(std::string content) : m_content(std::move(content)) {} String(std::string content) : std::string(std::move(content)) {}
String(const String& string) = default; explicit String(char content, CharCount count = 1) : std::string((size_t)(int)count, content) {}
String(String&& string) = default; explicit String(Codepoint cp, CharCount count = 1) { utf8::dump(back_inserter(*this), cp); }
explicit String(char content, CharCount count = 1) : m_content((size_t)(int)count, content) {}
explicit String(Codepoint cp, CharCount count = 1)
{
std::string str;
utf8::dump(back_inserter(str), cp);
for (CharCount i = 0; i < count; ++i)
m_content += str;
}
template<typename Iterator> template<typename Iterator>
String(Iterator begin, Iterator end) : m_content(begin, end) {} String(Iterator begin, Iterator end) : std::string(begin, end) {}
char operator[](ByteCount pos) const { return m_content[(int)pos]; } std::string& stdstr() { return *this; }
ByteCount length() const { return m_content.length(); } const std::string& stdstr() const { return *this; }
char operator[](ByteCount pos) const { return std::string::operator[]((int)pos); }
char& operator[](ByteCount pos) { return std::string::operator[]((int)pos); }
ByteCount length() const { return ByteCount{(int)std::string::length()}; }
CharCount char_length() const { return utf8::distance(begin(), end()); } CharCount char_length() const { return utf8::distance(begin(), end()); }
ByteCount byte_count_to(CharCount count) const { return utf8::advance(begin(), end(), (int)count) - begin(); } ByteCount byte_count_to(CharCount count) const { return utf8::advance(begin(), end(), (int)count) - begin(); }
CharCount char_count_to(ByteCount count) const { return utf8::distance(begin(), begin() + (int)count); } CharCount char_count_to(ByteCount count) const { return utf8::distance(begin(), begin() + (int)count); }
bool empty() const { return m_content.empty(); }
bool operator== (const String& other) const { return m_content == other.m_content; } String operator+(const String& other) const { return String{stdstr() + other.stdstr()}; }
bool operator!= (const String& other) const { return m_content != other.m_content; } String& operator+=(const String& other) { std::string::operator+=(other); return *this; }
bool operator< (const String& other) const { return m_content < other.m_content; } String operator+(const char* other) const { return String{stdstr() + other}; }
String& operator+=(const char* other) { std::string::operator+=(other); return *this; }
String operator+(char other) const { return String{stdstr() + other}; }
String& operator+=(char other) { std::string::operator+=(other); return *this; }
String operator+(Codepoint cp) const { String res = *this; utf8::dump(back_inserter(res), cp); return res; }
String& operator+=(Codepoint cp) { utf8::dump(back_inserter(*this), cp); return *this; }
String& operator= (const String& other) { m_content = other.m_content; return *this; } memoryview<char> data() const { return memoryview<char>(std::string::data(), size()); }
String& operator= (String&& other) { m_content = std::move(other.m_content); return *this; }
String operator+ (const String& other) const { return String(m_content + other.m_content); } String substr(ByteCount pos, ByteCount length = -1) const { return String{std::string::substr((int)pos, (int)length)}; }
String& operator+= (const String& other) { m_content += other.m_content; return *this; }
String operator+ (char c) const { return String(m_content + c); }
String& operator+= (char c) { m_content += c; return *this; }
memoryview<char> data() const { return memoryview<char>(m_content.data(), m_content.size()); }
const char* c_str() const { return m_content.c_str(); }
String substr(ByteCount pos, ByteCount length = -1) const { return String(m_content.substr((int)pos, (int)length)); }
String substr(CharCount pos, CharCount length = INT_MAX) const String substr(CharCount pos, CharCount length = INT_MAX) const
{ {
auto b = utf8::advance(begin(), end(), (int)pos); auto b = utf8::advance(begin(), end(), (int)pos);
@ -63,31 +53,6 @@ public:
return String(b,e); return String(b,e);
} }
String replace(const String& expression, const String& replacement) const; String replace(const String& expression, const String& replacement) const;
using iterator = std::string::const_iterator;
using riterator = std::string::const_reverse_iterator;
iterator begin() const { return m_content.begin(); }
iterator end() const { return m_content.end(); }
riterator rbegin() const { return m_content.rbegin(); }
riterator rend() const { return m_content.rend(); }
char front() const { return m_content.front(); }
char back() const { return m_content.back(); }
char& front() { return m_content.front(); }
char& back() { return m_content.back(); }
size_t hash() const { return std::hash<std::string>()(m_content); }
inline friend std::ostream& operator<<(std::ostream& os, const String& str)
{
return os << str.m_content;
}
enum { npos = -1 };
private:
std::string m_content;
}; };
inline String operator+(const char* lhs, const String& rhs) inline String operator+(const char* lhs, const String& rhs)
@ -100,6 +65,12 @@ inline String operator+(char lhs, const String& rhs)
return String(lhs) + rhs; return String(lhs) + rhs;
} }
inline String operator+(Codepoint lhs, const String& rhs)
{
return String(lhs) + rhs;
}
String int_to_str(int value); String int_to_str(int value);
int str_to_int(const String& str); int str_to_int(const String& str);
std::vector<String> split(const String& str, char separator); std::vector<String> split(const String& str, char separator);
@ -121,11 +92,11 @@ inline String codepoint_to_str(Codepoint cp)
namespace std namespace std
{ {
template<> template<>
struct hash<Kakoune::String> struct hash<Kakoune::String> : hash<std::string>
{ {
size_t operator()(const Kakoune::String& str) const size_t operator()(const Kakoune::String& str) const
{ {
return str.hash(); return hash<std::string>::operator()(str);
} }
}; };
} }