@@ -994,6 +994,7 @@ static int NSCall(lua_State *state) {
994
994
lh->sh ->enterExternalScript (cmd_buf);
995
995
lh->ons ->runScript ();
996
996
lh->sh ->leaveExternalScript ();
997
+ return 0 ;
997
998
}
998
999
999
1000
#define LUA_FUNC_LUT (s ) \
@@ -1115,6 +1116,50 @@ static int fmt_println(lua_State *state) {
1115
1116
return 0 ;
1116
1117
}
1117
1118
1119
+ static int string_split (lua_State* state) {
1120
+ const char * input = luaL_checkstring (state, 1 );
1121
+ size_t sep_len = 0 ;
1122
+ const char * sep = luaL_checklstring (state, 2 , &sep_len);
1123
+ size_t prev = 0 ;
1124
+ lua_newtable (state);
1125
+ size_t i = 0 ;
1126
+ while (true ) {
1127
+ i++;
1128
+ const char * next = strstr (input, sep);
1129
+ if (next) {
1130
+ lua_pushlstring (state, input, next - input);
1131
+ lua_rawseti (state, -2 , i);
1132
+ next += sep_len;
1133
+ input = next;
1134
+ continue ;
1135
+ }
1136
+ lua_pushstring (state, input);
1137
+ lua_rawseti (state, -2 , i);
1138
+ break ;
1139
+ }
1140
+ return 1 ;
1141
+ }
1142
+
1143
+ static int string_startswith (lua_State* state) {
1144
+ size_t cmp1_len = 0 ;
1145
+ const char * cmp1 = luaL_checklstring (state, 1 , &cmp1_len);
1146
+ size_t cmp2_len = 0 ;
1147
+ const char * cmp2 = luaL_checklstring (state, 2 , &cmp2_len);
1148
+ bool ok = !strncmp (cmp1, cmp2, cmp2_len);
1149
+ lua_pushboolean (state, ok);
1150
+ return 1 ;
1151
+ }
1152
+
1153
+ static int string_endswith (lua_State* state) {
1154
+ size_t cmp1_len = 0 ;
1155
+ const char * cmp1 = luaL_checklstring (state, 1 , &cmp1_len);
1156
+ size_t cmp2_len = 0 ;
1157
+ const char * cmp2 = luaL_checklstring (state, 2 , &cmp2_len);
1158
+ bool ok = !strncmp (cmp1+cmp1_len-cmp2_len, cmp2, cmp2_len);
1159
+ lua_pushboolean (state, ok);
1160
+ return 1 ;
1161
+ }
1162
+
1118
1163
static const struct luaL_Reg module_nsutf[] = {
1119
1164
LUA_FUNC_LUT (nsutf_from_ansi), LUA_FUNC_LUT (nsutf_to_ansi), {NULL , NULL }};
1120
1165
@@ -1187,6 +1232,14 @@ void LUAHandler::init(
1187
1232
luaL_register (state, " nsutf" , module_nsutf);
1188
1233
luaL_register (state, " dpshadow" , module_dpshadow);
1189
1234
luaL_register (state, " fmt" , module_fmt);
1235
+
1236
+ lua_getglobal (state, " string" );
1237
+ lua_pushcfunction (state, string_split);
1238
+ lua_setfield (state, -2 , " split" );
1239
+ lua_pushcfunction (state, string_startswith);
1240
+ lua_setfield (state, -2 , " startswith" );
1241
+ lua_pushcfunction (state, string_endswith);
1242
+ lua_setfield (state, -2 , " endswith" );
1190
1243
#endif
1191
1244
1192
1245
lua_pushlightuserdata (state, this );
0 commit comments