diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2015-05-28 09:43:25 -0700 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2015-05-28 09:43:25 -0700 |
commit | 0b64534a4450d6d198ce617209ebe38816b028ef (patch) | |
tree | 6e528d4ffc3a56d16a24ebfa8f4894cae0d39968 /tools/dump_cinit.lua | |
parent | e6dddd6c175cb23946c2d36180b3c2ef8d30e6ec (diff) | |
parent | 2cff15d35e4ff862e6a0811ae9e509c3d3352514 (diff) |
Merge pull request #21 from google/tablestrings
Restructure tables for C89 port and smaller size.
Diffstat (limited to 'tools/dump_cinit.lua')
-rw-r--r-- | tools/dump_cinit.lua | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/tools/dump_cinit.lua b/tools/dump_cinit.lua index 85e8a88..3742abd 100644 --- a/tools/dump_cinit.lua +++ b/tools/dump_cinit.lua @@ -47,6 +47,20 @@ function handler_types(base) return ret end +function octchar(num) + assert(num < 8) + local idx = num + 1 -- 1-based index + return string.sub("01234567", idx, idx) +end + +function c_escape(num) + assert(num < 256) + return string.format("\\%s%s%s", + octchar(math.floor(num / 64)), + octchar(math.floor(num / 8) % 8), + octchar(num % 8)); +end + -- const(f, label) -> UPB_LABEL_REPEATED, where f:label() == upb.LABEL_REPEATED function const(obj, name, base) local val = obj[name] @@ -218,7 +232,13 @@ function Dumper:tabkey(key) if type(key) == "nil" then return "UPB_TABKEY_NONE" elseif type(key) == "string" then - return string.format('UPB_TABKEY_STR("%s")', key) + local len = #key + local len1 = c_escape(len % 256) + local len2 = c_escape(math.floor(len / 256) % 256) + local len3 = c_escape(math.floor(len / (256 * 256)) % 256) + local len4 = c_escape(math.floor(len / (256 * 256 * 256)) % 256) + return string.format('UPB_TABKEY_STR("%s", "%s", "%s", "%s", "%s")', + len1, len2, len3, len4, key) else return string.format("UPB_TABKEY_NUM(%d)", key) end |