diff --git a/src/commands.cc b/src/commands.cc index 488c892f..c31b99c3 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -242,9 +242,9 @@ void edit(const CommandParameters& params, const Context& context) if (params.size() > 1) { - int line = std::max(0, atoi(params[1].c_str()) - 1); + int line = std::max(0, str_to_int(params[1]) - 1); int column = params.size() > 2 ? - std::max(0, atoi(params[2].c_str()) - 1) : 0; + std::max(0, str_to_int(params[2]) - 1) : 0; window.select(window.buffer().iterator_at({line, column})); } @@ -473,7 +473,7 @@ EnvVarMap params_to_env_var_map(const CommandParameters& params) for (size_t i = 0; i < params.size(); ++i) { param_name[sizeof(param_name) - 2] = '0' + i; - vars[param_name] = params[i].c_str(); + vars[param_name] = params[i]; } return vars; } @@ -567,7 +567,7 @@ void exec_commands_in_file(const CommandParameters& params, while (true) { if (not cat_with_previous) - command_line.clear(); + command_line = String(); size_t end_pos = pos; @@ -796,7 +796,7 @@ void menu(const CommandParameters& params, oss << "(empty cancels): "; String choice = prompt(oss.str(), complete_nothing); - int i = atoi(choice.c_str()); + int i = str_to_int(choice); if (i > 0 and i < (count / 2) + 1) CommandManager::instance().execute(parser[(i-1)*2+1], context); diff --git a/src/filters.cc b/src/filters.cc index 185dd1bf..d7cb7fdf 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -57,7 +57,7 @@ void expand_tabulations(Buffer& buffer, Modification& modification) } int count = tabstop - (column % tabstop); - modification.content.clear(); + modification.content = String(); for (int i = 0; i < count; ++i) modification.content += ' '; } diff --git a/src/option_manager.hh b/src/option_manager.hh index 49b8c9d5..f6d21bd2 100644 --- a/src/option_manager.hh +++ b/src/option_manager.hh @@ -31,7 +31,7 @@ public: bool operator==(const Option& other) const { return m_value == other.m_value; } bool operator!=(const Option& other) const { return m_value != other.m_value; } - int as_int() const { return atoi(m_value.c_str()); } + int as_int() const { return str_to_int(m_value); } String as_string() const { return m_value; } private: String m_value; diff --git a/src/string.cc b/src/string.cc index 30654256..9e2d1507 100644 --- a/src/string.cc +++ b/src/string.cc @@ -25,6 +25,11 @@ String int_to_str(int value) return String(buffer + pos); } +int str_to_int(const String& str) +{ + return atoi(str.c_str()); +} + std::vector split(const String& str, Character separator) { auto begin = str.begin(); diff --git a/src/string.hh b/src/string.hh index 0a8c0abb..cd5cfe1d 100644 --- a/src/string.hh +++ b/src/string.hh @@ -16,7 +16,9 @@ class String public: String() {} String(const char* content) : m_content(content) {} - String(const std::string& content) : m_content(content) {} + String(std::string content) : m_content(std::move(content)) {} + String(const String& string) = default; + String(String&& string) = default; explicit String(Character content) : m_content(std::string() + (char)content) {} template String(Iterator begin, Iterator end) : m_content(begin, end) {} @@ -30,6 +32,7 @@ public: bool operator< (const String& other) const { return m_content < other.m_content; } String& operator= (const String& other) { m_content = other.m_content; return *this; } + 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& operator+= (const String& other) { m_content += other.m_content; return *this; } @@ -41,7 +44,6 @@ public: const char* c_str() const { return m_content.c_str(); } String substr(size_t pos, size_t length = -1) const { return String(m_content.substr(pos, length)); } - void clear() { m_content.clear(); } class iterator { @@ -110,6 +112,7 @@ inline String operator+(Character lhs, const String& rhs) } String int_to_str(int value); +int str_to_int(const String& str); std::vector split(const String& str, Character separator); }