From 31c4ead7ac2ee4420da9e1b94a3260baa9d1e112 Mon Sep 17 00:00:00 2001 From: Erick Guan Date: Sat, 9 Mar 2024 19:48:14 +0100 Subject: [PATCH] Remove monkeypatch to String String#bytesize is available now --- lib/ffi-icu.rb | 1 - lib/ffi-icu/break_iterator.rb | 2 +- lib/ffi-icu/collation.rb | 20 ++++++++++---------- lib/ffi-icu/core_ext/string.rb | 7 ------- lib/ffi-icu/normalization.rb | 2 +- lib/ffi-icu/normalizer.rb | 4 ++-- lib/ffi-icu/transliteration.rb | 4 ++-- 7 files changed, 16 insertions(+), 24 deletions(-) delete mode 100644 lib/ffi-icu/core_ext/string.rb diff --git a/lib/ffi-icu.rb b/lib/ffi-icu.rb index be8db33..0865fff 100644 --- a/lib/ffi-icu.rb +++ b/lib/ffi-icu.rb @@ -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' diff --git a/lib/ffi-icu/break_iterator.rb b/lib/ffi-icu/break_iterator.rb index 6817b95..01ea748 100644 --- a/lib/ffi-icu/break_iterator.rb +++ b/lib/ffi-icu/break_iterator.rb @@ -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.bytesize, err) end end diff --git a/lib/ffi-icu/collation.rb b/lib/ffi-icu/collation.rb index 21979cf..0b39367 100644 --- a/lib/ffi-icu/collation.rb +++ b/lib/ffi-icu/collation.rb @@ -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.bytesize, + UCharPointer.from_string(b), b.bytesize ) 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.bytesize, + UCharPointer.from_string(b), b.bytesize) 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.bytesize, + UCharPointer.from_string(b), b.bytesize) end def equal?(*args) @@ -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.bytesize, + UCharPointer.from_string(b), b.bytesize) end def collate(sortable) @@ -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.bytesize, nil, 0) buffer = FFI::MemoryPointer.new(:char, size) - Lib.ucol_getSortKey(@c, ptr, string.jlength, buffer, size) + Lib.ucol_getSortKey(@c, ptr, string.bytesize, buffer, size) buffer.read_bytes(size - 1) end diff --git a/lib/ffi-icu/core_ext/string.rb b/lib/ffi-icu/core_ext/string.rb deleted file mode 100644 index ffafc86..0000000 --- a/lib/ffi-icu/core_ext/string.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class String - alias bytesize length unless method_defined?(:bytesize) - - alias jlength length unless method_defined?(:jlength) -end diff --git a/lib/ffi-icu/normalization.rb b/lib/ffi-icu/normalization.rb index 7fbe5ae..a6906c7 100644 --- a/lib/ffi-icu/normalization.rb +++ b/lib/ffi-icu/normalization.rb @@ -3,7 +3,7 @@ module ICU module Normalization def self.normalize(input, mode = :default) - input_length = input.jlength + input_length = input.bytesize needed_length = out_length = options = 0 in_ptr = UCharPointer.from_string(input) out_ptr = UCharPointer.new(out_length) diff --git a/lib/ffi-icu/normalizer.rb b/lib/ffi-icu/normalizer.rb index 6c4cf34..c3bcde8 100644 --- a/lib/ffi-icu/normalizer.rb +++ b/lib/ffi-icu/normalizer.rb @@ -11,7 +11,7 @@ def initialize(package_name = nil, name = 'nfc', mode = :decompose) end def normalize(input) - input_length = input.jlength + input_length = input.bytesize in_ptr = UCharPointer.from_string(input) needed_length = capacity = 0 out_ptr = UCharPointer.new(needed_length) @@ -35,7 +35,7 @@ def normalize(input) end def is_normailzed?(input) # rubocop:disable Naming/PredicateName - input_length = input.jlength + input_length = input.bytesize in_ptr = UCharPointer.from_string(input) Lib.check_error do |error| diff --git a/lib/ffi-icu/transliteration.rb b/lib/ffi-icu/transliteration.rb index 1ee69e2..831ec6b 100644 --- a/lib/ffi-icu/transliteration.rb +++ b/lib/ffi-icu/transliteration.rb @@ -26,14 +26,14 @@ def initialize(id, rules = nil, direction = :forward) rules_length = 0 if rules - rules_length = rules.jlength + 1 + rules_length = rules.bytesize + 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.bytesize, direction, rules, rules_length, @parse_error, status) @tr = FFI::AutoPointer.new(ptr, Lib.method(:utrans_close)) end