tomlib 0.1.0 → 0.2.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: 16903f85ce6caa68598bf97a4e631d2f69312a7019cb0901d668c40fafa24fb6
4
- data.tar.gz: 8fc0cf9b86b65dc8e3a455ffdbca94151a1ca74b1252e72600edcb872338de06
3
+ metadata.gz: d7ad9c10cfeccb998798bb265701ea92cba22f9b11ada9159c91ef13b43de9e2
4
+ data.tar.gz: df67f723a19ea5034ddc766239ec96b3539eaf907edfa459138b6792f55864a2
5
5
  SHA512:
6
- metadata.gz: ddd0190b6aa540128cafa6fdf20bcd16b7a7e0d6c4eaa1cbb65f993708388bedea553f0deda33884b26cb93996dd7ddc4825c7bc36b2223eb21eb43fcae6b2aa
7
- data.tar.gz: 65e1c88710a7a3f5ca65ca75026e59b64a525d5232c063359c976259ab8e7b10eac503a6aa665d7a2cbe21f7106a8573b9a3faf361b88a121c6a3af46269a744
6
+ metadata.gz: 1e06be42183ae1ed551ce3e8d60c07442c78133b1b17b222a2e2df105b10beb9dae28628376570c14d80955230d3bbe3158b551d1872c41c6bce629b47c95b29
7
+ data.tar.gz: df5e56a1a3f4d405c5b2906137e8bdb0de19ba5c1183edc4ba3fe0d7427022d9db62692a9b7afb22004c0fe46d68816b1586b5d2539817da08ea0d3b847892cd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## [0.2.0] - 2022-08-04
2
+
3
+ - Refactor Tomlib::Dumper to be a littel faster and generate squashed nested tables
4
+ ```
5
+ e.g. instead of this:
6
+ [a]
7
+ [a.b]
8
+ [a.b.c]
9
+
10
+ the output will be this:
11
+ [a.b.c]
12
+ ```
13
+ - Add mention about compliance and passed tests in README.
14
+ - Declare global variables with `rb_global_variable`.
15
+ It may prevent VM from crashing in some cases.
16
+
1
17
  ## [0.1.0] - 2022-08-02
2
18
 
3
- Initial release
19
+ - Initial release
data/README.md CHANGED
@@ -3,7 +3,11 @@
3
3
  Tomlib is a TOML parser and generator for Ruby. It is fast and standards-compliant by relying
4
4
  on native [tomlc99](https://github.com/cktan/tomlc99) parser.
5
5
 
6
+ ## Compliance
7
+
6
8
  Tomlib is TOML v1.0 compliant.
9
+ It passes both [BurntSushi/toml-test](https://github.com/BurntSushi/toml-test) and
10
+ [iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
7
11
 
8
12
  ## Installation
9
13
 
@@ -34,12 +38,7 @@ To parse a TOML document use:
34
38
  ```ruby
35
39
  require 'tomlib'
36
40
 
37
- Tomlib.parse(<<~TOML)
38
- firstName = "John"
39
- lastName = "Doe"
40
- hobbies = [ "Singing", "Dancing" ]
41
-
42
- [address]
41
+ Tomlib.load(<<~TOML)
43
42
  firstName = "John"
44
43
  lastName = "Doe"
45
44
  hobbies = [ "Singing", "Dancing" ]
@@ -119,10 +118,10 @@ Tomlib.dump(hash, indent: false)
119
118
 
120
119
  ## Performance
121
120
 
122
- `Tomlib` parsing is ~300x faster than `toml-rb` and ~15x faster than `Tomlrb`
123
- for usual use case (~5KB TOML document size).
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).
124
123
 
125
- Generating TOML document is about 1.7x faster than `toml-rb`.
124
+ Generating TOML document is about 2x faster than `toml-rb`.
126
125
 
127
126
  For full comparison take a look at
128
127
  [benchmarks](https://github.com/kgiszczak/tomlib/tree/master/benchmarks)
data/lib/tomlib/dumper.rb CHANGED
@@ -27,17 +27,37 @@ module Tomlib
27
27
  # @api private
28
28
  NAN = 'nan'.freeze
29
29
 
30
+ def initialize(use_indent: true)
31
+ @use_indent = use_indent
32
+ end
33
+
34
+ # Generate TOML string from ruby Hash
35
+ #
36
+ # @param [Hash] hash
37
+ #
38
+ # @return [String]
39
+ #
40
+ # @api private
41
+ def dump(hash)
42
+ result = dump_hash(hash)
43
+
44
+ result[0] = '' if result[0] == "\n"
45
+
46
+ result
47
+ end
48
+
49
+ private
50
+
30
51
  # Generate TOML string from ruby Hash
31
52
  #
32
53
  # @param [Hash] hash
33
54
  # @param [String, nil] base_key
34
- # @param [String] indent
35
- # @param [true, false] use_indent
55
+ # @param [Integer] indent_level
36
56
  #
37
57
  # @return [String]
38
58
  #
39
59
  # @api private
40
- def dump(hash, base_key = nil, indent = '', use_indent: true)
60
+ def dump_hash(hash, base_key = nil, indent_level = 0)
41
61
  header = ''
42
62
  footer = ''
43
63
 
@@ -45,34 +65,36 @@ module Tomlib
45
65
  toml_key = to_toml_key(key)
46
66
 
47
67
  if value.is_a?(Hash)
68
+ skip = !value.empty? && value.values.all? { |e| e.is_a?(Hash) }
48
69
  compound_key = to_toml_compound_key(base_key, toml_key)
49
- next_indent = use_indent && base_key ? indent + INDENT : indent
50
70
 
51
- footer << "\n".freeze << next_indent << '['.freeze << compound_key << "]\n".freeze
52
- footer << dump(value, compound_key, next_indent, use_indent: use_indent)
71
+ unless skip
72
+ indent = @use_indent ? INDENT * indent_level : ''.freeze
73
+ footer << "\n".freeze << indent << '['.freeze << compound_key << "]\n".freeze
74
+ end
75
+
76
+ footer << dump_hash(value, compound_key, skip ? indent_level : indent_level + 1)
53
77
  elsif value.is_a?(Array) && value.all? { |e| e.is_a?(Hash) }
54
78
  compound_key = to_toml_compound_key(base_key, toml_key)
55
- next_indent = use_indent && base_key ? indent + INDENT : indent
79
+ indent = @use_indent ? INDENT * indent_level : ''.freeze
56
80
 
57
81
  value.each do |el|
58
- footer << "\n".freeze << next_indent << '[['.freeze << compound_key << "]]\n".freeze
59
- footer << dump(el, compound_key, next_indent, use_indent: use_indent)
82
+ footer << "\n".freeze << indent << '[['.freeze << compound_key << "]]\n".freeze
83
+ footer << dump_hash(el, compound_key, indent_level + 1)
60
84
  end
61
85
  else
86
+ indent_length = indent_level > 0 ? indent_level - 1 : 0
87
+ indent = @use_indent ? INDENT * indent_length : ''.freeze
62
88
  header << indent << toml_key << ' = '.freeze << to_toml_value(value) << "\n".freeze
63
89
  end
64
90
  end
65
91
 
66
- footer.gsub!(/\A\n/, ''.freeze) if header.empty?
67
92
  header << footer
68
93
  end
69
94
 
70
- private
71
-
72
95
  # Generate TOML key from Hash key
73
96
  #
74
97
  # @param [String] key
75
- # @param [true, false] use_indent
76
98
  #
77
99
  # @return [String]
78
100
  #
Binary file
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Tomlib
4
4
  # @api private
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
data/lib/tomlib.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'date'
4
+
3
5
  require_relative 'tomlib/dumper'
4
6
  require_relative 'tomlib/tomlib'
5
7
  require_relative 'tomlib/version'
@@ -40,6 +42,6 @@ module Tomlib
40
42
  #
41
43
  # @api public
42
44
  def self.dump(hash, indent: true)
43
- Dumper.new.dump(hash, use_indent: indent)
45
+ Dumper.new(use_indent: indent).dump(hash)
44
46
  end
45
47
  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.1.0
4
+ version: 0.2.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-02 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fast TOML parser and generator with native extension.
14
14
  email: