php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
formatter_main.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | https://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Stanislav Malyshev <stas@zend.com> |
12 +----------------------------------------------------------------------+
13 */
14
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
19#include <unicode/ustring.h>
20#include <unicode/uloc.h>
21
22#include "php_intl.h"
23#include "formatter_class.h"
24#include "intl_convert.h"
25
26/* {{{ */
27static int numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
28{
29 char* locale;
30 char* pattern = NULL;
31 size_t locale_len = 0, pattern_len = 0;
32 zend_long style;
33 UChar* spattern = NULL;
34 int32_t spattern_len = 0;
36
38 Z_PARAM_STRING(locale, locale_len)
39 Z_PARAM_LONG(style)
41 Z_PARAM_STRING_OR_NULL(pattern, pattern_len)
43
44 if (error_handling != NULL) {
46 *error_handling_replaced = 1;
47 }
48
50 object = return_value;
52 if (FORMATTER_OBJECT(nfo)) {
53 zend_throw_error(NULL, "NumberFormatter object is already constructed");
54 return FAILURE;
55 }
56
57 /* Convert pattern (if specified) to UTF-16. */
58 if(pattern && pattern_len) {
59 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(nfo));
60 INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16");
61 }
62
63 if(locale_len == 0) {
64 locale = (char *)intl_locale_get_default();
65 }
66
67 if (strlen(uloc_getISO3Language(locale)) == 0) {
68 zend_argument_value_error(1, "\"%s\" is invalid", locale);
69 return FAILURE;
70 }
71
72 /* Create an ICU number formatter. */
73 FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
74
75 if(spattern) {
76 efree(spattern);
77 }
78
79 INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed");
80 return SUCCESS;
81}
82/* }}} */
83
84/* {{{ Create number formatter. */
93/* }}} */
94
95/* {{{ NumberFormatter object constructor. */
97{
98 zend_error_handling error_handling;
99 bool error_handling_replaced = 0;
100
102 if (numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
103 if (!EG(exception)) {
104 zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
105 }
106 }
107 if (error_handling_replaced) {
108 zend_restore_error_handling(&error_handling);
109 }
110}
111/* }}} */
112
113/* {{{ Get formatter's last error code. */
115{
117
118 /* Parse parameters. */
120 &object, NumberFormatter_ce_ptr ) == FAILURE )
121 {
123 }
124
125 nfo = Z_INTL_NUMBERFORMATTER_P(object);
126
127 /* Return formatter's last error code. */
129}
130/* }}} */
131
132/* {{{ Get text description for formatter's last error code. */
134{
135 zend_string *message = NULL;
137
138 /* Parse parameters. */
140 &object, NumberFormatter_ce_ptr ) == FAILURE )
141 {
143 }
144
145 nfo = Z_INTL_NUMBERFORMATTER_P(object);
146
147 /* Return last error message. */
149 RETURN_STR(message);
150}
151/* }}} */
bool exception
Definition assert.c:30
zend_class_entry * NumberFormatter_ce_ptr
#define Z_INTL_NUMBERFORMATTER_P(zv)
#define FORMATTER_METHOD_INIT_VARS
#define FORMATTER_METHOD_FETCH_OBJECT_NO_CHECK
#define FORMATTER_OBJECT(nfo)
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
zend_class_entry * IntlException_ce_ptr
Definition intl_error.c:30
void intl_convert_utf8_to_utf16(UChar **target, int32_t *target_len, const char *src, size_t src_len, UErrorCode *status)
#define INTL_DATA_ERROR_P(obj)
Definition intl_data.h:39
#define INTL_CTOR_CHECK_STATUS(obj, msg)
Definition intl_data.h:95
#define INTL_DATA_ERROR_CODE(obj)
Definition intl_data.h:40
#define INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len)
Definition intl_data.h:125
zend_string * intl_error_get_message(intl_error *err)
Definition intl_error.c:116
#define PHP_FUNCTION
Definition php.h:364
#define PHP_METHOD
Definition php.h:365
const char * intl_locale_get_default(void)
Definition php_intl.c:91
numfmt_get_error_code(NumberFormatter $formatter)
numfmt_create(string $locale, int $style, ?string $pattern=null)
numfmt_get_error_message(NumberFormatter $formatter)
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
#define INTERNAL_FUNCTION_PARAMETERS
Definition zend.h:49
@ EH_THROW
Definition zend.h:433
ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current)
Definition zend_API.c:5242
ZEND_API void zend_restore_error_handling(zend_error_handling *saved)
Definition zend_API.c:5253
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
Definition zend.h:50
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type)
Definition zend_API.c:1849
ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec,...)
Definition zend_API.c:1314
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_NULL()
Definition zend_API.h:1036
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define Z_PARAM_STRING(dest, dest_len)
Definition zend_API.h:2071
#define ZEND_PARSE_PARAMETERS_END_EX(failure)
Definition zend_API.h:1630
#define Z_PARAM_STRING_OR_NULL(dest, dest_len)
Definition zend_API.h:2074
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETURN_STR(s)
Definition zend_API.h:1039
#define ZEND_THIS
Definition zend_API.h:523
#define getThis()
Definition zend_API.h:526
#define efree(ptr)
Definition zend_alloc.h:155
strlen(string $string)
ZEND_API ZEND_COLD zend_object * zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code)
#define EG(v)
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
@ FAILURE
Definition zend_types.h:61
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval * return_value