Replace std::tie with structured bindings
This commit is contained in:
parent
90dd084993
commit
4b72cfe530
|
@ -320,8 +320,7 @@ void write_buffer_to_file(Buffer& buffer, StringView filename, bool force, bool
|
||||||
void write_buffer_to_backup_file(Buffer& buffer)
|
void write_buffer_to_backup_file(Buffer& buffer)
|
||||||
{
|
{
|
||||||
String path = real_path(buffer.name());
|
String path = real_path(buffer.name());
|
||||||
StringView dir, file;
|
auto [dir,file] = split_path(path);
|
||||||
std::tie(dir,file) = split_path(path);
|
|
||||||
|
|
||||||
char pattern[PATH_MAX];
|
char pattern[PATH_MAX];
|
||||||
if (dir.empty())
|
if (dir.empty())
|
||||||
|
@ -440,8 +439,7 @@ CandidateList complete_filename(StringView prefix, const Regex& ignored_regex,
|
||||||
ByteCount cursor_pos, FilenameFlags flags)
|
ByteCount cursor_pos, FilenameFlags flags)
|
||||||
{
|
{
|
||||||
prefix = prefix.substr(0, cursor_pos);
|
prefix = prefix.substr(0, cursor_pos);
|
||||||
StringView dirname, fileprefix;
|
auto [dirname, fileprefix] = split_path(prefix);
|
||||||
std::tie(dirname, fileprefix) = split_path(prefix);
|
|
||||||
auto parsed_dirname = parse_filename(dirname);
|
auto parsed_dirname = parse_filename(dirname);
|
||||||
|
|
||||||
const bool check_ignored_regex = not ignored_regex.empty() and
|
const bool check_ignored_regex = not ignored_regex.empty() and
|
||||||
|
@ -469,8 +467,7 @@ CandidateList complete_filename(StringView prefix, const Regex& ignored_regex,
|
||||||
CandidateList complete_command(StringView prefix, ByteCount cursor_pos)
|
CandidateList complete_command(StringView prefix, ByteCount cursor_pos)
|
||||||
{
|
{
|
||||||
String real_prefix = parse_filename(prefix.substr(0, cursor_pos));
|
String real_prefix = parse_filename(prefix.substr(0, cursor_pos));
|
||||||
StringView dirname, fileprefix;
|
auto [dirname, fileprefix] = split_path(real_prefix);
|
||||||
std::tie(dirname, fileprefix) = split_path(real_prefix);
|
|
||||||
|
|
||||||
if (not dirname.empty())
|
if (not dirname.empty())
|
||||||
{
|
{
|
||||||
|
|
|
@ -258,11 +258,10 @@ using JsonObject = HashMap<String, Value>;
|
||||||
|
|
||||||
static bool is_digit(char c) { return c >= '0' and c <= '9'; }
|
static bool is_digit(char c) { return c >= '0' and c <= '9'; }
|
||||||
|
|
||||||
std::tuple<Value, const char*>
|
struct JsonResult { Value value; const char* new_pos; };
|
||||||
parse_json(const char* pos, const char* end)
|
|
||||||
{
|
|
||||||
using Result = std::tuple<Value, const char*>;
|
|
||||||
|
|
||||||
|
JsonResult parse_json(const char* pos, const char* end)
|
||||||
|
{
|
||||||
if (not skip_while(pos, end, is_blank))
|
if (not skip_while(pos, end, is_blank))
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
|
@ -270,12 +269,12 @@ parse_json(const char* pos, const char* end)
|
||||||
{
|
{
|
||||||
auto digit_end = pos;
|
auto digit_end = pos;
|
||||||
skip_while(digit_end, end, is_digit);
|
skip_while(digit_end, end, is_digit);
|
||||||
return Result{ Value{str_to_int({pos, digit_end})}, digit_end };
|
return { Value{str_to_int({pos, digit_end})}, digit_end };
|
||||||
}
|
}
|
||||||
if (end - pos > 4 and StringView{pos, pos+4} == "true")
|
if (end - pos > 4 and StringView{pos, pos+4} == "true")
|
||||||
return Result{ Value{true}, pos+4 };
|
return { Value{true}, pos+4 };
|
||||||
if (end - pos > 5 and StringView{pos, pos+5} == "false")
|
if (end - pos > 5 and StringView{pos, pos+5} == "false")
|
||||||
return Result{ Value{false}, pos+5 };
|
return { Value{false}, pos+5 };
|
||||||
if (*pos == '"')
|
if (*pos == '"')
|
||||||
{
|
{
|
||||||
String value;
|
String value;
|
||||||
|
@ -296,7 +295,7 @@ parse_json(const char* pos, const char* end)
|
||||||
if (*string_end == '"')
|
if (*string_end == '"')
|
||||||
{
|
{
|
||||||
value += StringView{pos, string_end};
|
value += StringView{pos, string_end};
|
||||||
return Result{std::move(value), string_end+1};
|
return {std::move(value), string_end+1};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
|
@ -307,14 +306,14 @@ parse_json(const char* pos, const char* end)
|
||||||
if (++pos == end)
|
if (++pos == end)
|
||||||
throw runtime_error("unable to parse array");
|
throw runtime_error("unable to parse array");
|
||||||
if (*pos == ']')
|
if (*pos == ']')
|
||||||
return Result{std::move(array), pos+1};
|
return {std::move(array), pos+1};
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Value element;
|
auto [element, new_pos] = parse_json(pos, end);
|
||||||
std::tie(element, pos) = parse_json(pos, end);
|
|
||||||
if (not element)
|
if (not element)
|
||||||
return {};
|
return {};
|
||||||
|
pos = new_pos;
|
||||||
array.push_back(std::move(element));
|
array.push_back(std::move(element));
|
||||||
if (not skip_while(pos, end, is_blank))
|
if (not skip_while(pos, end, is_blank))
|
||||||
return {};
|
return {};
|
||||||
|
@ -322,7 +321,7 @@ parse_json(const char* pos, const char* end)
|
||||||
if (*pos == ',')
|
if (*pos == ',')
|
||||||
++pos;
|
++pos;
|
||||||
else if (*pos == ']')
|
else if (*pos == ']')
|
||||||
return Result{std::move(array), pos+1};
|
return {std::move(array), pos+1};
|
||||||
else
|
else
|
||||||
throw runtime_error("unable to parse array, expected ',' or ']'");
|
throw runtime_error("unable to parse array, expected ',' or ']'");
|
||||||
}
|
}
|
||||||
|
@ -333,25 +332,24 @@ parse_json(const char* pos, const char* end)
|
||||||
throw runtime_error("unable to parse object");
|
throw runtime_error("unable to parse object");
|
||||||
JsonObject object;
|
JsonObject object;
|
||||||
if (*pos == '}')
|
if (*pos == '}')
|
||||||
return Result{std::move(object), pos+1};
|
return {std::move(object), pos+1};
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Value name_value;
|
auto [name_value, name_end] = parse_json(pos, end);
|
||||||
std::tie(name_value, pos) = parse_json(pos, end);
|
|
||||||
if (not name_value)
|
if (not name_value)
|
||||||
return {};
|
return {};
|
||||||
|
pos = name_end;
|
||||||
String& name = name_value.as<String>();
|
String& name = name_value.as<String>();
|
||||||
if (not skip_while(pos, end, is_blank))
|
if (not skip_while(pos, end, is_blank))
|
||||||
return {};
|
return {};
|
||||||
if (*pos++ != ':')
|
if (*pos++ != ':')
|
||||||
throw runtime_error("expected :");
|
throw runtime_error("expected :");
|
||||||
|
|
||||||
Value element;
|
auto [element, element_end] = parse_json(pos, end);
|
||||||
std::tie(element, pos) = parse_json(pos, end);
|
|
||||||
if (not element)
|
if (not element)
|
||||||
return {};
|
return {};
|
||||||
|
pos = element_end;
|
||||||
object.insert({ std::move(name), std::move(element) });
|
object.insert({ std::move(name), std::move(element) });
|
||||||
if (not skip_while(pos, end, is_blank))
|
if (not skip_while(pos, end, is_blank))
|
||||||
return {};
|
return {};
|
||||||
|
@ -359,7 +357,7 @@ parse_json(const char* pos, const char* end)
|
||||||
if (*pos == ',')
|
if (*pos == ',')
|
||||||
++pos;
|
++pos;
|
||||||
else if (*pos == '}')
|
else if (*pos == '}')
|
||||||
return Result{std::move(object), pos+1};
|
return {std::move(object), pos+1};
|
||||||
else
|
else
|
||||||
throw runtime_error("unable to parse object, expected ',' or '}'");
|
throw runtime_error("unable to parse object, expected ',' or '}'");
|
||||||
}
|
}
|
||||||
|
@ -367,8 +365,7 @@ parse_json(const char* pos, const char* end)
|
||||||
throw runtime_error("unable to parse json");
|
throw runtime_error("unable to parse json");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<Value, const char*>
|
auto parse_json(StringView json) { return parse_json(json.begin(), json.end()); }
|
||||||
parse_json(StringView json) { return parse_json(json.begin(), json.end()); }
|
|
||||||
|
|
||||||
void JsonUI::eval_json(const Value& json)
|
void JsonUI::eval_json(const Value& json)
|
||||||
{
|
{
|
||||||
|
@ -488,8 +485,8 @@ void JsonUI::parse_requests(EventMode mode)
|
||||||
const char* pos = nullptr;
|
const char* pos = nullptr;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Value json;
|
auto [json, new_pos] = parse_json(m_requests);
|
||||||
std::tie(json, pos) = parse_json(m_requests);
|
pos = new_pos;
|
||||||
if (json)
|
if (json)
|
||||||
eval_json(json);
|
eval_json(json);
|
||||||
}
|
}
|
||||||
|
@ -510,18 +507,18 @@ void JsonUI::parse_requests(EventMode mode)
|
||||||
UnitTest test_json_parser{[]()
|
UnitTest test_json_parser{[]()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
auto value = std::get<0>(parse_json(R"({ "jsonrpc": "2.0", "method": "keys", "params": [ "b", "l", "a", "h" ] })"));
|
auto value = parse_json(R"({ "jsonrpc": "2.0", "method": "keys", "params": [ "b", "l", "a", "h" ] })").value;
|
||||||
kak_assert(value);
|
kak_assert(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto value = std::get<0>(parse_json("[10,20]"));
|
auto value = parse_json("[10,20]").value;
|
||||||
kak_assert(value and value.is_a<JsonArray>());
|
kak_assert(value and value.is_a<JsonArray>());
|
||||||
kak_assert(value.as<JsonArray>().at(1).as<int>() == 20);
|
kak_assert(value.as<JsonArray>().at(1).as<int>() == 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto value = std::get<0>(parse_json("{}"));
|
auto value = parse_json("{}").value;
|
||||||
kak_assert(value and value.is_a<JsonObject>());
|
kak_assert(value and value.is_a<JsonObject>());
|
||||||
kak_assert(value.as<JsonObject>().empty());
|
kak_assert(value.as<JsonObject>().empty());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user