Skip to content

Commit 1ff15cf

Browse files
authored
moving ctype functions to runtime-common (#1454)
1 parent 9d3d3cc commit 1ff15cf

6 files changed

Lines changed: 183 additions & 104 deletions

File tree

builtin-functions/kphp-light/stdlib/system-functions.txt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
<?php
22

3+
function ctype_alnum(mixed $text): bool;
4+
5+
function ctype_alpha(mixed $text): bool;
6+
7+
function ctype_cntrl(mixed $text): bool;
8+
9+
function ctype_digit(mixed $text): bool;
10+
11+
function ctype_graph(mixed $text): bool;
12+
13+
function ctype_lower(mixed $text): bool;
14+
15+
function ctype_print(mixed $text): bool;
16+
17+
function ctype_punct(mixed $text): bool;
18+
19+
function ctype_space(mixed $text): bool;
20+
21+
function ctype_upper(mixed $text): bool;
22+
23+
function ctype_xdigit(mixed $text): bool;
24+
325
function php_uname($mode ::: string = "a"): string;
426

527
function posix_getpid(): int;
@@ -49,10 +71,3 @@ function raise_sigsegv () ::: void;
4971

5072
/** @kphp-extern-func-info stub */
5173
function system($command ::: string, int &$result_code = 0): int;
52-
53-
/** @kphp-extern-func-info stub generation-required */
54-
function ctype_alnum(mixed $text): bool;
55-
/** @kphp-extern-func-info stub generation-required */
56-
function ctype_digit(mixed $text): bool;
57-
/** @kphp-extern-func-info stub generation-required */
58-
function ctype_xdigit(mixed $text): bool;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Compiler for PHP (aka KPHP)
2+
// Copyright (c) 2025 LLC «V Kontakte»
3+
// Distributed under the GPL v3 License, see LICENSE.notice.txt
4+
5+
#pragma once
6+
7+
#include "runtime-common/core/runtime-core.h"
8+
9+
#include <algorithm>
10+
#include <cctype>
11+
#include <cstdint>
12+
13+
namespace ctype_impl_ {
14+
inline bool ctype_impl(const mixed& text, int (*iswhat)(int), bool allow_digits, bool allow_minus) noexcept {
15+
if (text.is_string()) {
16+
const string& str = text.as_string();
17+
if (str.empty()) {
18+
return false;
19+
}
20+
return std::all_of(str.c_str(), str.c_str() + str.size(), [iswhat](uint8_t c) noexcept { return iswhat(c); });
21+
}
22+
23+
if (text.is_int()) {
24+
const int64_t i = text.as_int();
25+
if (i <= 255 && i >= 0) {
26+
return iswhat(i);
27+
} else if (i >= -128 && i < 0) {
28+
return iswhat(i + 256);
29+
} else if (i >= 0) {
30+
return allow_digits;
31+
} else {
32+
return allow_minus;
33+
}
34+
}
35+
36+
return false;
37+
}
38+
} // namespace ctype_impl_
39+
40+
inline bool f$ctype_alnum(const mixed& text) noexcept {
41+
return ctype_impl_::ctype_impl(text, std::isalnum, true, false);
42+
}
43+
44+
inline bool f$ctype_alpha(const mixed& text) noexcept {
45+
return ctype_impl_::ctype_impl(text, std::isalpha, false, false);
46+
}
47+
48+
inline bool f$ctype_cntrl(const mixed& text) noexcept {
49+
return ctype_impl_::ctype_impl(text, std::iscntrl, false, false);
50+
}
51+
52+
inline bool f$ctype_digit(const mixed& text) noexcept {
53+
return ctype_impl_::ctype_impl(text, std::isdigit, true, false);
54+
}
55+
56+
inline bool f$ctype_graph(const mixed& text) noexcept {
57+
return ctype_impl_::ctype_impl(text, std::isgraph, true, true);
58+
}
59+
60+
inline bool f$ctype_lower(const mixed& text) noexcept {
61+
return ctype_impl_::ctype_impl(text, std::islower, false, false);
62+
}
63+
64+
inline bool f$ctype_print(const mixed& text) noexcept {
65+
return ctype_impl_::ctype_impl(text, std::isprint, true, true);
66+
}
67+
68+
inline bool f$ctype_punct(const mixed& text) noexcept {
69+
return ctype_impl_::ctype_impl(text, std::ispunct, false, false);
70+
}
71+
72+
inline bool f$ctype_space(const mixed& text) noexcept {
73+
return ctype_impl_::ctype_impl(text, std::isspace, false, false);
74+
}
75+
76+
inline bool f$ctype_upper(const mixed& text) noexcept {
77+
return ctype_impl_::ctype_impl(text, std::isupper, false, false);
78+
}
79+
80+
inline bool f$ctype_xdigit(const mixed& text) noexcept {
81+
return ctype_impl_::ctype_impl(text, std::isxdigit, true, false);
82+
}

runtime/ctype.cpp

Lines changed: 0 additions & 77 deletions
This file was deleted.

runtime/ctype.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

runtime/runtime.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/
6969
confdata-global-manager.cpp
7070
confdata-keys.cpp
7171
critical_section.cpp
72-
ctype.cpp
7372
curl.cpp
7473
curl-async.cpp
7574
env.cpp
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
@ok
2+
<?php
3+
4+
var_dump(ctype_alnum("abc123"));
5+
var_dump(ctype_alnum("123"));
6+
var_dump(ctype_alnum("abc"));
7+
var_dump(ctype_alnum("abc 123"));
8+
var_dump(ctype_alnum("abc!"));
9+
var_dump(ctype_alnum(""));
10+
11+
var_dump(ctype_alpha("abcDEF"));
12+
var_dump(ctype_alpha("abc"));
13+
var_dump(ctype_alpha("ABC"));
14+
var_dump(ctype_alpha("abc123"));
15+
var_dump(ctype_alpha("abc!"));
16+
var_dump(ctype_alpha(""));
17+
18+
var_dump(ctype_digit("123456"));
19+
var_dump(ctype_digit("0"));
20+
var_dump(ctype_digit("123abc"));
21+
var_dump(ctype_digit("12.34"));
22+
var_dump(ctype_digit(""));
23+
var_dump(ctype_digit(" "));
24+
25+
var_dump(ctype_lower("abcdef"));
26+
var_dump(ctype_lower("abc"));
27+
var_dump(ctype_lower("abc123"));
28+
var_dump(ctype_lower("abcDef"));
29+
var_dump(ctype_lower("ABC"));
30+
var_dump(ctype_lower(""));
31+
32+
var_dump(ctype_upper("ABCDEF"));
33+
var_dump(ctype_upper("ABC"));
34+
var_dump(ctype_upper("ABC123"));
35+
var_dump(ctype_upper("abc"));
36+
var_dump(ctype_upper("AbC"));
37+
var_dump(ctype_upper(""));
38+
39+
var_dump(ctype_space(" "));
40+
var_dump(ctype_space("\t"));
41+
var_dump(ctype_space("\n"));
42+
var_dump(ctype_space(" \n\t"));
43+
var_dump(ctype_space("abc"));
44+
var_dump(ctype_space(""));
45+
46+
var_dump(ctype_xdigit("0123456789abcdef"));
47+
var_dump(ctype_xdigit("ABCDEF"));
48+
var_dump(ctype_xdigit("123G"));
49+
var_dump(ctype_xdigit("xyz"));
50+
var_dump(ctype_xdigit(""));
51+
var_dump(ctype_xdigit("0FfA"));
52+
53+
var_dump(ctype_cntrl("\n"));
54+
var_dump(ctype_cntrl("\r\t"));
55+
var_dump(ctype_cntrl("\0"));
56+
var_dump(ctype_cntrl("abc"));
57+
var_dump(ctype_cntrl(" "));
58+
var_dump(ctype_cntrl(""));
59+
60+
var_dump(ctype_graph("abc123!@#"));
61+
var_dump(ctype_graph("!@#"));
62+
var_dump(ctype_graph("abc\n"));
63+
var_dump(ctype_graph(" "));
64+
var_dump(ctype_graph(""));
65+
var_dump(ctype_graph("abc"));
66+
67+
var_dump(ctype_print("abc123!@# "));
68+
var_dump(ctype_print(" "));
69+
var_dump(ctype_print("abc\n"));
70+
var_dump(ctype_print("\t"));
71+
var_dump(ctype_print(""));
72+
var_dump(ctype_print("abc!"));
73+
74+
var_dump(ctype_punct("!@#$%^&*()"));
75+
var_dump(ctype_punct(".,;:!?"));
76+
var_dump(ctype_punct("abc!"));
77+
var_dump(ctype_punct(" "));
78+
var_dump(ctype_punct(""));
79+
var_dump(ctype_punct("!@#abc"));

0 commit comments

Comments
 (0)