llvm.org GIT mirror llvm / 7850dd0
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 Jakob Stoklund Olesen 9 years ago
2 changed file(s) with 19 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
4545 /// compare_numeric - Compare strings, handle embedded numbers.
4646 int StringRef::compare_numeric(StringRef RHS) const {
4747 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
48 if (Data[I] == RHS.Data[I])
49 continue;
48 // Check for sequences of digits.
5049 if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
51 // The longer sequence of numbers is larger. This doesn't really handle
52 // prefixed zeros well.
53 for (size_t J = I+1; J != E+1; ++J) {
50 // The longer sequence of numbers is considered larger.
51 // This doesn't really handle prefixed zeros well.
52 size_t J;
53 for (J = I + 1; J != E + 1; ++J) {
5454 bool ld = J < Length && ascii_isdigit(Data[J]);
5555 bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
5656 if (ld != rd)
5858 if (!rd)
5959 break;
6060 }
61 // The two number sequences have the same length (J-I), just memcmp them.
62 if (int Res = compareMemory(Data + I, RHS.Data + I, J - I))
63 return Res < 0 ? -1 : 1;
64 // Identical number sequences, continue search after the numbers.
65 I = J - 1;
66 continue;
6167 }
62 return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
68 if (Data[I] != RHS.Data[I])
69 return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
6370 }
6471 if (Length == RHS.Length)
6572 return 0;
7272 EXPECT_EQ( 1, StringRef("2").compare_numeric("1"));
7373 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty"));
7474 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1"));
75 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0"));
76 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16"));
77 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16"));
78 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0"));
79 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0"));
80 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0"));
7581 }
7682
7783 TEST(StringRefTest, Operators) {