Skip to content

Commit 54769b4

Browse files
authored
Merge pull request #24 from BirdeeHub/FIXES
fix(decode): all decode bugs fixed, but unsure about API again
2 parents 48f193d + c0ea87e commit 54769b4

14 files changed

Lines changed: 937 additions & 842 deletions

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ LIBFLAG ?= -shared
1212
CFLAGS += $(LIBFLAG) -I"$(LUA_INCDIR)"
1313
TESTDIR := $(SRC)/tests
1414
SRCS := $(SRC)/src/tomlua.c \
15-
$(SRC)/src/decode_str.c \
16-
$(SRC)/src/decode_inline_value.c \
1715
$(SRC)/src/decode.c \
1816
$(SRC)/src/encode.c \
1917
$(SRC)/src/dates.c

README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ It is not intended to replace packages like [toml\_edit](https://github.com/nvim
1414
* Fast parsing of TOML files into Lua tables.
1515
* Emits TOML from Lua tables (also quickly).
1616
* Compatible with Lua 5.1+.
17-
* Supports embedding in Lua C modules or Nix packaging.
1817
* Some advanced TOML compliance features are optional (`fancy_dates`, etc.).
18+
* Allows you to read directly into an existing lua table of defaults. This cuts out the step of merging them yourself, making it even more performant and easier to use!
1919

2020
## Limitations
2121

@@ -27,8 +27,8 @@ It is not intended to replace packages like [toml\_edit](https://github.com/nvim
2727

2828
Basic benchmarking shows promising results:
2929

30-
* Slightly slower than `cjson` (1.5x), despite parsing TOML instead of JSON.
31-
* Around **10x faster** than `toml_edit` for parsing.
30+
* Slightly slower than `cjson` (2x longer), despite parsing TOML instead of JSON.
31+
* Around 7x faster than `toml_edit` for parsing (not accounting for the added ability to read directly into an existing lua table).
3232

3333
## Installation
3434

@@ -98,6 +98,13 @@ tomlua.opts.fancy_dates = true
9898

9999
local data, err = tomlua.decode(some_string)
100100

101+
local data, err = tomlua.decode(some_string, {
102+
some = "defaults",
103+
can_go = "here",
104+
and = "tables and arrays defined in headings will extend the associated value",
105+
and_inline = "values from the toml will override it instead",
106+
})
107+
101108
-- encode always accepts fancy dates, never outputs fancy tables, and is unaffected by all opts
102109
-- instead you may customize dates by replacing them with tomlua date types
103110
-- and you may decide to make some strings multiline with tomlua.str_2_mul
@@ -187,20 +194,3 @@ As a result of that, editing existing toml files, or emitting them at all, only
187194
This means that it makes a lot of sense to use a simple but fast parser to parse config files on startup.
188195

189196
And then later you can use one which is better for editing but is slower when making things like a settings page which may edit the file.
190-
191-
Randomly selected benchmark run of 100_000 parses of the example toml file:
192-
193-
```
194-
-- tomlua
195-
Parsed TOML 100000 times in 2.142739 seconds, avg. 46669.239697 iterations per second, avg. 21.43 µs/iteration
196-
-- cjson, parsing output of tomlua decode
197-
Parsed JSON 100000 times in 1.284222 seconds, avg. 77868.156752 iterations per second, avg. 12.84 µs/iteration
198-
speed tomlua/cjson: 59.93%, duration tomlua/cjson: 166.85%
199-
-- toml_edit
200-
Parsed TOML 100000 times in 15.833271 seconds, avg. 6315.814338 iterations per second, avg. 158.33 µs/iteration
201-
speed tomlua/toml_edit: 738.93%, duration tomlua/toml_edit: 13.53%
202-
-- tomlua encode
203-
Emitted TOML 100000 times in 2.307145 seconds, avg. 43343.612994 iterations per second, avg. 23.07 µs/iteration
204-
-- cjson encode
205-
Emitted JSON 100000 times in 1.479101 seconds, avg. 67608.635245 iterations per second, avg. 14.79 µs/iteration
206-
```

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
GREP = "${pkgs.gnugrep}/bin/grep";
7979
BEAR = "${pkgs.bear}/bin/bear";
8080
shellHook = ''
81-
make clean build bear
81+
make clean bear build
8282
[ "$(whoami)" == "birdee" ] && exec zsh
8383
'';
8484
};

src/dates.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static const char *DATE_FIELD_NAMES[TOMLDATE_DATE_LENGTH] = {
4343
};
4444
typedef int TomlDate[TOMLDATE_DATE_LENGTH];
4545

46-
static TOMLDATE_FIELDS string_2_date_field_idx(const char *str) {
46+
static inline TOMLDATE_FIELDS string_2_date_field_idx(const char *str) {
4747
for (uint8_t i = 0; i < TOMLDATE_DATE_LENGTH; i++) {
4848
if (strcmp(str, DATE_FIELD_NAMES[i]) == 0) return i;
4949
}

0 commit comments

Comments
 (0)