From 26f0fd4cc6577e52b08604d32469b221242bfb9c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 13 May 2013 14:23:07 +0200 Subject: [PATCH] Use more std::* for string handling --- src/assert.cc | 2 +- src/client_manager.cc | 2 +- src/color.cc | 4 +--- src/command_manager.cc | 4 ++-- src/commands.cc | 16 ++++++++-------- src/highlighters.cc | 4 ++-- src/input_handler.cc | 7 +++---- src/main.cc | 14 +++++++------- src/normal.cc | 7 ++++--- src/option_types.hh | 21 ++++++++++++--------- src/remote.cc | 6 +++--- src/shell_manager.cc | 2 +- src/string.cc | 27 --------------------------- src/string.hh | 22 +++++++++++++++++++--- src/unit_tests.cc | 4 ---- 15 files changed, 64 insertions(+), 78 deletions(-) diff --git a/src/assert.cc b/src/assert.cc index 0e316be8..b51815a5 100644 --- a/src/assert.cc +++ b/src/assert.cc @@ -20,7 +20,7 @@ private: void on_assert_failed(const char* message) { - String debug_info = "pid: " + int_to_str(getpid()); + String debug_info = "pid: " + to_string(getpid()); int res = system(("xmessage -buttons 'quit:0,ignore:1' '"_str + message + "\n[Debug Infos]\n" + debug_info + "'").c_str()); switch (res) diff --git a/src/client_manager.cc b/src/client_manager.cc index 64c406aa..10bbbc69 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -28,7 +28,7 @@ String ClientManager::generate_name() const { for (int i = 0; true; ++i) { - String name = "unnamed" + int_to_str(i); + String name = "unnamed" + to_string(i); bool found = false; for (auto& client : m_clients) { diff --git a/src/color.cc b/src/color.cc index 6067dcc5..7e8e9b01 100644 --- a/src/color.cc +++ b/src/color.cc @@ -2,8 +2,6 @@ #include "exception.hh" -#include - namespace Kakoune { @@ -22,7 +20,7 @@ Color str_to_color(const String& color) static const Regex rgb_regex{"rgb:[0-9a-fA-F]{6}"}; if (boost::regex_match(color, rgb_regex)) { - long int l = strtol(color.c_str() + 4, NULL, 16); + long l = stol(color.substr(ByteCount{4}), NULL, 16); return { (unsigned char)((l >> 16) & 0xFF), (unsigned char)((l >> 8) & 0xFF), (unsigned char)(l & 0xFF) }; diff --git a/src/command_manager.cc b/src/command_manager.cc index 87848860..4bad2eb4 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -79,9 +79,9 @@ bool is_horizontal_blank(char c) struct unterminated_string : parse_error { - unterminated_string(const String& open, const String& close, int nest = 0) + unterminated_string(const std::string& open, const std::string& close, int nest = 0) : parse_error{"unterminated string '" + open + "..." + close + "'" + - (nest > 0 ? "(nesting: " + int_to_str(nest) + ")" : "")} + (nest > 0 ? "(nesting: " + to_string(nest) + ")" : "")} {} }; diff --git a/src/commands.cc b/src/commands.cc index 7cc57d57..7f381853 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -125,9 +125,9 @@ void edit(const CommandParameters& params, Context& context) if (param_count > 1) { - int line = std::max(0, str_to_int(parser[1]) - 1); + int line = std::max(0, stoi(parser[1]) - 1); int column = param_count > 2 ? - std::max(0, str_to_int(parser[2]) - 1) : 0; + std::max(0, stoi(parser[2]) - 1) : 0; context.editor().select(context.buffer().iterator_at({ line, column })); if (context.has_window()) @@ -427,12 +427,12 @@ void define_command(const CommandParameters& params, Context& context) completer = [=](const Context& context, const CommandParameters& params, size_t token_to_complete, ByteCount pos_in_token) { - EnvVarMap vars = { - {"token_to_complete", int_to_str(token_to_complete) }, - { "pos_in_token", int_to_str((int)pos_in_token) } - }; - String output = ShellManager::instance().eval(shell_cmd, context, params, vars); - return split(output, '\n'); + EnvVarMap vars = { + { "token_to_complete", to_string(token_to_complete) }, + { "pos_in_token", to_string(pos_in_token) } + }; + String output = ShellManager::instance().eval(shell_cmd, context, params, vars); + return split(output, '\n'); }; } CommandManager::instance().register_command(cmd_name, cmd, completer); diff --git a/src/highlighters.cc b/src/highlighters.cc index be06189b..1cab851f 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -134,9 +134,9 @@ HighlighterAndId colorize_regex_factory(const HighlighterParameters params, cons throw runtime_error("wrong colorspec: '" + *it + "' expected :[,]"); - int capture = str_to_int(String(res[1].first, res[1].second)); + int capture = stoi(res[1].str()); const ColorPair*& color = colors[capture]; - color = &get_color(String(res[2].first, res[2].second)); + color = &get_color(res[2].str()); } String id = "colre'" + params[0] + "'"; diff --git a/src/input_handler.cc b/src/input_handler.cc index 01a8e471..7f3387c1 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -536,18 +536,17 @@ static BufferCompletion complete_opt(const BufferIterator& pos, OptionManager& o boost::smatch match; if (boost::regex_match(desc.begin(), desc.end(), match, re)) { - BufferCoord coord{ str_to_int({match[1].first, match[1].second}) - 1, - str_to_int({match[2].first, match[2].second}) - 1 }; + BufferCoord coord{ stoi(match[1].str()) - 1, stoi(match[2].str()) - 1 }; if (not pos.buffer().is_valid(coord)) return {}; BufferIterator beg{pos.buffer(), coord}; BufferIterator end = beg; if (match[3].matched) { - ByteCount len = str_to_int({match[3].first, match[3].second}); + ByteCount len = stoi(match[3].str()); end = beg + len; } - size_t timestamp = (size_t)str_to_int(String(match[4].first, match[4].second)); + size_t timestamp = (size_t)stoi(match[4].str()); size_t longest_completion = 0; for (auto it = opt.begin() + 1; it != opt.end(); ++it) diff --git a/src/main.cc b/src/main.cc index 47259bf7..84f5dd8e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -64,7 +64,7 @@ void register_env_vars() { return context.buffer().display_name(); }); shell_manager.register_env_var("timestamp", [](const String& name, const Context& context) - { return int_to_str(context.buffer().timestamp()); }); + { return to_string(context.buffer().timestamp()); }); shell_manager.register_env_var("selection", [](const String& name, const Context& context) { return context.editor().main_selection().content(); }); @@ -90,21 +90,21 @@ void register_env_vars() { return ClientManager::instance().get_client(context).name(); }); shell_manager.register_env_var("cursor_line", [](const String& name, const Context& context) - { return int_to_str((int)context.editor().main_selection().last().line() + 1); }); + { return to_string(context.editor().main_selection().last().line() + 1); }); shell_manager.register_env_var("cursor_column", [](const String& name, const Context& context) - { return int_to_str((int)context.editor().main_selection().last().column() + 1); }); + { return to_string(context.editor().main_selection().last().column() + 1); }); shell_manager.register_env_var("selection_desc", [](const String& name, const Context& context) { auto& sel = context.editor().main_selection(); auto beg = sel.begin(); - return int_to_str((int)beg.line() + 1) + ':' + int_to_str((int)beg.column() + 1) + '+' + int_to_str((int)(sel.end() - beg)); }); + return to_string(beg.line() + 1) + ':' + to_string(beg.column() + 1) + '+' + to_string((sel.end() - beg)); }); shell_manager.register_env_var("window_width", [](const String& name, const Context& context) - { return int_to_str((int)context.window().dimensions().column); }); + { return to_string(context.window().dimensions().column); }); shell_manager.register_env_var("window_height", [](const String& name, const Context& context) - { return int_to_str((int)context.window().dimensions().line); }); + { return to_string(context.window().dimensions().line); }); } void register_registers() @@ -223,7 +223,7 @@ int main(int argc, char* argv[]) register_filters(); write_debug("*** This is the debug buffer, where debug info will be written ***"); - write_debug("pid: " + int_to_str(getpid())); + write_debug("pid: " + to_string(getpid())); write_debug("utf-8 test: é á ï"); Server server; diff --git a/src/normal.cc b/src/normal.cc index 8d7b19cb..796db658 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -320,7 +320,8 @@ void use_selection_as_search_pattern(Context& context) void yank(Context& context) { RegisterManager::instance()['"'] = context.editor().selections_content(); - context.print_status({ "yanked " + int_to_str(context.editor().selections().size()) + " selections", get_color("Information") }); + context.print_status({ "yanked " + to_string(context.editor().selections().size()) + + " selections", get_color("Information") }); } void cat_yank(Context& context) @@ -331,7 +332,7 @@ void cat_yank(Context& context) str += sel; RegisterManager::instance()['"'] = memoryview(str); context.print_status({ "concatenated and yanked " + - int_to_str(sels.size()) + " selections", get_color("Information") }); + to_string(sels.size()) + " selections", get_color("Information") }); } void erase(Context& context) @@ -496,7 +497,7 @@ void deindent(Context& context) auto restore_sels = on_scope_end([&]{ editor.select((SelectionList)std::move(sels)); }); editor.select(select_whole_lines); editor.multi_select(std::bind(select_all_matches, _1, - Regex{"^\\h{1," + int_to_str(width) + "}"})); + Regex{"^\\h{1," + to_string(width) + "}"})); editor.erase(); } diff --git a/src/option_types.hh b/src/option_types.hh index 121b8f56..ac7e4c32 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -15,8 +15,8 @@ namespace Kakoune inline String option_to_string(const String& opt) { return opt; } inline void option_from_string(const String& str, String& opt) { opt = str; } -inline String option_to_string(int opt) { return int_to_str(opt); } -inline void option_from_string(const String& str, int& opt) { opt = str_to_int(str); } +inline String option_to_string(int opt) { return to_string(opt); } +inline void option_from_string(const String& str, int& opt) { opt = stoi(str); } inline bool option_add(int& opt, int val) { opt += val; return val != 0; } inline String option_to_string(bool opt) { return opt ? "true" : "false"; } @@ -145,16 +145,19 @@ void option_from_string(const String& str, std::tuple& opt) TupleOptionDetail::from_string(elems, opt); } -template -inline String option_to_string(const StronglyTypedNumber& opt) { return int_to_str((int)opt); } - -template -inline void option_from_string(const String& str, StronglyTypedNumber& opt) +template +inline String option_to_string(const StronglyTypedNumber& opt) { - opt = StronglyTypedNumber{str_to_int(str)}; + return to_string(opt); } -template +template +inline void option_from_string(const String& str, StronglyTypedNumber& opt) +{ + opt = StronglyTypedNumber{stoi(str)}; +} + +template inline bool option_add(StronglyTypedNumber& opt, StronglyTypedNumber val) { diff --git a/src/remote.cc b/src/remote.cc index 94128871..d25ace62 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -242,12 +242,12 @@ private: RemoteUI::RemoteUI(int socket) : m_socket_watcher(socket, [this](FDWatcher&) { if (m_input_callback) m_input_callback(); }) { - write_debug("remote client connected: " + int_to_str(m_socket_watcher.fd())); + write_debug("remote client connected: " + to_string(m_socket_watcher.fd())); } RemoteUI::~RemoteUI() { - write_debug("remote client disconnected: " + int_to_str(m_socket_watcher.fd())); + write_debug("remote client disconnected: " + to_string(m_socket_watcher.fd())); close(m_socket_watcher.fd()); } @@ -504,7 +504,7 @@ private: }; Server::Server() - : m_filename{"/tmp/kak-" + int_to_str(getpid())} + : m_filename{"/tmp/kak-" + to_string(getpid())} { int listen_sock = socket(AF_UNIX, SOCK_STREAM, 0); fcntl(listen_sock, F_SETFD, FD_CLOEXEC); diff --git a/src/shell_manager.cc b/src/shell_manager.cc index f85ec92e..dcb5afd8 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -43,7 +43,7 @@ String ShellManager::pipe(const String& input, close(read_pipe[1]); close(error_pipe[1]); - memoryview data = input.data(); + auto data = input.data(); write(write_pipe[1], data.pointer(), data.size()); close(write_pipe[1]); diff --git a/src/string.cc b/src/string.cc index cecf0279..e7dd167f 100644 --- a/src/string.cc +++ b/src/string.cc @@ -5,33 +5,6 @@ namespace Kakoune { -String int_to_str(int value) -{ - const bool negative = value < 0; - if (negative) - value = -value; - - char buffer[16]; - size_t pos = sizeof(buffer); - buffer[--pos] = 0; - do - { - buffer[--pos] = '0' + (value % 10); - value /= 10; - } - while (value); - - if (negative) - buffer[--pos] = '-'; - - return String(buffer + pos); -} - -int str_to_int(const String& str) -{ - return atoi(str.c_str()); -} - std::vector split(const String& str, char separator) { auto begin = str.begin(); diff --git a/src/string.hh b/src/string.hh index edfafd02..e579b741 100644 --- a/src/string.hh +++ b/src/string.hh @@ -64,6 +64,16 @@ 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+(char lhs, const String& rhs) { return String(lhs) + rhs; @@ -74,9 +84,6 @@ inline String operator+(Codepoint lhs, const String& rhs) return String(lhs) + rhs; } - -String int_to_str(int value); -int str_to_int(const String& str); std::vector split(const String& str, char separator); inline String operator"" _str(const char* str, size_t) @@ -94,6 +101,15 @@ inline String codepoint_to_str(Codepoint cp) String option_to_string(const Regex& re); void option_from_string(const String& str, Regex& re); + +using std::to_string; + +template +std::string to_string(const StronglyTypedNumber& val) +{ + return to_string((ValueType)val); +} + } namespace std diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 9aeb9b92..9c3dfd14 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -99,10 +99,6 @@ void test_utf8() void test_string() { - kak_assert(int_to_str(124) == "124"); - kak_assert(int_to_str(-129) == "-129"); - kak_assert(int_to_str(0) == "0"); - kak_assert(String("youpi ") + "matin" == "youpi matin"); std::vector splited = split("youpi:matin::tchou", ':');