Skip to content

Commit 8fc9acf

Browse files
Fix truncating datetimes and not allowing multline keys as table keys (#9)
- Datetimes were incorrectly being truncated even though the string match patterns were okay - Will now check and error out if a multi-line (triple quoted) string is used as a table key
1 parent fa122a3 commit 8fc9acf

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package = "tinytoml"
2-
version = "0.0.3-1"
2+
version = "0.0.4-1"
33

44
source = {
55
url = "git://github.com/FourierTransformer/tinytoml.git",
6-
tag = "0.0.3"
6+
tag = "0.0.4"
77
}
88

99
description = {
@@ -32,3 +32,4 @@ build = {
3232
}
3333
}
3434
}
35+

tinytoml.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local tinytoml = {}
1919

2020

2121

22-
tinytoml._VERSION = "tinytoml 0.0.1"
22+
tinytoml._VERSION = "tinytoml 0.0.4"
2323
tinytoml._DESCRIPTION = "a single-file pure Lua TOML parser"
2424
tinytoml._URL = "https://github.com/FourierTransformer/tinytoml"
2525
tinytoml._LICENSE = "MIT"
@@ -326,6 +326,7 @@ local function close_string(sm)
326326

327327

328328
if second == chars.DOUBLE_QUOTE and third == chars.DOUBLE_QUOTE then
329+
if sm.mode == "table" then _error(sm, "Cannot have multiline strings as table keys", "table") end
329330
sm.multiline_string = true
330331
start_field = sm.i + 3
331332

@@ -411,6 +412,7 @@ local function close_literal_string(sm)
411412

412413

413414
if second == chars.SINGLE_QUOTE and third == chars.SINGLE_QUOTE then
415+
if sm.mode == "table" then _error(sm, "Cannot have multiline strings as table keys", "table") end
414416
sm.multiline_string = true
415417
start_field = sm.i + 3
416418

@@ -619,18 +621,18 @@ local function validate_datetime(sm, value)
619621

620622
if sm.ext:find("^%.%d+$") then
621623
sm.value_type = "datetime-local"
622-
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext:sub(1, 4))
624+
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext)
623625
return true
624626
elseif sm.ext:find("^%.%d+Z$") then
625627
sm.value_type = "datetime"
626-
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext:sub(1, 4))
628+
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext)
627629
return true
628630
elseif sm.ext:find("^%.%d+[+-]%d%d:%d%d$") then
629631
sm._, sm.end_seq, hour, min = sm.ext:find("^%.%d+[+-](%d%d):(%d%d)$")
630632
if _tointeger(hour) > 23 then _error(sm, "Hours must be less than 24. Found hour: " .. hour .. "in: " .. sm.match, "offset-date-time") end
631633
if _tointeger(min) > 59 then _error(sm, "Minutes must be less than 60. Found minute: " .. min .. "in: " .. sm.match, "offset-date-time") end
632634
sm.value_type = "datetime"
633-
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext:sub(1, 4))
635+
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext)
634636
return true
635637
elseif sm.ext:find("^[Zz]$") then
636638
sm.value_type = "datetime"

tinytoml.tl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local record tinytoml
1919
_LICENSE: string
2020
end
2121

22-
tinytoml._VERSION = "tinytoml 0.0.1"
22+
tinytoml._VERSION = "tinytoml 0.0.4"
2323
tinytoml._DESCRIPTION = "a single-file pure Lua TOML parser"
2424
tinytoml._URL = "https://github.com/FourierTransformer/tinytoml"
2525
tinytoml._LICENSE = "MIT"
@@ -298,7 +298,7 @@ local function handle_backslash_escape(sm: StateMachine): string, boolean
298298

299299
-- unicode escape sequences
300300
-- hex escapes coming in toml 1.1.0, will need to update
301-
sm._, sm.end_seq, sm.match, sm.ext = sm.input:find("^(u)([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])", sm.i+1) as (integer, integer, string)
301+
sm._, sm.end_seq, sm.match, sm.ext = sm.input:find("^(u)([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])", sm.i+1) as (integer, integer, string, string)
302302
if not sm.match then
303303
sm._, sm.end_seq, sm.match, sm.ext = sm.input:find("^(U)([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])", sm.i+1) as (integer, integer, string, string)
304304
end
@@ -326,6 +326,7 @@ local function close_string(sm: StateMachine)
326326

327327
-- check for multiline and setup accordingly
328328
if second == chars.DOUBLE_QUOTE and third == chars.DOUBLE_QUOTE then
329+
if sm.mode == "table" then _error(sm, "Cannot have multiline strings as table keys", "table") end
329330
sm.multiline_string = true
330331
start_field = sm.i + 3
331332
-- toml allows a newline at the beginning of a triple quote
@@ -411,6 +412,7 @@ local function close_literal_string(sm: StateMachine)
411412

412413
-- check for multiline and setup accordingly
413414
if second == chars.SINGLE_QUOTE and third == chars.SINGLE_QUOTE then
415+
if sm.mode == "table" then _error(sm, "Cannot have multiline strings as table keys", "table") end
414416
sm.multiline_string = true
415417
start_field = sm.i + 3
416418
-- toml allows a newline at the beginning of a triple quote
@@ -619,18 +621,18 @@ local function validate_datetime(sm: StateMachine, value: string): boolean
619621
--TODO: maybe refactor at some point?
620622
if sm.ext:find("^%.%d+$") then
621623
sm.value_type = "datetime-local"
622-
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext:sub(1, 4))
624+
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext)
623625
return true
624626
elseif sm.ext:find("^%.%d+Z$") then
625627
sm.value_type = "datetime"
626-
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext:sub(1, 4))
628+
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext)
627629
return true
628630
elseif sm.ext:find("^%.%d+[+-]%d%d:%d%d$") then
629631
sm._, sm.end_seq, hour, min = sm.ext:find("^%.%d+[+-](%d%d):(%d%d)$") as (integer, integer, string, string)
630632
if _tointeger(hour) > 23 then _error(sm, "Hours must be less than 24. Found hour: " .. hour .. "in: " .. sm.match, "offset-date-time") end
631633
if _tointeger(min) > 59 then _error(sm, "Minutes must be less than 60. Found minute: " .. min .. "in: " .. sm.match, "offset-date-time") end
632634
sm.value_type = "datetime"
633-
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext:sub(1, 4))
635+
sm.value = sm.type_conversion[sm.value_type](sm.match .. sm.ext)
634636
return true
635637
elseif sm.ext:find("^[Zz]$") then
636638
sm.value_type = "datetime"

0 commit comments

Comments
 (0)