php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
msgformat_format.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
21#include "php_intl.h"
22#include "msgformat_class.h"
23#include "msgformat_data.h"
24#include "msgformat_helpers.h"
25#include "intl_convert.h"
26
27#ifndef Z_ADDREF_P
28#define Z_ADDREF_P(z) ((z)->refcount++)
29#endif
30
31/* {{{ */
32static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value)
33{
34 UChar* formatted = NULL;
35 int32_t formatted_len = 0;
36
37 umsg_format_helper(mfo, Z_ARRVAL_P(args), &formatted, &formatted_len);
38
39 if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
40 if (formatted) {
41 efree(formatted);
42 }
44 } else {
45 INTL_METHOD_RETVAL_UTF8(mfo, formatted, formatted_len, 1);
46 }
47}
48/* }}} */
49
50/* {{{ Format a message. */
52{
53 zval *args;
55
56
57 /* Parse parameters. */
59 &object, MessageFormatter_ce_ptr, &args ) == FAILURE )
60 {
62 }
63
64 /* Fetch the object. */
66
67 msgfmt_do_format(mfo, args, return_value);
68}
69/* }}} */
70
71/* {{{ Format a message. */
73{
74 zval *args;
75 UChar *spattern = NULL;
76 int spattern_len = 0;
77 char *pattern = NULL;
78 size_t pattern_len = 0;
79 const char *slocale = NULL;
80 size_t slocale_len = 0;
82 MessageFormatter_object *mfo = &mf;
83 UParseError parse_error;
84
85 /* Parse parameters. */
87 &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
88 {
90 }
91
92 INTL_CHECK_LOCALE_LEN(slocale_len);
93
94 memset(mfo, 0, sizeof(*mfo));
96
97 if(pattern && pattern_len) {
98 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
99 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
100 {
102 "msgfmt_format_message: error converting pattern to UTF-16", 0 );
104 }
105 } else {
106 spattern_len = 0;
107 spattern = NULL;
108 }
109
110 if(slocale_len == 0) {
111 slocale = intl_locale_get_default();
112 }
113
114#ifdef MSG_FORMAT_QUOTE_APOS
115 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
116 intl_error_set(/* intl_error* */ NULL, U_INVALID_FORMAT_ERROR,
117 "msgfmt_format_message: error converting pattern to quote-friendly format", 0 );
119 }
120#endif
121
122 /* Create an ICU message formatter. */
123 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
124 if(spattern && spattern_len) {
125 efree(spattern);
126 }
127
128 /* Cannot use INTL_METHOD_CHECK_STATUS() as we need to free the message object formatter */
129 if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
131 char *msg = NULL;
132 smart_str parse_error_str;
133 parse_error_str = intl_parse_error_to_string( &parse_error );
134 spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
135 smart_str_free( &parse_error_str );
136
137 /* Pass NULL to intl_error* parameter to store message in global Intl error msg stack */
138 intl_error_set_code(/* intl_error* */ NULL, INTL_DATA_ERROR_CODE( mfo ) );
139 intl_errors_set_custom_msg(/* intl_error* */ NULL, msg, 1 );
140
141 efree( msg );
142 } else {
143 intl_errors_set_custom_msg(/* intl_error* */ NULL, "Creating message formatter failed", 0 );
144 }
145 umsg_close(MSG_FORMAT_OBJECT(mfo));
147 }
148
149 msgfmt_do_format(mfo, args, return_value);
150
151 /* drop the temporary formatter */
153}
154/* }}} */
const U_INVALID_FORMAT_ERROR
const U_PATTERN_SYNTAX_ERROR
const U_ILLEGAL_ARGUMENT_ERROR
memset(ptr, 0, type->size)
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
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_CODE(obj)
Definition intl_data.h:40
#define INTL_METHOD_RETVAL_UTF8(obj, ustring, ulen, free_it)
Definition intl_data.h:103
#define INTL_CHECK_LOCALE_LEN(locale_len)
Definition intl_data.h:116
void intl_errors_set_custom_msg(intl_error *err, const char *msg, int copyMsg)
Definition intl_error.c:187
void intl_error_set(intl_error *err, UErrorCode code, const char *msg, int copyMsg)
Definition intl_error.c:161
void intl_error_set_code(intl_error *err, UErrorCode err_code)
Definition intl_error.c:141
smart_str intl_parse_error_to_string(UParseError *pe)
Definition intl_error.c:206
zend_class_entry * MessageFormatter_ce_ptr
#define MSG_FORMAT_METHOD_INIT_VARS
#define MSG_FORMAT_METHOD_FETCH_OBJECT
#define MSG_FORMAT_OBJECT(mfo)
void msgformat_data_free(msgformat_data *mf_data)
void msgformat_data_init(msgformat_data *mf_data)
void umsg_format_helper(MessageFormatter_object *mfo, HashTable *args, UChar **formatted, int *formatted_len)
#define PHP_FUNCTION
Definition php.h:364
const char * intl_locale_get_default(void)
Definition php_intl.c:91
msgfmt_format(MessageFormatter $formatter, array $values)
msgfmt_format_message(string $locale, string $pattern, array $values)
char * msg
Definition phpdbg.h:289
#define spprintf
Definition spprintf.h:29
zend_string * s
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_FALSE
Definition zend_API.h:1058
#define RETURN_THROWS()
Definition zend_API.h:1060
#define getThis()
Definition zend_API.h:526
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
zval * args
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
@ FAILURE
Definition zend_types.h:61
zval * return_value