tomlib 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb163d03d5ca307ad810914faacadd2ba5650d01d6ed7458849be829cd0d080b
4
- data.tar.gz: a4e213b0ae1d737d9fd13f0e01381a20a3bf5bfcfa35c8d5021b120b51e1e96c
3
+ metadata.gz: aad6b4a6455bd446b09db888f1fc491a66521e0680594f74eeba9d10314cc8b6
4
+ data.tar.gz: fe599fd32583ba52c6702d29a9a27d805ba3cb0889e3df4b6612e2cebc6d1c75
5
5
  SHA512:
6
- metadata.gz: 943608fb548cb2448e83191cacb95aea330fd60a0bdb117c0fd96f2b2adc885f2b8ec2f46bfa455531d3308bee239f6d0996a95f8322e33282580813658d542e
7
- data.tar.gz: 71132c66ba2eaa88cbb7607c847e82883c0f0ffc3aa601e464d7131b2a15406702d1221a4607cc2d3cd9c597670e2be77c7f0afa023b08928f8ab052ffe98ea7
6
+ metadata.gz: a9f44b0a8b4f65b58ebf058646ca69d1fabdb935f352523cf9836f6387a92b5712ee59e233ec4efe92beb5a8feae28dd03af630f7601db236564d0a5afc79b86
7
+ data.tar.gz: aa654d7e31d34113629798a9336c9ead2e2071eb1935b3c80974f4c44da6333bf9b11719668ed43d148a4d85df6b1032f5fc8a306cde7e7620d9316ad7e9bb9a
data/CHANGELOG.md CHANGED
@@ -1,4 +1,12 @@
1
- ## [0.4.0] - [2022-08-06]
1
+ ## [0.6.0] - 2023-05-05
2
+
3
+ - Correctly escape special characters
4
+
5
+ ## [0.5.0] - 2022-08-16
6
+
7
+ - Add support for Ruby 2.6
8
+
9
+ ## [0.4.0] - 2022-08-06
2
10
 
3
11
  - Correctly dump empty arrays
4
12
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Tomlib
2
2
 
3
- Tomlib is a TOML parser and generator for Ruby. It is fast and standards-compliant by relying
4
- on native [tomlc99](https://github.com/cktan/tomlc99) parser.
3
+ Tomlib is a TOML parser and generator for Ruby. It uses native C extension based on
4
+ fast and standards-compliant [tomlc99](https://github.com/cktan/tomlc99) parser.
5
5
 
6
6
  ## Compliance
7
7
 
@@ -11,7 +11,7 @@ It passes both [BurntSushi/toml-test](https://github.com/BurntSushi/toml-test) a
11
11
 
12
12
  ## Installation
13
13
 
14
- Tomlib supports Ruby (MRI) 2.7+
14
+ Tomlib supports Ruby (MRI) 2.6+
15
15
 
16
16
  Add this line to your application's Gemfile:
17
17
 
@@ -118,10 +118,11 @@ Tomlib.dump(hash, indent: false)
118
118
 
119
119
  ## Performance
120
120
 
121
- `Tomlib` parsing is ~300x faster than `toml-rb`, ~15x faster than `Tomlrb`
122
- and ~3x faster than `perfect_toml` for usual use case (~5KB TOML document size).
121
+ When parsing documents, `Tomlib` is more than 600x (400x with yjit) faster than `toml-rb`,
122
+ 23x (17x with yjit) faster than `Tomlrb` and almost 5x (3.5x with yjit)
123
+ faster than `perfect_toml` (~5KB TOML document size).
123
124
 
124
- Generating TOML document is about 2x faster than `toml-rb`.
125
+ When generating TOML documents, it is about 1.5x (1.7x with yjit) faster than `toml-rb`.
125
126
 
126
127
  For full comparison take a look at
127
128
  [benchmarks](https://github.com/kgiszczak/tomlib/tree/master/benchmarks)
data/ext/tomlib/tomlib.c CHANGED
@@ -96,7 +96,15 @@ static VALUE toml_timestamp_to_rb_value(const toml_timestamp_t *ts) {
96
96
  VALUE rb_second = rb_rational_raw(DBL2NUM(second), INT2FIX(1000));
97
97
 
98
98
  if (ts->z) {
99
- VALUE rb_tz = rb_str_new2(ts->z);
99
+ VALUE rb_tz;
100
+
101
+ // Ruby 2.6 doesn't accept "Z" as a timezone offset
102
+ if (*ts->z == 'Z' || *ts->z == 'z') {
103
+ rb_tz = rb_str_new2("+00:00");
104
+ } else {
105
+ rb_tz = rb_str_new2(ts->z);
106
+ }
107
+
100
108
  rb_time = rb_funcall(
101
109
  rb_cTime,
102
110
  id_new,
@@ -312,7 +320,7 @@ static VALUE tomlib_key_type(VALUE self, VALUE rb_key) {
312
320
 
313
321
  if (str_len == 0) return sym_escape;
314
322
 
315
- for(long i = 0; i < str_len; i++) {
323
+ for (long i = 0; i < str_len; i++) {
316
324
  const char c = *(str + i);
317
325
 
318
326
  if (c == '\n') {
data/lib/tomlib/dumper.rb CHANGED
@@ -27,6 +27,22 @@ module Tomlib
27
27
  # @api private
28
28
  NAN = 'nan'.freeze
29
29
 
30
+ # Regex to match escape chars
31
+ # @api private
32
+ ESCAPE_CHARS_REGEX = /[\x00-\x1F\x22\x5C\x7F]/.freeze
33
+
34
+ # Escape chars mapping
35
+ # @api private
36
+ ESCAPE_CHARS = {
37
+ "\b" => 'b',
38
+ "\t" => 't',
39
+ "\n" => 'n',
40
+ "\f" => 'f',
41
+ "\r" => 'r',
42
+ '"' => '"',
43
+ '\\' => '\\',
44
+ }.freeze
45
+
30
46
  def initialize(use_indent: true)
31
47
  @use_indent = use_indent
32
48
  end
@@ -106,7 +122,7 @@ module Tomlib
106
122
 
107
123
  case key_type(key)
108
124
  when :quoted
109
- key.inspect
125
+ escape_string(key)
110
126
  when :escape
111
127
  key.dump.gsub('\n', '\\\\\n')
112
128
  else
@@ -138,7 +154,7 @@ module Tomlib
138
154
  def to_toml_value(value)
139
155
  case value
140
156
  when String
141
- value.inspect
157
+ escape_string(value)
142
158
  when Float, BigDecimal
143
159
  to_toml_float(value)
144
160
  when Time, DateTime
@@ -170,5 +186,26 @@ module Tomlib
170
186
 
171
187
  value.to_s
172
188
  end
189
+
190
+ # Escapes TOML special characters
191
+ #
192
+ # @param [String] str
193
+ #
194
+ # @return [String]
195
+ #
196
+ # @api private
197
+ def escape_string(str)
198
+ str = str.gsub(ESCAPE_CHARS_REGEX) do |chr|
199
+ c = ESCAPE_CHARS[chr]
200
+
201
+ if c
202
+ '\\' << c
203
+ else
204
+ format('\\u%04X', chr.ord)
205
+ end
206
+ end
207
+
208
+ '"' << str << '"'.freeze
209
+ end
173
210
  end
174
211
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Tomlib
4
4
  # @api private
5
- VERSION = '0.4.0'
5
+ VERSION = '0.6.0'
6
6
  end
data/tomlib.gemspec CHANGED
@@ -32,5 +32,5 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  spec.extensions = ['ext/tomlib/extconf.rb']
34
34
 
35
- spec.required_ruby_version = '>= 2.7.0'
35
+ spec.required_ruby_version = '>= 2.6.0'
36
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tomlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kamil Giszczak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-06 00:00:00.000000000 Z
11
+ date: 2023-05-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fast TOML parser and generator with native extension.
14
14
  email:
@@ -47,14 +47,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
47
  requirements:
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
- version: 2.7.0
50
+ version: 2.6.0
51
51
  required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  requirements: []
57
- rubygems_version: 3.3.7
57
+ rubygems_version: 3.4.10
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Fast TOML parser and generator with native extension.