json_append_string args: * - lua_State * - JSON strbuf * - String (Lua stack index) * * Returns nothing. Doesn't remove string from Lua stack */
| 461 | * |
| 462 | * Returns nothing. Doesn't remove string from Lua stack */ |
| 463 | static void json_append_string(lua_State *l, strbuf_t *json, int lindex) |
| 464 | { |
| 465 | const char *escstr; |
| 466 | int i; |
| 467 | const char *str; |
| 468 | size_t len; |
| 469 | |
| 470 | str = lua_tolstring(l, lindex, &len); |
| 471 | |
| 472 | /* Worst case is len * 6 (all unicode escapes). |
| 473 | * This buffer is reused constantly for small strings |
| 474 | * If there are any excess pages, they won't be hit anyway. |
| 475 | * This gains ~5% speedup. */ |
| 476 | strbuf_ensure_empty_length(json, len * 6 + 2); |
| 477 | |
| 478 | strbuf_append_char_unsafe(json, '\"'); |
| 479 | for (i = 0; i < len; i++) { |
| 480 | escstr = char2escape[(unsigned char)str[i]]; |
| 481 | if (escstr) |
| 482 | strbuf_append_string(json, escstr); |
| 483 | else |
| 484 | strbuf_append_char_unsafe(json, str[i]); |
| 485 | } |
| 486 | strbuf_append_char_unsafe(json, '\"'); |
| 487 | } |
| 488 | |
| 489 | /* Find the size of the array on the top of the Lua stack |
| 490 | * -1 object (not a pure array) |
no test coverage detected