* lua_ap_regex; r:regex(string, pattern [, flags]) * - Evaluates a regex and returns captures if matched */
| 1167 | * - Evaluates a regex and returns captures if matched |
| 1168 | */ |
| 1169 | static int lua_ap_regex(lua_State *L) |
| 1170 | { |
| 1171 | request_rec *r; |
| 1172 | int i, |
| 1173 | rv, |
| 1174 | flags; |
| 1175 | const char *pattern, |
| 1176 | *source; |
| 1177 | char *err; |
| 1178 | ap_regex_t regex; |
| 1179 | ap_regmatch_t matches[MODLUA_MAX_REG_MATCH+1]; |
| 1180 | |
| 1181 | luaL_checktype(L, 1, LUA_TUSERDATA); |
| 1182 | luaL_checktype(L, 2, LUA_TSTRING); |
| 1183 | luaL_checktype(L, 3, LUA_TSTRING); |
| 1184 | r = ap_lua_check_request_rec(L, 1); |
| 1185 | source = lua_tostring(L, 2); |
| 1186 | pattern = lua_tostring(L, 3); |
| 1187 | flags = luaL_optinteger(L, 4, 0); |
| 1188 | |
| 1189 | rv = ap_regcomp(®ex, pattern, flags); |
| 1190 | if (rv) { |
| 1191 | lua_pushboolean(L, 0); |
| 1192 | err = apr_palloc(r->pool, 256); |
| 1193 | ap_regerror(rv, ®ex, err, 256); |
| 1194 | lua_pushstring(L, err); |
| 1195 | return 2; |
| 1196 | } |
| 1197 | |
| 1198 | if (regex.re_nsub > MODLUA_MAX_REG_MATCH) { |
| 1199 | lua_pushboolean(L, 0); |
| 1200 | err = apr_palloc(r->pool, 64); |
| 1201 | apr_snprintf(err, 64, |
| 1202 | "regcomp found %d matches; only %d allowed.", |
| 1203 | regex.re_nsub, MODLUA_MAX_REG_MATCH); |
| 1204 | lua_pushstring(L, err); |
| 1205 | return 2; |
| 1206 | } |
| 1207 | |
| 1208 | rv = ap_regexec(®ex, source, MODLUA_MAX_REG_MATCH, matches, 0); |
| 1209 | if (rv == AP_REG_NOMATCH) { |
| 1210 | lua_pushboolean(L, 0); |
| 1211 | return 1; |
| 1212 | } |
| 1213 | |
| 1214 | lua_newtable(L); |
| 1215 | for (i = 0; i <= regex.re_nsub; i++) { |
| 1216 | lua_pushinteger(L, i); |
| 1217 | if (matches[i].rm_so >= 0 && matches[i].rm_eo >= 0) |
| 1218 | lua_pushstring(L, |
| 1219 | apr_pstrndup(r->pool, source + matches[i].rm_so, |
| 1220 | matches[i].rm_eo - matches[i].rm_so)); |
| 1221 | else |
| 1222 | lua_pushnil(L); |
| 1223 | lua_settable(L, -3); |
| 1224 | |
| 1225 | } |
| 1226 | return 1; |
nothing calls this directly
no test coverage detected