Skip to content

Commit 411dfba

Browse files
authored
Simplified the use of sizeof(SSO::data)
BTW made tiny-utf8 compile under Clang < 3.7
1 parent fb8b170 commit 411dfba

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

include/tinyutf8/tinyutf8.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,22 +632,23 @@ namespace tiny_utf8
632632
// Layout used, if sso is active
633633
struct SSO
634634
{
635-
data_type data[sizeof(NON_SSO)-1];
636-
unsigned char data_len; // This field holds ( sizeof(SSO::data) - num_characters ) << 1
635+
enum : size_type{ size = sizeof(NON_SSO)-1 };
636+
data_type data[size];
637+
unsigned char data_len; // This field holds ( size - num_characters ) << 1
637638

638639
SSO( data_type value ) noexcept :
639640
data{ value , '\0' }
640-
, data_len( (unsigned char)( sizeof(SSO::data) - 1u ) << 1 )
641+
, data_len( (unsigned char)( size - 1u ) << 1 )
641642
{}
642643
SSO( size_type data_len ) noexcept :
643644
// Note: No initialization of .data (important for the constructor of basic_utf8_string(value_type)...)
644-
data_len( (unsigned char)( ( sizeof(SSO::data) - data_len ) << 1 ) )
645+
data_len( (unsigned char)( ( size - data_len ) << 1 ) )
645646
{
646647
data[data_len] = '\0'; // Add delimiter to actual data
647648
}
648649
SSO() noexcept :
649650
data{ '\0' }
650-
, data_len( (unsigned char)( sizeof(SSO::data) - 0 ) << 1 )
651+
, data_len( (unsigned char)( size - 0 ) << 1 )
651652
{}
652653
};
653654

@@ -661,13 +662,13 @@ namespace tiny_utf8
661662
protected: //! Static helper methods
662663

663664
//! Get the maximum number of bytes (excluding the trailing '\0') that can be stored within a basic_utf8_string object
664-
static constexpr inline size_type get_sso_capacity() noexcept { return sizeof(SSO::data); }
665+
static constexpr inline size_type get_sso_capacity() noexcept { return SSO::size; }
665666

666667
//! SFINAE helpers for constructors
667668
template<size_type L>
668-
using enable_if_small_string = typename std::enable_if<( L <= sizeof(SSO::data) ), bool>::type;
669+
using enable_if_small_string = typename std::enable_if<( L <= SSO::size ), bool>::type;
669670
template<size_type L>
670-
using enable_if_not_small_string = typename std::enable_if<( L > sizeof(SSO::data) ), bool>::type;
671+
using enable_if_not_small_string = typename std::enable_if<( L > SSO::size ), bool>::type;
671672

672673
// Template to enable overloads, if the supplied type T is a character array without known bounds
673674
template<typename T, typename CharType, typename _DataType = bool>
@@ -916,7 +917,7 @@ namespace tiny_utf8
916917

917918
//! Set the data length (also enables SSO)
918919
inline void set_sso_data_len( unsigned char data_len = 0 ) noexcept {
919-
t_sso.data_len = (unsigned char)( sizeof(SSO::data) - data_len ) << 1;
920+
t_sso.data_len = (unsigned char)( SSO::size - data_len ) << 1;
920921
}
921922

922923
//! Get the data length (when SSO is active)

0 commit comments

Comments
 (0)