DEFOLD
| 616 | |
| 617 | // DEFOLD |
| 618 | static void json_append_metadata_string(lua_State *l, strbuf_t *json, int lindex) |
| 619 | { |
| 620 | const char *escstr; |
| 621 | unsigned i; |
| 622 | const char *str; |
| 623 | size_t len; |
| 624 | |
| 625 | int top = lua_gettop(l); |
| 626 | |
| 627 | lua_pushvalue(l, lindex); |
| 628 | // -1: userdata |
| 629 | |
| 630 | lua_getglobal(l, "tostring"); |
| 631 | // -2: userdata |
| 632 | // -1: tostring |
| 633 | |
| 634 | lua_pushvalue(l, -2); |
| 635 | // -3: userdata |
| 636 | // -2: tostring |
| 637 | // -1: userdata |
| 638 | |
| 639 | lua_pcall(l, 1, 1, 0); |
| 640 | // -2: userdata |
| 641 | // -1: string |
| 642 | |
| 643 | str = lua_tolstring(l, -1, &len); |
| 644 | |
| 645 | /* Worst case is len * 6 (all unicode escapes). |
| 646 | * This buffer is reused constantly for small strings |
| 647 | * If there are any excess pages, they won't be hit anyway. |
| 648 | * This gains ~5% speedup. */ |
| 649 | strbuf_ensure_empty_length(json, len * 6 + 2); |
| 650 | |
| 651 | strbuf_append_char_unsafe(json, '\"'); |
| 652 | for (i = 0; i < len; i++) { |
| 653 | escstr = char2escape[(unsigned char)str[i]]; |
| 654 | if (escstr) |
| 655 | strbuf_append_string(json, escstr); |
| 656 | else |
| 657 | strbuf_append_char_unsafe(json, str[i]); |
| 658 | } |
| 659 | strbuf_append_char_unsafe(json, '\"'); |
| 660 | |
| 661 | lua_pop(l, 2); // pop the string+userdata |
| 662 | |
| 663 | assert(top == lua_gettop(l)); |
| 664 | } |
| 665 | // DEFOLD END |
| 666 | |
| 667 |
no test coverage detected