Skip to content

Commit 53b35cc

Browse files
[libc][NFC] clean up printf_core and scanf_core
Add LIBC_INLINE annotations to functions and fix variable cases within printf_core and scanf_core.
1 parent 39f09ec commit 53b35cc

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

libc/src/stdio/printf_core/core_structs.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct FormatSection {
5353

5454
// This operator is only used for testing and should be automatically
5555
// optimized out for release builds.
56-
bool operator==(const FormatSection &other) const {
56+
LIBC_INLINE bool operator==(const FormatSection &other) const {
5757
if (has_conv != other.has_conv)
5858
return false;
5959

@@ -93,11 +93,11 @@ template <typename T> LIBC_INLINE constexpr TypeDesc type_desc_from_type() {
9393
if constexpr (cpp::is_same_v<T, void>) {
9494
return TypeDesc{0, PrimaryType::Unknown};
9595
} else {
96-
constexpr bool isPointer = cpp::is_pointer_v<T>;
97-
constexpr bool isFloat = cpp::is_floating_point_v<T>;
98-
return TypeDesc{sizeof(T), isPointer ? PrimaryType::Pointer
99-
: isFloat ? PrimaryType::Float
100-
: PrimaryType::Integer};
96+
constexpr bool IS_POINTER = cpp::is_pointer_v<T>;
97+
constexpr bool IS_FLOAT = cpp::is_floating_point_v<T>;
98+
return TypeDesc{sizeof(T), IS_POINTER ? PrimaryType::Pointer
99+
: IS_FLOAT ? PrimaryType::Float
100+
: PrimaryType::Integer};
101101
}
102102
}
103103

libc/src/stdio/printf_core/float_dec_converter.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ class PaddingWriter {
103103
size_t min_width = 0;
104104

105105
public:
106-
PaddingWriter() {}
107-
PaddingWriter(const FormatSection &to_conv, char init_sign_char)
106+
LIBC_INLINE PaddingWriter() {}
107+
LIBC_INLINE PaddingWriter(const FormatSection &to_conv, char init_sign_char)
108108
: left_justified((to_conv.flags & FormatFlags::LEFT_JUSTIFIED) > 0),
109109
leading_zeroes((to_conv.flags & FormatFlags::LEADING_ZEROES) > 0),
110110
sign_char(init_sign_char),
111111
min_width(to_conv.min_width > 0 ? to_conv.min_width : 0) {}
112112

113-
int write_left_padding(Writer *writer, size_t total_digits) {
113+
LIBC_INLINE int write_left_padding(Writer *writer, size_t total_digits) {
114114
// The pattern is (spaces) (sign) (zeroes), but only one of spaces and
115115
// zeroes can be written, and only if the padding amount is positive.
116116
int padding_amount =
@@ -133,7 +133,7 @@ class PaddingWriter {
133133
return 0;
134134
}
135135

136-
int write_right_padding(Writer *writer, size_t total_digits) {
136+
LIBC_INLINE int write_right_padding(Writer *writer, size_t total_digits) {
137137
// If and only if the conversion is left justified, there may be trailing
138138
// spaces.
139139
int padding_amount =
@@ -170,7 +170,7 @@ class FloatWriter {
170170
Writer *writer; // Writes to the final output.
171171
PaddingWriter padding_writer; // Handles prefixes/padding, uses total_digits.
172172

173-
int flush_buffer(bool round_up_max_blocks = false) {
173+
LIBC_INLINE int flush_buffer(bool round_up_max_blocks = false) {
174174
const char MAX_BLOCK_DIGIT = (round_up_max_blocks ? '0' : '9');
175175

176176
// Write the most recent buffered block, and mark has_written
@@ -249,17 +249,18 @@ class FloatWriter {
249249
(sizeof(int) * 8));
250250

251251
public:
252-
FloatWriter(Writer *init_writer, bool init_has_decimal_point,
253-
const PaddingWriter &init_padding_writer)
252+
LIBC_INLINE FloatWriter(Writer *init_writer, bool init_has_decimal_point,
253+
const PaddingWriter &init_padding_writer)
254254
: has_decimal_point(init_has_decimal_point), writer(init_writer),
255255
padding_writer(init_padding_writer) {}
256256

257-
void init(size_t init_total_digits, size_t init_digits_before_decimal) {
257+
LIBC_INLINE void init(size_t init_total_digits,
258+
size_t init_digits_before_decimal) {
258259
total_digits = init_total_digits;
259260
digits_before_decimal = init_digits_before_decimal;
260261
}
261262

262-
void write_first_block(BlockInt block, bool exp_format = false) {
263+
LIBC_INLINE void write_first_block(BlockInt block, bool exp_format = false) {
263264
const DecimalString buf(block);
264265
const cpp::string_view int_to_str = buf.view();
265266
size_t digits_buffered = int_to_str.size();
@@ -280,7 +281,7 @@ class FloatWriter {
280281
}
281282
}
282283

283-
int write_middle_block(BlockInt block) {
284+
LIBC_INLINE int write_middle_block(BlockInt block) {
284285
if (block == MAX_BLOCK) { // Buffer max blocks in case of rounding
285286
++max_block_count;
286287
} else { // If a non-max block has been found
@@ -301,9 +302,9 @@ class FloatWriter {
301302
return 0;
302303
}
303304

304-
int write_last_block(BlockInt block, size_t block_digits,
305-
RoundDirection round, int exponent = 0,
306-
char exp_char = '\0') {
305+
LIBC_INLINE int write_last_block(BlockInt block, size_t block_digits,
306+
RoundDirection round, int exponent = 0,
307+
char exp_char = '\0') {
307308
bool has_exp = (exp_char != '\0');
308309

309310
char end_buff[BLOCK_SIZE];
@@ -458,13 +459,13 @@ class FloatWriter {
458459
return WRITE_OK;
459460
}
460461

461-
int write_zeroes(uint32_t num_zeroes) {
462+
LIBC_INLINE int write_zeroes(uint32_t num_zeroes) {
462463
RET_IF_RESULT_NEGATIVE(flush_buffer());
463464
RET_IF_RESULT_NEGATIVE(writer->write('0', num_zeroes));
464465
return 0;
465466
}
466467

467-
int right_pad() {
468+
LIBC_INLINE int right_pad() {
468469
return padding_writer.write_right_padding(writer, total_digits);
469470
}
470471
};

libc/src/stdio/printf_core/writer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct WriteBuffer {
4545
// write as much of new_str to the buffer as it can. The current position in
4646
// the buffer will be reset iff stream_writer is called. Calling this with an
4747
// empty string will flush the buffer if relevant.
48-
int overflow_write(cpp::string_view new_str) {
48+
LIBC_INLINE int overflow_write(cpp::string_view new_str) {
4949
// If there is a stream_writer, write the contents of the buffer, then
5050
// new_str, then clear the buffer.
5151
if (stream_writer != nullptr) {

libc/src/stdio/scanf_core/core_structs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct FormatSection {
4646

4747
cpp::bitset<256> scan_set;
4848

49-
bool operator==(const FormatSection &other) {
49+
LIBC_INLINE bool operator==(const FormatSection &other) {
5050
if (has_conv != other.has_conv)
5151
return false;
5252

libc/src/stdio/scanf_core/reader.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ class Reader {
3838

3939
public:
4040
// TODO: Set buff_len with a proper constant
41-
Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}
41+
LIBC_INLINE Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}
4242

43-
Reader(void *stream, StreamGetc stream_getc_in, StreamUngetc stream_ungetc_in,
44-
ReadBuffer *stream_buffer = nullptr)
43+
LIBC_INLINE Reader(void *stream, StreamGetc stream_getc_in,
44+
StreamUngetc stream_ungetc_in,
45+
ReadBuffer *stream_buffer = nullptr)
4546
: rb(stream_buffer), input_stream(stream), stream_getc(stream_getc_in),
4647
stream_ungetc(stream_ungetc_in) {}
4748

@@ -63,7 +64,7 @@ class Reader {
6364
// this is a file reader, else c is ignored.
6465
void ungetc(char c);
6566

66-
size_t chars_read() { return cur_chars_read; }
67+
LIBC_INLINE size_t chars_read() { return cur_chars_read; }
6768
};
6869

6970
} // namespace scanf_core

0 commit comments

Comments
 (0)