Skip to content

Commit 98ab87f

Browse files
committed
Revert "[libc] Better IntegerToString API"
This reverts commit 910cc05.
1 parent 7069144 commit 98ab87f

File tree

15 files changed

+521
-625
lines changed

15 files changed

+521
-625
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ add_header_library(
9090
HDRS
9191
integer_to_string.h
9292
DEPENDS
93-
libc.src.__support.common
94-
libc.src.__support.CPP.limits
9593
libc.src.__support.CPP.span
9694
libc.src.__support.CPP.string_view
9795
libc.src.__support.CPP.type_traits
96+
libc.src.__support.common
9897
)
9998

10099

libc/src/__support/CPP/string.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ LIBC_INLINE string operator+(const char *lhs, const string &rhs) {
195195

196196
namespace internal {
197197
template <typename T> string to_dec_string(T value) {
198-
const IntegerToString<T> buffer(value);
199-
return buffer.view();
198+
char dec_buf[IntegerToString::dec_bufsize<T>()];
199+
auto maybe_string_view = IntegerToString::dec(value, dec_buf);
200+
const auto &string_view = *maybe_string_view;
201+
return string(string_view.data(), string_view.size());
200202
}
201203
} // namespace internal
202204

libc/src/__support/CPP/stringstream.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
1010
#define LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
1111

12-
#include "span.h"
1312
#include "string_view.h"
13+
#include "span.h"
1414
#include "type_traits.h"
1515

1616
#include "src/__support/integer_to_string.h"
@@ -58,8 +58,11 @@ class StringStream {
5858
// Write the |val| as string.
5959
template <typename T, enable_if_t<is_integral_v<T>, int> = 0>
6060
StringStream &operator<<(T val) {
61-
const IntegerToString<T> buffer(val);
62-
return *this << buffer.view();
61+
char buffer[IntegerToString::dec_bufsize<T>()];
62+
auto int_to_str = IntegerToString::dec(val, buffer);
63+
if (int_to_str)
64+
return operator<<(*int_to_str);
65+
return *this;
6366
}
6467

6568
template <typename T, enable_if_t<is_floating_point_v<T>, int> = 0>

libc/src/__support/FPUtil/fpbits_str.h

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818

1919
namespace __llvm_libc {
2020

21-
namespace details {
22-
23-
// Format T as uppercase hexadecimal number with leading zeros.
24-
template <typename T>
25-
using ZeroPaddedHexFmt = IntegerToString<
26-
T, typename radix::Hex::WithWidth<(sizeof(T) * 2)>::WithPrefix::Uppercase>;
27-
28-
} // namespace details
29-
3021
// Converts the bits to a string in the following format:
3122
// "0x<NNN...N> = S: N, E: 0xNNNN, M:0xNNN...N"
3223
// 1. N is a hexadecimal digit.
@@ -42,31 +33,36 @@ template <typename T> LIBC_INLINE cpp::string str(fputil::FPBits<T> x) {
4233
if (x.is_inf())
4334
return x.get_sign() ? "(-Infinity)" : "(+Infinity)";
4435

45-
const auto sign_char = [](bool sign) -> char { return sign ? '1' : '0'; };
46-
47-
cpp::string s;
36+
auto zerofill = [](char *arr, size_t n) {
37+
for (size_t i = 0; i < n; ++i)
38+
arr[i] = '0';
39+
};
4840

49-
const details::ZeroPaddedHexFmt<UIntType> bits(x.bits);
50-
s += bits.view();
41+
cpp::string s("0x");
42+
char bitsbuf[IntegerToString::hex_bufsize<UIntType>()];
43+
zerofill(bitsbuf, sizeof(bitsbuf));
44+
IntegerToString::hex(x.bits, bitsbuf, false);
45+
s += cpp::string(bitsbuf, sizeof(bitsbuf));
5146

52-
s += " = (S: ";
53-
s += sign_char(x.get_sign());
47+
s += " = (";
48+
s += cpp::string("S: ") + (x.get_sign() ? "1" : "0");
5449

55-
s += ", E: ";
56-
const details::ZeroPaddedHexFmt<uint16_t> exponent(x.get_unbiased_exponent());
57-
s += exponent.view();
50+
char expbuf[IntegerToString::hex_bufsize<uint16_t>()];
51+
zerofill(expbuf, sizeof(expbuf));
52+
IntegerToString::hex(x.get_unbiased_exponent(), expbuf, false);
53+
s += cpp::string(", E: 0x") + cpp::string(expbuf, sizeof(expbuf));
5854

5955
if constexpr (cpp::is_same_v<T, long double> &&
6056
fputil::FloatProperties<long double>::MANTISSA_WIDTH == 63) {
61-
s += ", I: ";
62-
s += sign_char(x.get_implicit_bit());
57+
s += cpp::string(", I: ") + (x.get_implicit_bit() ? "1" : "0");
6358
}
6459

65-
s += ", M: ";
66-
const details::ZeroPaddedHexFmt<UIntType> mantissa(x.get_mantissa());
67-
s += mantissa.view();
60+
char mantbuf[IntegerToString::hex_bufsize<UIntType>()] = {'0'};
61+
zerofill(mantbuf, sizeof(mantbuf));
62+
IntegerToString::hex(x.get_mantissa(), mantbuf, false);
63+
s += cpp::string(", M: 0x") + cpp::string(mantbuf, sizeof(mantbuf));
6864

69-
s += ')';
65+
s += ")";
7066
return s;
7167
}
7268

libc/src/__support/StringUtil/error_to_string.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ namespace internal {
2323

2424
constexpr size_t max_buff_size() {
2525
constexpr size_t unknown_str_len = sizeof("Unknown error");
26+
constexpr size_t max_num_len =
27+
__llvm_libc::IntegerToString::dec_bufsize<int>();
2628
// the buffer should be able to hold "Unknown error" + ' ' + num_str
27-
return (unknown_str_len + 1 + IntegerToString<int>::buffer_size()) *
28-
sizeof(char);
29+
return (unknown_str_len + 1 + max_num_len) * sizeof(char);
2930
}
3031

3132
// This is to hold error strings that have to be custom built. It may be
@@ -50,7 +51,7 @@ cpp::string_view build_error_string(int err_num, cpp::span<char> buffer) {
5051
// if the buffer can't hold "Unknown error" + ' ' + num_str, then just
5152
// return "Unknown error".
5253
if (buffer.size() <
53-
(sizeof("Unknown error") + 1 + IntegerToString<int>::buffer_size()))
54+
(sizeof("Unknown error") + 1 + IntegerToString::dec_bufsize<int>()))
5455
return const_cast<char *>("Unknown error");
5556

5657
cpp::StringStream buffer_stream(

libc/src/__support/StringUtil/signal_to_string.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ namespace internal {
2424

2525
constexpr size_t max_buff_size() {
2626
constexpr size_t base_str_len = sizeof("Real-time signal");
27+
constexpr size_t max_num_len =
28+
__llvm_libc::IntegerToString::dec_bufsize<int>();
2729
// the buffer should be able to hold "Real-time signal" + ' ' + num_str
28-
return (base_str_len + 1 + IntegerToString<int>::buffer_size()) *
29-
sizeof(char);
30+
return (base_str_len + 1 + max_num_len) * sizeof(char);
3031
}
3132

3233
// This is to hold signal strings that have to be custom built. It may be
@@ -53,7 +54,7 @@ cpp::string_view build_signal_string(int sig_num, cpp::span<char> buffer) {
5354
// if the buffer can't hold "Unknown signal" + ' ' + num_str, then just
5455
// return "Unknown signal".
5556
if (buffer.size() <
56-
(base_str.size() + 1 + IntegerToString<int>::buffer_size()))
57+
(base_str.size() + 1 + IntegerToString::dec_bufsize<int>()))
5758
return base_str;
5859

5960
cpp::StringStream buffer_stream(

0 commit comments

Comments
 (0)