aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/APInt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Support/APInt.cpp')
-rw-r--r--llvm/lib/Support/APInt.cpp20
1 files changed, 19 insertions, 1 deletions
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 05b1526..e686b97 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -2161,7 +2161,8 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
}
void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
- bool formatAsCLiteral, bool UpperCase) const {
+ bool formatAsCLiteral, bool UpperCase,
+ bool InsertSeparators) const {
assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
Radix == 36) &&
"Radix should be 2, 8, 10, 16, or 36!");
@@ -2187,6 +2188,9 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
}
}
+ // Number of digits in a group between separators.
+ unsigned Grouping = (Radix == 8 || Radix == 10) ? 3 : 4;
+
// First, check for a zero value and just short circuit the logic below.
if (isZero()) {
while (*Prefix) {
@@ -2223,9 +2227,13 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
++Prefix;
};
+ int Pos = 0;
while (N) {
+ if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
+ *--BufPtr = '\'';
*--BufPtr = Digits[N % Radix];
N /= Radix;
+ Pos++;
}
Str.append(BufPtr, std::end(Buffer));
return;
@@ -2257,17 +2265,27 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
unsigned MaskAmt = Radix - 1;
+ int Pos = 0;
while (Tmp.getBoolValue()) {
unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt;
+ if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
+ Str.push_back('\'');
+
Str.push_back(Digits[Digit]);
Tmp.lshrInPlace(ShiftAmt);
+ Pos++;
}
} else {
+ int Pos = 0;
while (Tmp.getBoolValue()) {
uint64_t Digit;
udivrem(Tmp, Radix, Tmp, Digit);
assert(Digit < Radix && "divide failed");
+ if (InsertSeparators && Pos % Grouping == 0 && Pos > 0)
+ Str.push_back('\'');
+
Str.push_back(Digits[Digit]);
+ Pos++;
}
}