This is an archive of the discontinued LLVM Phabricator instance.

[libc] add scanf reader
ClosedPublic

Authored by michaelrj on Oct 31 2022, 2:59 PM.

Details

Summary

This is the interface that will be used to read from a file or string in
scanf. This patch also adds the string and file implementations of the
reader, although the file reader is not yet complete since ungetc has
not yet been implemented.

Diff Detail

Event Timeline

michaelrj created this revision.Oct 31 2022, 2:59 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptOct 31 2022, 2:59 PM
michaelrj requested review of this revision.Oct 31 2022, 2:59 PM
michaelrj updated this revision to Diff 472446.Nov 1 2022, 4:44 PM

add guards for systems that don't support FILE

The code LGTM but hard to comment on the design without seeing how they are used.

michaelrj updated this revision to Diff 472678.Nov 2 2022, 10:27 AM

adjust unget behavior to match the standard method.

sivachandra accepted this revision.Nov 7 2022, 12:17 AM

LGTM with minor fixes.

libc/src/stdio/scanf_core/reader.h
38

There could be a valid \0 character in the input stream before EOF. Will this cause any problems? Either way, this seems to me like the method should return a cpp::optional value?

42

This comment should be next to where the ignore is actually happening and explain why.

This revision is now accepted and ready to land.Nov 7 2022, 12:17 AM
michaelrj updated this revision to Diff 473737.Nov 7 2022, 10:23 AM
michaelrj marked an inline comment as done.

add comment explaining ungetc behavior

libc/src/stdio/scanf_core/reader.h
38

a \0 in a file will be treated the same as a \0 in a regular string; no scanf formats are allowed to cross it. As for if this should be optional, I don't think there should be any difference between the end of a file and the end of a string, so representing them both with a '\0' seems a lot simpler than adding machinery everywhere characters are read to handle optionals.

michaelrj updated this revision to Diff 473771.Nov 7 2022, 1:32 PM

adjust comment before landing

This revision was landed with ongoing or failed builds.Nov 7 2022, 1:49 PM
This revision was automatically updated to reflect the committed changes.