Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 22 additions & 7 deletions builtin-functions/kphp-light/stdlib/system-functions.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
<?php

function ctype_alnum(mixed $text): bool;

function ctype_alpha(mixed $text): bool;

function ctype_cntrl(mixed $text): bool;

function ctype_digit(mixed $text): bool;

function ctype_graph(mixed $text): bool;

function ctype_lower(mixed $text): bool;

function ctype_print(mixed $text): bool;

function ctype_punct(mixed $text): bool;

function ctype_space(mixed $text): bool;

function ctype_upper(mixed $text): bool;

function ctype_xdigit(mixed $text): bool;

function php_uname($mode ::: string = "a"): string;

function posix_getpid(): int;
Expand Down Expand Up @@ -49,10 +71,3 @@ function raise_sigsegv () ::: void;

/** @kphp-extern-func-info stub */
function system($command ::: string, int &$result_code = 0): int;

/** @kphp-extern-func-info stub generation-required */
function ctype_alnum(mixed $text): bool;
/** @kphp-extern-func-info stub generation-required */
function ctype_digit(mixed $text): bool;
/** @kphp-extern-func-info stub generation-required */
function ctype_xdigit(mixed $text): bool;
82 changes: 82 additions & 0 deletions runtime-common/stdlib/string/ctype-functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include "runtime-common/core/runtime-core.h"

#include <algorithm>
#include <cctype>
#include <cstdint>

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);
}
77 changes: 0 additions & 77 deletions runtime/ctype.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions runtime/ctype.h

This file was deleted.

1 change: 0 additions & 1 deletion runtime/runtime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions tests/phpt/string_functions/012_ctype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@ok
<?php

var_dump(ctype_alnum("abc123"));
var_dump(ctype_alnum("123"));
var_dump(ctype_alnum("abc"));
var_dump(ctype_alnum("abc 123"));
var_dump(ctype_alnum("abc!"));
var_dump(ctype_alnum(""));

var_dump(ctype_alpha("abcDEF"));
var_dump(ctype_alpha("abc"));
var_dump(ctype_alpha("ABC"));
var_dump(ctype_alpha("abc123"));
var_dump(ctype_alpha("abc!"));
var_dump(ctype_alpha(""));

var_dump(ctype_digit("123456"));
var_dump(ctype_digit("0"));
var_dump(ctype_digit("123abc"));
var_dump(ctype_digit("12.34"));
var_dump(ctype_digit(""));
var_dump(ctype_digit(" "));

var_dump(ctype_lower("abcdef"));
var_dump(ctype_lower("abc"));
var_dump(ctype_lower("abc123"));
var_dump(ctype_lower("abcDef"));
var_dump(ctype_lower("ABC"));
var_dump(ctype_lower(""));

var_dump(ctype_upper("ABCDEF"));
var_dump(ctype_upper("ABC"));
var_dump(ctype_upper("ABC123"));
var_dump(ctype_upper("abc"));
var_dump(ctype_upper("AbC"));
var_dump(ctype_upper(""));

var_dump(ctype_space(" "));
var_dump(ctype_space("\t"));
var_dump(ctype_space("\n"));
var_dump(ctype_space(" \n\t"));
var_dump(ctype_space("abc"));
var_dump(ctype_space(""));

var_dump(ctype_xdigit("0123456789abcdef"));
var_dump(ctype_xdigit("ABCDEF"));
var_dump(ctype_xdigit("123G"));
var_dump(ctype_xdigit("xyz"));
var_dump(ctype_xdigit(""));
var_dump(ctype_xdigit("0FfA"));

var_dump(ctype_cntrl("\n"));
var_dump(ctype_cntrl("\r\t"));
var_dump(ctype_cntrl("\0"));
var_dump(ctype_cntrl("abc"));
var_dump(ctype_cntrl(" "));
var_dump(ctype_cntrl(""));

var_dump(ctype_graph("abc123!@#"));
var_dump(ctype_graph("!@#"));
var_dump(ctype_graph("abc\n"));
var_dump(ctype_graph(" "));
var_dump(ctype_graph(""));
var_dump(ctype_graph("abc"));

var_dump(ctype_print("abc123!@# "));
var_dump(ctype_print(" "));
var_dump(ctype_print("abc\n"));
var_dump(ctype_print("\t"));
var_dump(ctype_print(""));
var_dump(ctype_print("abc!"));

var_dump(ctype_punct("!@#$%^&*()"));
var_dump(ctype_punct(".,;:!?"));
var_dump(ctype_punct("abc!"));
var_dump(ctype_punct(" "));
var_dump(ctype_punct(""));
var_dump(ctype_punct("!@#abc"));
Loading