Skip to content

Commit 43744c5

Browse files
author
BirdeeHub
committed
fix(dates): dates improved (but not fixed entirely)
1 parent 6dc9cf9 commit 43744c5

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎src/decode_inline_value.c‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ bool decode_inline_value(lua_State *L, str_iter *src, str_buf *buf, const Tomlua
320320
bool is_float = false;
321321
bool is_date = false;
322322
bool t_used = false;
323+
bool last_was_T_space = false;
323324
bool z_used = false;
324325
bool was_underscore = true;
325326
if (curr.v == '+') {
@@ -351,13 +352,13 @@ bool decode_inline_value(lua_State *L, str_iter *src, str_buf *buf, const Tomlua
351352
}
352353
while (iter_peek(src).ok) {
353354
char ch = iter_peek(src).v;
354-
if (ch == '_') {
355+
if (ch == '_' && !last_was_T_space) {
355356
iter_skip(src);
356357
if (was_underscore) {
357358
return set_tmlerr(new_tmlerr(L, erridx), false, 46, "consecutive underscores not allowed in numbers");
358359
}
359360
was_underscore = true;
360-
} else if (ch == 'e' || ch == 'E') {
361+
} else if ((ch == 'e' || ch == 'E') && !last_was_T_space) {
361362
is_float = true;
362363
buf_push(buf, ch);
363364
iter_skip(src);
@@ -367,37 +368,47 @@ bool decode_inline_value(lua_State *L, str_iter *src, str_buf *buf, const Tomlua
367368
iter_skip(src);
368369
}
369370
was_underscore = false;
370-
} else if (ch == ':') {
371+
} else if (ch == ':' && !last_was_T_space) {
371372
is_date = true;
372373
was_underscore = false;
373374
buf_push(buf, ch);
374375
iter_skip(src);
375-
} else if (ch == '-') {
376+
} else if (ch == '-' && !last_was_T_space) {
376377
is_date = true;
377378
was_underscore = false;
378379
buf_push(buf, ch);
379380
iter_skip(src);
380-
} else if (is_date && !t_used && (ch == 'T' || ch == ' ')) {
381+
} else if (is_date && !t_used && ch == 'T' && !last_was_T_space) {
381382
t_used = true;
382383
was_underscore = false;
383384
buf_push(buf, ch);
384385
iter_skip(src);
385-
} else if (is_date && !z_used && ch == 'Z') {
386+
} else if (is_date && !t_used && ch == ' ' && !last_was_T_space) {
387+
t_used = true;
388+
was_underscore = false;
389+
last_was_T_space = true;
390+
iter_skip(src);
391+
} else if (is_date && !z_used && ch == 'Z' && !last_was_T_space) {
386392
z_used = true;
387393
was_underscore = false;
388394
buf_push(buf, ch);
389395
iter_skip(src);
390-
} else if (ch == '.') {
396+
} else if (ch == '.' && !last_was_T_space) {
391397
is_float = true;
392398
was_underscore = false;
393399
buf_push(buf, ch);
394400
iter_skip(src);
395401
} else if (ch >= '0' && ch <= '9') {
402+
if (last_was_T_space) {
403+
if (!buf_push(buf, ' ')) return set_tmlerr(new_tmlerr(L, erridx), false, 56, "failed to push space to date");
404+
}
405+
last_was_T_space = false;
396406
was_underscore = false;
397407
buf_push(buf, ch);
398408
iter_skip(src);
399409
} else {
400410
was_underscore = false;
411+
last_was_T_space = false;
401412
break;
402413
}
403414
}

‎tests/decode_tests.lua‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,6 @@ String'''
15031503
it(err == nil, "Should not error")
15041504
it(#data.logs == 2, "Should have two logs")
15051505
it(tostring(data.logs[1].timestamp) == "1979-05-27T07:32:00Z", "First log timestamp should be correct")
1506-
print("?"..data.logs[2].timestamp.."?")
15071506
it(tostring(data.logs[2].timestamp) == "1979-05-27", "Second log timestamp should be correct")
15081507
end)
15091508

@@ -1518,11 +1517,10 @@ String'''
15181517
timestamp = 1979-05-27 # Local date
15191518
message = "Log entry 2"
15201519
]=]
1521-
local data, err = tomlua_fancy_dates.decode(toml_str)
1520+
local data, err = tomlua_default.decode(toml_str)
15221521
it(err == nil, "Should not error")
15231522
it(#data.logs == 2, "Should have two logs")
15241523
it(data.logs[1].timestamp == "1979-05-27T07:32:00Z", "First log timestamp should be correct")
1525-
print("?"..data.logs[2].timestamp.."?")
15261524
it(data.logs[2].timestamp == "1979-05-27", "Second log timestamp should be correct")
15271525
end)
15281526

@@ -1795,8 +1793,6 @@ String''' # Another multiline content
17951793
it(err == nil, "Should not error")
17961794
it(#data.logs == 2, "Should have two logs")
17971795
it(tostring(data.logs[1].timestamp) == "1979-05-27T07:32:00Z", "First log timestamp should be correct")
1798-
print("!"..data.logs[2].timestamp.."!")
1799-
-- TODO: figure out why this is getting an extra space added to it?!
18001796
it(tostring(data.logs[2].timestamp) == "1979-05-27", "Second log timestamp should be correct")
18011797
end)
18021798

0 commit comments

Comments
 (0)