Use some more C++20 features (#1231)

This commit is contained in:
Rangi
2023-11-11 20:47:53 -05:00
committed by GitHub
parent dde9f2bb79
commit 31529524c8
4 changed files with 13 additions and 17 deletions

View File

@@ -1197,7 +1197,7 @@ static bool processFilename(char const *name)
if (fstat(input, &stat) == -1) { if (fstat(input, &stat) == -1) {
report("FATAL: Failed to stat \"%s\": %s\n", name, strerror(errno)); report("FATAL: Failed to stat \"%s\": %s\n", name, strerror(errno));
} else if (!S_ISREG(stat.st_mode)) { // FIXME: Do we want to support other types? } else if (!S_ISREG(stat.st_mode)) { // TODO: Do we want to support other types?
report("FATAL: \"%s\" is not a regular file, and thus cannot be modified in-place\n", report("FATAL: \"%s\" is not a regular file, and thus cannot be modified in-place\n",
name); name);
} else if (stat.st_size < 0x150) { } else if (stat.st_size < 0x150) {

View File

@@ -4,6 +4,7 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <charconv>
#include <cinttypes> #include <cinttypes>
#include <climits> #include <climits>
#include <cstdint> #include <cstdint>
@@ -218,21 +219,18 @@ static void readLine(std::filebuf &file, std::string &buffer) {
} }
} }
// FIXME: Normally we'd use `std::from_chars`, but that's not available with GCC 7
/* /*
* 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, std::string::size_type &n) {
std::string::size_type start = n; uintmax_t value = 0;
auto result = std::from_chars(str.data(), str.data() + str.size(), value);
uintmax_t value = 0; // Use a larger type to handle overflow more easily if ((bool)result.ec) {
for (auto end = std::min(str.length(), str.find_first_not_of("0123456789"sv, n)); n < end; return std::nullopt;
++n) {
value = std::min(value * 10 + (str[n] - '0'), (uintmax_t)std::numeric_limits<U>::max);
} }
n += result.ptr - str.data();
return n > start ? std::optional<U>{value} : std::nullopt; return std::optional<U>{value};
} }
static std::optional<Rgba> parseColor(std::string const &str, std::string::size_type &n, static std::optional<Rgba> parseColor(std::string const &str, std::string::size_type &n,
@@ -334,8 +332,7 @@ static void parseGPLFile(std::filebuf &file) {
std::string line; std::string line;
readLine(file, line); readLine(file, line);
// FIXME: C++20 will allow `!line.starts_with` instead of `line.rfind` with 0 if (!line.starts_with("GIMP Palette")) {
if (line.rfind("GIMP Palette", 0)) {
error("Palette file does not appear to be a GPL palette file"); error("Palette file does not appear to be a GPL palette file");
return; return;
} }
@@ -350,8 +347,7 @@ static void parseGPLFile(std::filebuf &file) {
break; break;
} }
// FIXME: C++20 will allow `line.starts_with` instead of `!line.rfind` with 0 if (line.starts_with("#") || line.starts_with("Name:") || line.starts_with("Column:")) {
if (!line.rfind("#", 0) || !line.rfind("Name:", 0) || !line.rfind("Column:", 0)) {
continue; continue;
} }

View File

@@ -402,7 +402,7 @@ int main(int argc, char *argv[])
parseScrambleSpec(musl_optarg); parseScrambleSpec(musl_optarg);
break; break;
case 's': case 's':
// FIXME: nobody knows what this does, figure it out // TODO: implement "smart linking" with `-s`
(void)musl_optarg; (void)musl_optarg;
warning(NULL, 0, "Nobody has any idea what `-s` does"); warning(NULL, 0, "Nobody has any idea what `-s` does");
break; break;

View File

@@ -330,8 +330,8 @@ static int compareSymbols(void const *a, void const *b)
char const *sym1_name = sym1->sym->name; char const *sym1_name = sym1->sym->name;
char const *sym2_name = sym2->sym->name; char const *sym2_name = sym2->sym->name;
bool sym1_local = !!strchr(sym1_name, '.'); bool sym1_local = strchr(sym1_name, '.');
bool sym2_local = !!strchr(sym2_name, '.'); bool sym2_local = strchr(sym2_name, '.');
if (sym1_local != sym2_local) { if (sym1_local != sym2_local) {
size_t sym1_len = strlen(sym1_name); size_t sym1_len = strlen(sym1_name);