remove string literals

This commit is contained in:
Vogel, Rick 2023-04-04 14:30:55 +02:00
parent 809966a220
commit 527d8a39c8
3 changed files with 77 additions and 233 deletions

View File

@ -238,116 +238,9 @@ namespace QskRgb
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 namespace literals
{
namespace details
{ {
template< QRgb V > template< QRgb V >
constexpr QRgb parse_hex_literal_h() constexpr QRgb parse_hex_literal_h()
@ -392,23 +285,10 @@ namespace QskRgb
static_assert( C == '0', "invalid hex prefix character" ); static_assert( C == '0', "invalid hex prefix character" );
return parse_hex_literal_x< Cs... >(); return parse_hex_literal_x< Cs... >();
} }
}
namespace integral 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 // converts a hex literal from '0xRRGGBB[AA]' to '0xAARRGGBB' integer
template< char... Cs > template< char... Cs >
constexpr QRgb operator""_rgba() noexcept constexpr QRgb operator""_rgba() noexcept
@ -417,7 +297,7 @@ namespace QskRgb
constexpr auto rrggbbaa = 10; constexpr auto rrggbbaa = 10;
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa, static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa,
"invalid color literal length" ); "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 return sizeof...( Cs ) == rrggbbaa
? qRgba( qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) ) ? qRgba( qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) )
: qRgba( qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) ); : qRgba( qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) );
@ -431,28 +311,12 @@ namespace QskRgb
constexpr auto aarrggbb = 10; constexpr auto aarrggbb = 10;
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb, static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb,
"invalid color literal length" ); "invalid color literal length" );
return parse_hex_literal_0< Cs... >(); return details::parse_hex_literal_0< Cs... >();
} }
} }
namespace color 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 // converts a hex literal from '0xRRGGBB[AA]' to a QColor
template< char... Cs > template< char... Cs >
constexpr QColor operator""_rgba() noexcept constexpr QColor operator""_rgba() noexcept
@ -461,7 +325,7 @@ namespace QskRgb
constexpr auto rrggbbaa = 10; constexpr auto rrggbbaa = 10;
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa, static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == rrggbbaa,
"invalid color literal length" ); "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 return sizeof...( Cs ) == rrggbbaa
? QColor{ qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) } ? QColor{ qAlpha( c ), qRed( c ), qGreen( c ), qBlue( c ) }
: QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) }; : QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) };
@ -475,10 +339,34 @@ namespace QskRgb
constexpr auto aarrggbb = 10; constexpr auto aarrggbb = 10;
static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb, static_assert( sizeof...( Cs ) == rrggbb || sizeof...( Cs ) == aarrggbb,
"invalid color literal length" ); "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 ) }; return QColor{ qRed( c ), qGreen( c ), qBlue( c ), qAlpha( c ) };
} }
} }
template< char... Cs >
constexpr QRgb operator""_qrgba() noexcept
{
return integral::operator""_rgba<Cs...>();
}
template< char... Cs >
constexpr QRgb operator""_qargb() noexcept
{
return integral::operator""_argb<Cs...>();
}
template< char... Cs >
constexpr QColor operator""_crgba() noexcept
{
return color::operator""_rgba<Cs...>();
}
template< char... Cs >
constexpr QColor operator""_cargb() noexcept
{
return color::operator""_argb<Cs...>();
}
} }
} }

View File

@ -6,53 +6,24 @@
#ifdef QSK_REQUIRE_CONSTEXPR_LITERALS #ifdef QSK_REQUIRE_CONSTEXPR_LITERALS
namespace QskRgb::literals::integral 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 // RGBA hex literal to QRgb with defaulted alpha = 0xFF
static_assert( 0x112233_rgba == 0xFF112233, "invalid or not constexpr" ); 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( 0xAABBCC_rgba == 0xFFAABBCC, "invalid or not constexpr" ); static_assert( 0x112233Aa_rgba == 0xAA112233, "invalid or not constexpr" );
static_assert( 0x112233aa_rgba == 0xaa112233, "invalid or not constexpr" );
// ARGB hex literal to QRgb with defaulted alpha = 0xFF // ARGB hex literal to QRgb with defaulted alpha = 0xFF
static_assert( 0x112233_argb == 0xFF112233, "invalid or not constexpr" ); 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( 0xAABBCC_argb == 0xFFAABBCC, "invalid or not constexpr" ); static_assert( 0x112233Aa_argb == 0x112233aa, "invalid or not constexpr" );
static_assert( 0x112233aa_argb == 0x112233aa, "invalid or not constexpr" );
} }
#endif #endif
void QskRgbLiterals::parsing_data() namespace QskRgb::literals
{ {
QTest::addColumn< QString >( "text" ); static_assert( std::is_same_v< decltype( 0x112233_qrgba ), QRgb >, "Invalid return type!" );
QTest::addColumn< QRgb >( "expected" ); 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!" );
QTest::newRow( "" ) << "" << ( QRgb ) 0; static_assert( std::is_same_v< decltype( 0x112233_crgba ), QColor >, "Invalid return type!" );
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 );
} }
void QskRgbLiterals::qrgbLiterals_data() void QskRgbLiterals::qrgbLiterals_data()
@ -62,12 +33,6 @@ void QskRgbLiterals::qrgbLiterals_data()
using namespace QskRgb::literals::integral; 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( "0x112233_rgba" ) << 0x112233_rgba << ( QRgb ) 0xFF112233;
QTest::newRow( "0xaabbcc_rgba" ) << 0xaabbcc_rgba << ( QRgb ) 0xFFAABBCC; QTest::newRow( "0xaabbcc_rgba" ) << 0xaabbcc_rgba << ( QRgb ) 0xFFAABBCC;
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" ); QTest::addColumn< QColor >( "expected" );
using namespace QskRgb::literals::color; 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_rgba" ) << 0x112233_rgba << QColor::fromRgb( 0x11, 0x22, 0x33 );
QTest::newRow( "0x112233_argb" ) << 0x112233_argb << QColor::fromRgb( 0x11, 0x22, 0x33 ); QTest::newRow( "0x112233_argb" ) << 0x112233_argb << QColor::fromRgb( 0x11, 0x22, 0x33 );
QTest::newRow( "0x112233AA_rgba" ) QTest::newRow( "0x112233AA_rgba" )

View File

@ -7,8 +7,6 @@ class QskRgbLiterals final : public QObject
Q_OBJECT Q_OBJECT
private Q_SLOTS: private Q_SLOTS:
void parsing_data();
void parsing();
void qrgbLiterals_data(); void qrgbLiterals_data();
void qrgbLiterals(); void qrgbLiterals();
void colorLiterals_data(); void colorLiterals_data();