Use more std::* for string handling

This commit is contained in:
Maxime Coste 2013-05-13 14:23:07 +02:00
parent 56ab33c9d6
commit 26f0fd4cc6
15 changed files with 64 additions and 78 deletions

View File

@ -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)

View File

@ -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)
{

View File

@ -2,8 +2,6 @@
#include "exception.hh"
#include <stdlib.h>
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) };

View File

@ -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) + ")" : "")}
{}
};

View File

@ -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);

View File

@ -134,9 +134,9 @@ HighlighterAndId colorize_regex_factory(const HighlighterParameters params, cons
throw runtime_error("wrong colorspec: '" + *it +
"' expected <capture>:<fgcolor>[,<bgcolor>]");
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] + "'";

View File

@ -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)

View File

@ -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;

View File

@ -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<String>(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();
}

View File

@ -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<Types...>& opt)
TupleOptionDetail<sizeof...(Types)-1, Types...>::from_string(elems, opt);
}
template<typename RealType, typename ValueType = int>
inline String option_to_string(const StronglyTypedNumber<RealType, ValueType>& opt) { return int_to_str((int)opt); }
template<typename RealType, typename ValueType = int>
inline void option_from_string(const String& str, StronglyTypedNumber<RealType, ValueType>& opt)
template<typename RealType, typename ValueType>
inline String option_to_string(const StronglyTypedNumber<RealType, ValueType>& opt)
{
opt = StronglyTypedNumber<RealType, ValueType>{str_to_int(str)};
return to_string(opt);
}
template<typename RealType, typename ValueType = int>
template<typename RealType, typename ValueType>
inline void option_from_string(const String& str, StronglyTypedNumber<RealType, ValueType>& opt)
{
opt = StronglyTypedNumber<RealType, ValueType>{stoi(str)};
}
template<typename RealType, typename ValueType>
inline bool option_add(StronglyTypedNumber<RealType, ValueType>& opt,
StronglyTypedNumber<RealType, ValueType> val)
{

View File

@ -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);

View File

@ -43,7 +43,7 @@ String ShellManager::pipe(const String& input,
close(read_pipe[1]);
close(error_pipe[1]);
memoryview<char> data = input.data();
auto data = input.data();
write(write_pipe[1], data.pointer(), data.size());
close(write_pipe[1]);

View File

@ -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<String> split(const String& str, char separator)
{
auto begin = str.begin();

View File

@ -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<String> 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<typename RealType, typename ValueType>
std::string to_string(const StronglyTypedNumber<RealType, ValueType>& val)
{
return to_string((ValueType)val);
}
}
namespace std

View File

@ -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<String> splited = split("youpi:matin::tchou", ':');