More string cleanups

This commit is contained in:
Maxime Coste 2014-12-08 13:59:29 +00:00
parent 9f4af93780
commit 87d312b6d4
7 changed files with 25 additions and 41 deletions

View File

@ -108,7 +108,7 @@ DisplayLine Client::generate_mode_line() const
if (context().buffer().is_modified()) if (context().buffer().is_modified())
status.push_back({ "[+]", info_face }); status.push_back({ "[+]", info_face });
if (m_input_handler.is_recording()) if (m_input_handler.is_recording())
status.push_back({ "[recording ("_str + m_input_handler.recording_reg() + ")]", info_face }); status.push_back({ "[recording ("_str + StringView{m_input_handler.recording_reg()} + ")]", info_face });
if (context().buffer().flags() & Buffer::Flags::New) if (context().buffer().flags() & Buffer::Flags::New)
status.push_back({ "[new file]", info_face }); status.push_back({ "[new file]", info_face });
if (context().user_hooks_support().is_disabled()) if (context().user_hooks_support().is_disabled())

View File

@ -112,7 +112,7 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
if (client->context().is_editing()) if (client->context().is_editing())
throw runtime_error("client '" + client->context().name() + "' is inserting in '" + throw runtime_error("client '" + client->context().name() + "' is inserting in '" +
buffer.display_name() + '\''); buffer.display_name() + "'");
// change client context to edit the first buffer which is not the // change client context to edit the first buffer which is not the
// specified one. As BufferManager stores buffer according to last // specified one. As BufferManager stores buffer according to last

View File

@ -203,7 +203,7 @@ Token parse_percent_token(StringView line, ByteCount& pos)
String token = get_until_delimiter(line, pos, opening_delimiter, String token = get_until_delimiter(line, pos, opening_delimiter,
closing_delimiter); closing_delimiter);
if (throw_on_unterminated and pos == length) if (throw_on_unterminated and pos == length)
throw unterminated_string("%" + type_name + opening_delimiter, throw unterminated_string("%" + type_name + StringView{opening_delimiter},
closing_delimiter, 0); closing_delimiter, 0);
return {type, token_start, pos, std::move(token)}; return {type, token_start, pos, std::move(token)};
} }

View File

@ -87,7 +87,7 @@ String compact_path(StringView filename)
char cwd[1024]; char cwd[1024];
getcwd(cwd, 1024); getcwd(cwd, 1024);
String real_cwd = real_path(cwd) + '/'; String real_cwd = real_path(cwd) + "/";
if (prefix_match(real_filename, real_cwd)) if (prefix_match(real_filename, real_cwd))
return real_filename.substr(real_cwd.length()); return real_filename.substr(real_cwd.length());

View File

@ -122,7 +122,7 @@ String key_to_str(Key key)
default: break; default: break;
} }
if (named) if (named)
res = '<' + res + '>'; res = StringView{'<'} + res + StringView{'>'};
return res; return res;
} }

View File

@ -120,7 +120,7 @@ void register_env_vars()
[](StringView name, const Context& context) [](StringView name, const Context& context)
{ auto& sel = context.selections().main(); { auto& sel = context.selections().main();
auto beg = sel.min(); auto beg = sel.min();
return to_string(beg.line + 1) + '.' + to_string(beg.column + 1) + '+' + return to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" +
to_string((int)context.buffer().distance(beg, sel.max())+1); } to_string((int)context.buffer().distance(beg, sel.max())+1); }
}, { }, {
"selections_desc", "selections_desc",
@ -132,7 +132,7 @@ void register_env_vars()
auto beg = sel.min(); auto beg = sel.min();
if (not res.empty()) if (not res.empty())
res += ':'; res += ':';
res += to_string(beg.line + 1) + '.' + to_string(beg.column + 1) + '+' + res += to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" +
to_string((int)context.buffer().distance(beg, sel.max())+1); to_string((int)context.buffer().distance(beg, sel.max())+1);
} }
return res; return res;

View File

@ -44,13 +44,9 @@ public:
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); }
String operator+(const String& other) const { return String{stdstr() + other.stdstr()}; }
String& operator+=(const String& other) { std::string::operator+=(other); return *this; } String& operator+=(const String& other) { std::string::operator+=(other); return *this; }
String operator+(const char* other) const { return String{stdstr() + other}; }
String& operator+=(const char* other) { std::string::operator+=(other); return *this; } 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+=(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+=(Codepoint cp) { utf8::dump(back_inserter(*this), cp); return *this; }
StringView substr(ByteCount pos, ByteCount length = INT_MAX) const; StringView substr(ByteCount pos, ByteCount length = INT_MAX) const;
@ -176,34 +172,26 @@ inline StringView String::substr(CharCount pos, CharCount length) const
return StringView{*this}.substr(pos, length); return StringView{*this}.substr(pos, length);
} }
inline String operator+(const char* lhs, const String& rhs)
{
return String(lhs) + rhs;
}
inline String operator+(const std::string& lhs, const String& rhs)
{
return String(lhs) + rhs;
}
inline String operator+(const String& lhs, const std::string& rhs)
{
return lhs + String(rhs);
}
inline String& operator+=(String& lhs, StringView rhs) inline String& operator+=(String& lhs, StringView rhs)
{ {
lhs.append(rhs.data(), (size_t)(int)rhs.length()); lhs.append(rhs.data(), (size_t)(int)rhs.length());
return lhs; return lhs;
} }
inline String operator+(const char* lhs, StringView rhs) inline String operator+(const String& lhs, const String& rhs)
{ {
String res = lhs; String res = lhs;
res += rhs; res += rhs;
return res; return res;
} }
inline String operator+(StringView lhs, StringView rhs)
{
String res{lhs.begin(), lhs.end()};
res += rhs;
return res;
}
inline String operator+(const String& lhs, StringView rhs) inline String operator+(const String& lhs, StringView rhs)
{ {
String res = lhs; String res = lhs;
@ -218,28 +206,24 @@ inline String operator+(StringView lhs, const String& rhs)
return res; return res;
} }
inline String operator+(const char* lhs, StringView rhs)
{
return StringView{lhs} + rhs;
}
inline String operator+(StringView lhs, const char* rhs) inline String operator+(StringView lhs, const char* rhs)
{ {
String res{lhs.begin(), lhs.end()}; return lhs + StringView{rhs};
res.append(rhs);
return res;
} }
inline String operator+(char lhs, const String& rhs) inline String operator+(const char* lhs, const String& rhs)
{ {
return String(lhs) + rhs; return StringView{lhs} + rhs;
} }
inline String operator+(Codepoint lhs, const String& rhs) inline String operator+(const String& lhs, const char* rhs)
{ {
return String(lhs) + rhs; return lhs + StringView{rhs};
}
inline String operator+(StringView lhs, StringView rhs)
{
String res{lhs.begin(), lhs.end()};
res += rhs;
return res;
} }
std::vector<String> split(StringView str, char separator, char escape); std::vector<String> split(StringView str, char separator, char escape);