diff options
author | Shmuel Zeigerman <solomuz0@gmail.com> | 2012-02-13 23:38:07 +0200 |
---|---|---|
committer | Shmuel Zeigerman <solomuz0@gmail.com> | 2012-02-13 23:38:07 +0200 |
commit | e98c08314819bc5f609889087b329d0b78180d41 (patch) | |
tree | 1661e5542d7f85beab292966bc99611107ee225e /src | |
parent | 94b08ae226ca1e2ccb4c88ab51d2679b858d646e (diff) | |
download | lrexlib-e98c08314819bc5f609889087b329d0b78180d41.tar.gz |
TRE binding: add wide-character functions.
Diffstat (limited to 'src')
-rw-r--r-- | src/common.c | 46 | ||||
-rw-r--r-- | src/common.h | 3 | ||||
-rw-r--r-- | src/tre/Makefile | 1 | ||||
-rw-r--r-- | src/tre/ltre_w.c | 45 |
4 files changed, 47 insertions, 48 deletions
diff --git a/src/common.c b/src/common.c index 09d595c..0b444c0 100644 --- a/src/common.c +++ b/src/common.c @@ -166,7 +166,7 @@ void buffer_addvalue (TBuffer *buf, int stackpos) { buffer_addlstring (buf, p, len); } -static void bufferZ_addlstring (TBuffer *buf, const void *src, size_t len) { +void bufferZ_addlstring (TBuffer *buf, const void *src, size_t len) { int n; size_t header[2] = { ID_STRING }; header[1] = len; @@ -176,7 +176,7 @@ static void bufferZ_addlstring (TBuffer *buf, const void *src, size_t len) { if (n) buffer_addlstring (buf, NULL, N_ALIGN - n); } -static void bufferZ_addnum (TBuffer *buf, size_t num) { +void bufferZ_addnum (TBuffer *buf, size_t num) { size_t header[2] = { ID_NUMBER }; header[1] = num; buffer_addlstring (buf, header, sizeof (header)); @@ -220,47 +220,6 @@ void bufferZ_putrepstring (TBuffer *BufRep, int reppos, int nsub) { } } -#ifdef WIDE_CHAR_GSUB -/* 1. When called repeatedly on the same TBuffer, its existing data - is discarded and overwritten by the new data. - 2. The TBuffer's array is never shrunk by this function. -*/ -void bufferZ_putrepstringW (TBuffer *BufRep, int reppos, int nsub) { - wchar_t dbuf[] = { 0, 0 }; - size_t replen; - const wchar_t *p = (const wchar_t*) lua_tolstring (BufRep->L, reppos, &replen); - replen /= sizeof(wchar_t); - const wchar_t *end = p + replen; - BufRep->top = 0; - while (p < end) { - const wchar_t *q; - for (q = p; q < end && *q != L'%'; ++q) - {} - if (q != p) - bufferZ_addlstring (BufRep, p, (q - p) * sizeof(wchar_t)); - if (q < end) { - if (++q < end) { /* skip % */ - if (iswdigit (*q)) { - int num; - *dbuf = *q; - num = wcstol (dbuf, NULL, 10); - if (num == 1 && nsub == 0) - num = 0; - else if (num > nsub) { - freelist_free (BufRep->freelist); - luaL_error (BufRep->L, "invalid capture index"); - } - bufferZ_addnum (BufRep, num); - } - else bufferZ_addlstring (BufRep, q, 1 * sizeof(wchar_t)); - } - p = q + 1; - } - else break; - } -} -#endif - /****************************************************************************** The intended use of this function is as follows: size_t iter = 0; @@ -287,4 +246,3 @@ int bufferZ_next (TBuffer *buf, size_t *iter, size_t *num, const char **str) { } return 0; } - diff --git a/src/common.h b/src/common.h index 04ef7cb..95aa79d 100644 --- a/src/common.h +++ b/src/common.h @@ -83,8 +83,9 @@ void buffer_addvalue (TBuffer *buf, int stackpos); void buffer_pushresult (TBuffer *buf); void bufferZ_putrepstring (TBuffer *buf, int reppos, int nsub); -void bufferZ_putrepstringW (TBuffer *buf, int reppos, int nsub); int bufferZ_next (TBuffer *buf, size_t *iter, size_t *len, const char **str); +void bufferZ_addlstring (TBuffer *buf, const void *src, size_t len); +void bufferZ_addnum (TBuffer *buf, size_t num); int get_int_field (lua_State *L, const char* field); void set_int_field (lua_State *L, const char* field, int val); diff --git a/src/tre/Makefile b/src/tre/Makefile index 2350108..2d3964e 100644 --- a/src/tre/Makefile +++ b/src/tre/Makefile @@ -26,7 +26,6 @@ REGNAME = tre # === END OF USER SETTINGS === OBJ = ltre.o ltre_w.o ../common.o -MYCFLAGS = -DWIDE_CHAR_GSUB include ../common.mak diff --git a/src/tre/ltre_w.c b/src/tre/ltre_w.c index 7f8f87b..d3bceb7 100644 --- a/src/tre/ltre_w.c +++ b/src/tre/ltre_w.c @@ -10,6 +10,8 @@ #include <tre/tre.h> +void bufferZ_putrepstringW (TBuffer *BufRep, int reppos, int nsub); + /* These 2 settings may be redefined from the command-line or the makefile. * They should be kept in sync between themselves and with the target name. */ @@ -132,8 +134,8 @@ static int generic_atfind (lua_State *L, int tfind) { res_match.pmatch = ud->match; /* execute the search */ - res = tre_reganexec (&ud->r, argE.text, argE.textlen - argE.startoffset, - &res_match, argP, argE.eflags); + res = tre_regawnexec (&ud->r, (const wchar_t*)argE.text, + (argE.textlen - argE.startoffset)/ALG_CHARSIZE, &res_match, argP, argE.eflags); if (ALG_ISMATCH (res)) { ALG_PUSHOFFSETS (L, ud, argE.startoffset, 0); if (tfind) @@ -218,3 +220,42 @@ void add_wide_lib (lua_State *L, int methods) { luaL_register(L, NULL, methods ? posixmeta : rexlib); } + +/* 1. When called repeatedly on the same TBuffer, its existing data + is discarded and overwritten by the new data. + 2. The TBuffer's array is never shrunk by this function. +*/ +void bufferZ_putrepstringW (TBuffer *BufRep, int reppos, int nsub) { + wchar_t dbuf[] = { 0, 0 }; + size_t replen; + const wchar_t *p = (const wchar_t*) lua_tolstring (BufRep->L, reppos, &replen); + replen /= sizeof(wchar_t); + const wchar_t *end = p + replen; + BufRep->top = 0; + while (p < end) { + const wchar_t *q; + for (q = p; q < end && *q != L'%'; ++q) + {} + if (q != p) + bufferZ_addlstring (BufRep, p, (q - p) * sizeof(wchar_t)); + if (q < end) { + if (++q < end) { /* skip % */ + if (iswdigit (*q)) { + int num; + *dbuf = *q; + num = wcstol (dbuf, NULL, 10); + if (num == 1 && nsub == 0) + num = 0; + else if (num > nsub) { + freelist_free (BufRep->freelist); + luaL_error (BufRep->L, "invalid capture index"); + } + bufferZ_addnum (BufRep, num); + } + else bufferZ_addlstring (BufRep, q, 1 * sizeof(wchar_t)); + } + p = q + 1; + } + else break; + } +} |