/** regexp_matched : 'regexp -> n:int -> string? <doc>Return the [n]th matched block by the regexp. If [n] is 0 then return the whole matched substring. If the [n]th matched block was optional and not matched, returns null</doc> **/ static value regexp_matched( value o, value n ) { pcredata *d; int m; val_check_kind(o,k_regexp); d = PCRE(o); val_check(n,int); m = val_int(n); if( m < 0 || m >= d->nmatchs || val_is_null(d->str) ) val_throw(alloc_string("regexp@regexp_matched")); { int start = d->matchs[m*2]; int len = d->matchs[m*2+1] - start; if( start == -1 ) return alloc_null(); return copy_string( val_string(d->str)+start,len ); } }
/** regexp_matched_pos : 'regexp -> n:int -> { pos => int, len => int } <doc>Return the [n]th matched block position by the regexp. If [n] is 0 then return the whole matched substring position</doc> **/ static value regexp_matched_pos( value o, value n ) { pcredata *d; int m; val_check_kind(o,k_regexp); d = PCRE(o); val_check(n,int); m = val_int(n); if( m < 0 || m >= d->nmatchs || val_is_null(d->str) ) return alloc_null(); { int start = d->matchs[m*2]; int len = d->matchs[m*2+1] - start; value o = alloc_empty_object(); alloc_field(o,id_pos,alloc_int(start)); alloc_field(o,id_len,alloc_int(len)); return o; } }
bool _hx_regexp_match(Dynamic handle, String string, int pp, int ll) { if( pp < 0 || ll < 0 || pp > string.length || pp + ll > string.length ) return false; pcredata *d = PCRE(handle); if( pcre_exec(d->r,NULL,string.__s,ll+pp,pp,0,d->matchs,d->nmatchs * 3) >= 0 ) { d->string = string; return true; } else { d->string = String(); return false; } }
bool _hx_regexp_match(Dynamic handle, String string, int pos, int len) { if( pos < 0 || len < 0 || pos > string.length || pos + len > string.length ) return false; pcredata *d = PCRE(handle); if( d->run(string,pos,len) ) { d->string = string; HX_OBJ_WB_GET(d, d->string.raw_ref()); return true; } else { d->string = String(); return false; } }
/** regexp_match : 'regexp -> string -> pos:int -> len:int -> bool <doc>Match [len] chars of a string starting at [pos] using the regexp. Return true if match found</doc> **/ static value regexp_match( value o, value s, value p, value len ) { pcredata *d; int pp,ll; val_check_kind(o,k_regexp); val_check(s,string); val_check(p,int); val_check(len,int); pp = val_int(p); ll = val_int(len); if( pp < 0 || ll < 0 || pp > val_strlen(s) || pp + ll > val_strlen(s) ) return alloc_null(); d = PCRE(o); if( pcre_exec(d->r,NULL,val_string(s),ll+pp,pp,0,d->matchs,d->nmatchs * 3) >= 0 ) { d->str = s; return alloc_bool(true); } else { d->str = alloc_null(); return alloc_bool(false); } }
/** regexp_matched : 'regexp -> n:int -> string? <doc>Return the [n]th matched block by the regexp. If [n] is 0 then return the whole matched substring. If the [n]th matched block was optional and not matched, returns null</doc> **/ static value regexp_matched( value o, value n ) { pcredata *d; int m; val_check_kind(o,k_regexp); d = PCRE(o); val_check(n,int); m = val_int(n); if( m < 0 || m >= d->nmatchs || val_is_null(d->str) ) neko_error(); { int start = d->matchs[m*2]; int len = d->matchs[m*2+1] - start; value str; if( start == -1 ) return val_null; str = alloc_empty_string(len); memcpy((char*)val_string(str),val_string(d->str)+start,len); return str; } }
/** regexp_match : 'regexp -> string -> pos:int -> len:int -> bool <doc>Match [len] chars of a string starting at [pos] using the regexp. Return true if match found</doc> **/ static value regexp_match( value o, value s, value p, value len ) { pcredata *d; int pp,ll; val_check_kind(o,k_regexp); val_check(s,string); val_check(p,int); val_check(len,int); pp = val_int(p); ll = val_int(len); if( pp < 0 || ll < 0 || pp > val_strlen(s) || pp + ll > val_strlen(s) ) neko_error(); d = PCRE(o); if( do_exec(d,val_string(s),ll+pp,pp) ) { d->str = s; return val_true; } else { d->str = val_null; return val_false; } }
/** regexp_replace_fun : 'regexp -> from:string -> f:('regexp -> any) -> string <doc>Perform a replacement of all matched substrings by calling [f] for every match</doc> **/ static value regexp_replace_fun( value o, value s, value f ) { val_check_kind(o,k_regexp); val_check(s,string); val_check_function(f,1); { pcredata *d = PCRE(o); buffer b = alloc_buffer(NULL); int pos = 0; int len = val_strlen(s); const char *str = val_string(s); d->str = s; while( pcre_exec(d->r,NULL,str,len,pos,0,d->matchs,d->nmatchs * 3) >= 0 ) { buffer_append_sub(b,str+pos,d->matchs[0] - pos); val_buffer(b,val_call1(f,o)); pos = d->matchs[1]; } d->str = alloc_null(); buffer_append_sub(b,str+pos,len-pos); return buffer_to_string(b); } }
static value do_replace( value o, value s, value s2, bool all ) { val_check_kind(o,k_regexp); val_check(s,string); val_check(s2,string); { pcredata *d = PCRE(o); buffer b = alloc_buffer(NULL); int pos = 0; int len = val_strlen(s); const char *str = val_string(s); const char *str2 = val_string(s2); int len2 = val_strlen(s2); while( pcre_exec(d->r,NULL,str,len,pos,0,d->matchs,d->nmatchs * 3) >= 0 ) { buffer_append_sub(b,str+pos,d->matchs[0] - pos); buffer_append_sub(b,str2,len2); pos = d->matchs[1]; if( !all ) break; } d->str = alloc_null(); buffer_append_sub(b,str+pos,len-pos); return buffer_to_string(b); } }
static void free_regexp( value p ) { pcre_free( PCRE(p)->r ); }