summaryrefslogtreecommitdiff
path: root/ext/intl/normalizer/normalizer_normalize.c
diff options
context:
space:
mode:
authorAnatol Belski <ab@php.net>2018-04-06 10:10:17 +0200
committerAnatol Belski <ab@php.net>2018-04-06 10:12:44 +0200
commit36014ad23f3fe1018371e66a0bbf9244ad34d721 (patch)
tree7d1ed133dec1ba4891e23254f9621e414d198742 /ext/intl/normalizer/normalizer_normalize.c
parentef79b3ae0c79cb4abecbc42bbb2540d5507e9ac1 (diff)
downloadphp-git-36014ad23f3fe1018371e66a0bbf9244ad34d721.tar.gz
Replace the deprecated API by the newer one available with ICU 56+
Diffstat (limited to 'ext/intl/normalizer/normalizer_normalize.c')
-rw-r--r--ext/intl/normalizer/normalizer_normalize.c83
1 files changed, 82 insertions, 1 deletions
diff --git a/ext/intl/normalizer/normalizer_normalize.c b/ext/intl/normalizer/normalizer_normalize.c
index c0ab8341d4..e39b2ca73f 100644
--- a/ext/intl/normalizer/normalizer_normalize.c
+++ b/ext/intl/normalizer/normalizer_normalize.c
@@ -19,7 +19,12 @@
#endif
#include "php_intl.h"
+#if U_ICU_VERSION_MAJOR_NUM < 56
#include "unicode/unorm.h"
+#endif
+#if U_ICU_VERSION_MAJOR_NUM >= 49
+#include <unicode/unorm2.h>
+#endif
#include "normalizer.h"
#include "normalizer_class.h"
#include "normalizer_normalize.h"
@@ -28,6 +33,70 @@
#include <unicode/utf8.h>
#endif
+
+#if U_ICU_VERSION_MAJOR_NUM >= 49
+static const UNormalizer2 *intl_get_normalizer(zend_long form, UErrorCode *err)
+{/*{{{*/
+ switch (form)
+ {
+ case NORMALIZER_FORM_C:
+ return unorm2_getNFCInstance(err);
+ break;
+ case NORMALIZER_FORM_D:
+ return unorm2_getNFDInstance(err);
+ break;
+ case NORMALIZER_FORM_KC:
+ return unorm2_getNFKCInstance(err);
+ break;
+ case NORMALIZER_FORM_KD:
+ return unorm2_getNFKDInstance(err);
+ break;
+ }
+
+ *err = U_ILLEGAL_ARGUMENT_ERROR;
+ return NULL;
+}/*}}}*/
+
+static int32_t intl_normalize(zend_long form, const UChar *src, int32_t src_len, UChar *dst, int32_t dst_len, UErrorCode *err)
+{/*{{{*/
+ const UNormalizer2 *norm;
+
+ /* Mimic the behavior of ICU < 56. */
+ if (UNEXPECTED(NORMALIZER_NONE == form)) {
+ /* FIXME This is a noop which should be removed somewhen after PHP 7.3.*/
+ zend_error(E_DEPRECATED, "Normalizer::NONE is obsolete with ICU 56 and above and will be removed in later PHP versions");
+
+ if (dst_len >= src_len) {
+ memmove(dst, src, sizeof(UChar) * src_len);
+ dst[src_len] = '\0';
+ *err = U_ZERO_ERROR;
+ return src_len;
+ }
+
+ *err = U_BUFFER_OVERFLOW_ERROR;
+ return -1;
+ }
+
+ norm = intl_get_normalizer(form, err);
+ if(U_FAILURE(*err)) {
+ return -1;
+ }
+
+ return unorm2_normalize(norm, src, src_len, dst, dst_len, err);
+}/*}}}*/
+
+static UBool intl_is_normalized(zend_long form, const UChar *uinput, int32_t uinput_len, UErrorCode *err)
+{/*{{{*/
+ const UNormalizer2 *norm = intl_get_normalizer(form, err);
+
+ if(U_FAILURE(*err)) {
+ return FALSE;
+ }
+
+ return unorm2_isNormalized(norm, uinput, uinput_len, err);
+}/*}}}*/
+#endif
+
/* {{{ proto string Normalizer::normalize( string $input [, string $form = FORM_C] )
* Normalize a string. }}} */
/* {{{ proto string normalizer_normalize( string $input [, string $form = FORM_C] )
@@ -110,7 +179,11 @@ PHP_FUNCTION( normalizer_normalize )
uret_buf = eumalloc( uret_len + 1 );
/* normalize */
+#if U_ICU_VERSION_MAJOR_NUM < 56
size_needed = unorm_normalize( uinput, uinput_len, form, (int32_t) 0 /* options */, uret_buf, uret_len, &status);
+#else
+ size_needed = intl_normalize(form, uinput, uinput_len, uret_buf, uret_len, &status);
+#endif
/* Bail out if an unexpected error occurred.
* (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
@@ -133,7 +206,11 @@ PHP_FUNCTION( normalizer_normalize )
status = U_ZERO_ERROR;
/* try normalize again */
+#if U_ICU_VERSION_MAJOR_NUM < 56
size_needed = unorm_normalize( uinput, uinput_len, form, (int32_t) 0 /* options */, uret_buf, uret_len, &status);
+#else
+ size_needed = intl_normalize(form, uinput, uinput_len, uret_buf, uret_len, &status);
+#endif
/* Bail out if an unexpected error occurred. */
if( U_FAILURE(status) ) {
@@ -167,7 +244,7 @@ PHP_FUNCTION( normalizer_normalize )
/* {{{ proto bool Normalizer::isNormalized( string $input [, string $form = FORM_C] )
* Test if a string is in a given normalization form. }}} */
-/* {{{ proto bool normalizer_is_normalize( string $input [, string $form = FORM_C] )
+/* {{{ proto bool normalizer_is_normalized( string $input [, string $form = FORM_C] )
* Test if a string is in a given normalization form.
*/
PHP_FUNCTION( normalizer_is_normalized )
@@ -232,7 +309,11 @@ PHP_FUNCTION( normalizer_is_normalized )
/* test string */
+#if U_ICU_VERSION_MAJOR_NUM < 56
uret = unorm_isNormalizedWithOptions( uinput, uinput_len, form, (int32_t) 0 /* options */, &status);
+#else
+ uret = intl_is_normalized(form, uinput, uinput_len, &status);
+#endif
efree( uinput );