Replace various adhoc operator+ based formatting with format func

This commit is contained in:
Maxime Coste 2015-03-30 23:06:02 +01:00
parent 13a5af70ae
commit 6e1a388544
7 changed files with 29 additions and 45 deletions

View File

@ -532,19 +532,6 @@ ByteCoord Buffer::last_modification_coord() const
String Buffer::debug_description() const String Buffer::debug_description() const
{ {
String res = display_name() + "\n";
res += " Flags: ";
if (m_flags & Flags::File)
res += "File (" + name() + ") ";
if (m_flags & Flags::New)
res += "New ";
if (m_flags & Flags::Fifo)
res += "Fifo ";
if (m_flags & Flags::NoUndo)
res += "NoUndo ";
res += "\n";
size_t content_size = 0; size_t content_size = 0;
for (auto& line : m_lines) for (auto& line : m_lines)
content_size += (int)line->strview().length(); content_size += (int)line->strview().length();
@ -554,9 +541,13 @@ String Buffer::debug_description() const
additional_size += undo_group.size() * sizeof(Modification); additional_size += undo_group.size() * sizeof(Modification);
additional_size += m_changes.size() * sizeof(Change); additional_size += m_changes.size() * sizeof(Change);
res += " Used mem: content=" + to_string(content_size) + return format("{}\nFlags: {}{}{}{}\nUsed mem: content={} additional={}\n",
" additional=" + to_string(additional_size) + "\n"; display_name(),
return res; (m_flags & Flags::File) ? "File (" + name() + ") " : "",
(m_flags & Flags::New) ? "New " : "",
(m_flags & Flags::Fifo) ? "Fifo " : "",
(m_flags & Flags::NoUndo) ? "NoUndo " : "",
content_size, additional_size);
} }
} }

View File

@ -36,7 +36,7 @@ void CommandManager::register_command(String command_name,
struct parse_error : runtime_error struct parse_error : runtime_error
{ {
parse_error(StringView error) parse_error(StringView error)
: runtime_error{"parse error: " + error} {} : runtime_error{format("parse error: {}", error)} {}
}; };
namespace namespace
@ -125,7 +125,7 @@ String get_until_delimiter(StringView base, ByteCount& pos,
struct unknown_expand : parse_error struct unknown_expand : parse_error
{ {
unknown_expand(StringView name) unknown_expand(StringView name)
: parse_error{"unknown expand '" + name + "'"} {} : parse_error{format("unknown expand '{}'", name)} {}
}; };
template<bool throw_on_invalid> template<bool throw_on_invalid>
@ -169,14 +169,6 @@ void skip_blanks_and_comments(StringView base, ByteCount& pos)
} }
} }
struct unterminated_string : parse_error
{
unterminated_string(StringView open, StringView close, int nest = 0)
: parse_error{"unterminated string '" + open + "..." + close + "'" +
(nest > 0 ? "(nesting: " + to_string(nest) + ")" : "")}
{}
};
template<bool throw_on_unterminated> template<bool throw_on_unterminated>
Token parse_percent_token(StringView line, ByteCount& pos) Token parse_percent_token(StringView line, ByteCount& pos)
{ {
@ -187,8 +179,8 @@ Token parse_percent_token(StringView line, ByteCount& pos)
StringView type_name = line.substr(type_start, pos - type_start); StringView type_name = line.substr(type_start, pos - type_start);
if (throw_on_unterminated and pos == length) if (throw_on_unterminated and pos == length)
throw parse_error{"expected a string delimiter after '%" + throw parse_error{format("expected a string delimiter after '%{}'",
type_name + "'"}; type_name)};
Token::Type type = token_type<throw_on_unterminated>(type_name); Token::Type type = token_type<throw_on_unterminated>(type_name);
static const UnorderedMap<char, char> matching_delimiters = { static const UnorderedMap<char, char> matching_delimiters = {
@ -205,8 +197,9 @@ 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 + StringView{opening_delimiter}, throw parse_error{format("unterminated string '%{}{}...{}'",
closing_delimiter, 0); type_name, opening_delimiter,
closing_delimiter)};
return {type, token_start, pos, std::move(token)}; return {type, token_start, pos, std::move(token)};
} }
else else
@ -237,7 +230,7 @@ TokenList parse(StringView line)
token_start = ++pos; token_start = ++pos;
String token = get_until_delimiter(line, pos, delimiter); String token = get_until_delimiter(line, pos, delimiter);
if (throw_on_unterminated and pos == length) if (throw_on_unterminated and pos == length)
throw unterminated_string(delimiter, delimiter); throw parse_error{format("unterminated string {0}...{0}", delimiter)};
result.emplace_back(delimiter == '"' ? Token::Type::RawEval result.emplace_back(delimiter == '"' ? Token::Type::RawEval
: Token::Type::Raw, : Token::Type::Raw,
token_start, pos, std::move(token)); token_start, pos, std::move(token));

View File

@ -77,7 +77,7 @@ String real_path(StringView filename)
{ {
if (non_existing.empty()) if (non_existing.empty())
return res; return res;
return res + "/"_str + non_existing; return format("{}/{}", res, non_existing);
} }
auto it = find(existing.rbegin(), existing.rend(), '/'); auto it = find(existing.rbegin(), existing.rend(), '/');

View File

@ -430,7 +430,7 @@ bool InsertCompleter::try_complete(CompleteFunc complete_func)
} }
catch (runtime_error& e) catch (runtime_error& e)
{ {
write_debug("error while trying to run completer: "_str + e.what()); write_debug(format("error while trying to run completer: {}", e.what()));
return false; return false;
} }
if (not m_completions.is_valid()) if (not m_completions.is_valid())

View File

@ -117,15 +117,15 @@ 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 format("{}.{}+{}", beg.line + 1, beg.column + 1,
to_string((int)context.buffer().distance(beg, sel.max())+1); } context.buffer().distance(beg, sel.max())+1); }
}, { }, {
"selections_desc", "selections_desc",
[](StringView name, const Context& context) [](StringView name, const Context& context)
{ return join(transformed(context.selections(), [&](const Selection& sel) { { return join(transformed(context.selections(), [&](const Selection& sel) {
auto beg = sel.min(); auto beg = sel.min();
return to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" + return format("{}.{}+{}", beg.line + 1, beg.column + 1,
to_string((int)context.buffer().distance(beg, sel.max())+1); context.buffer().distance(beg, sel.max())+1);
}), ':'); } }), ':'); }
}, { }, {
"window_width", "window_width",
@ -623,12 +623,12 @@ int main(int argc, char* argv[])
} }
catch (Kakoune::exception& error) catch (Kakoune::exception& error)
{ {
on_assert_failed(("uncaught exception ("_str + typeid(error).name() + "):\n" + error.what()).c_str()); on_assert_failed(format("uncaught exception ({}):\n{}", typeid(error).name(), error.what()).c_str());
return -1; return -1;
} }
catch (std::exception& error) catch (std::exception& error)
{ {
on_assert_failed(("uncaught exception ("_str + typeid(error).name() + "):\n" + error.what()).c_str()); on_assert_failed(format("uncaught exception ({}):\n{}", typeid(error).name(), error.what()).c_str());
return -1; return -1;
} }
catch (...) catch (...)

View File

@ -470,8 +470,8 @@ void select_next_match(const Buffer& buffer, SelectionList& selections,
void yank(Context& context, NormalParams params) void yank(Context& context, NormalParams params)
{ {
RegisterManager::instance()[params.reg] = context.selections_content(); RegisterManager::instance()[params.reg] = context.selections_content();
context.print_status({ "yanked " + to_string(context.selections().size()) + context.print_status({ format("yanked {} selections to register {}",
" selections to register " + StringView{params.reg}, context.selections().size(), params.reg),
get_face("Information") }); get_face("Information") });
} }
@ -1106,8 +1106,8 @@ void jump(Context& context, NormalParams)
void save_selections(Context& context, NormalParams) void save_selections(Context& context, NormalParams)
{ {
context.push_jump(); context.push_jump();
context.print_status({ "saved " + to_string(context.selections().size()) + context.print_status({ format("saved {} selections", context.selections().size()),
" selections", get_face("Information") }); get_face("Information") });
} }
void align(Context& context, NormalParams) void align(Context& context, NormalParams)

View File

@ -37,8 +37,8 @@ void StringRegistry::debug_stats() const
total_refcount += st.second->refcount - 1; total_refcount += st.second->refcount - 1;
total_size += (int)st.second->length; total_size += (int)st.second->length;
} }
write_debug(" data size: " + to_string(total_size) + ", mean: " + to_string((float)total_size/count)); write_debug(format(" data size: {}, mean: {}", total_size, (float)total_size/count));
write_debug(" refcounts: " + to_string(total_refcount) + ", mean: " + to_string((float)total_refcount/count)); write_debug(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count));
} }
} }