Avoid using indirect C++ types

This commit is contained in:
Rangi42
2025-04-23 00:53:20 -04:00
parent 762e2311d2
commit a72843748f
3 changed files with 12 additions and 14 deletions

View File

@@ -12,7 +12,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string_view> #include <string_view>
#include <type_traits>
#include <vector> #include <vector>
#include "extern/getopt.hpp" #include "extern/getopt.hpp"
@@ -283,8 +282,8 @@ static std::vector<size_t> readAtFile(std::string const &path, std::vector<char>
// We only filter out `EOF`, but calling `isblank()` on anything else is UB! // We only filter out `EOF`, but calling `isblank()` on anything else is UB!
static_assert( static_assert(
std::remove_reference_t<decltype(*file)>::traits_type::eof() == EOF, std::streambuf::traits_type::eof() == EOF,
"isblank(char_traits<...>::eof()) is UB!" "isblank(std::streambuf::traits_type::eof()) is UB!"
); );
std::vector<size_t> argvOfs; std::vector<size_t> argvOfs;

View File

@@ -45,7 +45,7 @@ constexpr uint8_t singleToHex(char c) {
} }
template<typename Str> // Should be std::string or std::string_view template<typename Str> // Should be std::string or std::string_view
static void skipWhitespace(Str const &str, typename Str::size_type &pos) { static void skipWhitespace(Str const &str, size_t &pos) {
pos = std::min(str.find_first_not_of(" \t"sv, pos), str.length()); pos = std::min(str.find_first_not_of(" \t"sv, pos), str.length());
} }
@@ -54,9 +54,8 @@ void parseInlinePalSpec(char const * const rawArg) {
// Palettes are separated by colons. // Palettes are separated by colons.
std::string_view arg(rawArg); std::string_view arg(rawArg);
using size_type = decltype(arg)::size_type;
auto parseError = [&rawArg, &arg](size_type ofs, size_type len, char const *msg) { auto parseError = [&rawArg, &arg](size_t ofs, size_t len, char const *msg) {
(void)arg; // With NDEBUG, `arg` is otherwise not used (void)arg; // With NDEBUG, `arg` is otherwise not used
assume(ofs <= arg.length()); assume(ofs <= arg.length());
assume(len <= arg.length()); assume(len <= arg.length());
@@ -80,7 +79,7 @@ void parseInlinePalSpec(char const * const rawArg) {
options.palSpec.clear(); options.palSpec.clear();
options.palSpec.emplace_back(); // Value-initialized, not default-init'd, so we get zeros options.palSpec.emplace_back(); // Value-initialized, not default-init'd, so we get zeros
size_type n = 0; // Index into the argument size_t n = 0; // Index into the argument
// TODO: store max `nbColors` ever reached, and compare against palette size later // TODO: store max `nbColors` ever reached, and compare against palette size later
size_t nbColors = 0; // Number of colors in the current palette size_t nbColors = 0; // Number of colors in the current palette
for (;;) { for (;;) {
@@ -222,7 +221,7 @@ static bool readLine(std::filebuf &file, std::string &buffer) {
// Parses the initial part of a string_view, advancing the "read index" as it does // Parses the initial part of a string_view, advancing the "read index" as it does
template<typename U> // Should be uint*_t template<typename U> // Should be uint*_t
static std::optional<U> parseDec(std::string const &str, std::string::size_type &n) { static std::optional<U> parseDec(std::string const &str, size_t &n) {
uintmax_t value = 0; uintmax_t value = 0;
auto result = std::from_chars(str.data() + n, str.data() + str.size(), value); auto result = std::from_chars(str.data() + n, str.data() + str.size(), value);
if (static_cast<bool>(result.ec)) { if (static_cast<bool>(result.ec)) {
@@ -233,7 +232,7 @@ static std::optional<U> parseDec(std::string const &str, std::string::size_type
} }
static std::optional<Rgba> static std::optional<Rgba>
parseColor(std::string const &str, std::string::size_type &n, uint16_t i) { parseColor(std::string const &str, size_t &n, uint16_t i) {
std::optional<uint8_t> r = parseDec<uint8_t>(str, n); std::optional<uint8_t> r = parseDec<uint8_t>(str, n);
if (!r) { if (!r) {
error("Failed to parse color #%d (\"%s\"): invalid red component", i + 1, str.c_str()); error("Failed to parse color #%d (\"%s\"): invalid red component", i + 1, str.c_str());
@@ -281,7 +280,7 @@ static void parsePSPFile(std::filebuf &file) {
line.clear(); line.clear();
requireLine("PSP", file, line); requireLine("PSP", file, line);
std::string::size_type n = 0; size_t n = 0;
std::optional<uint16_t> nbColors = parseDec<uint16_t>(line, n); std::optional<uint16_t> nbColors = parseDec<uint16_t>(line, n);
if (!nbColors || n != line.length()) { if (!nbColors || n != line.length()) {
error("Invalid \"number of colors\" line in PSP file (%s)", line.c_str()); error("Invalid \"number of colors\" line in PSP file (%s)", line.c_str());
@@ -347,7 +346,7 @@ static void parseGPLFile(std::filebuf &file) {
continue; continue;
} }
std::string::size_type n = 0; size_t n = 0;
skipWhitespace(line, n); skipWhitespace(line, n);
// Skip empty lines, or lines that contain just a comment. // Skip empty lines, or lines that contain just a comment.
if (line.length() == n || line[n] == '#') { if (line.length() == n || line[n] == '#') {
@@ -604,7 +603,7 @@ void parseExternalPalSpec(char const *arg) {
}; };
auto iter = auto iter =
std::find_if(RANGE(parsers), [&arg, &ptr](decltype(parsers)::value_type const &parser) { std::find_if(RANGE(parsers), [&arg, &ptr](auto const &parser) {
return strncasecmp(arg, std::get<0>(parser), ptr - arg) == 0; return strncasecmp(arg, std::get<0>(parser), ptr - arg) == 0;
}); });
if (iter == parsers.end()) { if (iter == parsers.end()) {

View File

@@ -36,7 +36,7 @@ public:
// color), then the other color is returned. Otherwise, `nullptr` is returned. // color), then the other color is returned. Otherwise, `nullptr` is returned.
[[nodiscard]] [[nodiscard]]
Rgba const *registerColor(Rgba const &rgba) { Rgba const *registerColor(Rgba const &rgba) {
decltype(_colors)::value_type &slot = _colors[rgba.cgbColor()]; std::optional<Rgba> &slot = _colors[rgba.cgbColor()];
if (rgba.cgbColor() == Rgba::transparent) { if (rgba.cgbColor() == Rgba::transparent) {
options.hasTransparentPixels = true; options.hasTransparentPixels = true;
@@ -52,7 +52,7 @@ public:
} }
size_t size() const { size_t size() const {
return std::count_if(RANGE(_colors), [](decltype(_colors)::value_type const &slot) { return std::count_if(RANGE(_colors), [](std::optional<Rgba> const &slot) {
return slot.has_value() && !slot->isTransparent(); return slot.has_value() && !slot->isTransparent();
}); });
} }