static HRESULT do_regexp_match_next(script_ctx_t *ctx, RegExpInstance *regexp, DWORD rem_flags, jsstr_t *str, match_state_t *ret) { HRESULT hres; hres = regexp_execute(regexp->jsregexp, ctx, &ctx->tmp_heap, str->str, jsstr_length(str), ret); if(FAILED(hres)) return hres; if(hres == S_FALSE) { if(rem_flags & REM_RESET_INDEX) set_last_index(regexp, 0); return S_FALSE; } if(!(rem_flags & REM_NO_CTX_UPDATE) && ctx->last_match != str) { jsstr_release(ctx->last_match); ctx->last_match = jsstr_addref(str); } if(!(rem_flags & REM_NO_CTX_UPDATE)) { DWORD i, n = min(sizeof(ctx->match_parens)/sizeof(ctx->match_parens[0]), ret->paren_count); for(i=0; i < n; i++) { if(ret->parens[i].index == -1) { ctx->match_parens[i].index = 0; ctx->match_parens[i].length = 0; }else { ctx->match_parens[i].index = ret->parens[i].index; ctx->match_parens[i].length = ret->parens[i].length; } } if(n < sizeof(ctx->match_parens)/sizeof(ctx->match_parens[0])) memset(ctx->match_parens+n, 0, sizeof(ctx->match_parens) - n*sizeof(ctx->match_parens[0])); } set_last_index(regexp, ret->cp-str->str); if(!(rem_flags & REM_NO_CTX_UPDATE)) { ctx->last_match_index = ret->cp-str->str-ret->match_len; ctx->last_match_length = ret->match_len; } return S_OK; }
static HRESULT WINAPI RegExp2_Test(IRegExp2 *iface, BSTR sourceString, VARIANT_BOOL *pMatch) { RegExp2 *This = impl_from_IRegExp2(iface); match_state_t *result; heap_pool_t *mark; HRESULT hres; TRACE("(%p)->(%s %p)\n", This, debugstr_w(sourceString), pMatch); if(!This->pattern) { *pMatch = VARIANT_TRUE; return S_OK; } if(!This->regexp) { This->regexp = regexp_new(NULL, &This->pool, This->pattern, strlenW(This->pattern), This->flags, FALSE); if(!This->regexp) return E_FAIL; }else { hres = regexp_set_flags(&This->regexp, NULL, &This->pool, This->flags); if(FAILED(hres)) return hres; } mark = heap_pool_mark(&This->pool); result = alloc_match_state(This->regexp, &This->pool, sourceString); if(!result) { heap_pool_clear(mark); return E_OUTOFMEMORY; } hres = regexp_execute(This->regexp, NULL, &This->pool, sourceString, SysStringLen(sourceString), result); heap_pool_clear(mark); if(hres == S_OK) { *pMatch = VARIANT_TRUE; }else if(hres == S_FALSE) { *pMatch = VARIANT_FALSE; hres = S_OK; } return hres; }
static HRESULT WINAPI RegExp2_Execute(IRegExp2 *iface, BSTR sourceString, IDispatch **ppMatches) { RegExp2 *This = impl_from_IRegExp2(iface); match_state_t *result; const WCHAR *pos; IMatchCollection2 *match_collection; IMatch2 *add = NULL; HRESULT hres; TRACE("(%p)->(%s %p)\n", This, debugstr_w(sourceString), ppMatches); if(!This->pattern) { DWORD i, len = SysStringLen(sourceString); hres = create_match_collection2(&match_collection); if(FAILED(hres)) return hres; for(i=0; i<=len; i++) { hres = create_match2(i, NULL, &add); if(FAILED(hres)) break; hres = add_match(match_collection, add); if(FAILED(hres)) break; IMatch2_Release(add); if(!(This->flags & REG_GLOB)) break; } if(FAILED(hres)) { IMatchCollection2_Release(match_collection); return hres; } *ppMatches = (IDispatch*)match_collection; return S_OK; } if(!This->regexp) { This->regexp = regexp_new(NULL, &This->pool, This->pattern, strlenW(This->pattern), This->flags, FALSE); if(!This->regexp) return E_FAIL; }else { hres = regexp_set_flags(&This->regexp, NULL, &This->pool, This->flags); if(FAILED(hres)) return hres; } hres = create_match_collection2(&match_collection); if(FAILED(hres)) return hres; pos = sourceString; while(1) { result = alloc_match_state(This->regexp, NULL, pos); if(!result) { hres = E_OUTOFMEMORY; break; } hres = regexp_execute(This->regexp, NULL, &This->pool, sourceString, SysStringLen(sourceString), result); if(hres != S_OK) { heap_free(result); break; } pos = result->cp; hres = create_match2(result->cp-result->match_len-sourceString, &result, &add); heap_free(result); if(FAILED(hres)) break; hres = add_match(match_collection, add); IMatch2_Release(add); if(FAILED(hres)) break; if(!(This->flags & REG_GLOB)) break; } if(FAILED(hres)) { IMatchCollection2_Release(match_collection); return hres; } *ppMatches = (IDispatch*)match_collection; return S_OK; }