json_escape 0.0.0 → 0.1.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: 67b2fb0af20b092995334983baefd17b3682e65442bbddcbaf6c21cd54fc8cad
4
- data.tar.gz: 4caebb4f7840f22477fc2b57ad10c7f44eb77a015221f689375ad5fd476d4436
3
+ metadata.gz: 5a389e1d0a7c064c39662f24a63d9b8d1e5fd2ee8020c79da2468dbd8a546bcd
4
+ data.tar.gz: ca09cd472d630efee98f0a7d4b0f7f66f970b8087c92bb8f3533ceefebace942
5
5
  SHA512:
6
- metadata.gz: c3056572f2465c7f75d551c3de03e34dd366482dcee6a85bcc662bfe7c97fb9650e64a4d3d8d0b950652c022e9e07869946254ef167b6c958aaa384892e29ad8
7
- data.tar.gz: 8bc9c230dc6f4c43e8253b3ce44dd547d8970e680d44f4e4d9a88c20e8b07cccca9faa7a7f60e52f5751a782965d9ed3d7950944863590b0a3433bed0c5b1e1c
6
+ metadata.gz: 8c31ed7c66c65cabe30cb10a1ce267fb39054e39e43f57a4e7213c8ee4b97f06dc23a56e1b6492f83c94f9f9acdf0bed6f998b5c0427e1da44281cbbcdd7b5b7
7
+ data.tar.gz: 1ffafad8b821031c3c5c73133a9f1cf1671bf3b5d69ff0b737526755c209cc727547943887ab6c2fa931b8f7e9ddaaabb6a12a77f37757009a230e82265277e4
data/README.md CHANGED
@@ -1,24 +1,14 @@
1
1
  # JsonEscape
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ This is a proof of concept for implementing ActiveSupport's `escape_json` in C
4
+ (and a pure-Ruby implemntation for platforms other than CRuby).
4
5
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/json_escape`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
-
15
- If bundler is not being used to manage dependencies, install the gem by executing:
16
-
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
6
+ This is to test, benchmark, and prove it's utility. I think this should be
7
+ upstreamed into another library.
18
8
 
19
9
  ## Usage
20
10
 
21
- TODO: Write usage instructions here
11
+ Don't.
22
12
 
23
13
  ## Development
24
14
 
data/Rakefile CHANGED
@@ -1,4 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- task default: %i[]
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ if RUBY_ENGINE == "jruby"
13
+ task :clobber # noop
14
+ task :compile # noop
15
+
16
+ task default: %i[test]
17
+ else
18
+ require "rake/extensiontask"
19
+
20
+ Rake::ExtensionTask.new("json_escape") do |ext|
21
+ ext.lib_dir = "lib/json_escape"
22
+ end
23
+
24
+ task default: %i[clobber compile test]
25
+ end
data/benchmark.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "benchmark/ips"
2
+ require "json_escape"
3
+
4
+ benchmarks = [
5
+ "twitter.json",
6
+ "citm_catalog.json",
7
+ "canada.json"
8
+ ]
9
+
10
+ Benchmark.ips do |x|
11
+ benchmarks.each do |benchmark|
12
+ data = File.read("#{__dir__}/test/benchmark_data/#{benchmark}")
13
+ x.report "JsonEscape.json_escape(#{benchmark})" do
14
+ JsonEscape.json_escape(data)
15
+ end
16
+
17
+ x.report "JsonEscape::Pure.json_escape(#{benchmark})" do
18
+ JsonEscape::Pure.json_escape(data)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ create_makefile("json_escape/json_escape")
@@ -0,0 +1,99 @@
1
+ #include "ruby.h"
2
+ #include "ruby/encoding.h"
3
+
4
+ VALUE rb_mJsonEscape;
5
+
6
+ /* strlen("\\u2029") == 6 */
7
+ #define ESCAPE_JSON_MAX_LEN 6
8
+
9
+ static inline long
10
+ json_escaped_length(VALUE str)
11
+ {
12
+ const long len = RSTRING_LEN(str);
13
+ if (len >= LONG_MAX / ESCAPE_JSON_MAX_LEN) {
14
+ ruby_malloc_size_overflow(len, ESCAPE_JSON_MAX_LEN);
15
+ }
16
+ return len * ESCAPE_JSON_MAX_LEN;
17
+ }
18
+
19
+ static VALUE
20
+ escape_json(VALUE self, VALUE str)
21
+ {
22
+ if (!RB_TYPE_P(str, T_STRING)) {
23
+ str = rb_convert_type(str, T_STRING, "String", "to_s");
24
+ }
25
+
26
+ rb_encoding *enc = rb_enc_get(str);
27
+ if (enc != rb_utf8_encoding() && enc != rb_usascii_encoding()) {
28
+ rb_raise(rb_eEncCompatError, "input string must be UTF-8 or ASCII");
29
+ }
30
+
31
+ const char *cstr = RSTRING_PTR(str);
32
+ const unsigned long str_len = RSTRING_LEN(str);
33
+ const char *end = cstr + RSTRING_LEN(str);
34
+
35
+ size_t initial_match = strcspn(cstr, "&<>\xe2");
36
+ if (initial_match == str_len) {
37
+ return str;
38
+ }
39
+
40
+ VALUE escaped = rb_str_buf_new(json_escaped_length(str));
41
+ rb_str_resize(escaped, json_escaped_length(str));
42
+ char *buf = RSTRING_PTR(escaped);
43
+ char *dest = buf;
44
+
45
+ memcpy(dest, cstr, initial_match);
46
+ cstr += initial_match;
47
+ dest += initial_match;
48
+
49
+ while (cstr < end) {
50
+ const char c = *cstr++;
51
+
52
+ #define JSON_ESCAPE_CONCAT(s) do { \
53
+ memcpy(dest, ("\\u" s), strlen(s) + 2); \
54
+ dest += strlen(s) + 2; \
55
+ } while (0)
56
+
57
+ if (0) {
58
+ }
59
+ else if (c == '&') {
60
+ JSON_ESCAPE_CONCAT("0026");
61
+ }
62
+ else if (c == '>') {
63
+ JSON_ESCAPE_CONCAT("003e");
64
+ }
65
+ else if (c == '<') {
66
+ JSON_ESCAPE_CONCAT("003c");
67
+ }
68
+ else if (c == '\xe2' && cstr[0] == '\x80' && cstr[1] == '\xa8') {
69
+ JSON_ESCAPE_CONCAT("2028");
70
+ cstr += 2;
71
+ }
72
+ else if (c == '\xe2' && cstr[0] == '\x80' && cstr[1] == '\xa9') {
73
+ JSON_ESCAPE_CONCAT("2029");
74
+ cstr += 2;
75
+ }
76
+ else {
77
+ *dest++ = c;
78
+ }
79
+
80
+ initial_match = strcspn(cstr, "&<>\xe2");
81
+ memcpy(dest, cstr, initial_match);
82
+ cstr += initial_match;
83
+ dest += initial_match;
84
+
85
+ #undef JSON_ESCAPE_CONCAT
86
+ }
87
+
88
+ rb_str_resize(escaped, dest - buf);
89
+ rb_enc_associate(escaped, rb_enc_get(str));
90
+
91
+ return escaped;
92
+ }
93
+
94
+ void
95
+ Init_json_escape(void)
96
+ {
97
+ rb_mJsonEscape = rb_define_module("JsonEscape");
98
+ rb_define_method(rb_mJsonEscape, "json_escape", escape_json, 1);
99
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef JSON_ESCAPE_H
2
+ #define JSON_ESCAPE_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* JSON_ESCAPE_H */
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonEscape
4
- VERSION = "0.0.0"
4
+ VERSION = "0.1.0"
5
5
  end
data/lib/json_escape.rb CHANGED
@@ -3,6 +3,26 @@
3
3
  require_relative "json_escape/version"
4
4
 
5
5
  module JsonEscape
6
- class Error < StandardError; end
7
- # Your code goes here...
6
+ module Pure
7
+ JSON_ESCAPE = { "&" => '\u0026', ">" => '\u003e', "<" => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
8
+ JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
9
+ private_constant :JSON_ESCAPE, :JSON_ESCAPE_REGEXP
10
+
11
+ def json_escape(json)
12
+ json = json.to_s
13
+ if json.encoding != Encoding::UTF_8 && json.encoding != Encoding::ASCII
14
+ raise Encoding::CompatibilityError, "input string must be UTF-8 or ASCII"
15
+ end
16
+ json.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
17
+ end
18
+ module_function :json_escape
19
+ end
20
+
21
+ begin
22
+ require_relative "json_escape/json_escape"
23
+ rescue LoadError
24
+ include Pure
25
+ end
26
+
27
+ module_function :json_escape
8
28
  end
metadata CHANGED
@@ -1,20 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_escape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-23 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: benchmark-ips
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
13
69
  description: JSON escape
14
70
  email:
15
71
  - john@hawthorn.email
16
72
  executables: []
17
- extensions: []
73
+ extensions:
74
+ - ext/json_escape/extconf.rb
18
75
  extra_rdoc_files: []
19
76
  files:
20
77
  - CODE_OF_CONDUCT.md
@@ -22,10 +79,12 @@ files:
22
79
  - LICENSE.txt
23
80
  - README.md
24
81
  - Rakefile
25
- - json_escape.gemspec
82
+ - benchmark.rb
83
+ - ext/json_escape/extconf.rb
84
+ - ext/json_escape/json_escape.c
85
+ - ext/json_escape/json_escape.h
26
86
  - lib/json_escape.rb
27
87
  - lib/json_escape/version.rb
28
- - sig/json_escape.rbs
29
88
  homepage: https://github.com/jhawthorn/json_escape
30
89
  licenses:
31
90
  - MIT
data/json_escape.gemspec DELETED
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/json_escape/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "json_escape"
7
- spec.version = JsonEscape::VERSION
8
- spec.authors = ["John Hawthorn"]
9
- spec.email = ["john@hawthorn.email"]
10
-
11
- spec.summary = "JSON escape"
12
- spec.description = "JSON escape"
13
- spec.homepage = "https://github.com/jhawthorn/json_escape"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
16
-
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = spec.homepage
19
- spec.metadata["changelog_uri"] = spec.homepage
20
-
21
- # Specify which files should be added to the gem when it is released.
22
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(__dir__) do
24
- `git ls-files -z`.split("\x0").reject do |f|
25
- (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
26
- end
27
- end
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
31
-
32
- # Uncomment to register a new dependency of your gem
33
- # spec.add_dependency "example-gem", "~> 1.0"
34
-
35
- # For more information and examples about making a new gem, check out our
36
- # guide at: https://bundler.io/guides/creating_gem.html
37
- end
data/sig/json_escape.rbs DELETED
@@ -1,4 +0,0 @@
1
- module JsonEscape
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end