| 79 | } |
| 80 | |
| 81 | int StringUtil::split(lua_State *L) |
| 82 | { |
| 83 | const char *s = luaL_checkstring(L, 1); |
| 84 | size_t len = 0; |
| 85 | const char *sep = luaL_checklstring(L, 2, &len); |
| 86 | const char *e = NULL; |
| 87 | bool skipEmpty = false; |
| 88 | if (lua_gettop(L) == 3) |
| 89 | { |
| 90 | skipEmpty = lua_toboolean(L, 3); |
| 91 | } |
| 92 | int i = 1; |
| 93 | |
| 94 | lua_newtable(L); /* result */ |
| 95 | |
| 96 | /* repeat for each separator */ |
| 97 | while ((e = strstr(s, sep)) != NULL) |
| 98 | { |
| 99 | if (!skipEmpty || e-s > 0) |
| 100 | { |
| 101 | lua_pushlstring(L, s, e-s); /* push substring */ |
| 102 | lua_rawseti(L, -2, i++); |
| 103 | } |
| 104 | s = e + len; /* skip separator */ |
| 105 | } |
| 106 | |
| 107 | /* push last substring */ |
| 108 | lua_pushstring(L, s); |
| 109 | lua_rawseti(L, -2, i); |
| 110 | |
| 111 | return 1; /* return the table */ |
| 112 | } |
| 113 | |
| 114 | int StringUtil::indexof(lua_State *L) |
| 115 | { |
no test coverage detected