LLLexer: Factor hex char parsing.

llvm-svn: 143101
This commit is contained in:
Benjamin Kramer 2011-10-27 14:08:01 +00:00
parent 651475977d
commit 7e10fef545

View File

@ -55,18 +55,22 @@ uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
return Result;
}
static char parseHexChar(char C) {
if (C >= '0' && C <= '9')
return C-'0';
if (C >= 'A' && C <= 'F')
return C-'A'+10;
if (C >= 'a' && C <= 'f')
return C-'a'+10;
return 0;
}
uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
uint64_t Result = 0;
for (; Buffer != End; ++Buffer) {
uint64_t OldRes = Result;
Result *= 16;
char C = *Buffer;
if (C >= '0' && C <= '9')
Result += C-'0';
else if (C >= 'A' && C <= 'F')
Result += C-'A'+10;
else if (C >= 'a' && C <= 'f')
Result += C-'a'+10;
Result += parseHexChar(*Buffer);
if (Result < OldRes) { // Uh, oh, overflow detected!!!
Error("constant bigger than 64 bits detected!");
@ -82,24 +86,12 @@ void LLLexer::HexToIntPair(const char *Buffer, const char *End,
for (int i=0; i<16; i++, Buffer++) {
assert(Buffer != End);
Pair[0] *= 16;
char C = *Buffer;
if (C >= '0' && C <= '9')
Pair[0] += C-'0';
else if (C >= 'A' && C <= 'F')
Pair[0] += C-'A'+10;
else if (C >= 'a' && C <= 'f')
Pair[0] += C-'a'+10;
Pair[0] += parseHexChar(*Buffer);
}
Pair[1] = 0;
for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
Pair[1] *= 16;
char C = *Buffer;
if (C >= '0' && C <= '9')
Pair[1] += C-'0';
else if (C >= 'A' && C <= 'F')
Pair[1] += C-'A'+10;
else if (C >= 'a' && C <= 'f')
Pair[1] += C-'a'+10;
Pair[1] += parseHexChar(*Buffer);
}
if (Buffer != End)
Error("constant bigger than 128 bits detected!");
@ -113,24 +105,12 @@ void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
assert(Buffer != End);
Pair[1] *= 16;
char C = *Buffer;
if (C >= '0' && C <= '9')
Pair[1] += C-'0';
else if (C >= 'A' && C <= 'F')
Pair[1] += C-'A'+10;
else if (C >= 'a' && C <= 'f')
Pair[1] += C-'a'+10;
Pair[1] += parseHexChar(*Buffer);
}
Pair[0] = 0;
for (int i=0; i<16; i++, Buffer++) {
Pair[0] *= 16;
char C = *Buffer;
if (C >= '0' && C <= '9')
Pair[0] += C-'0';
else if (C >= 'A' && C <= 'F')
Pair[0] += C-'A'+10;
else if (C >= 'a' && C <= 'f')
Pair[0] += C-'a'+10;
Pair[0] += parseHexChar(*Buffer);
}
if (Buffer != End)
Error("constant bigger than 128 bits detected!");
@ -149,9 +129,7 @@ static void UnEscapeLexed(std::string &Str) {
*BOut++ = '\\'; // Two \ becomes one
BIn += 2;
} else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
*BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
BIn[3] = Tmp; // Restore character
*BOut = parseHexChar(BIn[1]) * 16 + parseHexChar(BIn[2]);
BIn += 3; // Skip over handled chars
++BOut;
} else {