Skip to content

Commit 68eaf24

Browse files
author
Jakob Riedle
committed
Fixed #49 - major bug in basic_utf8_string::compare
1 parent a4c5421 commit 68eaf24

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

tinyutf8.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,9 +2142,10 @@ namespace tiny_utf8
21422142
*/
21432143
inline int compare( const basic_utf8_string& str ) const noexcept {
21442144
size_type my_size = size(), str_size = str.size();
2145-
if( my_size != str_size )
2146-
return my_size < str_size ? -1 : 1;
2147-
return std::memcmp( data() , str.data() , my_size );
2145+
int result = std::memcmp( data() , str.data() , my_size < str_size ? my_size : str_size );
2146+
if( !result && my_size != str_size )
2147+
result = my_size < str_size ? -1 : 1;
2148+
return result;
21482149
}
21492150
/**
21502151
* Compare this string with the supplied one.
@@ -2158,9 +2159,10 @@ namespace tiny_utf8
21582159
*/
21592160
inline int compare( const std::string& str ) const noexcept {
21602161
size_type my_size = size(), str_size = str.size();
2161-
if( my_size != str_size )
2162-
return my_size < str_size ? -1 : 1;
2163-
return std::memcmp( data() , str.data() , my_size );
2162+
int result = std::memcmp( data() , str.data() , my_size < str_size ? my_size : str_size );
2163+
if( !result && my_size != str_size )
2164+
result = my_size < str_size ? -1 : 1;
2165+
return result;
21642166
}
21652167
/**
21662168
* Compares this string with the supplied one.

0 commit comments

Comments
 (0)