MaybeMutable the collection builtins
[hiphop-php.git] / hphp / runtime / ext / ctype / ext_ctype.cpp
blobe35805e0ffecc48a51194845f777c6b327b04cf7
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1997-2010 The PHP Group |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 3.01 of the PHP license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.php.net/license/3_01.txt |
12 | If you did not receive a copy of the PHP license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@php.net so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #include "hphp/runtime/ext/extension.h"
20 namespace HPHP {
22 ///////////////////////////////////////////////////////////////////////////////
24 static bool ctype(const Variant& v, int (*iswhat)(int)) {
25 if (v.isInteger()) {
26 int64_t n = v.toInt64();
27 if (n <= 255 && n >= 0) {
28 return iswhat(n);
31 if (n >= -128 && n < 0) {
32 return iswhat(n + 256);
35 return ctype(v.toString(), iswhat);
38 if (v.isString()) {
39 String s = v.toString();
40 if (!s.empty()) {
41 const char *p = s.data();
42 const char *e = s.data() + s.size();
43 while (p < e) {
44 if (!iswhat((int)*(unsigned char *)(p++))) {
45 return false;
48 return true;
51 return false;
54 // Use lambdas wrapping the ctype.h functions because of linker weirdness on
55 // OS X Mavericks.
57 bool HHVM_FUNCTION(ctype_alnum, const Variant& text) {
58 return ctype(text, [] (int i) -> int { return isalnum(i); });
61 bool HHVM_FUNCTION(ctype_alpha, const Variant& text) {
62 return ctype(text, [] (int i) -> int { return isalpha(i); });
65 bool HHVM_FUNCTION(ctype_cntrl, const Variant& text) {
66 return ctype(text, [] (int i) -> int { return iscntrl(i); });
69 bool HHVM_FUNCTION(ctype_digit, const Variant& text) {
70 return ctype(text, [] (int i) -> int { return isdigit(i); });
73 bool HHVM_FUNCTION(ctype_graph, const Variant& text) {
74 return ctype(text, [] (int i) -> int { return isgraph(i); });
77 bool HHVM_FUNCTION(ctype_lower, const Variant& text) {
78 return ctype(text, [] (int i) -> int { return islower(i); });
81 bool HHVM_FUNCTION(ctype_print, const Variant& text) {
82 return ctype(text, [] (int i) -> int { return isprint(i); });
85 bool HHVM_FUNCTION(ctype_punct, const Variant& text) {
86 return ctype(text, [] (int i) -> int { return ispunct(i); });
89 bool HHVM_FUNCTION(ctype_space, const Variant& text) {
90 return ctype(text, [] (int i) -> int { return isspace(i); });
93 bool HHVM_FUNCTION(ctype_upper, const Variant& text) {
94 return ctype(text, [] (int i) -> int { return isupper(i); });
97 bool HHVM_FUNCTION(ctype_xdigit, const Variant& text) {
98 return ctype(text, [] (int i) -> int { return isxdigit(i); });
101 ///////////////////////////////////////////////////////////////////////////////
103 struct CtypeExtension final : Extension {
104 CtypeExtension() : Extension("ctype") {}
105 void moduleInit() override {
106 HHVM_FE(ctype_alnum);
107 HHVM_FE(ctype_alpha);
108 HHVM_FE(ctype_cntrl);
109 HHVM_FE(ctype_digit);
110 HHVM_FE(ctype_graph);
111 HHVM_FE(ctype_lower);
112 HHVM_FE(ctype_print);
113 HHVM_FE(ctype_punct);
114 HHVM_FE(ctype_space);
115 HHVM_FE(ctype_upper);
116 HHVM_FE(ctype_xdigit);
118 loadSystemlib();
120 } s_ctype_extension;
122 ///////////////////////////////////////////////////////////////////////////////