Skip to content

Commit 591015e

Browse files
committed
🧹 gucc: move contains helper into string_utils
1 parent 24a250d commit 591015e

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

‎gucc/include/gucc/string_utils.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <algorithm> // for transform
55
#include <string> // for string
66
#include <string_view> // for string_view
7-
#include <vector> // for vector
7+
#include <type_traits>
8+
#include <vector> // for vector
89

910
#if defined(__clang__)
1011
#pragma clang diagnostic push
@@ -74,6 +75,20 @@ constexpr auto make_split_view(std::string_view str, char delim = '\n') noexcept
7475
| ranges::views::filter(second);
7576
}
7677

78+
template <typename T, typename npos_type = std::remove_cvref_t<decltype(T::npos)>>
79+
concept string_findable = requires(T value) {
80+
// check that type of T::npos is T::size_type
81+
{ npos_type{} } -> std::same_as<typename T::size_type>;
82+
// check that type of T::find is T::size_type
83+
{ value.find(std::string_view{""}) } -> std::same_as<typename T::size_type>;
84+
};
85+
86+
// simple helper function to check if string contains a string
87+
constexpr auto contains(string_findable auto const& str, std::string_view needle) noexcept -> bool {
88+
using str_type = std::remove_reference_t<decltype(str)>;
89+
return str.find(needle) != str_type::npos;
90+
}
91+
7792
} // namespace gucc::utils
7893

7994
#endif // STRING_UTILS_HPP

‎gucc/src/bootloader.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "gucc/bootloader.hpp"
22
#include "gucc/initcpio.hpp"
33
#include "gucc/io_utils.hpp"
4+
#include "gucc/string_utils.hpp"
45

56
#include <fmt/compile.h>
67
#include <fmt/format.h>
@@ -132,20 +133,6 @@ GRUB_DISABLE_RECOVERY=true
132133
#GRUB_DISABLE_OS_PROBER=false
133134
)"sv;
134135

135-
template <typename T, typename npos_type = std::remove_cvref_t<decltype(T::npos)>>
136-
concept string_findable = requires(T value) {
137-
// check that type of T::npos is T::size_type
138-
{ npos_type{} } -> std::same_as<typename T::size_type>;
139-
// check that type of T::find is T::size_type
140-
{ value.find(std::string_view{""}) } -> std::same_as<typename T::size_type>;
141-
};
142-
143-
// simple helper function to check if string contains a string
144-
constexpr auto contains(string_findable auto const& str, std::string_view needle) noexcept -> bool {
145-
using str_type = std::remove_reference_t<decltype(str)>;
146-
return str.find(needle) != str_type::npos;
147-
}
148-
149136
constexpr auto convert_boolean_val(std::string_view needle, bool value) noexcept -> std::string_view {
150137
if (needle == "GRUB_ENABLE_CRYPTODISK="sv || needle == "GRUB_DISABLE_SUBMENU="sv) {
151138
return value ? "y"sv : "n"sv;
@@ -176,7 +163,7 @@ auto parse_grub_line(const gucc::bootloader::GrubConfig& grub_config, std::strin
176163
CONV_OPT_F_S("GRUB_BACKGROUND=", grub_config.background, "/path/to/wallpaper");
177164
CONV_OPT_F_S("GRUB_THEME=", grub_config.theme, "/path/to/gfxtheme");
178165
CONV_OPT_F_S("GRUB_INIT_TUNE=", grub_config.init_tune, "480 440 1");
179-
if (contains(line, "=y") || contains(line, "=true") || contains(line, "=false")) {
166+
if (gucc::utils::contains(line, "=y") || gucc::utils::contains(line, "=true") || gucc::utils::contains(line, "=false")) {
180167
// booleans
181168
CONV_OPT_B("GRUB_ENABLE_CRYPTODISK=", grub_config.enable_cryptodisk, "y");
182169
CONV_OPT_B("GRUB_DISABLE_LINUX_UUID=", grub_config.disable_linux_uuid, "true");

0 commit comments

Comments
 (0)