From: Andrei Zmievski Date: Fri, 19 Aug 2005 22:00:21 +0000 (+0000) Subject: Unicode support for ord() and chr(). X-Git-Tag: PRE_NEW_OCI8_EXTENSION~141 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=23086f5f1cac52080def5e69181f69219fda1292;p=php Unicode support for ord() and chr(). --- diff --git a/ext/standard/string.c b/ext/standard/string.c index 1b8d6bfe49..2dc60ddbb8 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -2835,8 +2835,8 @@ PHP_FUNCTION(quotemeta) } /* }}} */ -/* {{{ proto int ord(string character) - Returns ASCII value of character */ +/* {{{ proto int ord(text character) + Returns the codepoint value of a character */ PHP_FUNCTION(ord) { zval **str; @@ -2844,14 +2844,18 @@ PHP_FUNCTION(ord) if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_string_ex(str); + convert_to_text_ex(str); - RETURN_LONG((unsigned char) Z_STRVAL_PP(str)[0]); + if (Z_TYPE_PP(str) == IS_UNICODE) { + RETURN_LONG(zend_get_codepoint_at(Z_USTRVAL_PP(str), Z_USTRLEN_PP(str), 0)); + } else { + RETURN_LONG((unsigned char) Z_STRVAL_PP(str)[0]); + } } /* }}} */ -/* {{{ proto string chr(int ascii) - Converts ASCII code to a character */ +/* {{{ proto text chr(int codepoint) + Converts a codepoint number to a character */ PHP_FUNCTION(chr) { zval **num; @@ -2862,10 +2866,22 @@ PHP_FUNCTION(chr) } convert_to_long_ex(num); - temp[0] = (char) Z_LVAL_PP(num); - temp[1] = 0; + if (UG(unicode)) { + UChar buf[2]; + int32_t buf_len; - RETVAL_STRINGL(temp, 1, 1); + if (Z_LVAL_PP(num) > UCHAR_MAX_VALUE) { + php_error(E_WARNING, "Codepoint value cannot be greater than %X", UCHAR_MAX_VALUE); + return; + } + buf_len = zend_codepoint_to_uchar((uint32_t)Z_LVAL_PP(num), buf); + RETURN_UNICODEL(buf, buf_len, 1); + } else { + temp[0] = (char) Z_LVAL_PP(num); + temp[1] = 0; + + RETVAL_STRINGL(temp, 1, 1); + } } /* }}} */