From: Andrei Zmievski Date: Thu, 4 May 2006 21:22:17 +0000 (+0000) Subject: Add a global ASCII connverter, macros to return single codepoints X-Git-Tag: BEFORE_NEW_OUTPUT_API~308 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=534cad706424e252377e732e8979904a8cebe83f;p=php Add a global ASCII connverter, macros to return single codepoints (UCHAR32), and zend_unicode_to_ascii() function. --- diff --git a/Zend/zend.c b/Zend/zend.c index 8e172fb521..38cb08d469 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -42,10 +42,8 @@ # define GLOBAL_CONSTANTS_TABLE EG(zend_constants) #endif -#ifndef __GNUC__ ZEND_API zstr null_zstr; ZEND_API zstr empty_zstr; -#endif #if defined(ZEND_WIN32) && ZEND_DEBUG BOOL WINAPI IsDebuggerPresent(VOID); @@ -906,6 +904,7 @@ static void unicode_globals_ctor(zend_unicode_globals *unicode_globals TSRMLS_DC { unicode_globals->unicode = 0; unicode_globals->utf8_conv = NULL; + unicode_globals->ascii_conv = NULL; unicode_globals->fallback_encoding_conv = NULL; unicode_globals->runtime_encoding_conv = NULL; unicode_globals->output_encoding_conv = NULL; @@ -913,6 +912,8 @@ static void unicode_globals_ctor(zend_unicode_globals *unicode_globals TSRMLS_DC unicode_globals->http_input_encoding_conv = NULL; unicode_globals->filesystem_encoding_conv = NULL; zend_set_converter_encoding(&unicode_globals->utf8_conv, "UTF-8"); + zend_set_converter_encoding(&unicode_globals->ascii_conv, "US-ASCII"); + zend_set_converter_error_mode(unicode_globals->ascii_conv, ZEND_FROM_UNICODE, ZEND_CONV_ERROR_STOP); unicode_globals->from_error_mode = ZEND_CONV_ERROR_SUBST; memset(unicode_globals->from_subst_char, 0, 3 * sizeof(UChar)); zend_codepoint_to_uchar(0x3f, unicode_globals->from_subst_char); @@ -941,6 +942,9 @@ static void unicode_globals_dtor(zend_unicode_globals *unicode_globals TSRMLS_DC if (unicode_globals->utf8_conv) { ucnv_close(unicode_globals->utf8_conv); } + if (unicode_globals->ascii_conv) { + ucnv_close(unicode_globals->ascii_conv); + } zend_hash_destroy(&unicode_globals->flex_compatible); } diff --git a/Zend/zend.h b/Zend/zend.h index 6046f614a2..ebd5f6a116 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -246,11 +246,6 @@ typedef union _zstr { void *v; } zstr; -#ifdef __GNUC__ -# define ZSTR(x) ((zstr)(x)) -# define NULL_ZSTR ZSTR((void*)NULL) -# define EMPTY_ZSTR ZSTR("\0\0") -#else extern ZEND_API zstr null_zstr; extern ZEND_API zstr empty_zstr; @@ -263,7 +258,6 @@ static inline zstr _to_zstr(void *v) { # define ZSTR(x) _to_zstr(x) # define NULL_ZSTR null_zstr # define EMPTY_ZSTR empty_zstr -#endif #define EMPTY_STR ((UChar*)"\0\0") diff --git a/Zend/zend_API.h b/Zend/zend_API.h index 6c71fa290e..89c49a1179 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -807,6 +807,12 @@ END_EXTERN_C() Z_TYPE_P(z) = IS_UNICODE; \ } +#define ZVAL_UCHAR32(z, ch) { \ + UChar buf[3]; \ + int buf_len = zend_codepoint_to_uchar(ch, buf); \ + ZVAL_UNICODEL(z, buf, buf_len, 1); \ +} + #define ZVAL_EMPTY_STRING(z) { \ Z_STRLEN_P(z) = 0; \ Z_STRVAL_P(z) = STR_EMPTY_ALLOC(); \ @@ -881,6 +887,7 @@ END_EXTERN_C() #define RETVAL_EMPTY_STRING() ZVAL_EMPTY_STRING(return_value) #define RETVAL_UNICODE(u, duplicate) ZVAL_UNICODE(return_value, u, duplicate) #define RETVAL_UNICODEL(u, l, duplicate) ZVAL_UNICODEL(return_value, u, l, duplicate) +#define RETVAL_UCHAR32(ch) ZVAL_UCHAR32(return_value, ch) #define RETVAL_EMPTY_UNICODE() ZVAL_EMPTY_UNICODE(return_value) #define RETVAL_ZVAL(zv, copy, dtor) ZVAL_ZVAL(return_value, zv, copy, dtor) #define RETVAL_FALSE ZVAL_BOOL(return_value, 0) @@ -899,6 +906,7 @@ END_EXTERN_C() #define RETURN_EMPTY_STRING() { RETVAL_EMPTY_STRING(); return; } #define RETURN_UNICODE(u, duplicate) { RETVAL_UNICODE(u, duplicate); return; } #define RETURN_UNICODEL(u, l, duplicate) { RETVAL_UNICODEL(u, l, duplicate); return; } +#define RETURN_UCHAR32(ch) { RETVAL_UCHAR32(ch); return; } #define RETURN_EMPTY_UNICODE() { RETVAL_EMPTY_UNICODE(); return; } #define RETURN_ZVAL(zv, copy, dtor) { RETVAL_ZVAL(zv, copy, dtor); return; } #define RETURN_FALSE { RETVAL_FALSE; return; } diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index e4bdef54a0..537ea34408 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -301,6 +301,7 @@ struct _zend_unicode_globals { UConverter *http_input_encoding_conv;/* http input encoding converter */ UConverter *filesystem_encoding_conv;/* default filesystem converter (entries, not contents) */ UConverter *utf8_conv; /* all-purpose UTF-8 converter */ + UConverter *ascii_conv; /* all-purpose ASCII converter */ uint16_t from_error_mode; UChar from_subst_char[3]; diff --git a/Zend/zend_unicode.c b/Zend/zend_unicode.c index ffa7ee254d..f5ef2d1468 100644 --- a/Zend/zend_unicode.c +++ b/Zend/zend_unicode.c @@ -373,6 +373,23 @@ ZEND_API void zend_convert_encodings(UConverter *target_conv, UConverter *source } /* }}} */ +/* {{{ zend_unicode_to_ascii */ +ZEND_API char* zend_unicode_to_ascii(const UChar *us, int us_len TSRMLS_DC) +{ + char *cs; + int cs_len; + UErrorCode status = U_ZERO_ERROR; + + zend_convert_from_unicode(UG(ascii_conv), &cs, &cs_len, us, us_len, &status); + if (U_FAILURE(status)) { + efree(cs); + return NULL; + } + + return cs; +} +/* }}} */ + /* {{{ zend_raise_conversion_error_ex */ ZEND_API void zend_raise_conversion_error_ex(char *message, UConverter *conv, zend_conv_direction dir, int error_char_offset, int use_exception TSRMLS_DC) { diff --git a/Zend/zend_unicode.h b/Zend/zend_unicode.h index 9423dfa7d2..46a2023a82 100644 --- a/Zend/zend_unicode.h +++ b/Zend/zend_unicode.h @@ -73,6 +73,8 @@ void zend_collator_destroy(zend_collator *zcoll); ZEND_API int zend_convert_to_unicode(UConverter *conv, UChar **target, int *target_len, const char *source, int source_len, UErrorCode *status); ZEND_API int zend_convert_from_unicode(UConverter *conv, char **target, int *target_len, const UChar *source, int source_len, UErrorCode *status); ZEND_API void zend_convert_encodings(UConverter *target_conv, UConverter *source_conv, char **target, int *target_len, const char *source, int source_len, UErrorCode *status); +ZEND_API char* zend_unicode_to_ascii(const UChar *us, int us_len TSRMLS_DC); + ZEND_API int zval_string_to_unicode_ex(zval *string, UConverter *conv TSRMLS_DC); ZEND_API int zval_string_to_unicode(zval *string TSRMLS_DC); ZEND_API int zval_unicode_to_string(zval *string, UConverter *conv TSRMLS_DC);