| 107 | } |
| 108 | |
| 109 | int Regex::regexFind(lua_State *L) |
| 110 | { |
| 111 | QRegularExpression *regex = checkRegex(L, true); |
| 112 | int params = lua_gettop(L); // regex(kiko.regex) target(string) initpos(number) |
| 113 | const char *errMsg = nullptr; |
| 114 | int initpos = 0; |
| 115 | if(params < 2 || lua_type(L, 2) != LUA_TSTRING) |
| 116 | { |
| 117 | errMsg = "expect string as match target"; |
| 118 | } |
| 119 | else if(params == 3 && lua_type(L, 3) == LUA_TNUMBER) |
| 120 | { |
| 121 | initpos = lua_tonumber(L, 3); |
| 122 | if(initpos > 0) initpos--; // 1-based |
| 123 | } |
| 124 | else if(params != 2) |
| 125 | { |
| 126 | errMsg = "expect number as initial matching position"; |
| 127 | } |
| 128 | if(errMsg) |
| 129 | { |
| 130 | luaL_error(L, "expect: regex(kiko.regex) target(string) initpos(number)"); |
| 131 | } |
| 132 | QRegularExpressionMatch match = regex->match(QString(lua_tostring(L, 2)), initpos); |
| 133 | if(match.hasMatch()) |
| 134 | { |
| 135 | lua_pushinteger(L, match.capturedStart() + 1); // 1-based |
| 136 | lua_pushinteger(L, match.capturedEnd()); // 0-based, after the ending position of the substring captured |
| 137 | QStringList groups = match.capturedTexts(); |
| 138 | int retSize = 2; |
| 139 | if(groups.size() > 1) |
| 140 | { |
| 141 | for(int i = 1; i < groups.size(); ++i) |
| 142 | { |
| 143 | lua_pushstring(L, groups.at(i).toStdString().c_str()); |
| 144 | ++retSize; |
| 145 | } |
| 146 | } |
| 147 | return retSize; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | QRegularExpressionMatchIterator *Regex::checkRegexMatch(lua_State *L, int idx) |
| 153 | { |
nothing calls this directly
no test coverage detected