String RegExp::format(const char* str, const char* fmt, uint start) { if (_peer == NULL) return fmt; String ret; SQRex* rex = (SQRex*)_peer; const char *begin, *end; bool found = sqstd_rex_search(rex, str+start, &begin, &end); if (!found) return ret; uint n = sqstd_rex_getsubexpcount(rex); for (const char* f = fmt; *f; ++f) { int ch = *f; if (ch != '%') { ret.push_back(ch); continue; } ch = *++f; if (ch == '*') ret.append(str); else if (ch == '<') ret.append(str, begin); else if (ch == '>') ret.append(end); else if ('0' <= ch && ch <= '9') { uint subIdx = ch - '0'; if (subIdx < n) { SQRexMatch match; sqstd_rex_getsubexp(rex, subIdx, &match); ret.append(match.begin, match.len); } else NIT_THROW_FMT(EX_INVALID_PARAMS, "no subexp: %d", subIdx); } else ret.push_back(ch); } return ret; }
bool RegExp::captureAll(const char* str, StringVector& outResults, uint start) { if (_peer == NULL) return false; SQRex* rex = (SQRex*)_peer; bool found = sqstd_rex_search(rex, str+start, NULL, NULL); if (!found) return false; SQInteger n = sqstd_rex_getsubexpcount(rex); for (SQInteger i=0; i < n; ++i) { SQRexMatch match; sqstd_rex_getsubexp(rex, i, &match); outResults.push_back(String()); outResults.back().assign(match.begin, match.begin + match.len); } return true; }
bool RegExp::captureAll(const char* str, vector<SubExp>::type& outResults, uint start) { if (_peer == NULL) return false; SQRex* rex = (SQRex*)_peer; bool found = sqstd_rex_search(rex, str+start, NULL, NULL); if (!found) return false; SQInteger n = sqstd_rex_getsubexpcount(rex); for (SQInteger i=0; i < n; ++i) { SQRexMatch match; sqstd_rex_getsubexp(rex, i, &match); outResults.push_back(SubExp()); SubExp& e = outResults.back(); e.begin = match.begin; e.end = match.begin + match.len; } return true; }
static SQInteger _regexp_capture( HSQUIRRELVM v ) { SETUP_REX( v ); const SQChar *str, *begin, *end; SQInteger start = 0; sq_getstring( v, 2, &str ); if ( sq_gettop( v ) > 2 ) sq_getinteger( v, 3, &start ); if ( sqstd_rex_search( self, str + start, &begin, &end ) == SQTrue ) { SQInteger n = sqstd_rex_getsubexpcount( self ); SQRexMatch match; sq_newarray( v, 0 ); for ( SQInteger i = 0;i < n; i++ ) { sqstd_rex_getsubexp( self, i, &match ); if ( match.len > 0 ) _addrexmatch( v, str, match.begin, match.begin + match.len ); else _addrexmatch( v, str, str, str ); //empty match sq_arrayappend( v, -2 ); } return 1; } return 0; }
uint RegExp::getSubExpCount() { return _peer ? sqstd_rex_getsubexpcount((SQRex*)_peer) : 0; }
static SQInteger _regexp_subexpcount( HSQUIRRELVM v ) { SETUP_REX( v ); sq_pushinteger( v, sqstd_rex_getsubexpcount( self ) ); return 1; }