Factor out RRANGE macro like RANGE

This commit is contained in:
Rangi42
2025-09-19 16:49:14 -04:00
parent e31bcabbaa
commit d8aff148bb
3 changed files with 7 additions and 11 deletions

View File

@@ -97,6 +97,7 @@ static inline int clz(unsigned int x) {
// For lack of <ranges>, this adds some more brevity // For lack of <ranges>, this adds some more brevity
#define RANGE(s) std::begin(s), std::end(s) #define RANGE(s) std::begin(s), std::end(s)
#define RRANGE(s) std::rbegin(s), std::rend(s)
// MSVC does not inline `strlen()` or `.length()` of a constant string // MSVC does not inline `strlen()` or `.length()` of a constant string
template<int N> template<int N>

View File

@@ -1955,10 +1955,9 @@ static Token yylex_NORMAL() {
static Token yylex_RAW() { static Token yylex_RAW() {
// This is essentially a highly modified `readString` // This is essentially a highly modified `readString`
std::string str; std::string str;
size_t parenDepth = 0;
int c; int c;
for (;;) { for (size_t parenDepth = 0;;) {
c = peek(); c = peek();
switch (c) { switch (c) {
@@ -2075,7 +2074,7 @@ append:
finish: // Can't `break` out of a nested `for`-`switch` finish: // Can't `break` out of a nested `for`-`switch`
// Trim left and right blank space // Trim left and right blank space
str.erase(str.begin(), std::find_if_not(RANGE(str), isBlankSpace)); str.erase(str.begin(), std::find_if_not(RANGE(str), isBlankSpace));
str.erase(std::find_if_not(str.rbegin(), str.rend(), isBlankSpace).base(), str.end()); str.erase(std::find_if_not(RRANGE(str), isBlankSpace).base(), str.end());
// Returning COMMAs to the parser would mean that two consecutive commas // Returning COMMAs to the parser would mean that two consecutive commas
// (i.e. an empty argument) need to return two different tokens (STRING // (i.e. an empty argument) need to return two different tokens (STRING

View File

@@ -918,21 +918,17 @@ auto Palette::begin() -> decltype(colors)::iterator {
auto Palette::end() -> decltype(colors)::iterator { auto Palette::end() -> decltype(colors)::iterator {
// Return an iterator pointing past the last non-empty element. // Return an iterator pointing past the last non-empty element.
// Since the palette may contain gaps, we must scan from the end. // Since the palette may contain gaps, we must scan from the end.
return std::find_if( return std::find_if(RRANGE(colors), [](uint16_t c) { return c != UINT16_MAX; }).base();
colors.rbegin(), colors.rend(), [](uint16_t c) { return c != UINT16_MAX; }
).base();
} }
auto Palette::begin() const -> decltype(colors)::const_iterator { auto Palette::begin() const -> decltype(colors)::const_iterator {
// Skip the first slot if reserved for transparency // Same as the non-const begin().
return colors.begin() + options.hasTransparentPixels; return colors.begin() + options.hasTransparentPixels;
} }
auto Palette::end() const -> decltype(colors)::const_iterator { auto Palette::end() const -> decltype(colors)::const_iterator {
// Same as the non-const end(). // Same as the non-const end().
return std::find_if( return std::find_if(RRANGE(colors), [](uint16_t c) { return c != UINT16_MAX; }).base();
colors.rbegin(), colors.rend(), [](uint16_t c) { return c != UINT16_MAX; }
).base();
} }
uint8_t Palette::size() const { uint8_t Palette::size() const {