diff --git a/builtin-functions/kphp-light/stdlib/system-functions.txt b/builtin-functions/kphp-light/stdlib/system-functions.txt index 3ec1bcec1e..4c7e9c5fa6 100644 --- a/builtin-functions/kphp-light/stdlib/system-functions.txt +++ b/builtin-functions/kphp-light/stdlib/system-functions.txt @@ -1,5 +1,27 @@ +#include +#include + +namespace ctype_impl_ { +inline bool ctype_impl(const mixed& text, int (*iswhat)(int), bool allow_digits, bool allow_minus) noexcept { + if (text.is_string()) { + const string& str = text.as_string(); + if (str.empty()) { + return false; + } + return std::all_of(str.c_str(), str.c_str() + str.size(), [iswhat](uint8_t c) noexcept { return iswhat(c); }); + } + + if (text.is_int()) { + const int64_t i = text.as_int(); + if (i <= 255 && i >= 0) { + return iswhat(i); + } else if (i >= -128 && i < 0) { + return iswhat(i + 256); + } else if (i >= 0) { + return allow_digits; + } else { + return allow_minus; + } + } + + return false; +} +} // namespace ctype_impl_ + +inline bool f$ctype_alnum(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isalnum, true, false); +} + +inline bool f$ctype_alpha(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isalpha, false, false); +} + +inline bool f$ctype_cntrl(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::iscntrl, false, false); +} + +inline bool f$ctype_digit(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isdigit, true, false); +} + +inline bool f$ctype_graph(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isgraph, true, true); +} + +inline bool f$ctype_lower(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::islower, false, false); +} + +inline bool f$ctype_print(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isprint, true, true); +} + +inline bool f$ctype_punct(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::ispunct, false, false); +} + +inline bool f$ctype_space(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isspace, false, false); +} + +inline bool f$ctype_upper(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isupper, false, false); +} + +inline bool f$ctype_xdigit(const mixed& text) noexcept { + return ctype_impl_::ctype_impl(text, std::isxdigit, true, false); +} diff --git a/runtime/ctype.cpp b/runtime/ctype.cpp deleted file mode 100644 index 6932d35c9b..0000000000 --- a/runtime/ctype.cpp +++ /dev/null @@ -1,77 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2020 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime/ctype.h" - -#include -#include - -static bool ctype_impl(const mixed& text, int (*iswhat)(int), bool allow_digits, bool allow_minus) noexcept { - if (text.is_string()) { - const string& str = text.as_string(); - if (str.empty()) { - return false; - } - return std::all_of(str.c_str(), str.c_str() + str.size(), [iswhat](std::uint8_t c) { return iswhat(c); }); - } - - if (text.is_int()) { - const int64_t i = text.as_int(); - if (i <= 255 && i >= 0) { - return iswhat(i); - } else if (i >= -128 && i < 0) { - return iswhat(i + 256); - } else if (i >= 0) { - return allow_digits; - } else { - return allow_minus; - } - } - - return false; -} - -bool f$ctype_alnum(const mixed& text) noexcept { - return ctype_impl(text, std::isalnum, true, false); -} - -bool f$ctype_alpha(const mixed& text) noexcept { - return ctype_impl(text, std::isalpha, false, false); -} - -bool f$ctype_cntrl(const mixed& text) noexcept { - return ctype_impl(text, std::iscntrl, false, false); -} - -bool f$ctype_digit(const mixed& text) noexcept { - return ctype_impl(text, std::isdigit, true, false); -} - -bool f$ctype_graph(const mixed& text) noexcept { - return ctype_impl(text, std::isgraph, true, true); -} - -bool f$ctype_lower(const mixed& text) noexcept { - return ctype_impl(text, std::islower, false, false); -} - -bool f$ctype_print(const mixed& text) noexcept { - return ctype_impl(text, std::isprint, true, true); -} - -bool f$ctype_punct(const mixed& text) noexcept { - return ctype_impl(text, std::ispunct, false, false); -} - -bool f$ctype_space(const mixed& text) noexcept { - return ctype_impl(text, std::isspace, false, false); -} - -bool f$ctype_upper(const mixed& text) noexcept { - return ctype_impl(text, std::isupper, false, false); -} - -bool f$ctype_xdigit(const mixed& text) noexcept { - return ctype_impl(text, std::isxdigit, true, false); -} diff --git a/runtime/ctype.h b/runtime/ctype.h deleted file mode 100644 index a62dd67061..0000000000 --- a/runtime/ctype.h +++ /dev/null @@ -1,19 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2022 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#pragma once - -#include "runtime-common/core/runtime-core.h" - -bool f$ctype_alnum(const mixed& text) noexcept; -bool f$ctype_alpha(const mixed& text) noexcept; -bool f$ctype_cntrl(const mixed& text) noexcept; -bool f$ctype_digit(const mixed& text) noexcept; -bool f$ctype_graph(const mixed& text) noexcept; -bool f$ctype_lower(const mixed& text) noexcept; -bool f$ctype_print(const mixed& text) noexcept; -bool f$ctype_punct(const mixed& text) noexcept; -bool f$ctype_space(const mixed& text) noexcept; -bool f$ctype_upper(const mixed& text) noexcept; -bool f$ctype_xdigit(const mixed& text) noexcept; diff --git a/runtime/runtime.cmake b/runtime/runtime.cmake index 9c161d5864..29a08819de 100644 --- a/runtime/runtime.cmake +++ b/runtime/runtime.cmake @@ -70,7 +70,6 @@ prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/ confdata-global-manager.cpp confdata-keys.cpp critical_section.cpp - ctype.cpp curl.cpp curl-async.cpp env.cpp diff --git a/tests/phpt/string_functions/012_ctype.php b/tests/phpt/string_functions/012_ctype.php new file mode 100644 index 0000000000..2263126897 --- /dev/null +++ b/tests/phpt/string_functions/012_ctype.php @@ -0,0 +1,79 @@ +@ok +