mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-03-01 01:30:36 +00:00
Fix a bug in compare_numeric().
Thanks to Alexandru Dura and Jonas Paulsson for finding it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140859 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
5bc93e782e
commit
7850dd0f25
@ -46,12 +46,12 @@ int StringRef::compare_lower(StringRef RHS) const {
|
|||||||
/// compare_numeric - Compare strings, handle embedded numbers.
|
/// compare_numeric - Compare strings, handle embedded numbers.
|
||||||
int StringRef::compare_numeric(StringRef RHS) const {
|
int StringRef::compare_numeric(StringRef RHS) const {
|
||||||
for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
|
for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
|
||||||
if (Data[I] == RHS.Data[I])
|
// Check for sequences of digits.
|
||||||
continue;
|
|
||||||
if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
|
if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
|
||||||
// The longer sequence of numbers is larger. This doesn't really handle
|
// The longer sequence of numbers is considered larger.
|
||||||
// prefixed zeros well.
|
// This doesn't really handle prefixed zeros well.
|
||||||
for (size_t J = I+1; J != E+1; ++J) {
|
size_t J;
|
||||||
|
for (J = I + 1; J != E + 1; ++J) {
|
||||||
bool ld = J < Length && ascii_isdigit(Data[J]);
|
bool ld = J < Length && ascii_isdigit(Data[J]);
|
||||||
bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
|
bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
|
||||||
if (ld != rd)
|
if (ld != rd)
|
||||||
@ -59,7 +59,14 @@ int StringRef::compare_numeric(StringRef RHS) const {
|
|||||||
if (!rd)
|
if (!rd)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// The two number sequences have the same length (J-I), just memcmp them.
|
||||||
|
if (int Res = compareMemory(Data + I, RHS.Data + I, J - I))
|
||||||
|
return Res < 0 ? -1 : 1;
|
||||||
|
// Identical number sequences, continue search after the numbers.
|
||||||
|
I = J - 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
if (Data[I] != RHS.Data[I])
|
||||||
return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
|
return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
|
||||||
}
|
}
|
||||||
if (Length == RHS.Length)
|
if (Length == RHS.Length)
|
||||||
|
@ -73,6 +73,12 @@ TEST(StringRefTest, StringOps) {
|
|||||||
EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
|
EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
|
||||||
EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
|
EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
|
||||||
EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
|
EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
|
||||||
|
EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
|
||||||
|
EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
|
||||||
|
EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
|
||||||
|
EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
|
||||||
|
EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
|
||||||
|
EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringRefTest, Operators) {
|
TEST(StringRefTest, Operators) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user