From 527d8a39c8b99cb9dc1db8e48b8df0a99957ceca Mon Sep 17 00:00:00 2001 From: "Vogel, Rick" Date: Tue, 4 Apr 2023 14:30:55 +0200 Subject: [PATCH] remove string literals --- src/common/QskRgbValue.h | 248 ++++++++++------------------------ tests/test_QskRgbLiterals.cpp | 60 ++------ tests/test_QskRgbLiterals.h | 2 - 3 files changed, 77 insertions(+), 233 deletions(-) diff --git a/src/common/QskRgbValue.h b/src/common/QskRgbValue.h index 58d633c5..910f8f63 100644 --- a/src/common/QskRgbValue.h +++ b/src/common/QskRgbValue.h @@ -238,177 +238,57 @@ namespace QskRgb namespace QskRgb { - enum Format - { - ARGB, - RGBA - }; - - // Converts a '#12345678' color hex string to a '0x12345678' unsigned integer - QSK_EXPORT constexpr QRgb fromHexString( - const char* const str, const size_t len, const Format format ) noexcept - { - if ( ( len != 9 && len != 7 ) || str[ 0 ] != '#' ) - { - return 0; - } - - QRgb c = 0xFF; - - for ( size_t i = 1; i < len; ++i ) - { - switch ( str[ i ] ) - { - case '0': - c = ( c << 4 ) | 0x0; - break; - - case '1': - c = ( c << 4 ) | 0x1; - break; - - case '2': - c = ( c << 4 ) | 0x2; - break; - - case '3': - c = ( c << 4 ) | 0x3; - break; - - case '4': - c = ( c << 4 ) | 0x4; - break; - - case '5': - c = ( c << 4 ) | 0x5; - break; - - case '6': - c = ( c << 4 ) | 0x6; - break; - - case '7': - c = ( c << 4 ) | 0x7; - break; - - case '8': - c = ( c << 4 ) | 0x8; - break; - - case '9': - c = ( c << 4 ) | 0x9; - break; - - case 'A': - case 'a': - c = ( c << 4 ) | 0xA; - break; - - case 'B': - case 'b': - c = ( c << 4 ) | 0xB; - break; - - case 'C': - case 'c': - c = ( c << 4 ) | 0xC; - break; - - case 'D': - case 'd': - c = ( c << 4 ) | 0xD; - break; - - case 'E': - case 'e': - c = ( c << 4 ) | 0xE; - break; - - case 'F': - case 'f': - c = ( c << 4 ) | 0xF; - break; - - default: - c = ( c << 4 ) | 0x0; - break; - } - } - - switch ( format ) - { - case ARGB: - return c; - case RGBA: - return len == 9 ? qRgba( qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) ) - : qRgba( qRed( c ), qGreen( c ), qBlue( c ), 255 ); - } - - return 0; - } - namespace literals { - template< QRgb V > - constexpr QRgb parse_hex_literal_h() + namespace details { - return V; - } - - template< QRgb V, char C, char... Cs > - constexpr QRgb parse_hex_literal_h() - { - constexpr auto is_0_9 = C >= '0' && C <= '9'; - constexpr auto is_a_f = C >= 'a' && C <= 'f'; - constexpr auto is_A_F = C >= 'A' && C <= 'F'; - - static_assert( is_0_9 || is_a_f || is_A_F, "invalid hex character" ); - - if constexpr ( is_0_9 ) + template< QRgb V > + constexpr QRgb parse_hex_literal_h() { - return parse_hex_literal_h< ( V << 4 ) | ( C - '0' ), Cs... >(); + return V; } - if constexpr ( is_a_f ) - { - return parse_hex_literal_h< ( V << 4 ) | ( C - 'a' + 10 ), Cs... >(); - } - if constexpr ( is_A_F ) - { - return parse_hex_literal_h< ( V << 4 ) | ( C - 'A' + 10 ), Cs... >(); - } - } - template< char C, char... Cs > - constexpr QRgb parse_hex_literal_x() - { - static_assert( C == 'x', "invalid hex prefix character" ); - constexpr auto alpha = 0xFF; - return parse_hex_literal_h< alpha, Cs... >(); - } + template< QRgb V, char C, char... Cs > + constexpr QRgb parse_hex_literal_h() + { + constexpr auto is_0_9 = C >= '0' && C <= '9'; + constexpr auto is_a_f = C >= 'a' && C <= 'f'; + constexpr auto is_A_F = C >= 'A' && C <= 'F'; - template< char C, char... Cs > - constexpr QRgb parse_hex_literal_0() - { - static_assert( C == '0', "invalid hex prefix character" ); - return parse_hex_literal_x< Cs... >(); + static_assert( is_0_9 || is_a_f || is_A_F, "invalid hex character" ); + + if constexpr ( is_0_9 ) + { + return parse_hex_literal_h< ( V << 4 ) | ( C - '0' ), Cs... >(); + } + if constexpr ( is_a_f ) + { + return parse_hex_literal_h< ( V << 4 ) | ( C - 'a' + 10 ), Cs... >(); + } + if constexpr ( is_A_F ) + { + return parse_hex_literal_h< ( V << 4 ) | ( C - 'A' + 10 ), Cs... >(); + } + } + + template< char C, char... Cs > + constexpr QRgb parse_hex_literal_x() + { + static_assert( C == 'x', "invalid hex prefix character" ); + constexpr auto alpha = 0xFF; + return parse_hex_literal_h< alpha, Cs... >(); + } + + template< char C, char... Cs > + constexpr QRgb parse_hex_literal_0() + { + static_assert( C == '0', "invalid hex prefix character" ); + return parse_hex_literal_x< Cs... >(); + } } namespace integral { - // converts a hex string from '#RRGGBB[AA]' to '0xAARRGGBB' integer - QSK_EXPORT constexpr QRgb operator""_rgba( - const char* const str, const size_t len ) noexcept - { - return fromHexString( str, len, RGBA ); - } - - // converts a hex string from '#[AA]RRGGBB' to '0xAARRGGBB' integer - QSK_EXPORT constexpr QRgb operator""_argb( - const char* const str, const size_t len ) noexcept - { - return fromHexString( str, len, ARGB ); - } - // converts a hex literal from '0xRRGGBB[AA]' to '0xAARRGGBB' integer template< char... Cs > constexpr QRgb operator""_rgba() noexcept @@ -417,7 +297,7 @@ namespace QskRgb constexpr auto rrggbbaa = 10; static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa, "invalid color literal length" ); - constexpr auto c = parse_hex_literal_0< Cs... >(); + constexpr auto c = details::parse_hex_literal_0< Cs... >(); return sizeof...( Cs ) == rrggbbaa ? qRgba( qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) ) : qRgba( qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) ); @@ -431,28 +311,12 @@ namespace QskRgb constexpr auto aarrggbb = 10; static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb, "invalid color literal length" ); - return parse_hex_literal_0< Cs... >(); + return details::parse_hex_literal_0< Cs... >(); } } namespace color { - // converts a hex string from '#RRGGBB[AA]' to a QColor - QSK_EXPORT constexpr QColor operator""_rgba( - const char* const str, const size_t len ) noexcept - { - const auto c = fromHexString( str, len, RGBA ); - return QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) }; - } - - // converts a hex string from '#[AA]RRGGBB' to a QColor - QSK_EXPORT constexpr QColor operator""_argb( - const char* const str, const size_t len ) noexcept - { - const auto c = fromHexString( str, len, ARGB ); - return QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) }; - } - // converts a hex literal from '0xRRGGBB[AA]' to a QColor template< char... Cs > constexpr QColor operator""_rgba() noexcept @@ -461,7 +325,7 @@ namespace QskRgb constexpr auto rrggbbaa = 10; static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa, "invalid color literal length" ); - constexpr auto c = parse_hex_literal_0< Cs... >(); + constexpr auto c = details::parse_hex_literal_0< Cs... >(); return sizeof...( Cs ) == rrggbbaa ? QColor{ qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) } : QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) }; @@ -475,10 +339,34 @@ namespace QskRgb constexpr auto aarrggbb = 10; static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb, "invalid color literal length" ); - constexpr auto c = parse_hex_literal_0< Cs... >(); + constexpr auto c = details::parse_hex_literal_0< Cs... >(); return QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) }; } } + + template< char... Cs > + constexpr QRgb operator""_qrgba() noexcept + { + return integral::operator""_rgba(); + } + + template< char... Cs > + constexpr QRgb operator""_qargb() noexcept + { + return integral::operator""_argb(); + } + + template< char... Cs > + constexpr QColor operator""_crgba() noexcept + { + return color::operator""_rgba(); + } + + template< char... Cs > + constexpr QColor operator""_cargb() noexcept + { + return color::operator""_argb(); + } } } diff --git a/tests/test_QskRgbLiterals.cpp b/tests/test_QskRgbLiterals.cpp index e7b38e4c..db197d31 100644 --- a/tests/test_QskRgbLiterals.cpp +++ b/tests/test_QskRgbLiterals.cpp @@ -6,53 +6,24 @@ #ifdef QSK_REQUIRE_CONSTEXPR_LITERALS namespace QskRgb::literals::integral { - // RGBA hex string to QRgb with defaulted alpha = 0xFF - static_assert( "#123456"_rgba == 0xFF123456, "invalid or not constexpr" ); - static_assert( "#123456"_argb == 0xFF123456, "invalid or not constexpr" ); - - // ARGB hex string to QRgb with defaulted alpha = 0xFF - static_assert( "#AA112233"_argb == 0xAA112233, "invalid or not constexpr" ); - static_assert( "#112233AA"_rgba == 0xAA112233, "invalid or not constexpr" ); - // RGBA hex literal to QRgb with defaulted alpha = 0xFF static_assert( 0x112233_rgba == 0xFF112233, "invalid or not constexpr" ); - static_assert( 0xaabbcc_rgba == 0xFFAABBCC, "invalid or not constexpr" ); - static_assert( 0xAABBCC_rgba == 0xFFAABBCC, "invalid or not constexpr" ); - static_assert( 0x112233aa_rgba == 0xaa112233, "invalid or not constexpr" ); + static_assert( 0xAaBbCc_rgba == 0xFFAABBCC, "invalid or not constexpr" ); + static_assert( 0x112233Aa_rgba == 0xAA112233, "invalid or not constexpr" ); // ARGB hex literal to QRgb with defaulted alpha = 0xFF static_assert( 0x112233_argb == 0xFF112233, "invalid or not constexpr" ); - static_assert( 0xaabbcc_argb == 0xFFAABBCC, "invalid or not constexpr" ); - static_assert( 0xAABBCC_argb == 0xFFAABBCC, "invalid or not constexpr" ); - static_assert( 0x112233aa_argb == 0x112233aa, "invalid or not constexpr" ); + static_assert( 0xAaBbCc_argb == 0xFFAABBCC, "invalid or not constexpr" ); + static_assert( 0x112233Aa_argb == 0x112233aa, "invalid or not constexpr" ); } #endif -void QskRgbLiterals::parsing_data() +namespace QskRgb::literals { - QTest::addColumn< QString >( "text" ); - QTest::addColumn< QRgb >( "expected" ); - - QTest::newRow( "" ) << "" << ( QRgb ) 0; - QTest::newRow( "#" ) << "#" << ( QRgb ) 0; - QTest::newRow( "#1" ) << "#1" << ( QRgb ) 0; - QTest::newRow( "#12" ) << "#12" << ( QRgb ) 0; - QTest::newRow( "#123" ) << "#123" << ( QRgb ) 0; - QTest::newRow( "#1234" ) << "#1234" << ( QRgb ) 0; - QTest::newRow( "#12345" ) << "#12345" << ( QRgb ) 0; - QTest::newRow( "#123456" ) << "#123456" << ( QRgb ) 0xFF123456; - QTest::newRow( "#1234567" ) << "#1234567" << ( QRgb ) 0; - QTest::newRow( "#12345678" ) << "#12345678" << ( QRgb ) 0x12345678; - QTest::newRow( "#123456789" ) << "#123456789" << ( QRgb ) 0; -} - -void QskRgbLiterals::parsing() -{ - QFETCH( QString, text ); - QFETCH( QRgb, expected ); - - const auto actual = text.toStdString(); - QCOMPARE( QskRgb::fromHexString( actual.c_str(), actual.size(), QskRgb::ARGB ), expected ); + static_assert( std::is_same_v< decltype( 0x112233_qrgba ), QRgb >, "Invalid return type!" ); + static_assert( std::is_same_v< decltype( 0x112233_qargb ), QRgb >, "Invalid return type!" ); + static_assert( std::is_same_v< decltype( 0x112233_cargb ), QColor >, "Invalid return type!" ); + static_assert( std::is_same_v< decltype( 0x112233_crgba ), QColor >, "Invalid return type!" ); } void QskRgbLiterals::qrgbLiterals_data() @@ -62,12 +33,6 @@ void QskRgbLiterals::qrgbLiterals_data() using namespace QskRgb::literals::integral; - QTest::newRow( "\"#123456\"_rgba" ) << "#123456"_rgba << ( QRgb ) 0xFF123456; - QTest::newRow( "\"#123456\"_argb" ) << "#123456"_argb << ( QRgb ) 0xFF123456; - - QTest::newRow( "\"#AA112233\"_argb" ) << "#AA112233"_argb << ( QRgb ) 0xAA112233; - QTest::newRow( "\"#112233AA\"_rgba" ) << "#112233AA"_rgba << ( QRgb ) 0xAA112233; - QTest::newRow( "0x112233_rgba" ) << 0x112233_rgba << ( QRgb ) 0xFF112233; QTest::newRow( "0xaabbcc_rgba" ) << 0xaabbcc_rgba << ( QRgb ) 0xFFAABBCC; QTest::newRow( "0xAABBCC_rgba" ) << 0xAABBCC_rgba << ( QRgb ) 0xFFAABBCC; @@ -93,13 +58,6 @@ void QskRgbLiterals::colorLiterals_data() QTest::addColumn< QColor >( "expected" ); using namespace QskRgb::literals::color; - QTest::newRow( "\"#112233\"_rgba" ) << "#112233"_rgba << QColor::fromRgb( 0x11, 0x22, 0x33 ); - QTest::newRow( "\"#112233\"_argb" ) << "#112233"_argb << QColor::fromRgb( 0x11, 0x22, 0x33 ); - QTest::newRow( "\"#112233AA\"_rgba" ) - << "#112233AA"_rgba << QColor::fromRgb( 0x11, 0x22, 0x33, 0xAA ); - QTest::newRow( "\"#112233AA\"_argb" ) - << "#112233AA"_argb << QColor::fromRgb( 0x22, 0x33, 0xAA, 0x11 ); - QTest::newRow( "0x112233_rgba" ) << 0x112233_rgba << QColor::fromRgb( 0x11, 0x22, 0x33 ); QTest::newRow( "0x112233_argb" ) << 0x112233_argb << QColor::fromRgb( 0x11, 0x22, 0x33 ); QTest::newRow( "0x112233AA_rgba" ) diff --git a/tests/test_QskRgbLiterals.h b/tests/test_QskRgbLiterals.h index b353e282..2fbba419 100644 --- a/tests/test_QskRgbLiterals.h +++ b/tests/test_QskRgbLiterals.h @@ -7,8 +7,6 @@ class QskRgbLiterals final : public QObject Q_OBJECT private Q_SLOTS: - void parsing_data(); - void parsing(); void qrgbLiterals_data(); void qrgbLiterals(); void colorLiterals_data();