From 6e1a3885441b14543d663c3ad5175a78153963c3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 30 Mar 2015 23:06:02 +0100 Subject: [PATCH] Replace various adhoc operator+ based formatting with format func --- src/buffer.cc | 23 +++++++---------------- src/command_manager.cc | 23 ++++++++--------------- src/file.cc | 2 +- src/insert_completer.cc | 2 +- src/main.cc | 12 ++++++------ src/normal.cc | 8 ++++---- src/shared_string.cc | 4 ++-- 7 files changed, 29 insertions(+), 45 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index fe162486..93808fd6 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -532,19 +532,6 @@ ByteCoord Buffer::last_modification_coord() 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; for (auto& line : m_lines) content_size += (int)line->strview().length(); @@ -554,9 +541,13 @@ String Buffer::debug_description() const additional_size += undo_group.size() * sizeof(Modification); additional_size += m_changes.size() * sizeof(Change); - res += " Used mem: content=" + to_string(content_size) + - " additional=" + to_string(additional_size) + "\n"; - return res; + return format("{}\nFlags: {}{}{}{}\nUsed mem: content={} additional={}\n", + display_name(), + (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); } } diff --git a/src/command_manager.cc b/src/command_manager.cc index 62018bc8..f6e72cbd 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -36,7 +36,7 @@ void CommandManager::register_command(String command_name, struct parse_error : runtime_error { parse_error(StringView error) - : runtime_error{"parse error: " + error} {} + : runtime_error{format("parse error: {}", error)} {} }; namespace @@ -125,7 +125,7 @@ String get_until_delimiter(StringView base, ByteCount& pos, struct unknown_expand : parse_error { unknown_expand(StringView name) - : parse_error{"unknown expand '" + name + "'"} {} + : parse_error{format("unknown expand '{}'", name)} {} }; template @@ -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 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); if (throw_on_unterminated and pos == length) - throw parse_error{"expected a string delimiter after '%" + - type_name + "'"}; + throw parse_error{format("expected a string delimiter after '%{}'", + type_name)}; Token::Type type = token_type(type_name); static const UnorderedMap matching_delimiters = { @@ -205,8 +197,9 @@ Token parse_percent_token(StringView line, ByteCount& pos) String token = get_until_delimiter(line, pos, opening_delimiter, closing_delimiter); if (throw_on_unterminated and pos == length) - throw unterminated_string("%" + type_name + StringView{opening_delimiter}, - closing_delimiter, 0); + throw parse_error{format("unterminated string '%{}{}...{}'", + type_name, opening_delimiter, + closing_delimiter)}; return {type, token_start, pos, std::move(token)}; } else @@ -237,7 +230,7 @@ TokenList parse(StringView line) token_start = ++pos; String token = get_until_delimiter(line, pos, delimiter); 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 : Token::Type::Raw, token_start, pos, std::move(token)); diff --git a/src/file.cc b/src/file.cc index a3cc8bbb..dfffb043 100644 --- a/src/file.cc +++ b/src/file.cc @@ -77,7 +77,7 @@ String real_path(StringView filename) { if (non_existing.empty()) return res; - return res + "/"_str + non_existing; + return format("{}/{}", res, non_existing); } auto it = find(existing.rbegin(), existing.rend(), '/'); diff --git a/src/insert_completer.cc b/src/insert_completer.cc index 073347a9..24c0c27d 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -430,7 +430,7 @@ bool InsertCompleter::try_complete(CompleteFunc complete_func) } 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; } if (not m_completions.is_valid()) diff --git a/src/main.cc b/src/main.cc index 2a0d0b58..94a9ce8a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -117,15 +117,15 @@ void register_env_vars() [](StringView name, const Context& context) { auto& sel = context.selections().main(); auto beg = sel.min(); - return to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" + - to_string((int)context.buffer().distance(beg, sel.max())+1); } + return format("{}.{}+{}", beg.line + 1, beg.column + 1, + context.buffer().distance(beg, sel.max())+1); } }, { "selections_desc", [](StringView name, const Context& context) { return join(transformed(context.selections(), [&](const Selection& sel) { auto beg = sel.min(); - return to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" + - to_string((int)context.buffer().distance(beg, sel.max())+1); + return format("{}.{}+{}", beg.line + 1, beg.column + 1, + context.buffer().distance(beg, sel.max())+1); }), ':'); } }, { "window_width", @@ -623,12 +623,12 @@ int main(int argc, char* argv[]) } 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; } 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; } catch (...) diff --git a/src/normal.cc b/src/normal.cc index 3ba7760a..e4d22cc6 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -470,8 +470,8 @@ void select_next_match(const Buffer& buffer, SelectionList& selections, void yank(Context& context, NormalParams params) { RegisterManager::instance()[params.reg] = context.selections_content(); - context.print_status({ "yanked " + to_string(context.selections().size()) + - " selections to register " + StringView{params.reg}, + context.print_status({ format("yanked {} selections to register {}", + context.selections().size(), params.reg), get_face("Information") }); } @@ -1106,8 +1106,8 @@ void jump(Context& context, NormalParams) void save_selections(Context& context, NormalParams) { context.push_jump(); - context.print_status({ "saved " + to_string(context.selections().size()) + - " selections", get_face("Information") }); + context.print_status({ format("saved {} selections", context.selections().size()), + get_face("Information") }); } void align(Context& context, NormalParams) diff --git a/src/shared_string.cc b/src/shared_string.cc index a02dfcda..1d0cb5a9 100644 --- a/src/shared_string.cc +++ b/src/shared_string.cc @@ -37,8 +37,8 @@ void StringRegistry::debug_stats() const total_refcount += st.second->refcount - 1; total_size += (int)st.second->length; } - write_debug(" data size: " + to_string(total_size) + ", mean: " + to_string((float)total_size/count)); - write_debug(" refcounts: " + to_string(total_refcount) + ", mean: " + to_string((float)total_refcount/count)); + write_debug(format(" data size: {}, mean: {}", total_size, (float)total_size/count)); + write_debug(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count)); } }