@@ -162,32 +162,27 @@ end
162
162
163
163
global unpack : function
164
164
local _unpack = unpack or table .unpack
165
- local utf8char : function (number ): string
166
-
167
- if (tonumber ((_VERSION or ''):match ('[% d .]*$ ')) or 0) < 5.3 then
168
- -- from https :// stackoverflow.com/a/26237757
169
- utf8char = function (cp : number ): string
170
- if cp < 128 then
171
- return string .char (cp as integer )
172
- end
173
- local suffix = cp % 64
174
- local c4 = 128 + suffix
175
- cp = (cp - suffix ) / 64
176
- if cp < 32 then
177
- return string .char (192 + (cp as integer ), (c4 as integer ))
178
- end
179
- suffix = cp % 64
180
- local c3 = 128 + suffix
181
- cp = (cp - suffix ) / 64
182
- if cp < 16 then
183
- return string .char (224 + (cp as integer ), c3 as integer , c4 as integer )
184
- end
185
- suffix = cp % 64
186
- cp = (cp - suffix ) / 64
187
- return string .char (240 + (cp as integer ), 128 + (suffix as integer ), c3 as integer , c4 as integer )
188
- end
189
- else
190
- utf8char = utf8 .char
165
+ local _tointeger = math .tointeger or tonumber as function (string ): integer
166
+ -- function from https :// stackoverflow.com/a/26237757
167
+ local _utf8char = utf8 and utf8 .char or function (cp : number ): string
168
+ if cp < 128 then
169
+ return string .char (cp as integer )
170
+ end
171
+ local suffix = cp % 64
172
+ local c4 = 128 + suffix
173
+ cp = (cp - suffix ) / 64
174
+ if cp < 32 then
175
+ return string .char (192 + (cp as integer ), (c4 as integer ))
176
+ end
177
+ suffix = cp % 64
178
+ local c3 = 128 + suffix
179
+ cp = (cp - suffix ) / 64
180
+ if cp < 16 then
181
+ return string .char (224 + (cp as integer ), c3 as integer , c4 as integer )
182
+ end
183
+ suffix = cp % 64
184
+ cp = (cp - suffix ) / 64
185
+ return string .char (240 + (cp as integer ), 128 + (suffix as integer ), c3 as integer , c4 as integer )
191
186
end
192
187
193
188
local function validate_utf8 (input : string , toml_sub ? : boolean ): boolean , integer
@@ -307,7 +302,7 @@ local function handle_backslash_escape(sm: StateMachine): string, boolean
307
302
--if (sm .match == "x " and # sm .ext == 2) or -- hex escapes coming in toml 1.1.0, will need to update pattern in :find above as well
308
303
if (sm .match == "u " and # sm .ext == 4) or
309
304
(sm .match == "U " and # sm .ext == 8) then
310
- local codepoint_to_insert = utf8char (tonumber (sm .ext , 16))
305
+ local codepoint_to_insert = _utf8char (tonumber (sm .ext , 16))
311
306
if not validate_utf8 (codepoint_to_insert ) then
312
307
_error (sm , "Escaped UTF -8 sequence not valid UTF -8 character : \\" .. sm .match .. sm .ext , "string ")
313
308
end
@@ -505,7 +500,7 @@ local function validate_integer(sm: StateMachine, value: string): boolean
505
500
if sm .match then
506
501
if sm .match:find("^[-+]? 0[% d_ ]") then _error (sm , "Integers can 't start with a leading 0. Found integer : " .. sm .match , "integer ") end
507
502
sm .match = remove_underscores_number (sm , sm .match , "integer ")
508
- sm .value = tonumber (sm .match )
503
+ sm .value = _tointeger (sm .match )
509
504
sm .value_type = "integer "
510
505
return true
511
506
end
@@ -558,9 +553,9 @@ local function validate_datetime(sm: StateMachine, value: string): boolean
558
553
local hour , min , sec : string , string , string
559
554
sm ._ , sm ._ , sm .match , hour , min , sec , sm .ext = value:find("^((% d % d ):(% d % d ):(% d % d ))(.*)$ ") as (integer , integer , string , string , string , string , string )
560
555
if sm .match then
561
- if tonumber (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "local -time ") end
562
- if tonumber (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match , "local -time ") end
563
- if tonumber (sec ) > 60 then _error (sm , "Seconds must be less than 61. Found second : " .. sec .. "in : " .. sm .match , "local -time ") end
556
+ if _tointeger (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "local -time ") end
557
+ if _tointeger (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match , "local -time ") end
558
+ if _tointeger (sec ) > 60 then _error (sm , "Seconds must be less than 61. Found second : " .. sec .. "in : " .. sm .match , "local -time ") end
564
559
if sm .ext ~= "" then
565
560
if sm .ext:find("^% .% d +$ ") then
566
561
sm .value_type = "time -local "
@@ -575,12 +570,12 @@ local function validate_datetime(sm: StateMachine, value: string): boolean
575
570
end
576
571
577
572
local year_s , month_s , day_s : string , string , string
578
- local year , month , day : number , number , number
573
+ local year , month , day : integer , integer , integer
579
574
sm ._ , sm ._ , sm .match , year_s , month_s , day_s = value:find("^((% d % d % d % d )% -(% d % d )% -(% d % d ))$ ") as (integer , integer , string , string , string , string )
580
575
if sm .match then
581
- year , month , day = tonumber (year_s ), tonumber (month_s ), tonumber (day_s )
576
+ year , month , day = _tointeger (year_s ), _tointeger (month_s ), _tointeger (day_s )
582
577
if month == 0 or month > 12 then _error (sm , "Month must be between 01-12. Found month : " .. month .. "in : " .. sm .match , "local -date ") end
583
- if day == 0 or day > max_days_in_month [month as integer ] then _error (sm , "Too many days in the month . Found " .. day .. " days in month " .. month .. ", which only has " .. max_days_in_month [month as integer ] .. " days in : " .. sm .match , "local -date ") end
578
+ if day == 0 or day > max_days_in_month [month ] then _error (sm , "Too many days in the month . Found " .. day .. " days in month " .. month .. ", which only has " .. max_days_in_month [month as integer ] .. " days in : " .. sm .match , "local -date ") end
584
579
if month == 2 then
585
580
local leap_year = (year % 4 == 0) and not (year % 100 == 0) or (year % 400 == 0)
586
581
if leap_year == false then
@@ -607,12 +602,12 @@ local function validate_datetime(sm: StateMachine, value: string): boolean
607
602
value:find("^((% d % d % d % d )% -(% d % d )% -(% d % d )[Tt ](% d % d ):(% d % d ):(% d % d ))(.*)$ ")
608
603
as (integer , integer , string , string , string , string , string , string , string , string )
609
604
if sm .match then
610
- if tonumber (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "") end
611
- if tonumber (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match ) end
612
- if tonumber (sec ) > 60 then _error (sm , "Seconds must be less than 61. Found second : " .. sec .. "in : " .. sm .match ) end
613
- year , month , day = tonumber (year_s ), tonumber (month_s ), tonumber (day_s )
605
+ if _tointeger (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "") end
606
+ if _tointeger (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match ) end
607
+ if _tointeger (sec ) > 60 then _error (sm , "Seconds must be less than 61. Found second : " .. sec .. "in : " .. sm .match ) end
608
+ year , month , day = _tointeger (year_s ), _tointeger (month_s ), _tointeger (day_s )
614
609
if month == 0 or month > 12 then _error (sm , "Month must be between 01-12. Found month : " .. month .. "in : " .. sm .match ) end
615
- if day == 0 or day > max_days_in_month [month as integer ] then _error (sm , "Too many days in the month . Found " .. day .. " days in month " .. month .. ", which only has " .. max_days_in_month [month as integer ] .. " days in : " .. sm .match , "local -datetime ") end
610
+ if day == 0 or day > max_days_in_month [month ] then _error (sm , "Too many days in the month . Found " .. day .. " days in month " .. month .. ", which only has " .. max_days_in_month [month as integer ] .. " days in : " .. sm .match , "local -datetime ") end
616
611
if month == 2 then
617
612
local leap_year = (year % 4 == 0) and not (year % 100 == 0) or (year % 400 == 0)
618
613
if leap_year == false then
@@ -632,8 +627,8 @@ local function validate_datetime(sm: StateMachine, value: string): boolean
632
627
return true
633
628
elseif sm .ext:find("^% .% d +[+-]% d % d :% d % d$ ") then
634
629
sm ._ , sm .end_seq , hour , min = sm .ext:find("^% .% d +[+-](% d % d ):(% d % d )$ ") as (integer , integer , string , string )
635
- if tonumber (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "offset -date -time ") end
636
- if tonumber (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match , "offset -date -time ") end
630
+ if _tointeger (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "offset -date -time ") end
631
+ if _tointeger (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match , "offset -date -time ") end
637
632
sm .value_type = "datetime "
638
633
sm .value = sm .type_conversion [sm .value_type ](sm .match .. sm .ext:sub(1, 4))
639
634
return true
@@ -643,8 +638,8 @@ local function validate_datetime(sm: StateMachine, value: string): boolean
643
638
return true
644
639
elseif sm .ext:find("^[+-]% d % d :% d % d$ ") then
645
640
sm ._ , sm .end_seq , hour , min = sm .ext:find("^[+-](% d % d ):(% d % d )$ ") as (integer , integer , string , string )
646
- if tonumber (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "offset -date -time ") end
647
- if tonumber (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match , "offset -date -time ") end
641
+ if _tointeger (hour ) > 23 then _error (sm , "Hours must be less than 24. Found hour : " .. hour .. "in : " .. sm .match , "offset -date -time ") end
642
+ if _tointeger (min ) > 59 then _error (sm , "Minutes must be less than 60. Found minute : " .. min .. "in : " .. sm .match , "offset -date -time ") end
648
643
sm .value_type = "datetime "
649
644
sm .value = sm .type_conversion [sm .value_type ](sm .match .. sm .ext )
650
645
return true
0 commit comments