]> granicus.if.org Git - php/commitdiff
Add normalizer_get_raw_decomposition function
authorPaul Crovella <paul.crovella@gmail.com>
Wed, 21 Mar 2018 00:33:23 +0000 (17:33 -0700)
committerJoe Watkins <krakjoe@php.net>
Thu, 22 Mar 2018 22:27:39 +0000 (23:27 +0100)
Implements #76111 https://bugs.php.net/bug.php?id=76111

ext/intl/normalizer/normalizer_class.c
ext/intl/normalizer/normalizer_normalize.c
ext/intl/normalizer/normalizer_normalize.h
ext/intl/php_intl.c
ext/intl/tests/normalizer_get_raw_decomposition.phpt [new file with mode: 0644]
ext/intl/tests/ut_common.inc

index 87b274ebfc128cf4063b1f6ec12fb547faa04d29..95b738ef0734417f572517a1275904fbfee30447 100644 (file)
@@ -34,6 +34,10 @@ ZEND_BEGIN_ARG_INFO_EX( normalizer_args, 0, 0, 1 )
        ZEND_ARG_INFO( 0, form )
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX( decomposition_args, 0, 0, 1 )
+       ZEND_ARG_INFO( 0, input )
+ZEND_END_ARG_INFO();
+
 /* }}} */
 
 /* {{{ Normalizer_class_functions
@@ -43,6 +47,7 @@ ZEND_END_ARG_INFO()
 static const zend_function_entry Normalizer_class_functions[] = {
        ZEND_FENTRY( normalize, ZEND_FN( normalizer_normalize ), normalizer_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
        ZEND_FENTRY( isNormalized, ZEND_FN( normalizer_is_normalized ), normalizer_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
+       ZEND_FENTRY( getRawDecomposition, ZEND_FN( normalizer_get_raw_decomposition ), decomposition_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
        PHP_FE_END
 };
 /* }}} */
index 52780acdbcad678340b3f15342397be3ec42cc3c..ad031de7d41b3ace9dafe44e6c295ac23050ba54 100644 (file)
@@ -247,6 +247,52 @@ PHP_FUNCTION( normalizer_is_normalized )
 }
 /* }}} */
 
+/* {{{ proto string|null Normalizer::getRawDecomposition( string $input )
+ * Returns the Decomposition_Mapping property for the given UTF-8 encoded code point. }}} */
+/* {{{ proto string|null normalizer_get_raw_decomposition( string $input )
+ * Returns the Decomposition_Mapping property for the given UTF-8 encoded code point.
+ */
+PHP_FUNCTION( normalizer_get_raw_decomposition )
+{
+       char* input = NULL;
+       size_t input_length = 0;
+
+       UChar32 codepoint = -1;
+       int32_t offset = 0;
+
+    UErrorCode status = U_ZERO_ERROR;
+    const UNormalizer2 *nfkc = unorm2_getNFKCInstance(&status);
+    UChar decomposition[32];
+    int32_t decomposition_length;
+
+       intl_error_reset(NULL);
+
+       if ((zend_parse_parameters(ZEND_NUM_ARGS(), "s", &input, &input_length) == FAILURE)) {
+               return;
+       }
+
+       U8_NEXT(input, offset, input_length, codepoint);
+       if ((size_t)offset != input_length) {
+               intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
+               intl_error_set_custom_msg(NULL, "Input string must be exactly one UTF-8 encoded code point long.", 0);
+               return;
+       }
+
+       if ((codepoint < UCHAR_MIN_VALUE) || (codepoint > UCHAR_MAX_VALUE)) {
+               intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
+               intl_error_set_custom_msg(NULL, "Code point out of range", 0);
+               return;
+       }
+       
+       decomposition_length = unorm2_getRawDecomposition(nfkc, codepoint, decomposition, 32, &status);
+       if (decomposition_length == -1) {
+               RETURN_NULL();
+       }
+
+       RETVAL_NEW_STR(intl_convert_utf16_to_utf8(decomposition, decomposition_length, &status));
+}
+/* }}} */
+
 /*
  * Local variables:
  * tab-width: 4
index c282c567950035b6760eb797cb8378d99cfe808d..f8d032177735584442080377e593581030c7bb20 100644 (file)
@@ -21,5 +21,6 @@
 
 PHP_FUNCTION( normalizer_normalize );
 PHP_FUNCTION( normalizer_is_normalized );
+PHP_FUNCTION( normalizer_get_raw_decomposition );
 
 #endif // NORMALIZER_NORMALIZE_H
index 7b2f206e5218e4653006eb73fb8143931cc590db..0afc96b08198408fd84012119c2185fc3c698c2e 100644 (file)
@@ -206,6 +206,10 @@ ZEND_BEGIN_ARG_INFO_EX(normalizer_args, 0, 0, 1)
        ZEND_ARG_INFO(0, form)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(decomposition_args, 0, 0, 1)
+       ZEND_ARG_INFO(0, input)
+ZEND_END_ARG_INFO();
+
 ZEND_BEGIN_ARG_INFO_EX(grapheme_1_arg, 0, 0, 1)
        ZEND_ARG_INFO(0, string)
 ZEND_END_ARG_INFO()
@@ -662,6 +666,7 @@ static const zend_function_entry intl_functions[] = {
        /* normalizer functions */
        PHP_FE( normalizer_normalize, normalizer_args )
        PHP_FE( normalizer_is_normalized, normalizer_args )
+       PHP_FE( normalizer_get_raw_decomposition, decomposition_args )
 
        /* Locale functions */
        PHP_NAMED_FE( locale_get_default, zif_locale_get_default, locale_0_args )
diff --git a/ext/intl/tests/normalizer_get_raw_decomposition.phpt b/ext/intl/tests/normalizer_get_raw_decomposition.phpt
new file mode 100644 (file)
index 0000000..69eb013
--- /dev/null
@@ -0,0 +1,67 @@
+--TEST--
+normalizer_get_raw_decomposition()
+--SKIPIF--
+<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
+--FILE--
+<?php
+
+/*
+ * Try getting raw decomposition mappings
+ * with procedural and class methods.
+ */
+
+function ut_main()
+{
+       $result = '';
+       $strings = [
+               "a",
+        "\u{FFDA}",
+        "\u{FDFA}",
+        "",
+        "aa",
+        "\xF5",
+       ];
+    
+    foreach ($strings as $string) {
+        $decomposition = ut_norm_get_raw_decomposition($string);
+        $error_code = intl_get_error_code();
+        $error_message = intl_get_error_message();
+        
+        $string_hex = bin2hex($string);
+        $result .= "---------------------\n";
+        
+        if ($decomposition === null) {
+            $result .= "'$string_hex' has no decomposition mapping\n" ;
+        } else {
+            $result .= "'$string_hex' has the decomposition mapping '" . bin2hex($decomposition) . "'\n" ;
+        }
+        $result .= "error info: '$error_message' ($error_code)\n";
+    }
+
+       return $result;
+}
+
+include_once( 'ut_common.inc' );
+ut_run();
+
+?>
+--EXPECT--
+---------------------
+'61' has no decomposition mapping
+error info: 'U_ZERO_ERROR' (0)
+---------------------
+'efbf9a' has the decomposition mapping 'e385a1'
+error info: 'U_ZERO_ERROR' (0)
+---------------------
+'efb7ba' has the decomposition mapping 'd8b5d984d98920d8a7d984d984d98720d8b9d984d98ad98720d988d8b3d984d985'
+error info: 'U_ZERO_ERROR' (0)
+---------------------
+'' has no decomposition mapping
+error info: 'Input string must be exactly one UTF-8 encoded code point long.: U_ILLEGAL_ARGUMENT_ERROR' (1)
+---------------------
+'6161' has no decomposition mapping
+error info: 'Input string must be exactly one UTF-8 encoded code point long.: U_ILLEGAL_ARGUMENT_ERROR' (1)
+---------------------
+'f5' has no decomposition mapping
+error info: 'Code point out of range: U_ILLEGAL_ARGUMENT_ERROR' (1)
+
index 1b82cfa56c34bdd8521ed43da3eb8c666f182861..7ca3bc687ab9e2753aa7468459ef276214a13c06 100644 (file)
@@ -219,6 +219,10 @@ function ut_norm_is_normalized( $str, $form )
 {
     return $GLOBALS['oo-mode'] ? Normalizer::isNormalized( $str, $form ) : normalizer_is_normalized( $str, $form );
 }
+function ut_norm_get_raw_decomposition( $str )
+{
+    return $GLOBALS['oo-mode'] ? Normalizer::getRawDecomposition( $str ) : normalizer_get_raw_decomposition( $str );
+}
 
 /*
  * Wrappers around Collator methods to run them in either OO- or procedural mode.