Skip to content

Commit

Permalink
Limited encoding (#368)
Browse files Browse the repository at this point in the history
* WIP

* Format and test limited encoding

* Format and test limited encoding

* Prepare release

* Fix rubocop complaints
  • Loading branch information
ohler55 authored Jan 12, 2025
1 parent a9a4aa7 commit c9c42ce
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All changes to the Ox gem are documented here. Releases follow semantic versioning.

## [2.14.20] - 2025-01-12

### Fixed

- The instruction encoding attribute is now used to set the encoding in the `:limited` mode.

## [2.14.19] - 2024-12-25

### Fixed
Expand Down
9 changes: 9 additions & 0 deletions ext/ox/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ static void read_instruction(PInfo pi) {
} else {
pi->pcb->instruct(pi, target, attrs.head, content_ptr);
}
} else {
for (Attr a = attrs.head; a < attrs.tail; a++) {
if (0 == strcasecmp(a->name, "encoding")) {
strncpy(pi->options->encoding, a->value, sizeof(pi->options->encoding) - 1);
pi->options->encoding[sizeof(pi->options->encoding) - 1] = '\0';
pi->options->rb_enc = rb_enc_find(a->value);
break;
}
}
}
attr_stack_cleanup(&attrs);
if (content_ptr != content) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ox/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Ox
# Current version of the module.
VERSION = '2.14.19'
VERSION = '2.14.20'
end
12 changes: 6 additions & 6 deletions test/perf_mars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
require 'optparse'
require 'ox'

it = 5000
iter = 5000

opts = OptionParser.new
opts.on('-i', '--iterations [Int]', Integer, 'iterations') { |i| it = i }
opts.on('-i', '--iterations [Int]', Integer, 'iterations') { |i| iter = i }
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
files = opts.parse(ARGV)

Expand Down Expand Up @@ -80,28 +80,28 @@ def initialize(v=[])
# pp a
# puts xml
start = Time.now
(1..it).each do
(1..iter).each do
obj = Ox.load(xml, mode: :object)
# pp obj
end
ox_load_time = Time.now - start

m = Marshal.dump(a)
start = Time.now
(1..it).each do
(1..iter).each do
obj = Marshal.load(m)
end
mars_load_time = Time.now - start

obj = Ox.load(xml, mode: :object)
start = Time.now
(1..it).each do
(1..iter).each do
xml = Ox.dump(a, indent: -1)
end
ox_dump_time = Time.now - start

start = Time.now
(1..it).each do
(1..iter).each do
m = Marshal.dump(a)
end
mars_dump_time = Time.now - start
Expand Down
7 changes: 7 additions & 0 deletions test/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,13 @@ def dump_and_load(obj, trace=false, circular=false)
end
loaded
end

def test_limit_encoding
Ox.default_options = $ox_object_options
xml = '<?xml version="1.0" encoding="UTF-8"?><doc><name>Martin</name></doc>'.encode('ASCII-8BIT')
enc = Ox.load(xml, mode: :limited).locate('name').first.text.encoding
assert_equal('UTF-8', enc.to_s)
end
end

class Bag
Expand Down

0 comments on commit c9c42ce

Please sign in to comment.