Tweak String class, and cleanup usages

remove String::clear, add operator=(String&&) add str_to_int(const String&)
to replace atoi.
This commit is contained in:
Maxime Coste 2012-06-27 14:26:29 +02:00
parent ae590f35d8
commit ecb3a737a9
5 changed files with 17 additions and 9 deletions

View File

@ -242,9 +242,9 @@ void edit(const CommandParameters& params, const Context& context)
if (params.size() > 1) 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 ? 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})); 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) for (size_t i = 0; i < params.size(); ++i)
{ {
param_name[sizeof(param_name) - 2] = '0' + i; param_name[sizeof(param_name) - 2] = '0' + i;
vars[param_name] = params[i].c_str(); vars[param_name] = params[i];
} }
return vars; return vars;
} }
@ -567,7 +567,7 @@ void exec_commands_in_file(const CommandParameters& params,
while (true) while (true)
{ {
if (not cat_with_previous) if (not cat_with_previous)
command_line.clear(); command_line = String();
size_t end_pos = pos; size_t end_pos = pos;
@ -796,7 +796,7 @@ void menu(const CommandParameters& params,
oss << "(empty cancels): "; oss << "(empty cancels): ";
String choice = prompt(oss.str(), complete_nothing); 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) if (i > 0 and i < (count / 2) + 1)
CommandManager::instance().execute(parser[(i-1)*2+1], context); CommandManager::instance().execute(parser[(i-1)*2+1], context);

View File

@ -57,7 +57,7 @@ void expand_tabulations(Buffer& buffer, Modification& modification)
} }
int count = tabstop - (column % tabstop); int count = tabstop - (column % tabstop);
modification.content.clear(); modification.content = String();
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
modification.content += ' '; modification.content += ' ';
} }

View File

@ -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; }
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; } String as_string() const { return m_value; }
private: private:
String m_value; String m_value;

View File

@ -25,6 +25,11 @@ String int_to_str(int value)
return String(buffer + pos); return String(buffer + pos);
} }
int str_to_int(const String& str)
{
return atoi(str.c_str());
}
std::vector<String> split(const String& str, Character separator) std::vector<String> split(const String& str, Character separator)
{ {
auto begin = str.begin(); auto begin = str.begin();

View File

@ -16,7 +16,9 @@ class String
public: public:
String() {} String() {}
String(const char* content) : m_content(content) {} 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) {} explicit String(Character content) : m_content(std::string() + (char)content) {}
template<typename Iterator> template<typename Iterator>
String(Iterator begin, Iterator end) : m_content(begin, end) {} 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; } 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= (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) const { return String(m_content + other.m_content); }
String& operator+= (const String& other) { m_content += other.m_content; return *this; } 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(); } 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)); } String substr(size_t pos, size_t length = -1) const { return String(m_content.substr(pos, length)); }
void clear() { m_content.clear(); }
class iterator class iterator
{ {
@ -110,6 +112,7 @@ inline String operator+(Character lhs, const String& rhs)
} }
String int_to_str(int value); String int_to_str(int value);
int str_to_int(const String& str);
std::vector<String> split(const String& str, Character separator); std::vector<String> split(const String& str, Character separator);
} }