Skip to content

[libc] Templatize the scanf Reader interface #131037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions libc/src/stdio/scanf_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ if(NOT(TARGET libc.src.__support.File.file) AND LLVM_LIBC_FULL_BUILD AND
return()
endif()

add_object_library(
add_header_library(
scanf_main
SRCS
scanf_main.cpp
HDRS
scanf_main.h
DEPENDS
Expand All @@ -87,14 +85,8 @@ add_header_library(
${use_system_file}
)

add_object_library(
add_header_library(
converter
SRCS
converter.cpp
string_converter.cpp
int_converter.cpp
float_converter.cpp
ptr_converter.cpp
HDRS
converter.h
converter_utils.h
Expand Down
73 changes: 0 additions & 73 deletions libc/src/stdio/scanf_core/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,78 +26,5 @@
namespace LIBC_NAMESPACE_DECL {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file can be deleted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

namespace scanf_core {

int convert(Reader *reader, const FormatSection &to_conv) {
int ret_val = 0;
switch (to_conv.conv_name) {
case '%':
return raw_match(reader, "%");
case 's':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_string(reader, to_conv);
case 'c':
case '[':
return convert_string(reader, to_conv);
case 'd':
case 'i':
case 'u':
case 'o':
case 'x':
case 'X':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_int(reader, to_conv);
#ifndef LIBC_COPT_SCANF_DISABLE_FLOAT
case 'f':
case 'F':
case 'e':
case 'E':
case 'a':
case 'A':
case 'g':
case 'G':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_float(reader, to_conv);
#endif // LIBC_COPT_SCANF_DISABLE_FLOAT
case 'n':
return convert_current_pos(reader, to_conv);
case 'p':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_pointer(reader, to_conv);
default:
return raw_match(reader, to_conv.raw_string);
}
return -1;
}

// raw_string is assumed to have a positive size.
int raw_match(Reader *reader, cpp::string_view raw_string) {
char cur_char = reader->getc();
int ret_val = READ_OK;
for (size_t i = 0; i < raw_string.size(); ++i) {
// Any space character matches any number of space characters.
if (internal::isspace(raw_string[i])) {
while (internal::isspace(cur_char)) {
cur_char = reader->getc();
}
} else {
if (raw_string[i] == cur_char) {
cur_char = reader->getc();
} else {
ret_val = MATCHING_FAILURE;
break;
}
}
}
reader->ungetc(cur_char);
return ret_val;
}

} // namespace scanf_core
} // namespace LIBC_NAMESPACE_DECL
83 changes: 81 additions & 2 deletions libc/src/stdio/scanf_core/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CONVERTER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/macros/config.h"
#include "src/stdio/scanf_core/core_structs.h"
#include "src/stdio/scanf_core/reader.h"

#ifndef LIBC_COPT_SCANF_DISABLE_FLOAT
#include "src/stdio/scanf_core/float_converter.h"
#endif // LIBC_COPT_SCANF_DISABLE_FLOAT
#include "src/stdio/scanf_core/current_pos_converter.h"
#include "src/stdio/scanf_core/int_converter.h"
#include "src/stdio/scanf_core/ptr_converter.h"
#include "src/stdio/scanf_core/string_converter.h"

#include <stddef.h>

namespace LIBC_NAMESPACE_DECL {
Expand All @@ -22,11 +31,81 @@ namespace scanf_core {
// convert will call a conversion function to convert the FormatSection into
// its string representation, and then that will write the result to the
// reader.
int convert(Reader *reader, const FormatSection &to_conv);
template <typename T>
int convert(Reader<T> *reader, const FormatSection &to_conv) {
int ret_val = 0;
switch (to_conv.conv_name) {
case '%':
return raw_match(reader, "%");
case 's':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_string(reader, to_conv);
case 'c':
case '[':
return convert_string(reader, to_conv);
case 'd':
case 'i':
case 'u':
case 'o':
case 'x':
case 'X':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_int(reader, to_conv);
#ifndef LIBC_COPT_SCANF_DISABLE_FLOAT
case 'f':
case 'F':
case 'e':
case 'E':
case 'a':
case 'A':
case 'g':
case 'G':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_float(reader, to_conv);
#endif // LIBC_COPT_SCANF_DISABLE_FLOAT
case 'n':
return convert_current_pos(reader, to_conv);
case 'p':
ret_val = raw_match(reader, " ");
if (ret_val != READ_OK)
return ret_val;
return convert_pointer(reader, to_conv);
default:
return raw_match(reader, to_conv.raw_string);
}
return -1;
}

// raw_match takes a raw string and matches it to the characters obtained from
// the reader.
int raw_match(Reader *reader, cpp::string_view raw_string);
template <typename T>
int raw_match(Reader<T> *reader, cpp::string_view raw_string) {
char cur_char = reader->getc();
int ret_val = READ_OK;
for (size_t i = 0; i < raw_string.size(); ++i) {
// Any space character matches any number of space characters.
if (internal::isspace(raw_string[i])) {
while (internal::isspace(cur_char)) {
cur_char = reader->getc();
}
} else {
if (raw_string[i] == cur_char) {
cur_char = reader->getc();
} else {
ret_val = MATCHING_FAILURE;
break;
}
}
}
reader->ungetc(cur_char);
return ret_val;
}

} // namespace scanf_core
} // namespace LIBC_NAMESPACE_DECL
Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/scanf_core/current_pos_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
namespace LIBC_NAMESPACE_DECL {
namespace scanf_core {

LIBC_INLINE int convert_current_pos(Reader *reader,
template <typename T>
LIBC_INLINE int convert_current_pos(Reader<T> *reader,
const FormatSection &to_conv) {
write_int_with_length(reader->chars_read(), to_conv);
return READ_OK;
Expand Down
Loading