php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
msgformat_parse.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/* {{{ */
28static void msgfmt_do_parse(MessageFormatter_object *mfo, char *source, size_t src_len, zval *return_value)
29{
30 zval *fargs;
31 int count = 0;
32 int i;
33 UChar *usource = NULL;
34 int usrc_len = 0;
35
36 intl_convert_utf8_to_utf16(&usource, &usrc_len, source, src_len, &INTL_DATA_ERROR_CODE(mfo));
37 INTL_METHOD_CHECK_STATUS(mfo, "Converting parse string failed");
38
39 umsg_parse_helper(MSG_FORMAT_OBJECT(mfo), &count, &fargs, usource, usrc_len, &INTL_DATA_ERROR_CODE(mfo));
40 if (usource) {
41 efree(usource);
42 }
43 INTL_METHOD_CHECK_STATUS(mfo, "Parsing failed");
44
46 for(i=0;i<count;i++) {
47 add_next_index_zval(return_value, &fargs[i]);
48 }
49 efree(fargs);
50}
51/* }}} */
52
53/* {{{ Parse a message */
55{
56 char *source;
57 size_t source_len;
59
60
61 /* Parse parameters. */
63 &object, MessageFormatter_ce_ptr, &source, &source_len ) == FAILURE )
64 {
66 }
67
68 /* Fetch the object. */
70
71 msgfmt_do_parse(mfo, source, source_len, return_value);
72}
73/* }}} */
74
75/* {{{ Parse a message. */
77{
78 UChar *spattern = NULL;
79 int spattern_len = 0;
80 char *pattern = NULL;
81 size_t pattern_len = 0;
82 char *slocale = NULL;
83 size_t slocale_len = 0;
84 char *source = NULL;
85 size_t src_len = 0;
87 MessageFormatter_object *mfo = &mf;
88
90 Z_PARAM_STRING(slocale, slocale_len)
91 Z_PARAM_STRING(pattern, pattern_len)
92 Z_PARAM_STRING(source, src_len)
94
95 INTL_CHECK_LOCALE_LEN(slocale_len);
96 memset(mfo, 0, sizeof(*mfo));
98
99 if(pattern && pattern_len) {
100 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
101 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
102 {
104 "msgfmt_parse_message: error converting pattern to UTF-16", 0 );
106 }
107 } else {
108 spattern_len = 0;
109 spattern = NULL;
110 }
111
112 if(slocale_len == 0) {
113 slocale = (char *)intl_locale_get_default();
114 }
115
116#ifdef MSG_FORMAT_QUOTE_APOS
117 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
119 "msgfmt_parse_message: error converting pattern to quote-friendly format", 0 );
121 }
122#endif
123
124 /* Create an ICU message formatter. */
125 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
126 if(spattern && spattern_len) {
127 efree(spattern);
128 }
129 INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
130
131 msgfmt_do_parse(mfo, source, src_len, return_value);
132
133 /* drop the temporary formatter */
135}
136/* }}} */
count(Countable|array $value, int $mode=COUNT_NORMAL)
const U_INVALID_FORMAT_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_CHECK_STATUS(obj, msg)
Definition intl_data.h:66
#define INTL_CHECK_LOCALE_LEN(locale_len)
Definition intl_data.h:116
void intl_error_set(intl_error *err, UErrorCode code, const char *msg, int copyMsg)
Definition intl_error.c:161
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_parse_helper(UMessageFormat *fmt, int *count, zval **args, UChar *source, int source_len, UErrorCode *status)
#define PHP_FUNCTION
Definition php.h:364
const char * intl_locale_get_default(void)
Definition php_intl.c:91
msgfmt_parse_message(string $locale, string $pattern, string $message)
msgfmt_parse(MessageFormatter $formatter, string $string)
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 ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define Z_PARAM_STRING(dest, dest_len)
Definition zend_API.h:2071
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define RETURN_THROWS()
Definition zend_API.h:1060
#define getThis()
Definition zend_API.h:526
#define array_init(arg)
Definition zend_API.h:537
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
@ FAILURE
Definition zend_types.h:61
zval * return_value