Skip to content

Commit

Permalink
Remove monkeypatch to String
Browse files Browse the repository at this point in the history
String#bytesize returns byte size which might be longer than character
size.
  • Loading branch information
erickguan committed Mar 11, 2024
1 parent 0ff6306 commit 2d961a0
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@

### Removed

- Stop monkeypatching `String#bytesize` or `String#jlength`.
1 change: 0 additions & 1 deletion lib/ffi-icu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def self.platform
end
end

require 'ffi-icu/core_ext/string'
require 'ffi-icu/lib'
require 'ffi-icu/lib/util'
require 'ffi-icu/uchar'
Expand Down
2 changes: 1 addition & 1 deletion lib/ffi-icu/break_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def text=(str)
@text = str

Lib.check_error do |err|
Lib.ubrk_setText(@iterator, UCharPointer.from_string(str), str.jlength, err)
Lib.ubrk_setText(@iterator, UCharPointer.from_string(str), str.size, err)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ffi-icu/chardet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def input_filter_enabled=(bool)

def declared_encoding=(str)
Lib.check_error do |ptr|
Lib.ucsdet_setDeclaredEncoding(@detector, str, str.bytesize, ptr)
Lib.ucsdet_setDeclaredEncoding(@detector, str, str.size, ptr)
end
end

Expand Down Expand Up @@ -75,7 +75,7 @@ def match_ptr_to_ruby(match_ptr)
def set_text(text) # rubocop:disable Naming/AccessorMethodName
Lib.check_error do |status|
data = FFI::MemoryPointer.from_string(text)
Lib.ucsdet_setText(@detector, data, text.bytesize, status)
Lib.ucsdet_setText(@detector, data, text.size, status)
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/ffi-icu/collation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ def locale
def compare(a, b)
Lib.ucol_strcoll(
@c,
UCharPointer.from_string(a), a.jlength,
UCharPointer.from_string(b), b.jlength
UCharPointer.from_string(a), a.size,
UCharPointer.from_string(b), b.size
)
end

def greater?(a, b)
Lib.ucol_greater(@c, UCharPointer.from_string(a), a.jlength,
UCharPointer.from_string(b), b.jlength)
Lib.ucol_greater(@c, UCharPointer.from_string(a), a.size,
UCharPointer.from_string(b), b.size)
end

def greater_or_equal?(a, b)
Lib.ucol_greaterOrEqual(@c, UCharPointer.from_string(a), a.jlength,
UCharPointer.from_string(b), b.jlength)
Lib.ucol_greaterOrEqual(@c, UCharPointer.from_string(a), a.size,
UCharPointer.from_string(b), b.size)
end

def equal?(*args)
Expand All @@ -96,8 +96,8 @@ def equal?(*args)

a, b = args

Lib.ucol_equal(@c, UCharPointer.from_string(a), a.jlength,
UCharPointer.from_string(b), b.jlength)
Lib.ucol_equal(@c, UCharPointer.from_string(a), a.size,
UCharPointer.from_string(b), b.size)
end

def collate(sortable)
Expand All @@ -116,9 +116,9 @@ def rules

def collation_key(string)
ptr = UCharPointer.from_string(string)
size = Lib.ucol_getSortKey(@c, ptr, string.jlength, nil, 0)
size = Lib.ucol_getSortKey(@c, ptr, string.size, nil, 0)
buffer = FFI::MemoryPointer.new(:char, size)
Lib.ucol_getSortKey(@c, ptr, string.jlength, buffer, size)
Lib.ucol_getSortKey(@c, ptr, string.size, buffer, size)
buffer.read_bytes(size - 1)
end

Expand Down
7 changes: 0 additions & 7 deletions lib/ffi-icu/core_ext/string.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/ffi-icu/normalization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ICU
module Normalization
def self.normalize(input, mode = :default)
input_length = input.jlength
input_length = input.size
needed_length = out_length = options = 0
in_ptr = UCharPointer.from_string(input)
out_ptr = UCharPointer.new(out_length)
Expand Down
4 changes: 2 additions & 2 deletions lib/ffi-icu/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(package_name = nil, name = 'nfc', mode = :decompose)
end

def normalize(input)
input_length = input.jlength
input_length = input.size
in_ptr = UCharPointer.from_string(input)
needed_length = capacity = 0
out_ptr = UCharPointer.new(needed_length)
Expand All @@ -35,7 +35,7 @@ def normalize(input)
end

def is_normailzed?(input) # rubocop:disable Naming/PredicateName
input_length = input.jlength
input_length = input.size
in_ptr = UCharPointer.from_string(input)

Lib.check_error do |error|
Expand Down
4 changes: 2 additions & 2 deletions lib/ffi-icu/number_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ def format(number)
'ICU version is too old to have unum_format_decimal')
end
string_version = number.to_s
needed_length = Lib.unum_format_decimal(@f, string_version, string_version.bytesize, out_ptr,
needed_length = Lib.unum_format_decimal(@f, string_version, string_version.size, out_ptr,
needed_length, nil, error)
end
when BigDecimal
string_version = number.to_s('F')
needed_length = if Lib.respond_to?(:unum_format_decimal)
Lib.unum_format_decimal(@f, string_version, string_version.bytesize, out_ptr,
Lib.unum_format_decimal(@f, string_version, string_version.size, out_ptr,
needed_length, nil, error)
else
Lib.unum_format_double(@f, number.to_f, out_ptr, needed_length, nil, error)
Expand Down
4 changes: 2 additions & 2 deletions lib/ffi-icu/transliteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def initialize(id, rules = nil, direction = :forward)
rules_length = 0

if rules
rules_length = rules.jlength + 1
rules_length = rules.size + 1
rules = UCharPointer.from_string(rules)
end

parse_error = Lib::UParseError.new
begin
Lib.check_error do |status|
ptr = Lib.utrans_openU(UCharPointer.from_string(id), id.jlength, direction, rules, rules_length,
ptr = Lib.utrans_openU(UCharPointer.from_string(id), id.size, direction, rules, rules_length,
@parse_error, status)
@tr = FFI::AutoPointer.new(ptr, Lib.method(:utrans_close))
end
Expand Down

0 comments on commit 2d961a0

Please sign in to comment.