Skip to content

Commit 848c876

Browse files
committed
Split StringReader into its own header
1 parent 7fb8a00 commit 848c876

File tree

9 files changed

+66
-35
lines changed

9 files changed

+66
-35
lines changed

libc/src/stdio/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ add_entrypoint_object(
117117
sscanf.h
118118
DEPENDS
119119
libc.src.__support.arg_list
120-
libc.src.stdio.scanf_core.reader
121120
libc.src.stdio.scanf_core.scanf_main
121+
libc.src.stdio.scanf_core.string_reader
122122
)
123123

124124
add_entrypoint_object(
@@ -129,8 +129,8 @@ add_entrypoint_object(
129129
vsscanf.h
130130
DEPENDS
131131
libc.src.__support.arg_list
132-
libc.src.stdio.scanf_core.reader
133132
libc.src.stdio.scanf_core.scanf_main
133+
libc.src.stdio.scanf_core.string_reader
134134
)
135135

136136
add_entrypoint_object(

libc/src/stdio/scanf_core/CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,15 @@ add_header_library(
8181
reader.h
8282
DEPENDS
8383
libc.src.__support.macros.attributes
84-
${file_deps}
85-
${use_system_file}
84+
)
85+
86+
add_header_library(
87+
string_reader
88+
HDRS
89+
string_reader.h
90+
DEPENDS
91+
.reader
92+
libc.src.__support.macros.attributes
8693
)
8794

8895
add_header_library(

libc/src/stdio/scanf_core/reader.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,6 @@ template <typename Derived> class Reader {
3939
LIBC_INLINE size_t chars_read() { return cur_chars_read; }
4040
};
4141

42-
class StringReader : public Reader<StringReader> {
43-
const char *buffer;
44-
[[maybe_unused]] size_t buff_len;
45-
size_t buff_cur = 0;
46-
47-
public:
48-
LIBC_INLINE StringReader(const char *buffer, size_t buff_len)
49-
: buffer(buffer), buff_len(buff_len) {}
50-
51-
LIBC_INLINE char getc() {
52-
char output = buffer[buff_cur];
53-
++buff_cur;
54-
return output;
55-
}
56-
LIBC_INLINE void ungetc(int) {
57-
if (buff_cur > 0) {
58-
// While technically c should be written back to the buffer, in scanf we
59-
// always write the character that was already there. Additionally, the
60-
// buffer is most likely to contain a string that isn't part of a file,
61-
// which may not be writable.
62-
--buff_cur;
63-
}
64-
}
65-
};
66-
6742
} // namespace scanf_core
6843
} // namespace LIBC_NAMESPACE_DECL
6944

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- Reader definition for scanf -----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_STRING_READER_H
10+
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_STRING_READER_H
11+
12+
#include "src/__support/macros/attributes.h" // For LIBC_INLINE
13+
#include "src/__support/macros/config.h"
14+
#include "src/stdio/scanf_core/reader.h"
15+
16+
#include <stddef.h>
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
namespace scanf_core {
20+
21+
class StringReader : public Reader<StringReader> {
22+
const char *buffer;
23+
[[maybe_unused]] size_t buff_len;
24+
size_t buff_cur = 0;
25+
26+
public:
27+
LIBC_INLINE StringReader(const char *buffer, size_t buff_len)
28+
: buffer(buffer), buff_len(buff_len) {}
29+
30+
LIBC_INLINE char getc() {
31+
char output = buffer[buff_cur];
32+
++buff_cur;
33+
return output;
34+
}
35+
LIBC_INLINE void ungetc(int) {
36+
if (buff_cur > 0) {
37+
// While technically c should be written back to the buffer, in scanf we
38+
// always write the character that was already there. Additionally, the
39+
// buffer is most likely to contain a string that isn't part of a file,
40+
// which may not be writable.
41+
--buff_cur;
42+
}
43+
}
44+
};
45+
46+
} // namespace scanf_core
47+
} // namespace LIBC_NAMESPACE_DECL
48+
49+
#endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_STRING_READER_H

libc/src/stdio/sscanf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "src/__support/CPP/limits.h"
1212
#include "src/__support/arg_list.h"
1313
#include "src/__support/macros/config.h"
14-
#include "src/stdio/scanf_core/reader.h"
1514
#include "src/stdio/scanf_core/scanf_main.h"
15+
#include "src/stdio/scanf_core/string_reader.h"
1616

1717
#include "hdr/stdio_macros.h"
1818
#include "hdr/types/FILE.h"

libc/src/stdio/vsscanf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "hdr/stdio_macros.h"
1212
#include "src/__support/CPP/limits.h"
1313
#include "src/__support/arg_list.h"
14-
#include "src/stdio/scanf_core/reader.h"
1514
#include "src/stdio/scanf_core/scanf_main.h"
15+
#include "src/stdio/scanf_core/string_reader.h"
1616

1717
#include <stdarg.h>
1818

libc/test/src/stdio/scanf_core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ add_libc_unittest(
3232
SRCS
3333
reader_test.cpp
3434
DEPENDS
35-
libc.src.stdio.scanf_core.reader
35+
libc.src.stdio.scanf_core.string_reader
3636
libc.src.__support.CPP.string_view
3737
COMPILE_OPTIONS
3838
${use_system_file}
@@ -45,8 +45,8 @@ add_libc_unittest(
4545
SRCS
4646
converter_test.cpp
4747
DEPENDS
48-
libc.src.stdio.scanf_core.reader
4948
libc.src.stdio.scanf_core.converter
49+
libc.src.stdio.scanf_core.string_reader
5050
libc.src.__support.CPP.string_view
5151
COMPILE_OPTIONS
5252
${use_system_file}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "src/__support/CPP/string_view.h"
1010
#include "src/stdio/scanf_core/converter.h"
1111
#include "src/stdio/scanf_core/core_structs.h"
12-
#include "src/stdio/scanf_core/reader.h"
12+
#include "src/stdio/scanf_core/string_reader.h"
1313

1414
#include "test/UnitTest/Test.h"
1515

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/CPP/string_view.h"
10-
#include "src/stdio/scanf_core/reader.h"
10+
#include "src/stdio/scanf_core/string_reader.h"
1111

1212
#include "test/UnitTest/Test.h"
1313

0 commit comments

Comments
 (0)