Skip to content

Commit 6b82192

Browse files
committed
Added locale support.
1 parent 72f675b commit 6b82192

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

ext/pcre/php_pcre.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ static int _php_free_pcre_cache(void *data)
7777
{
7878
pcre_cache_entry *pce = (pcre_cache_entry *) data;
7979
pefree(pce->re, 1);
80+
#if HAVE_SETLOCALE
81+
pefree((void*)pce->tables, 1);
82+
#endif
8083
return 1;
8184
}
8285

@@ -163,6 +166,10 @@ static pcre* _pcre_get_compiled_regex(char *regex, pcre_extra *extra, int *preg_
163166
int regex_len;
164167
int do_study = 0;
165168
int poptions = 0;
169+
unsigned const char *tables = NULL;
170+
#if HAVE_SETLOCALE
171+
char *locale = setlocale(LC_CTYPE, NULL);
172+
#endif
166173
pcre_cache_entry *pce;
167174
pcre_cache_entry new_entry;
168175
PCRE_LS_FETCH();
@@ -171,9 +178,15 @@ static pcre* _pcre_get_compiled_regex(char *regex, pcre_extra *extra, int *preg_
171178
back the compiled pattern, otherwise go on and compile it. */
172179
regex_len = strlen(regex);
173180
if (zend_hash_find(&PCRE_G(pcre_cache), regex, regex_len+1, (void **)&pce) == SUCCESS) {
174-
extra = pce->extra;
175-
*preg_options = pce->preg_options;
176-
return pce->re;
181+
#if HAVE_SETLOCALE
182+
if (!strcmp(pce->locale, locale)) {
183+
#endif
184+
extra = pce->extra;
185+
*preg_options = pce->preg_options;
186+
return pce->re;
187+
#if HAVE_SETLOCALE
188+
}
189+
#endif
177190
}
178191

179192
p = regex;
@@ -247,13 +260,18 @@ static pcre* _pcre_get_compiled_regex(char *regex, pcre_extra *extra, int *preg_
247260
return NULL;
248261
}
249262
}
263+
264+
#if HAVE_SETLOCALE
265+
if (strcmp(locale, "C"))
266+
tables = pcre_maketables();
267+
#endif
250268

251269
/* Compile pattern and display a warning if compilation failed. */
252270
re = pcre_compile(pattern,
253271
coptions,
254272
&error,
255273
&erroffset,
256-
NULL);
274+
tables);
257275

258276
if (re == NULL) {
259277
zend_error(E_WARNING, "Compilation failed: %s at offset %d\n", error, erroffset);
@@ -278,6 +296,10 @@ static pcre* _pcre_get_compiled_regex(char *regex, pcre_extra *extra, int *preg_
278296
new_entry.re = re;
279297
new_entry.extra = extra;
280298
new_entry.preg_options = poptions;
299+
#if HAVE_SETLOCALE
300+
new_entry.locale = locale;
301+
new_entry.tables = tables;
302+
#endif
281303
zend_hash_update(&PCRE_G(pcre_cache), regex, regex_len+1, (void *)&new_entry,
282304
sizeof(pcre_cache_entry), NULL);
283305

ext/pcre/php_pcre.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#if HAVE_PCRE
3636

3737
#include "pcrelib/pcre.h"
38+
#if HAVE_LOCALE_H
39+
#include <locale.h>
40+
#endif
3841

3942
extern void php_info_pcre(ZEND_MODULE_INFO_FUNC_ARGS);
4043
extern int php_minit_pcre(INIT_FUNC_ARGS);
@@ -55,6 +58,10 @@ typedef struct {
5558
pcre *re;
5659
pcre_extra *extra;
5760
int preg_options;
61+
#if HAVE_SETLOCALE
62+
char *locale;
63+
unsigned const char *tables;
64+
#endif
5865
} pcre_cache_entry;
5966

6067
typedef struct {

0 commit comments

Comments
 (0)