diff --git a/native/jni/init/getinfo.cpp b/native/jni/init/getinfo.cpp index 90f6ad736..c5103fe4d 100644 --- a/native/jni/init/getinfo.cpp +++ b/native/jni/init/getinfo.cpp @@ -13,48 +13,59 @@ using namespace std; vector mount_list; -static vector> parse_cmdline(const string &cmdline) { - vector> result; - size_t base = 0; - while (true) { - auto found = base; - while (((found = cmdline.find_first_of(" \"", found)) != string::npos) && - (cmdline[found] == '"')) { - if ((found = cmdline.find('"', found + 1)) == string::npos) - break; - found++; +template using chars = integer_sequence; + +template +static string extract_qutoed_str_until(chars, chars, + string_view sv, size_t &begin, bool "ed) { + string result; + char match_array[] = {escapes..., breaks..., '"'}; + string_view match(match_array, sizeof(match_array)); + for (auto end = begin;; ++end) { + end = sv.find_first_of(match, end); + if (end == string_view::npos || ((sv[end] == breaks) || ...) || + (!quoted && ((sv[end] == escapes) || ...))) { + result.append(sv.substr(begin, end - begin)); + begin = end; + return result; } - string piece; - auto source = cmdline.substr(base, found - base); - remove_copy(source.begin(), source.end(), back_insert_iterator(piece), '"'); - auto equal_sign = piece.find('='); - if (equal_sign == string::npos) { - if (!piece.empty()) - result.emplace_back(std::move(piece), ""); - } else { - result.emplace_back(piece.substr(0, equal_sign), piece.substr(equal_sign + 1)); + if (sv[end] == '"') { + quoted = !quoted; + result.append(sv.substr(begin, end - begin)); + begin = end + 1; } - if (found == string::npos) - break; - base = found + 1; } - return result; } -static vector> parse_bootconfig(const string &bootconfig) { - vector> result; - for (auto &line : split(bootconfig, "\n")) { - line.erase(remove(line.begin(), line.end(), '"'), line.end()); - auto equal_sign = line.find('='); - if (equal_sign == string::npos) { - if (!line.empty()) - result.emplace_back(move(line), ""); - } else { - result.emplace_back(rtrim(line.substr(0, equal_sign)), - ltrim(line.substr(equal_sign + 1))); +//[pair_split][key][assign_split][assign][assign_split][value][pair_split] +template +static auto parse(chars, chars, chars, string_view sv) { + vector> kv; + char next_array[] = {pair_spilt...}; + string_view next(next_array, sizeof(next_array)); + char skip_array[] = {assign..., assign_split...}; + string_view skip(skip_array, sizeof(skip_array)); + bool quoted = false; + for (size_t cur = 0u; cur < sv.size(); + cur = sv.find_first_not_of(next, cur)) { + auto key = extract_qutoed_str_until(chars{}, + chars{}, sv, cur, quoted); + cur = sv.find_first_not_of(skip, cur); + if (((cur == string_view::npos) || ... || (sv[cur] == pair_spilt))) { + kv.emplace_back(key, ""); + continue; } + auto value = extract_qutoed_str_until(chars{}, chars<>{}, sv, cur, quoted); + kv.emplace_back(key, value); } - return result; + return kv; +} + +static auto parse_cmdline(string_view sv) { + return parse(chars<' '>{}, chars<'='>{}, chars<>{}, sv); +} +static auto parse_bootconfig(string_view sv) { + return parse(chars<'\n'>{}, chars<'='>{}, chars<' '>{}, sv); } #define test_bit(bit, array) (array[bit / 8] & (1 << (bit % 8))) @@ -81,7 +92,7 @@ static bool check_key_combo() { if (events.empty()) return false; - run_finally fin([&]{ std::for_each(events.begin(), events.end(), close); }); + run_finally fin([&] { for_each(events.begin(), events.end(), close); }); // Return true if volume up key is held for more than 3 seconds int count = 0;