json 2.7.3 → 2.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +114 -0
- data/LEGAL +0 -52
- data/README.md +61 -57
- data/ext/json/ext/fbuffer/fbuffer.h +156 -66
- data/ext/json/ext/generator/extconf.rb +29 -0
- data/ext/json/ext/generator/generator.c +1166 -478
- data/ext/json/ext/generator/simd.h +112 -0
- data/ext/json/ext/parser/extconf.rb +4 -27
- data/ext/json/ext/parser/parser.c +1138 -1971
- data/ext/json/ext/vendor/fpconv.c +479 -0
- data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
- data/json.gemspec +7 -5
- data/lib/json/add/bigdecimal.rb +1 -1
- data/lib/json/add/symbol.rb +7 -2
- data/lib/json/common.rb +594 -219
- data/lib/json/ext/generator/state.rb +2 -31
- data/lib/json/ext.rb +28 -11
- data/lib/json/{pure → truffle_ruby}/generator.rb +264 -154
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +15 -20
- metadata +11 -17
- data/ext/json/ext/generator/generator.h +0 -129
- data/ext/json/ext/parser/parser.h +0 -60
- data/ext/json/ext/parser/parser.rl +0 -997
- data/lib/json/pure/parser.rb +0 -331
- data/lib/json/pure.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e71f977a9d4c1316007814d62236fd185f5aaade7a79f3e5d48a9ffde32f520
|
4
|
+
data.tar.gz: f1be8ac3136a6dcf48aa15c7ec08fa4dfcedb6f89b1b6ad8944727708a16e074
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23f2d490dfb7ea60b189f8227787fde0c53844f62c8e9023ba1d413a72b46b7a3b77836d1a6050dd0a2fa925370bd260da0a52d738bd1231c81ad1ef4a17adda
|
7
|
+
data.tar.gz: 22326ad3f75f99e20c7f1ad3cc0f519ffc56b7c85c94aa124a2ea47c8d0c86f604307fe504f216b347651d3c82df83623798dbdbabc45be78a1e4721cc7b8cbe
|
data/CHANGES.md
CHANGED
@@ -1,5 +1,119 @@
|
|
1
1
|
# Changes
|
2
2
|
|
3
|
+
### Unreleased
|
4
|
+
|
5
|
+
### 2025-05-12 (2.12.0)
|
6
|
+
|
7
|
+
* Improve floating point generation to not use scientific notation as much.
|
8
|
+
* Include line and column in parser errors. Both in the message and as exception attributes.
|
9
|
+
* Handle non-string hash keys with broken `to_s` implementations.
|
10
|
+
* `JSON.generate` now uses SSE2 (x86) or NEON (arm64) instructions when available to escape strings.
|
11
|
+
|
12
|
+
### 2025-04-25 (2.11.3)
|
13
|
+
|
14
|
+
* Fix a regression in `JSON.pretty_generate` that could cause indentation to be off once some `#to_json` has been called.
|
15
|
+
|
16
|
+
### 2025-04-24 (2.11.2)
|
17
|
+
|
18
|
+
* Add back `JSON::PRETTY_STATE_PROTOTYPE`. This constant was private API but is used by popular gems like `multi_json`.
|
19
|
+
It now emits a deprecation warning.
|
20
|
+
|
21
|
+
### 2025-04-24 (2.11.1)
|
22
|
+
|
23
|
+
* Add back `JSON.restore`, `JSON.unparse`, `JSON.fast_unparse` and `JSON.pretty_unparse`.
|
24
|
+
These were deprecated 16 years ago, but never emited warnings, only undocumented, so are
|
25
|
+
still used by a few gems.
|
26
|
+
|
27
|
+
### 2025-04-24 (2.11.0)
|
28
|
+
|
29
|
+
* Optimize Integer generation to be ~1.8x faster.
|
30
|
+
* Optimize Float generation to be ~10x faster.
|
31
|
+
* Fix `JSON.load` proc argument to substitute the parsed object with the return value.
|
32
|
+
This better match `Marshal.load` behavior.
|
33
|
+
* Deprecate `JSON.fast_generate` (it's not any faster, so pointless).
|
34
|
+
* Deprecate `JSON.load_default_options`.
|
35
|
+
* Deprecate `JSON.unsafe_load_default_options`.
|
36
|
+
* Deprecate `JSON.dump_default_options`.
|
37
|
+
* Deprecate `Kernel#j`
|
38
|
+
* Deprecate `Kernel#jj`
|
39
|
+
* Remove outdated `JSON.iconv`.
|
40
|
+
* Remove `Class#json_creatable?` monkey patch.
|
41
|
+
* Remove deprecated `JSON.restore` method.
|
42
|
+
* Remove deprecated `JSON.unparse` method.
|
43
|
+
* Remove deprecated `JSON.fast_unparse` method.
|
44
|
+
* Remove deprecated `JSON.pretty_unparse` method.
|
45
|
+
* Remove deprecated `JSON::UnparserError` constant.
|
46
|
+
* Remove outdated `JSON::MissingUnicodeSupport` constant.
|
47
|
+
|
48
|
+
### 2025-03-12 (2.10.2)
|
49
|
+
|
50
|
+
* Fix a potential crash in the C extension parser.
|
51
|
+
* Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` unadvertently changed it.
|
52
|
+
* Ensure document snippets that are included in parser errors don't include truncated multibyte characters.
|
53
|
+
* Ensure parser error snippets are valid UTF-8.
|
54
|
+
* Fix `JSON::GeneratorError#detailed_message` on Ruby < 3.2
|
55
|
+
|
56
|
+
### 2025-02-10 (2.10.1)
|
57
|
+
|
58
|
+
* Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`: `no implicit conversion of false into Proc (TypeError)`.
|
59
|
+
|
60
|
+
### 2025-02-10 (2.10.0)
|
61
|
+
|
62
|
+
* `strict: true` now accept symbols as values. Previously they'd only be accepted as hash keys.
|
63
|
+
* The C extension Parser has been entirely reimplemented from scratch.
|
64
|
+
* Introduced `JSON::Coder` as a new API allowing to customize how non native types are serialized in a non-global way.
|
65
|
+
* Introduced `JSON::Fragment` to allow assembling cached fragments in a safe way.
|
66
|
+
* The Java implementation of the generator received many optimizations.
|
67
|
+
|
68
|
+
### 2024-12-18 (2.9.1)
|
69
|
+
|
70
|
+
* Fix support for Solaris 10.
|
71
|
+
|
72
|
+
### 2024-12-03 (2.9.0)
|
73
|
+
|
74
|
+
* Fix C implementation of `script_safe` escaping to not confuse some other 3 wide characters with `\u2028` and `\u2029`.
|
75
|
+
e.g. `JSON.generate(["倩", "瀨"], script_safe: true)` would generate the wrong JSON.
|
76
|
+
* `JSON.dump(object, some_io)` now write into the IO in chunks while previously it would buffer the entire JSON before writing.
|
77
|
+
* `JSON::GeneratorError` now has a `#invalid_object` attribute, making it easier to understand why an object tree cannot be serialized.
|
78
|
+
* Numerous improvements to the JRuby extension.
|
79
|
+
|
80
|
+
### 2024-11-14 (2.8.2)
|
81
|
+
|
82
|
+
* `JSON.load_file` explictly read the file as UTF-8.
|
83
|
+
|
84
|
+
### 2024-11-06 (2.8.1)
|
85
|
+
|
86
|
+
* Fix the java packages to include the extension.
|
87
|
+
|
88
|
+
### 2024-11-06 (2.8.0)
|
89
|
+
|
90
|
+
* Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explictly enabled.
|
91
|
+
* Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
|
92
|
+
* Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
|
93
|
+
* Bump required Ruby version to 2.7.
|
94
|
+
* Add support for optionally parsing trailing commas, via `allow_trailing_comma: true`, which in cunjunction with the
|
95
|
+
pre-existing support for comments, make it suitable to parse `jsonc` documents.
|
96
|
+
* Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
|
97
|
+
* Some minor performance improvements to `JSON.dump` and `JSON.generate`.
|
98
|
+
* `JSON.pretty_generate` no longer include newline inside empty object and arrays.
|
99
|
+
|
100
|
+
### 2024-11-04 (2.7.6)
|
101
|
+
|
102
|
+
* Fix a regression in JSON.generate when dealing with Hash keys that are string subclasses, call `to_json` on them.
|
103
|
+
|
104
|
+
### 2024-10-25 (2.7.5)
|
105
|
+
|
106
|
+
* Fix a memory leak when `#to_json` methods raise an exception.
|
107
|
+
* Gracefully handle formatting configs being set to `nil` instead of `""`.
|
108
|
+
* Workaround another issue caused by conflicting versions of both `json_pure` and `json` being loaded.
|
109
|
+
|
110
|
+
### 2024-10-25 (2.7.4)
|
111
|
+
|
112
|
+
* Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
|
113
|
+
This bug would cause some gems with native extension to fail during compilation.
|
114
|
+
* Workaround different versions of `json` and `json_pure` being loaded (not officially supported).
|
115
|
+
* Make `json_pure` Ractor compatible.
|
116
|
+
|
3
117
|
### 2024-10-24 (2.7.3)
|
4
118
|
|
5
119
|
* Numerous performance optimizations in `JSON.generate` and `JSON.dump` (up to 2 times faster).
|
data/LEGAL
CHANGED
@@ -6,55 +6,3 @@
|
|
6
6
|
All the files in this distribution are covered under either the Ruby's
|
7
7
|
license (see the file COPYING) or public-domain except some files
|
8
8
|
mentioned below.
|
9
|
-
|
10
|
-
== MIT License
|
11
|
-
>>>
|
12
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
13
|
-
a copy of this software and associated documentation files (the
|
14
|
-
"Software"), to deal in the Software without restriction, including
|
15
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
16
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
17
|
-
permit persons to whom the Software is furnished to do so, subject to
|
18
|
-
the following conditions:
|
19
|
-
|
20
|
-
The above copyright notice and this permission notice shall be
|
21
|
-
included in all copies or substantial portions of the Software.
|
22
|
-
|
23
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
24
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
25
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
26
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
27
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
28
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
29
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
30
|
-
|
31
|
-
== Old-style BSD license
|
32
|
-
>>>
|
33
|
-
Redistribution and use in source and binary forms, with or without
|
34
|
-
modification, are permitted provided that the following conditions
|
35
|
-
are met:
|
36
|
-
1. Redistributions of source code must retain the above copyright
|
37
|
-
notice, this list of conditions and the following disclaimer.
|
38
|
-
2. Redistributions in binary form must reproduce the above copyright
|
39
|
-
notice, this list of conditions and the following disclaimer in the
|
40
|
-
documentation and/or other materials provided with the distribution.
|
41
|
-
3. Neither the name of the University nor the names of its contributors
|
42
|
-
may be used to endorse or promote products derived from this software
|
43
|
-
without specific prior written permission.
|
44
|
-
|
45
|
-
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
46
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
47
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
48
|
-
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
49
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
50
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
51
|
-
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
52
|
-
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
53
|
-
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
54
|
-
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
55
|
-
SUCH DAMAGE.
|
56
|
-
|
57
|
-
IMPORTANT NOTE::
|
58
|
-
|
59
|
-
From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
|
60
|
-
paragraph 3 above is now null and void.
|
data/README.md
CHANGED
@@ -5,16 +5,10 @@
|
|
5
5
|
## Description
|
6
6
|
|
7
7
|
This is an implementation of the JSON specification according to RFC 7159
|
8
|
-
http://www.ietf.org/rfc/rfc7159.txt .
|
8
|
+
http://www.ietf.org/rfc/rfc7159.txt .
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
* The quite a bit faster native extension variant, which is in parts
|
13
|
-
implemented in C or Java and comes with a parser generated by the [Ragel]
|
14
|
-
state machine compiler.
|
15
|
-
|
16
|
-
Both variants of the JSON generator generate UTF-8 character sequences by
|
17
|
-
default. If an :ascii\_only option with a true value is given, they escape all
|
10
|
+
The JSON generator generate UTF-8 character sequences by default.
|
11
|
+
If an :ascii\_only option with a true value is given, they escape all
|
18
12
|
non-ASCII and control characters with \uXXXX escape sequences, and support
|
19
13
|
UTF-16 surrogate pairs in order to be able to generate the whole range of
|
20
14
|
unicode code points.
|
@@ -27,10 +21,6 @@ endpoint.
|
|
27
21
|
|
28
22
|
## Installation
|
29
23
|
|
30
|
-
It's recommended to use the extension variant of JSON, because it's faster than
|
31
|
-
the pure ruby variant. If you cannot build it on your system, you can settle
|
32
|
-
for the latter.
|
33
|
-
|
34
24
|
Install the gem and add to the application's Gemfile by executing:
|
35
25
|
|
36
26
|
$ bundle add json
|
@@ -39,13 +29,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
39
29
|
|
40
30
|
$ gem install json
|
41
31
|
|
42
|
-
|
43
|
-
There is also a pure ruby json only variant of the gem, that can be installed
|
44
|
-
with:
|
45
|
-
|
46
|
-
$ gem install json_pure
|
47
|
-
|
48
|
-
## Usage
|
32
|
+
## Basic Usage
|
49
33
|
|
50
34
|
To use JSON you can
|
51
35
|
|
@@ -53,20 +37,6 @@ To use JSON you can
|
|
53
37
|
require 'json'
|
54
38
|
```
|
55
39
|
|
56
|
-
to load the installed variant (either the extension `'json'` or the pure
|
57
|
-
variant `'json_pure'`). If you have installed the extension variant, you can
|
58
|
-
pick either the extension variant or the pure variant by typing
|
59
|
-
|
60
|
-
```ruby
|
61
|
-
require 'json/ext'
|
62
|
-
```
|
63
|
-
|
64
|
-
or
|
65
|
-
|
66
|
-
```ruby
|
67
|
-
require 'json/pure'
|
68
|
-
```
|
69
|
-
|
70
40
|
Now you can parse a JSON document into a ruby data structure by calling
|
71
41
|
|
72
42
|
```ruby
|
@@ -82,50 +52,84 @@ You can also use the `pretty_generate` method (which formats the output more
|
|
82
52
|
verbosely and nicely) or `fast_generate` (which doesn't do any of the security
|
83
53
|
checks generate performs, e. g. nesting deepness checks).
|
84
54
|
|
85
|
-
|
86
|
-
|
55
|
+
## Casting non native types
|
56
|
+
|
57
|
+
JSON documents can only support Hashes, Arrays, Strings, Integers and Floats.
|
58
|
+
|
59
|
+
By default if you attempt to serialize something else, `JSON.generate` will
|
60
|
+
search for a `#to_json` method on that object:
|
87
61
|
|
88
62
|
```ruby
|
89
|
-
|
90
|
-
|
63
|
+
Position = Struct.new(:latitude, :longitude) do
|
64
|
+
def to_json(state = nil, *)
|
65
|
+
JSON::State.from_state(state).generate({
|
66
|
+
latitude: latitude,
|
67
|
+
longitude: longitude,
|
68
|
+
})
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
JSON.generate([
|
73
|
+
Position.new(12323.234, 435345.233),
|
74
|
+
Position.new(23434.676, 159435.324),
|
75
|
+
]) # => [{"latitude":12323.234,"longitude":435345.233},{"latitude":23434.676,"longitude":159435.324}]
|
91
76
|
```
|
92
77
|
|
93
|
-
|
78
|
+
If a `#to_json` method isn't defined on the object, `JSON.generate` will fallback to call `#to_s`:
|
94
79
|
|
95
80
|
```ruby
|
96
|
-
|
97
|
-
data = JSON['{"test":23}'] # => {"test"=>23}
|
81
|
+
JSON.generate(Object.new) # => "#<Object:0x000000011e768b98>"
|
98
82
|
```
|
99
83
|
|
100
|
-
|
101
|
-
you
|
84
|
+
Both of these behavior can be disabled using the `strict: true` option:
|
102
85
|
|
103
86
|
```ruby
|
104
|
-
|
87
|
+
JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
|
88
|
+
JSON.generate(Position.new(1, 2)) # => Position not allowed in JSON (JSON::GeneratorError)
|
105
89
|
```
|
106
90
|
|
107
|
-
|
91
|
+
## JSON::Coder
|
92
|
+
|
93
|
+
Since `#to_json` methods are global, it can sometimes be problematic if you need a given type to be
|
94
|
+
serialized in different ways in different locations.
|
95
|
+
|
96
|
+
Instead it is recommended to use the newer `JSON::Coder` API:
|
108
97
|
|
109
98
|
```ruby
|
110
|
-
|
111
|
-
|
99
|
+
module MyApp
|
100
|
+
API_JSON_CODER = JSON::Coder.new do |object|
|
101
|
+
case object
|
102
|
+
when Time
|
103
|
+
object.iso8601(3)
|
104
|
+
else
|
105
|
+
object
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
112
109
|
|
113
|
-
|
114
|
-
|
110
|
+
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
|
111
|
+
```
|
115
112
|
|
116
|
-
|
113
|
+
The provided block is called for all objects that don't have a native JSON equivalent, and
|
114
|
+
must return a Ruby object that has a native JSON equivalent.
|
117
115
|
|
118
|
-
|
119
|
-
object to JSON, it will by default only include the exception message.
|
116
|
+
## Combining JSON fragments
|
120
117
|
|
121
|
-
To
|
122
|
-
above, or specifically load the exception addition:
|
118
|
+
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
123
119
|
|
124
120
|
```ruby
|
125
|
-
|
121
|
+
posts_json = cache.fetch_multi(post_ids) do |post_id|
|
122
|
+
JSON.generate(Post.find(post_id))
|
123
|
+
end
|
124
|
+
posts_json.map! { |post_json| JSON::Fragment.new(post_json) }
|
125
|
+
JSON.generate({ posts: posts_json, count: posts_json.count })
|
126
126
|
```
|
127
127
|
|
128
|
-
##
|
128
|
+
## Round-tripping arbitrary types
|
129
|
+
|
130
|
+
> [!CAUTION]
|
131
|
+
> You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,
|
132
|
+
> as it can lead to remote code execution vulnerabilities.
|
129
133
|
|
130
134
|
To create a JSON document from a ruby data structure, you can call
|
131
135
|
`JSON.generate` like that:
|
@@ -191,7 +195,7 @@ JSON.parse json
|
|
191
195
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
192
196
|
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
193
197
|
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
194
|
-
JSON.
|
198
|
+
JSON.unsafe_load json
|
195
199
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
196
200
|
```
|
197
201
|
|
@@ -3,79 +3,143 @@
|
|
3
3
|
|
4
4
|
#include "ruby.h"
|
5
5
|
#include "ruby/encoding.h"
|
6
|
+
#include "../vendor/jeaiii-ltoa.h"
|
7
|
+
|
8
|
+
/* shims */
|
9
|
+
/* This is the fallback definition from Ruby 3.4 */
|
10
|
+
|
11
|
+
#ifndef RBIMPL_STDBOOL_H
|
12
|
+
#if defined(__cplusplus)
|
13
|
+
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
|
14
|
+
# include <cstdbool>
|
15
|
+
# endif
|
16
|
+
#elif defined(HAVE_STDBOOL_H)
|
17
|
+
# include <stdbool.h>
|
18
|
+
#elif !defined(HAVE__BOOL)
|
19
|
+
typedef unsigned char _Bool;
|
20
|
+
# define bool _Bool
|
21
|
+
# define true ((_Bool)+1)
|
22
|
+
# define false ((_Bool)+0)
|
23
|
+
# define __bool_true_false_are_defined
|
24
|
+
#endif
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#ifndef RB_UNLIKELY
|
28
|
+
#define RB_UNLIKELY(expr) expr
|
29
|
+
#endif
|
30
|
+
|
31
|
+
#ifndef RB_LIKELY
|
32
|
+
#define RB_LIKELY(expr) expr
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#ifndef MAYBE_UNUSED
|
36
|
+
# define MAYBE_UNUSED(x) x
|
37
|
+
#endif
|
38
|
+
|
39
|
+
enum fbuffer_type {
|
40
|
+
FBUFFER_HEAP_ALLOCATED = 0,
|
41
|
+
FBUFFER_STACK_ALLOCATED = 1,
|
42
|
+
};
|
6
43
|
|
7
44
|
typedef struct FBufferStruct {
|
45
|
+
enum fbuffer_type type;
|
8
46
|
unsigned long initial_length;
|
9
|
-
char *ptr;
|
10
47
|
unsigned long len;
|
11
48
|
unsigned long capa;
|
49
|
+
char *ptr;
|
50
|
+
VALUE io;
|
12
51
|
} FBuffer;
|
13
52
|
|
53
|
+
#define FBUFFER_STACK_SIZE 512
|
54
|
+
#define FBUFFER_IO_BUFFER_SIZE (16384 - 1)
|
14
55
|
#define FBUFFER_INITIAL_LENGTH_DEFAULT 1024
|
15
56
|
|
16
|
-
#define FBUFFER_PTR(fb) (fb->ptr)
|
17
|
-
#define FBUFFER_LEN(fb) (fb->len)
|
18
|
-
#define FBUFFER_CAPA(fb) (fb->capa)
|
57
|
+
#define FBUFFER_PTR(fb) ((fb)->ptr)
|
58
|
+
#define FBUFFER_LEN(fb) ((fb)->len)
|
59
|
+
#define FBUFFER_CAPA(fb) ((fb)->capa)
|
19
60
|
#define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
|
20
61
|
|
21
|
-
static FBuffer *fbuffer_alloc(unsigned long initial_length);
|
22
62
|
static void fbuffer_free(FBuffer *fb);
|
23
|
-
#ifndef JSON_GENERATOR
|
24
63
|
static void fbuffer_clear(FBuffer *fb);
|
25
|
-
#endif
|
26
64
|
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
|
27
|
-
#ifdef JSON_GENERATOR
|
28
65
|
static void fbuffer_append_long(FBuffer *fb, long number);
|
29
|
-
|
30
|
-
static
|
31
|
-
#ifdef JSON_GENERATOR
|
32
|
-
static VALUE fbuffer_to_s(FBuffer *fb);
|
33
|
-
#endif
|
66
|
+
static inline void fbuffer_append_char(FBuffer *fb, char newchr);
|
67
|
+
static VALUE fbuffer_finalize(FBuffer *fb);
|
34
68
|
|
35
|
-
|
36
|
-
#define RB_UNLIKELY(expr) expr
|
37
|
-
#endif
|
38
|
-
|
39
|
-
static FBuffer *fbuffer_alloc(unsigned long initial_length)
|
69
|
+
static void fbuffer_stack_init(FBuffer *fb, unsigned long initial_length, char *stack_buffer, long stack_buffer_size)
|
40
70
|
{
|
41
|
-
|
42
|
-
if (
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
71
|
+
fb->initial_length = (initial_length > 0) ? initial_length : FBUFFER_INITIAL_LENGTH_DEFAULT;
|
72
|
+
if (stack_buffer) {
|
73
|
+
fb->type = FBUFFER_STACK_ALLOCATED;
|
74
|
+
fb->ptr = stack_buffer;
|
75
|
+
fb->capa = stack_buffer_size;
|
76
|
+
}
|
47
77
|
}
|
48
78
|
|
49
79
|
static void fbuffer_free(FBuffer *fb)
|
50
80
|
{
|
51
|
-
if (fb->ptr
|
52
|
-
|
81
|
+
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
|
82
|
+
ruby_xfree(fb->ptr);
|
83
|
+
}
|
53
84
|
}
|
54
85
|
|
55
|
-
#ifndef JSON_GENERATOR
|
56
86
|
static void fbuffer_clear(FBuffer *fb)
|
57
87
|
{
|
58
88
|
fb->len = 0;
|
59
89
|
}
|
60
|
-
#endif
|
61
90
|
|
62
|
-
static
|
91
|
+
static void fbuffer_flush(FBuffer *fb)
|
63
92
|
{
|
64
|
-
|
65
|
-
|
93
|
+
rb_io_write(fb->io, rb_utf8_str_new(fb->ptr, fb->len));
|
94
|
+
fbuffer_clear(fb);
|
95
|
+
}
|
66
96
|
|
67
|
-
|
68
|
-
|
69
|
-
|
97
|
+
static void fbuffer_realloc(FBuffer *fb, unsigned long required)
|
98
|
+
{
|
99
|
+
if (required > fb->capa) {
|
100
|
+
if (fb->type == FBUFFER_STACK_ALLOCATED) {
|
101
|
+
const char *old_buffer = fb->ptr;
|
102
|
+
fb->ptr = ALLOC_N(char, required);
|
103
|
+
fb->type = FBUFFER_HEAP_ALLOCATED;
|
104
|
+
MEMCPY(fb->ptr, old_buffer, char, fb->len);
|
105
|
+
} else {
|
106
|
+
REALLOC_N(fb->ptr, char, required);
|
70
107
|
}
|
108
|
+
fb->capa = required;
|
109
|
+
}
|
110
|
+
}
|
71
111
|
|
72
|
-
|
112
|
+
static void fbuffer_do_inc_capa(FBuffer *fb, unsigned long requested)
|
113
|
+
{
|
114
|
+
if (RB_UNLIKELY(fb->io)) {
|
115
|
+
if (fb->capa < FBUFFER_IO_BUFFER_SIZE) {
|
116
|
+
fbuffer_realloc(fb, FBUFFER_IO_BUFFER_SIZE);
|
117
|
+
} else {
|
118
|
+
fbuffer_flush(fb);
|
119
|
+
}
|
73
120
|
|
74
|
-
if (
|
75
|
-
|
76
|
-
fb->capa = required;
|
121
|
+
if (RB_LIKELY(requested < fb->capa)) {
|
122
|
+
return;
|
77
123
|
}
|
78
124
|
}
|
125
|
+
|
126
|
+
unsigned long required;
|
127
|
+
|
128
|
+
if (RB_UNLIKELY(!fb->ptr)) {
|
129
|
+
fb->ptr = ALLOC_N(char, fb->initial_length);
|
130
|
+
fb->capa = fb->initial_length;
|
131
|
+
}
|
132
|
+
|
133
|
+
for (required = fb->capa; requested > required - fb->len; required <<= 1);
|
134
|
+
|
135
|
+
fbuffer_realloc(fb, required);
|
136
|
+
}
|
137
|
+
|
138
|
+
static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
|
139
|
+
{
|
140
|
+
if (RB_UNLIKELY(requested > fb->capa - fb->len)) {
|
141
|
+
fbuffer_do_inc_capa(fb, requested);
|
142
|
+
}
|
79
143
|
}
|
80
144
|
|
81
145
|
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
|
@@ -87,7 +151,13 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
|
|
87
151
|
}
|
88
152
|
}
|
89
153
|
|
90
|
-
|
154
|
+
/* Appends a character into a buffer. The buffer needs to have sufficient capacity, via fbuffer_inc_capa(...). */
|
155
|
+
static inline void fbuffer_append_reserved_char(FBuffer *fb, char chr)
|
156
|
+
{
|
157
|
+
fb->ptr[fb->len] = chr;
|
158
|
+
fb->len += 1;
|
159
|
+
}
|
160
|
+
|
91
161
|
static void fbuffer_append_str(FBuffer *fb, VALUE str)
|
92
162
|
{
|
93
163
|
const char *newstr = StringValuePtr(str);
|
@@ -97,50 +167,70 @@ static void fbuffer_append_str(FBuffer *fb, VALUE str)
|
|
97
167
|
|
98
168
|
fbuffer_append(fb, newstr, len);
|
99
169
|
}
|
100
|
-
#endif
|
101
170
|
|
102
|
-
static void fbuffer_append_char(FBuffer *fb, char newchr)
|
171
|
+
static inline void fbuffer_append_char(FBuffer *fb, char newchr)
|
103
172
|
{
|
104
173
|
fbuffer_inc_capa(fb, 1);
|
105
174
|
*(fb->ptr + fb->len) = newchr;
|
106
175
|
fb->len++;
|
107
176
|
}
|
108
177
|
|
109
|
-
|
110
|
-
static void freverse(char *start, char *end)
|
178
|
+
static inline char *fbuffer_cursor(FBuffer *fb)
|
111
179
|
{
|
112
|
-
|
113
|
-
|
114
|
-
while (end > start) {
|
115
|
-
c = *end, *end-- = *start, *start++ = c;
|
116
|
-
}
|
180
|
+
return fb->ptr + fb->len;
|
117
181
|
}
|
118
182
|
|
119
|
-
static
|
183
|
+
static inline void fbuffer_advance_to(FBuffer *fb, char *end)
|
120
184
|
{
|
121
|
-
|
122
|
-
long sign = number;
|
123
|
-
char* tmp = buf;
|
124
|
-
|
125
|
-
if (sign < 0) number = -number;
|
126
|
-
do *tmp++ = digits[number % 10]; while (number /= 10);
|
127
|
-
if (sign < 0) *tmp++ = '-';
|
128
|
-
freverse(buf, tmp - 1);
|
129
|
-
return tmp - buf;
|
185
|
+
fb->len = end - fb->ptr;
|
130
186
|
}
|
131
187
|
|
188
|
+
/*
|
189
|
+
* Appends the decimal string representation of \a number into the buffer.
|
190
|
+
*/
|
132
191
|
static void fbuffer_append_long(FBuffer *fb, long number)
|
133
192
|
{
|
134
|
-
|
135
|
-
|
136
|
-
|
193
|
+
/*
|
194
|
+
* The jeaiii_ultoa() function produces digits left-to-right,
|
195
|
+
* allowing us to write directly into the buffer, but we don't know
|
196
|
+
* the number of resulting characters.
|
197
|
+
*
|
198
|
+
* We do know, however, that the `number` argument is always in the
|
199
|
+
* range 0xc000000000000000 to 0x3fffffffffffffff, or, in decimal,
|
200
|
+
* -4611686018427387904 to 4611686018427387903. The max number of chars
|
201
|
+
* generated is therefore 20 (including a potential sign character).
|
202
|
+
*/
|
203
|
+
|
204
|
+
static const int MAX_CHARS_FOR_LONG = 20;
|
205
|
+
|
206
|
+
fbuffer_inc_capa(fb, MAX_CHARS_FOR_LONG);
|
207
|
+
|
208
|
+
if (number < 0) {
|
209
|
+
fbuffer_append_reserved_char(fb, '-');
|
210
|
+
|
211
|
+
/*
|
212
|
+
* Since number is always > LONG_MIN, `-number` will not overflow
|
213
|
+
* and is always the positive abs() value.
|
214
|
+
*/
|
215
|
+
number = -number;
|
216
|
+
}
|
217
|
+
|
218
|
+
char *end = jeaiii_ultoa(fbuffer_cursor(fb), number);
|
219
|
+
fbuffer_advance_to(fb, end);
|
137
220
|
}
|
138
221
|
|
139
|
-
static VALUE
|
222
|
+
static VALUE fbuffer_finalize(FBuffer *fb)
|
140
223
|
{
|
141
|
-
|
142
|
-
|
143
|
-
|
224
|
+
if (fb->io) {
|
225
|
+
fbuffer_flush(fb);
|
226
|
+
fbuffer_free(fb);
|
227
|
+
rb_io_flush(fb->io);
|
228
|
+
return fb->io;
|
229
|
+
} else {
|
230
|
+
VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
|
231
|
+
fbuffer_free(fb);
|
232
|
+
return result;
|
233
|
+
}
|
144
234
|
}
|
145
|
-
|
235
|
+
|
146
236
|
#endif
|