From b7d076feabc0af446e09224b74a10b860386f6fe Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Tue, 15 Jul 2003 21:40:37 +0000 Subject: [PATCH] Add support for run-time regex options (REG_NOTBOL and REG_NOTEOL), and more text transformation functions. --- include/gib_regex.h | 1 + libs/gib/gib_builtin.c | 64 ++++++++++++++++++++++++++++++++++++++---- libs/gib/gib_regex.c | 12 ++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/include/gib_regex.h b/include/gib_regex.h index 140123065..2d5f19569 100644 --- a/include/gib_regex.h +++ b/include/gib_regex.h @@ -42,4 +42,5 @@ void GIB_Regex_Init (void); regex_t *GIB_Regex_Compile (const char *regex, int cflags); const char *GIB_Regex_Error (void); int GIB_Regex_Translate_Options (const char *opstr); +int GIB_Regex_Translate_Runtime_Options (const char *opstr); unsigned int GIB_Regex_Apply_Match (regmatch_t match[10], dstring_t *dstr, unsigned int ofs, const char *replace); diff --git a/libs/gib/gib_builtin.c b/libs/gib/gib_builtin.c index 7f3fc7b44..e0cfe180a 100644 --- a/libs/gib/gib_builtin.c +++ b/libs/gib/gib_builtin.c @@ -551,7 +551,7 @@ GIB_Regex_Match_f (void) REG_EXTENDED | GIB_Regex_Translate_Options (GIB_Argv (3))))) GIB_Error ("regex", "%s: %s", GIB_Argv (0), GIB_Regex_Error ()); - else if (regexec (reg, GIB_Argv (1), 0, 0, 0)) + else if (regexec (reg, GIB_Argv (1), 0, 0, GIB_Regex_Translate_Runtime_Options (GIB_Argv (3)))) GIB_Return ("0"); else GIB_Return ("1"); @@ -584,7 +584,7 @@ GIB_Regex_Replace_f (void) && match[0].rm_eo) ofs += GIB_Regex_Apply_Match (match, GIB_Argd (1), ofs, GIB_Argv (4)); - else if (!regexec (reg, GIB_Argv (1), 10, match, 0) && match[0].rm_eo) + else if (!regexec (reg, GIB_Argv (1), 10, match, GIB_Regex_Translate_Runtime_Options (GIB_Argv (3))) && match[0].rm_eo) GIB_Regex_Apply_Match (match, GIB_Argd (1), 0, GIB_Argv (4)); GIB_Return (GIB_Argv (1)); } @@ -610,7 +610,7 @@ GIB_Regex_Extract_f (void) REG_EXTENDED | GIB_Regex_Translate_Options (GIB_Argv (3))))) GIB_Error ("regex", "%s: %s", GIB_Argv (0), GIB_Regex_Error ()); - else if (!regexec (reg, GIB_Argv (1), 32, match, 0) && match[0].rm_eo) { + else if (!regexec (reg, GIB_Argv (1), 32, match, GIB_Regex_Translate_Runtime_Options (GIB_Argv (3))) && match[0].rm_eo) { dsprintf (GIB_Return (0), "%lu", (unsigned long) match[0].rm_eo); for (i = 0; i < 32; i++) { if (match[i].rm_so != -1) { @@ -662,6 +662,58 @@ GIB_Text_Brown_f (void) } } +/* +static void +GIB_Text_To_Gold_f (void) +{ + if (GIB_Argc () != 2) + GIB_USAGE ("text"); + else if (GIB_CanReturn ()) { + dstring_t *dstr; + char *str; + + dstr = GIB_Return (0); + dstring_copystr (dstr, GIB_Argv(1)); + + for (str = dstr->str; *str; str++) { + switch (*str) { +*/ + +static void +GIB_Text_To_Decimal_f (void) +{ + if (GIB_Argc () != 2) + GIB_USAGE ("text"); + else if (GIB_CanReturn ()) { + char *str; + + for (str = GIB_Argv(1); *str; str++) + dsprintf (GIB_Return (0), "%i", (int) *str); + } +} + +static void +GIB_Text_From_Decimal_f (void) +{ + if (GIB_Argc () < 2) + GIB_USAGE ("num1 [...]"); + else if (GIB_CanReturn ()) { + unsigned int i; + dstring_t *dstr; + char *str; + + dstr = GIB_Return (0); + dstr->size = GIB_Argc(); + dstring_adjust (dstr); + + str = dstr->str; + + for (i = 1; i < GIB_Argc(); i++, str++) + *str = (char) atoi (GIB_Argv(i)); + *str = 0; + } +} + static void GIB_Thread_Create_f (void) { @@ -996,8 +1048,10 @@ GIB_Builtin_Init (qboolean sandbox) GIB_Builtin_Add ("regex::match", GIB_Regex_Match_f); GIB_Builtin_Add ("regex::replace", GIB_Regex_Replace_f); GIB_Builtin_Add ("regex::extract", GIB_Regex_Extract_f); - GIB_Builtin_Add ("text::white", GIB_Text_White_f); - GIB_Builtin_Add ("text::brown", GIB_Text_Brown_f); + GIB_Builtin_Add ("text::toWhite", GIB_Text_White_f); + GIB_Builtin_Add ("text::toBrown", GIB_Text_Brown_f); + GIB_Builtin_Add ("text::toDecimal", GIB_Text_To_Decimal_f); + GIB_Builtin_Add ("text::fromDecimal", GIB_Text_From_Decimal_f); GIB_Builtin_Add ("thread::create", GIB_Thread_Create_f); GIB_Builtin_Add ("thread::kill", GIB_Thread_Kill_f); GIB_Builtin_Add ("thread::getList", GIB_Thread_List_f); diff --git a/libs/gib/gib_regex.c b/libs/gib/gib_regex.c index 0ac989ce4..3cd8b3b97 100644 --- a/libs/gib/gib_regex.c +++ b/libs/gib/gib_regex.c @@ -133,6 +133,18 @@ GIB_Regex_Translate_Options (const char *opstr) return options; } +int +GIB_Regex_Translate_Runtime_Options (const char *opstr) +{ + int options = 0; + + if (strchr (opstr, '<')) + options |= REG_NOTBOL; + if (strchr (opstr, '>')) + options |= REG_NOTEOL; + return options; +} + unsigned int GIB_Regex_Apply_Match (regmatch_t match[10], dstring_t * dstr, unsigned int ofs, const char *replace)