-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Add vsscanf
function
#97529
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
Closed
Closed
[libc] Add vsscanf
function
#97529
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===-- Implementation of vsscanf -------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/stdio/vsscanf.h" | ||
|
||
#include "src/__support/CPP/limits.h" | ||
#include "src/__support/arg_list.h" | ||
#include "src/stdio/scanf_core/reader.h" | ||
#include "src/stdio/scanf_core/scanf_main.h" | ||
|
||
#include <stdarg.h> | ||
#include <stdio.h> | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(int, vsscanf, | ||
(const char *buffer, const char *format, va_list vlist)) { | ||
internal::ArgList args(vlist); // This holder class allows for easier copying | ||
// and pointer semantics, as well as handling | ||
// destruction automatically. | ||
|
||
scanf_core::ReadBuffer rb{const_cast<char *>(buffer), | ||
cpp::numeric_limits<size_t>::max()}; | ||
scanf_core::Reader reader(&rb); | ||
int ret_val = scanf_core::scanf_main(&reader, format, args); | ||
// This is done to avoid including stdio.h in the internals. On most systems | ||
// EOF is -1, so this will be transformed into just "return ret_val". | ||
return (ret_val == -1) ? EOF : ret_val; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header of vsscanf ------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_STDIO_VSSCANF_H | ||
#define LLVM_LIBC_SRC_STDIO_VSSCANF_H | ||
|
||
#include <stdarg.h> | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
int vsscanf(const char *s, const char *format, va_list vlist); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_STDIO_VSSCANF_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//===-- Unittests for vsscanf ---------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/CPP/limits.h" | ||
#include "src/__support/arg_list.h" | ||
|
||
#include "src/stdio/vsscanf.h" | ||
|
||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
TEST(LlvmLibcVSScanfTest, IntConvSimple) { | ||
int result = 0; | ||
|
||
auto fn = [](int fmt, ...) { | ||
va_list vlist; | ||
va_start(vlist, fmt); | ||
return LIBC_NAMESPACE::vsscanf("123", "%d", vlist); | ||
}; | ||
EXPECT_EQ(fn(0, &result), 1); | ||
EXPECT_EQ(result, 123); | ||
|
||
auto fn = [](int fmt, ...) { | ||
va_list vlist; | ||
va_start(vlist, fmt); | ||
return LIBC_NAMESPACE::vsscanf("456", "%i", vlist); | ||
}; | ||
EXPECT_EQ(fn(0, &result), 1); | ||
EXPECT_EQ(result, 456); | ||
|
||
auto fn = [](int fmt, ...) { | ||
va_list vlist; | ||
va_start(vlist, fmt); | ||
return LIBC_NAMESPACE::vsscanf("789", "%x", vlist); | ||
}; | ||
EXPECT_EQ(fn(0, &result), 1); | ||
EXPECT_EQ(result, 0x789); | ||
|
||
auto fn = [](int fmt, ...) { | ||
va_list vlist; | ||
va_start(vlist, fmt); | ||
return LIBC_NAMESPACE::vsscanf("012", "%o", vlist); | ||
}; | ||
EXPECT_EQ(fn(0, &result), 1); | ||
EXPECT_EQ(result, 012); | ||
|
||
auto fn = [](int fmt, ...) { | ||
va_list vlist; | ||
va_start(vlist, fmt); | ||
return LIBC_NAMESPACE::vsscanf("345", "%u", vlist); | ||
}; | ||
EXPECT_EQ(fn(0, &result), 1); | ||
EXPECT_EQ(result, 345); | ||
|
||
auto fn = [](int fmt, ...) { | ||
va_list vlist; | ||
va_start(vlist, fmt); | ||
return LIBC_NAMESPACE::vsscanf("10000000000000000000000000000000" | ||
"00000000000000000000000000000000" | ||
"00000000000000000000000000000000" | ||
"00000000000000000000000000000000" | ||
"00000000000000000000000000000000" | ||
"00000000000000000000000000000000" | ||
"00000000000000000000000000000000" | ||
"00000000000000000000000000000000" | ||
"00000000000000000000000000000000", | ||
"%d", vlist); | ||
}; | ||
EXPECT_EQ(fn(0, &result), 1); | ||
EXPECT_EQ(result, int(LIBC_NAMESPACE::cpp::numeric_limits<intmax_t>::max())); | ||
|
||
auto fn = [](int fmt, ...) { | ||
va_list vlist; | ||
va_start(vlist, fmt); | ||
return LIBC_NAMESPACE::vsscanf("Not an integer", "%d", vlist); | ||
}; | ||
EXPECT_EQ(fn(0, &result), 0); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should also add
vsscanf
toentrypoints.txt
for the relevant platforms