Skip to content

Commit bcb6d61

Browse files
committed
Merge ReadBuffer and Reader
1 parent 2610f9a commit bcb6d61

File tree

7 files changed

+37
-100
lines changed

7 files changed

+37
-100
lines changed

libc/src/stdio/scanf_core/converter.cpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

libc/src/stdio/scanf_core/reader.h

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,35 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818
namespace scanf_core {
1919

20-
template <typename Derived> struct ReadBuffer {
21-
LIBC_INLINE char getc() { return static_cast<Derived *>(this)->getc(); }
22-
LIBC_INLINE void ungetc(int c) { static_cast<Derived *>(this)->ungetc(c); }
20+
template <typename Derived> class Reader {
21+
size_t cur_chars_read = 0;
22+
23+
public:
24+
// This returns the next character from the input and advances it by one
25+
// character. When it hits the end of the string or file it returns '\0' to
26+
// signal to stop parsing.
27+
LIBC_INLINE char getc() {
28+
++cur_chars_read;
29+
return static_cast<Derived *>(this)->getc();
30+
}
31+
32+
// This moves the input back by one character, placing c into the buffer if
33+
// this is a file reader, else c is ignored.
34+
LIBC_INLINE void ungetc(int c) {
35+
--cur_chars_read;
36+
static_cast<Derived *>(this)->ungetc(c);
37+
}
38+
39+
LIBC_INLINE size_t chars_read() { return cur_chars_read; }
2340
};
2441

25-
class StringBuffer : public ReadBuffer<StringBuffer> {
42+
class StringReader : public Reader<StringReader> {
2643
const char *buffer;
2744
[[maybe_unused]] size_t buff_len;
2845
size_t buff_cur = 0;
2946

3047
public:
31-
LIBC_INLINE StringBuffer(const char *buffer, size_t buff_len)
48+
LIBC_INLINE StringReader(const char *buffer, size_t buff_len)
3249
: buffer(buffer), buff_len(buff_len) {}
3350

3451
LIBC_INLINE char getc() {
@@ -47,32 +64,6 @@ class StringBuffer : public ReadBuffer<StringBuffer> {
4764
}
4865
};
4966

50-
// TODO: We should be able to fold ReadBuffer into Reader.
51-
template <typename T> class Reader {
52-
ReadBuffer<T> *buffer;
53-
size_t cur_chars_read = 0;
54-
55-
public:
56-
LIBC_INLINE Reader(ReadBuffer<T> *buffer) : buffer(buffer) {}
57-
58-
// This returns the next character from the input and advances it by one
59-
// character. When it hits the end of the string or file it returns '\0' to
60-
// signal to stop parsing.
61-
LIBC_INLINE char getc() {
62-
++cur_chars_read;
63-
return buffer->getc();
64-
}
65-
66-
// This moves the input back by one character, placing c into the buffer if
67-
// this is a file reader, else c is ignored.
68-
LIBC_INLINE void ungetc(char c) {
69-
--cur_chars_read;
70-
buffer->ungetc(c);
71-
}
72-
73-
LIBC_INLINE size_t chars_read() { return cur_chars_read; }
74-
};
75-
7667
} // namespace scanf_core
7768
} // namespace LIBC_NAMESPACE_DECL
7869

libc/src/stdio/scanf_core/vfscanf_internal.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ LIBC_INLINE void ungetc(int c, ::FILE *f) { ::ungetc(c, f); }
9191

9292
namespace scanf_core {
9393

94-
class StreamBuffer : public ReadBuffer<StreamBuffer> {
94+
class StreamReader : public Reader<StreamReader> {
9595
::FILE *stream;
9696

9797
public:
98-
LIBC_INLINE StreamBuffer(::FILE *stream) : stream(stream) {}
98+
LIBC_INLINE StreamReader(::FILE *stream) : stream(stream) {}
9999

100100
LIBC_INLINE char getc() {
101101
return static_cast<char>(internal::getc(static_cast<FILE *>(stream)));
@@ -109,8 +109,7 @@ LIBC_INLINE int vfscanf_internal(::FILE *__restrict stream,
109109
const char *__restrict format,
110110
internal::ArgList &args) {
111111
internal::flockfile(stream);
112-
scanf_core::StreamBuffer buffer(stream);
113-
scanf_core::Reader<scanf_core::StreamBuffer> reader(&buffer);
112+
scanf_core::StreamReader reader(stream);
114113
int retval = scanf_core::scanf_main(&reader, format, args);
115114
if (retval == 0 && internal::ferror_unlocked(stream))
116115
retval = EOF;

libc/src/stdio/sscanf.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ LLVM_LIBC_FUNCTION(int, sscanf,
2929
// and pointer semantics, as well as handling
3030
// destruction automatically.
3131
va_end(vlist);
32-
scanf_core::StringBuffer rb(buffer, cpp::numeric_limits<size_t>::max());
33-
scanf_core::Reader<scanf_core::StringBuffer> reader(&rb);
32+
scanf_core::StringReader reader(buffer, cpp::numeric_limits<size_t>::max());
3433
int ret_val = scanf_core::scanf_main(&reader, format, args);
3534
// This is done to avoid including stdio.h in the internals. On most systems
3635
// EOF is -1, so this will be transformed into just "return ret_val".

libc/src/stdio/vsscanf.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ namespace LIBC_NAMESPACE_DECL {
2121
LLVM_LIBC_FUNCTION(int, vsscanf,
2222
(const char *buffer, const char *format, va_list vlist)) {
2323
internal::ArgList args(vlist);
24-
scanf_core::StringBuffer rb{const_cast<char *>(buffer),
25-
cpp::numeric_limits<size_t>::max()};
26-
scanf_core::Reader<scanf_core::StringBuffer> reader(&rb);
24+
scanf_core::StringReader reader(buffer, cpp::numeric_limits<size_t>::max());
2725
int ret_val = scanf_core::scanf_main(&reader, format, args);
2826
// This is done to avoid including stdio.h in the internals. On most systems
2927
// EOF is -1, so this will be transformed into just "return ret_val".

libc/test/src/stdio/scanf_core/converter_test.cpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616
TEST(LlvmLibcScanfConverterTest, RawMatchBasic) {
1717
const char *str = "abcdef";
18-
LIBC_NAMESPACE::scanf_core::StringBuffer str_reader{str, sizeof(str)};
19-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
20-
reader(&str_reader);
18+
LIBC_NAMESPACE::scanf_core::StringReader reader(str, sizeof(str));
2119

2220
// Reading "abc" should succeed.
2321
ASSERT_EQ(LIBC_NAMESPACE::scanf_core::raw_match(&reader, "abc"),
@@ -52,9 +50,7 @@ TEST(LlvmLibcScanfConverterTest, RawMatchBasic) {
5250

5351
TEST(LlvmLibcScanfConverterTest, RawMatchSpaces) {
5452
const char *str = " a \t\n b cd";
55-
LIBC_NAMESPACE::scanf_core::StringBuffer str_reader{str, sizeof(str)};
56-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
57-
reader(&str_reader);
53+
LIBC_NAMESPACE::scanf_core::StringReader reader(str, sizeof(str));
5854

5955
// Reading "a" should fail and not advance.
6056
// Since there's nothing in the format string (the second argument to
@@ -100,9 +96,7 @@ TEST(LlvmLibcScanfConverterTest, RawMatchSpaces) {
10096
TEST(LlvmLibcScanfConverterTest, StringConvSimple) {
10197
const char *str = "abcDEF123 654LKJihg";
10298
char result[20];
103-
LIBC_NAMESPACE::scanf_core::StringBuffer str_reader{str, sizeof(str)};
104-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
105-
reader(&str_reader);
99+
LIBC_NAMESPACE::scanf_core::StringReader reader(str, sizeof(str));
106100

107101
LIBC_NAMESPACE::scanf_core::FormatSection conv;
108102
conv.has_conv = true;
@@ -123,9 +117,7 @@ TEST(LlvmLibcScanfConverterTest, StringConvSimple) {
123117

124118
TEST(LlvmLibcScanfConverterTest, StringConvNoWrite) {
125119
const char *str = "abcDEF123 654LKJihg";
126-
LIBC_NAMESPACE::scanf_core::StringBuffer str_reader{str, sizeof(str)};
127-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
128-
reader(&str_reader);
120+
LIBC_NAMESPACE::scanf_core::StringReader reader(str, sizeof(str));
129121

130122
LIBC_NAMESPACE::scanf_core::FormatSection conv;
131123
conv.has_conv = true;
@@ -145,9 +137,7 @@ TEST(LlvmLibcScanfConverterTest, StringConvNoWrite) {
145137
TEST(LlvmLibcScanfConverterTest, StringConvWidth) {
146138
const char *str = "abcDEF123 654LKJihg";
147139
char result[6];
148-
LIBC_NAMESPACE::scanf_core::StringBuffer str_reader{str, sizeof(str)};
149-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
150-
reader(&str_reader);
140+
LIBC_NAMESPACE::scanf_core::StringReader reader(str, sizeof(str));
151141

152142
LIBC_NAMESPACE::scanf_core::FormatSection conv;
153143
conv.has_conv = true;
@@ -180,9 +170,7 @@ TEST(LlvmLibcScanfConverterTest, StringConvWidth) {
180170
TEST(LlvmLibcScanfConverterTest, CharsConv) {
181171
const char *str = "abcDEF123 654LKJihg MNOpqr&*(";
182172
char result[20];
183-
LIBC_NAMESPACE::scanf_core::StringBuffer str_reader{str, sizeof(str)};
184-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
185-
reader(&str_reader);
173+
LIBC_NAMESPACE::scanf_core::StringReader reader(str, sizeof(str));
186174

187175
LIBC_NAMESPACE::scanf_core::FormatSection conv;
188176
conv.has_conv = true;
@@ -236,9 +224,7 @@ TEST(LlvmLibcScanfConverterTest, CharsConv) {
236224
TEST(LlvmLibcScanfConverterTest, ScansetConv) {
237225
const char *str = "abcDEF[123] 654LKJihg";
238226
char result[20];
239-
LIBC_NAMESPACE::scanf_core::StringBuffer str_reader{str, sizeof(str)};
240-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
241-
reader(&str_reader);
227+
LIBC_NAMESPACE::scanf_core::StringReader reader(str, sizeof(str));
242228

243229
LIBC_NAMESPACE::scanf_core::FormatSection conv;
244230
conv.has_conv = true;

libc/test/src/stdio/scanf_core/reader_test.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ TEST(LlvmLibcScanfStringReaderTest, Constructor) {
1515
char str[10];
1616
// buff_len justneeds to be a big number. The specific value isn't important
1717
// in the real world.
18-
LIBC_NAMESPACE::scanf_core::StringBuffer rb{const_cast<char *>(str), 1000000};
19-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
20-
reader(&rb);
18+
LIBC_NAMESPACE::scanf_core::StringReader reader(const_cast<char *>(str), 1000000);
2119
}
2220

2321
TEST(LlvmLibcScanfStringReaderTest, SimpleRead) {
2422
const char *str = "abc";
25-
LIBC_NAMESPACE::scanf_core::StringBuffer rb{const_cast<char *>(str), 1000000};
26-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
27-
reader(&rb);
23+
LIBC_NAMESPACE::scanf_core::StringReader reader(const_cast<char *>(str), 1000000);
2824

2925
for (size_t i = 0; i < sizeof("abc"); ++i) {
3026
ASSERT_EQ(str[i], reader.getc());
@@ -33,9 +29,7 @@ TEST(LlvmLibcScanfStringReaderTest, SimpleRead) {
3329

3430
TEST(LlvmLibcScanfStringReaderTest, ReadAndReverse) {
3531
const char *str = "abcDEF123";
36-
LIBC_NAMESPACE::scanf_core::StringBuffer rb{const_cast<char *>(str), 1000000};
37-
LIBC_NAMESPACE::scanf_core::Reader<LIBC_NAMESPACE::scanf_core::StringBuffer>
38-
reader(&rb);
32+
LIBC_NAMESPACE::scanf_core::StringReader reader(const_cast<char *>(str), 1000000);
3933

4034
for (size_t i = 0; i < 5; ++i) {
4135
ASSERT_EQ(str[i], reader.getc());

0 commit comments

Comments
 (0)