From e438dd3c9b406ba36fbd98c25514b4a335f21b41 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Tue, 16 Feb 2021 12:50:48 +0330 Subject: [PATCH] LibC: Teach scanf how to read "%lu" and "%llu" (unsigned long{, long}) This makes the gcc port work again. --- Userland/Libraries/LibC/scanf.cpp | 38 +++++++++++++++++++++++++++++++ Userland/Tests/LibC/scanf.cpp | 5 ++++ 2 files changed, 43 insertions(+) diff --git a/Userland/Libraries/LibC/scanf.cpp b/Userland/Libraries/LibC/scanf.cpp index e1b30f6f69..bb51aad92d 100644 --- a/Userland/Libraries/LibC/scanf.cpp +++ b/Userland/Libraries/LibC/scanf.cpp @@ -163,6 +163,40 @@ struct read_element_concrete { } }; +template +struct read_element_concrete { + bool operator()(GenericLexer& lexer, va_list* ap) + { + lexer.ignore_while(isspace); + + auto* ptr = va_arg(*ap, ApT*); + unsigned long long value = 0; + char* endptr = nullptr; + auto nptr = lexer.remaining().characters_without_null_termination(); + if constexpr (kind == ReadKind::Normal) + value = strtoull(nptr, &endptr, 10); + if constexpr (kind == ReadKind::Octal) + value = strtoull(nptr, &endptr, 8); + if constexpr (kind == ReadKind::Hex) + value = strtoull(nptr, &endptr, 16); + if constexpr (kind == ReadKind::Infer) + value = strtoull(nptr, &endptr, 0); + + if (!endptr) + return false; + + if (endptr == nptr) + return false; + + auto diff = endptr - nptr; + ASSERT(diff > 0); + lexer.ignore((size_t)diff); + + *ptr = value; + return true; + } +}; + template struct read_element_concrete { bool operator()(GenericLexer& lexer, va_list* ap) @@ -211,12 +245,16 @@ struct read_element { case Long: if constexpr (IsSame::value) return read_element_concrete {}(input_lexer, ap); + if constexpr (IsSame::value) + return read_element_concrete {}(input_lexer, ap); if constexpr (IsSame::value) return read_element_concrete {}(input_lexer, ap); return false; case LongLong: if constexpr (IsSame::value) return read_element_concrete {}(input_lexer, ap); + if constexpr (IsSame::value) + return read_element_concrete {}(input_lexer, ap); if constexpr (IsSame::value) return read_element_concrete {}(input_lexer, ap); return false; diff --git a/Userland/Tests/LibC/scanf.cpp b/Userland/Tests/LibC/scanf.cpp index 4da0d92856..da358ddac2 100644 --- a/Userland/Tests/LibC/scanf.cpp +++ b/Userland/Tests/LibC/scanf.cpp @@ -31,6 +31,7 @@ typedef long double longdouble; typedef long long longlong; +typedef unsigned long unsignedlong; typedef char charstar[32]; template @@ -143,6 +144,7 @@ DECL_WITH_TYPE(longlong); DECL_WITH_TYPE(float); DECL_WITH_TYPE(double); DECL_WITH_TYPE(longdouble); +DECL_WITH_TYPE(unsignedlong); #undef DECL_WITH_TYPE @@ -179,6 +181,9 @@ const TestSuite test_suites[] { { "%u.%u.%u", "3.19", 2, 3, { unsignedarg0, unsignedarg1, unsignedarg2 }, { to_value_t(3u), to_value_t(19u) } }, // Failing test case from previous impl: { "SSH-%d.%d-%[^\n]\n", "SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1\n", 3, 3, { intarg0, intarg1, charstararg0 }, { to_value_t(2), to_value_t(0), str_to_value_t("OpenSSH_8.2p1 Ubuntu-4ubuntu0.1") } }, + // GCC failure tests + { "%d.%d.%d", "10.2.0", 3, 3, { intarg0, intarg1, intarg2 }, { to_value_t(10), to_value_t(2), to_value_t(0) } }, + { "%lu", "3054 ", 1, 1, { unsignedlongarg0 }, { to_value_t(3054ul) } }, }; bool g_any_failed = false;