HRESULT regexp_match_next(script_ctx_t *ctx, jsdisp_t *dispex, DWORD rem_flags, jsstr_t *jsstr, match_state_t **ret) { RegExpInstance *regexp = (RegExpInstance*)dispex; match_state_t *match; heap_pool_t *mark; const WCHAR *str; HRESULT hres; if((rem_flags & REM_CHECK_GLOBAL) && !(regexp->jsregexp->flags & REG_GLOB)) { if(rem_flags & REM_ALLOC_RESULT) *ret = NULL; return S_FALSE; } str = jsstr_flatten(jsstr); if(!str) return E_OUTOFMEMORY; if(rem_flags & REM_ALLOC_RESULT) { match = alloc_match_state(regexp->jsregexp, NULL, str); if(!match) return E_OUTOFMEMORY; *ret = match; } mark = heap_pool_mark(&ctx->tmp_heap); if(rem_flags & REM_NO_PARENS) { match = alloc_match_state(regexp->jsregexp, &ctx->tmp_heap, NULL); if(!match) { heap_pool_clear(mark); return E_OUTOFMEMORY; } match->cp = (*ret)->cp; match->match_len = (*ret)->match_len; }else { match = *ret; } hres = do_regexp_match_next(ctx, regexp, rem_flags, jsstr, str, match); if(rem_flags & REM_NO_PARENS) { (*ret)->cp = match->cp; (*ret)->match_len = match->match_len; } heap_pool_clear(mark); if(hres != S_OK && (rem_flags & REM_ALLOC_RESULT)) { heap_free(match); *ret = NULL; } return hres; }
static HRESULT regexp_match(script_ctx_t *ctx, jsdisp_t *dispex, jsstr_t *str, BOOL gflag, match_result_t **match_result, DWORD *result_cnt) { RegExpInstance *This = (RegExpInstance*)dispex; match_result_t *ret = NULL; match_state_t *result; DWORD i=0, ret_size = 0; heap_pool_t *mark; HRESULT hres; mark = heap_pool_mark(&ctx->tmp_heap); result = alloc_match_state(This->jsregexp, &ctx->tmp_heap, str->str); if(!result) { heap_pool_clear(mark); return E_OUTOFMEMORY; } while(1) { hres = do_regexp_match_next(ctx, This, 0, str, result); if(hres == S_FALSE) { hres = S_OK; break; } if(FAILED(hres)) break; if(ret_size == i) { if(ret) { match_result_t *old_ret = ret; ret = heap_realloc(old_ret, (ret_size <<= 1) * sizeof(match_result_t)); if(!ret) heap_free(old_ret); }else { ret = heap_alloc((ret_size=4) * sizeof(match_result_t)); } if(!ret) { hres = E_OUTOFMEMORY; break; } } ret[i].index = result->cp - str->str - result->match_len; ret[i++].length = result->match_len; if(!gflag && !(This->jsregexp->flags & REG_GLOB)) { hres = S_OK; break; } } heap_pool_clear(mark); if(FAILED(hres)) { heap_free(ret); return hres; } *match_result = ret; *result_cnt = i; return S_OK; }