17 typedef struct regex_entry regex_entry;
19 static struct hash* regex_hash;
21 regexp* regex_compile( const char* pattern )
23 regex_entry entry, *e = &entry;
24 entry.pattern = pattern;
27 regex_hash = hashinit(sizeof(regex_entry), "regex");
29 if ( hashenter( regex_hash, (HASHDATA **)&e ) )
30 e->regex = regcomp( (char*)pattern );
41 LIST* arg1 = lol_get( frame->args, 0 );
43 if ( arg1 && list_next(arg1) && list_next(list_next(arg1)) )
46 const char* source = arg1->string;
47 const char* pattern = list_next(arg1)->string;
48 regexp* repat = regex_compile( pattern );
50 if ( regexec( repat, (char*)source) )
52 LIST* subst = list_next(arg1);
54 while ((subst = list_next(subst)) != L0)
58 const char* in = subst->string;
61 for ( in = subst->string; *in && out < buf + BUFLEN; ++in )
63 if ( *in == '\\' || *in == '$' )
70 else if ( *in >= '0' && *in <= '9' )
72 unsigned n = *in - '0';
73 const size_t srclen = repat->endp[n] - repat->startp[n];
74 const size_t remaining = buf + BUFLEN - out;
75 const size_t len = srclen < remaining ? srclen : remaining;
76 memcpy( out, repat->startp[n], len );
80 /* fall through and copy the next character */
86 result = list_new( result, newstr( buf ) );