php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
msgformat.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/umsg.h>
21
22#include "php_intl.h"
23#include "msgformat_class.h"
24#include "msgformat_data.h"
25#include "intl_convert.h"
26
27/* {{{ */
28static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
29{
30 char* locale;
31 char* pattern;
32 size_t locale_len = 0, pattern_len = 0;
33 UChar* spattern = NULL;
34 int spattern_len = 0;
35 zval* object;
37 UParseError parse_error;
39
40 object = return_value;
42 Z_PARAM_STRING(locale, locale_len)
43 Z_PARAM_STRING(pattern, pattern_len)
45
46 if (error_handling != NULL) {
48 *error_handling_replaced = 1;
49 }
50
53
54 /* Convert pattern (if specified) to UTF-16. */
55 if(pattern && pattern_len) {
56 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
57 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
58 } else {
59 spattern_len = 0;
60 spattern = NULL;
61 }
62
63 if(locale_len == 0) {
64 locale = (char *)intl_locale_get_default();
65 }
66
67#ifdef MSG_FORMAT_QUOTE_APOS
68 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
69 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
70 }
71#endif
72
73 if ((mfo)->mf_data.orig_format) {
75 }
76
77 (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
78 (mfo)->mf_data.orig_format_len = pattern_len;
79
80 /* Create an ICU message formatter. */
81 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
82
83 if(spattern) {
84 efree(spattern);
85 }
86
88 char *msg = NULL;
89 smart_str parse_error_str;
90 parse_error_str = intl_parse_error_to_string( &parse_error );
91 spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
92 smart_str_free( &parse_error_str );
93
96
97 efree( msg );
98 return FAILURE;
99 }
100
101 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
102 return SUCCESS;
103}
104/* }}} */
105
106/* {{{ Create formatter. */
115/* }}} */
116
117/* {{{ MessageFormatter object constructor. */
119{
120 zend_error_handling error_handling;
121 bool error_handling_replaced = 0;
122
124 if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
125 if (!EG(exception)) {
129 }
130 }
131 if (error_handling_replaced) {
132 zend_restore_error_handling(&error_handling);
133 }
134}
135/* }}} */
136
137/* {{{ Get formatter's last error code. */
139{
140 zval* object = NULL;
142
143 /* Parse parameters. */
145 &object, MessageFormatter_ce_ptr ) == FAILURE )
146 {
148 }
149
150 mfo = Z_INTL_MESSAGEFORMATTER_P( object );
151
152 /* Return formatter's last error code. */
154}
155/* }}} */
156
157/* {{{ Get text description for formatter's last error code. */
159{
160 zend_string* message = NULL;
161 zval* object = NULL;
163
164 /* Parse parameters. */
166 &object, MessageFormatter_ce_ptr ) == FAILURE )
167 {
169 }
170
171 mfo = Z_INTL_MESSAGEFORMATTER_P( object );
172
173 /* Return last error message. */
174 message = intl_error_get_message( &mfo->mf_data.error );
175 RETURN_STR(message);
176}
177/* }}} */
bool exception
Definition assert.c:30
const U_PATTERN_SYNTAX_ERROR
char * err
Definition ffi.c:3029
#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
void intl_errors_set_custom_msg(intl_error *err, const char *msg, int copyMsg)
Definition intl_error.c:187
void intl_error_reset(intl_error *err)
Definition intl_error.c:78
void intl_error_set_code(intl_error *err, UErrorCode err_code)
Definition intl_error.c:141
UErrorCode intl_error_get_code(intl_error *err)
Definition intl_error.c:151
smart_str intl_parse_error_to_string(UParseError *pe)
Definition intl_error.c:206
zend_string * intl_error_get_message(intl_error *err)
Definition intl_error.c:116
zend_class_entry * MessageFormatter_ce_ptr
#define MSG_FORMAT_OBJECT(mfo)
#define MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK
#define Z_INTL_MESSAGEFORMATTER_P(zv)
void msgformat_data_free(msgformat_data *mf_data)
#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
msgfmt_get_error_message(MessageFormatter $formatter)
msgfmt_get_error_code(MessageFormatter $formatter)
msgfmt_create(string $locale, string $pattern)
char * msg
Definition phpdbg.h:289
#define spprintf
Definition spprintf.h:29
intl_error error
zend_string * s
#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
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_NULL()
Definition zend_API.h:1036
#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 ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#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 estrndup(s, length)
Definition zend_alloc.h:165
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
zend_string_release_ex(func->internal_function.function_name, 0)
ZEND_API ZEND_COLD zend_object * zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code)
#define EG(v)
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
@ FAILURE
Definition zend_types.h:61
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval * return_value
object