From b11bf029f8a1a5bfadaec1c397d466b1072d2b5d Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Wed, 9 Oct 2024 17:10:16 +0200 Subject: [PATCH] Split the escape/unescape logic in a separate file, add tests --- dashboard/escape_string.h | 122 ++++++++++++++++++++++++++++++++++++++ dashboard/settings.cpp | 85 +------------------------- 2 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 dashboard/escape_string.h diff --git a/dashboard/escape_string.h b/dashboard/escape_string.h new file mode 100644 index 00000000..07a5e77e --- /dev/null +++ b/dashboard/escape_string.h @@ -0,0 +1,122 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include + +constexpr std::string escape_string(const std::vector & app) +{ + constexpr std::string_view escaped_chars = R"( '"\)"; + std::string app_string; + for (const auto & i: app) + { + for (char c: i) + { + if (escaped_chars.find(c) == std::string_view::npos) + { + app_string += c; + } + else + { + app_string += '\\'; + app_string += c; + } + } + + app_string += ' '; + } + app_string.resize(app_string.size() - 1); + + return app_string; +} + +constexpr std::vector unescape_string(const std::string & app_string) +{ + std::vector app; + app.emplace_back(); + + bool seen_backslash = false; + bool seen_single_quote = false; + bool seen_double_quote = false; + for (auto c: app_string) + { + if (seen_backslash) + { + app.back() += c; + seen_backslash = false; + } + else if (seen_single_quote) + { + if (c == '\'') + seen_single_quote = false; + else if (c == '\\') + seen_backslash = true; + else + app.back() += c; + } + else if (seen_double_quote) + { + if (c == '"') + seen_double_quote = false; + else if (c == '\\') + seen_backslash = true; + else + app.back() += c; + } + else + { + switch (c) + { + case '\\': + seen_backslash = true; + break; + case '\'': + seen_single_quote = true; + break; + case '"': + seen_double_quote = true; + break; + case ' ': + if (not app.back().empty()) + app.emplace_back(); + break; + default: + app.back() += c; + } + } + } + + if (app.back().empty()) + app.pop_back(); + + return app; +} + +#ifdef __clang__ +static_assert(escape_string({"abc", "def"}) == R"(abc def)"); +static_assert(escape_string({"ab\"c", "def"}) == R"(ab\"c def)"); +static_assert(escape_string({"ab'c", "def"}) == R"(ab\'c def)"); +static_assert(escape_string({"ab\\c", "def"}) == R"(ab\\c def)"); + +static_assert(unescape_string(R"(abc def)") == std::vector{"abc", "def"}); +static_assert(unescape_string(R"(ab\"c def)") == std::vector{"ab\"c", "def"}); +static_assert(unescape_string(R"(ab\'c def)") == std::vector{"ab'c", "def"}); +static_assert(unescape_string(R"(ab\\c def)") == std::vector{"ab\\c", "def"}); +#endif diff --git a/dashboard/settings.cpp b/dashboard/settings.cpp index 91eb5e98..e6b4da77 100644 --- a/dashboard/settings.cpp +++ b/dashboard/settings.cpp @@ -18,6 +18,7 @@ #include "settings.h" +#include "escape_string.h" #include "rectangle_partitionner.h" #include "steam_app.h" #include "ui_settings.h" @@ -319,92 +320,12 @@ void settings::restore_defaults() void settings::set_application(const std::vector & app) { - static const std::string_view escaped_chars = R"( '"\)"; - std::string app_string; - for (const auto & i: app) - { - for (char c: i) - { - if (escaped_chars.find(c) == std::string_view::npos) - { - app_string += c; - } - else - { - app_string += '\\'; - app_string += c; - } - } - - app_string += ' '; - } - app_string.resize(app_string.size() - 1); - - ui->edit_game_path->setText(QString::fromStdString(app_string)); + ui->edit_game_path->setText(QString::fromStdString(escape_string(app))); } std::vector settings::application() { - std::string app_string = ui->edit_game_path->text().toStdString(); - - std::vector app; - app.emplace_back(); - - bool seen_backslash = false; - bool seen_single_quote = false; - bool seen_double_quote = false; - for (auto c: app_string) - { - if (seen_backslash) - { - app.back() += c; - seen_backslash = false; - } - else if (seen_single_quote) - { - if (c == '\'') - seen_single_quote = false; - else if (c == '\\') - seen_backslash = true; - else - app.back() += c; - } - else if (seen_double_quote) - { - if (c == '"') - seen_double_quote = false; - else if (c == '\\') - seen_backslash = true; - else - app.back() += c; - } - else - { - switch (c) - { - case '\\': - seen_backslash = true; - break; - case '\'': - seen_single_quote = true; - break; - case '"': - seen_double_quote = true; - break; - case ' ': - if (not app.back().empty()) - app.emplace_back(); - break; - default: - app.back() += c; - } - } - } - - if (app.back().empty()) - app.pop_back(); - - return app; + return unescape_string(ui->edit_game_path->text().toStdString()); } void settings::load_settings()