| 623 | #define ARGS_ERROR() { global_glua_chain_api->throw_exception(L, THINKYOUNG_API_SIMPLE_ERROR, "arguments wrong"); return 0; } |
| 624 | |
| 625 | static int str_split(lua_State *L) |
| 626 | { |
| 627 | if (lua_gettop(L) < 2 || !lua_isstring(L, 1) || !lua_isstring(L, 2)) |
| 628 | ARGS_ERROR(); |
| 629 | const char *s = luaL_checkstring(L, 1); |
| 630 | const char *sep = luaL_checkstring(L, 2); |
| 631 | if (strlen(sep) < 1) |
| 632 | { |
| 633 | lua_createtable(L, 0, 0); |
| 634 | lua_pushstring(L, s); |
| 635 | lua_seti(L, -2, 1); |
| 636 | return 1; |
| 637 | } |
| 638 | std::list<std::string> items; |
| 639 | std::string sstr(s); |
| 640 | std::string sepstr(sep); |
| 641 | size_t i = 0; |
| 642 | size_t last_pos = 0; |
| 643 | while (i < sstr.length()) |
| 644 | { |
| 645 | std::string sub = sstr.substr(i, sepstr.length()); |
| 646 | if (sub == sepstr && last_pos == i) |
| 647 | { |
| 648 | i += sepstr.length(); |
| 649 | last_pos = i; |
| 650 | continue; |
| 651 | } |
| 652 | if (sub == sepstr) |
| 653 | { |
| 654 | if (i > last_pos) |
| 655 | { |
| 656 | std::string item = sstr.substr(last_pos, i - last_pos); |
| 657 | i += sepstr.length(); |
| 658 | last_pos = i; |
| 659 | items.push_back(item); |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | i += sepstr.length(); |
| 664 | } |
| 665 | } |
| 666 | else |
| 667 | { |
| 668 | ++i; |
| 669 | } |
| 670 | } |
| 671 | if (i > last_pos) |
| 672 | { |
| 673 | std::string item = sstr.substr(last_pos, i - last_pos); |
| 674 | if (item != sepstr) |
| 675 | items.push_back(item); |
| 676 | } |
| 677 | i = 0; |
| 678 | lua_createtable(L, 0, 0); |
| 679 | for (auto it = items.begin(); it != items.end(); ++it) |
| 680 | { |
| 681 | lua_pushstring(L, it->c_str()); |
| 682 | lua_seti(L, -2, ++i); |
nothing calls this directly
no test coverage detected