Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limited encoding #368

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading