Skip to content

Commit

Permalink
Apply linting to source and test code
Browse files Browse the repository at this point in the history
  • Loading branch information
erickguan committed Mar 9, 2024
1 parent 09f871a commit e03210d
Show file tree
Hide file tree
Showing 35 changed files with 1,571 additions and 1,459 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
source 'https://rubygems.org'

gemspec

group :development, :test do
gem 'rake', '>= 12.3.3'
gem 'rspec', '~> 3.9'
gem 'rubocop', '~> 1.60'
gem 'rubocop-minitest', '~> 0.34.5'
gem 'rubocop-packaging', '~> 0.5.2'
gem 'rubocop-rspec', '~> 2.27'
end
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rcov = true
end

task :default => :spec
task default: :spec

begin
require 'yard'
require('yard')
YARD::Rake::YardocTask.new
rescue LoadError
task :yardoc do
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
task(:yardoc) do
abort('YARD is not available. In order to run yardoc, you must: sudo gem install yard')
end
end
16 changes: 7 additions & 9 deletions benchmark/detect.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# encoding: utf-8
require 'benchmark'

require "benchmark"

$LOAD_PATH.unshift "lib"
require "ffi-icu"
require "rchardet"
$LOAD_PATH.unshift('lib')
require 'ffi-icu'
require 'rchardet'

TESTS = 1000

Benchmark.bmbm do |results|
results.report("rchardet:") { TESTS.times { CharDet.detect("æåø") } }
results.report("ffi-icu:") { TESTS.times { ICU::CharDet.detect("æåø") } }
end
results.report('rchardet:') { TESTS.times { CharDet.detect('æåø') } }
results.report('ffi-icu:') { TESTS.times { ICU::CharDet.detect('æåø') } }
end
26 changes: 15 additions & 11 deletions benchmark/shared.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# encoding: utf-8
require 'benchmark'

require "benchmark"

$LOAD_PATH.unshift "lib"
require "ffi-icu"
require "rchardet"
$LOAD_PATH.unshift('lib')
require 'ffi-icu'
require 'rchardet'

TESTS = 1000

$rchardet = CharDet::UniversalDetector.new
$icu = ICU::CharDet::Detector.new
rchardet = CharDet::UniversalDetector.new
icu = ICU::CharDet::Detector.new

Benchmark.bmbm do |results|
results.report("rchardet instance:") { TESTS.times { $rchardet.reset; $rchardet.feed("æåø"); $rchardet.result } }
results.report("ffi-icu instance:") { TESTS.times { $icu.detect("æåø") } }
end
results.report('rchardet instance:') do
TESTS.times do
rchardet.reset
rchardet.feed('æåø')
rchardet.result
end
end
results.report('ffi-icu instance:') { TESTS.times { icu.detect('æåø') } }
end
51 changes: 22 additions & 29 deletions ffi-icu.gemspec
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
require_relative "lib/ffi-icu/version"
require_relative 'lib/ffi-icu/version'

Gem::Specification.new do |spec|
spec.name = "ffi-icu"
spec.name = 'ffi-icu'
spec.version = ICU::VERSION
spec.platform = Gem::Platform::RUBY # rely on FFI library, but being platform-independent

spec.required_rubygems_version = Gem::Requirement.new(">= 2.5.0")
spec.authors = ["Jari Bakken"]
spec.date = "2019-10-15"
spec.licenses = ["MIT"]
spec.summary = "Simple Ruby FFI wrappers for International Components for Unicode (ICU)."
spec.description = "Provides charset detection, locale sensitive collation and more. Depends on libicu."
spec.email = "jari.bakken@gmail.com"
spec.homepage = "https://github.com/erickguan/ffi-icu"

spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "bin"
spec.required_rubygems_version = Gem::Requirement.new('>= 2.5.0')
spec.authors = ['Jari Bakken']
spec.licenses = ['MIT']
spec.summary = 'Simple Ruby FFI wrappers for International Components for Unicode (ICU).'
spec.description = 'Provides charset detection, locale sensitive collation and more. Depends on libicu.'
spec.email = 'jari.bakken@gmail.com'
spec.homepage = 'https://github.com/erickguan/ffi-icu'

spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"

spec.files = Dir['lib/**/*.rb', 'Gemfile', 'ffi-icu.gemspec', 'Rakefile']
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.extra_rdoc_files = ['LICENSE', 'README.md']
spec.rdoc_options = ['--charset=UTF-8']

spec.extra_rdoc_files = ["LICENSE", "README.md"]
spec.rdoc_options = ["--charset=UTF-8"]
spec.required_ruby_version = '>= 3.0.0'

spec.add_runtime_dependency "ffi", "~> 1.0", ">= 1.0.9"
spec.add_runtime_dependency "bigdecimal", "~> 3.1"
spec.add_runtime_dependency('bigdecimal', '~> 3.1')
spec.add_runtime_dependency('ffi', '~> 1.0', '>= 1.0.9')

spec.add_development_dependency('rake', '>= 12.3.3')
spec.add_development_dependency('rspec', '~> 3.9')
spec.add_development_dependency('rubocop', '~> 1.60')
spec.add_development_dependency('rubocop-rspec', '~> 2.27')
spec.add_development_dependency('rubocop-minitest', '~> 0.34.5')
spec.add_development_dependency('rubocop-packaging', '~> 0.5.2')
spec.metadata['rubygems_mfa_required'] = 'true'
end
34 changes: 17 additions & 17 deletions lib/ffi-icu.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "rbconfig"
require "ffi"
require 'rbconfig'
require 'ffi'

module ICU
def self.platform
os = RbConfig::CONFIG["host_os"]
os = RbConfig::CONFIG['host_os']

case os
when /darwin/
Expand All @@ -20,17 +20,17 @@ def self.platform
end
end

require "ffi-icu/core_ext/string"
require "ffi-icu/lib"
require "ffi-icu/lib/util"
require "ffi-icu/uchar"
require "ffi-icu/chardet"
require "ffi-icu/collation"
require "ffi-icu/locale"
require "ffi-icu/transliteration"
require "ffi-icu/normalization"
require "ffi-icu/normalizer"
require "ffi-icu/break_iterator"
require "ffi-icu/number_formatting"
require "ffi-icu/time_formatting"
require "ffi-icu/duration_formatting"
require 'ffi-icu/core_ext/string'
require 'ffi-icu/lib'
require 'ffi-icu/lib/util'
require 'ffi-icu/uchar'
require 'ffi-icu/chardet'
require 'ffi-icu/collation'
require 'ffi-icu/locale'
require 'ffi-icu/transliteration'
require 'ffi-icu/normalization'
require 'ffi-icu/normalizer'
require 'ffi-icu/break_iterator'
require 'ffi-icu/number_formatting'
require 'ffi-icu/time_formatting'
require 'ffi-icu/duration_formatting'
35 changes: 17 additions & 18 deletions lib/ffi-icu/break_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class BreakIterator

def self.available_locales
(0...Lib.ubrk_countAvailable).map do |idx|
Lib.ubrk_getAvailable idx
Lib.ubrk_getAvailable(idx)
end
end

Expand All @@ -20,33 +20,33 @@ def initialize(type, locale)
def text=(str)
@text = str

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

def each(&blk)
def each
return to_enum(:each) unless block_given?

int = first

while int != DONE
yield int
yield(int)
int = self.next
end

self
end

def each_substring(&blk)
def each_substring
return to_enum(:each_substring) unless block_given?

# each_char needed for 1.8, where String#[] works on bytes, not characters
chars = text.each_char.to_a
low = first

while (high = self.next) != DONE
yield chars[low...high].join
yield(chars[low...high].join)
low = high
end

Expand All @@ -58,36 +58,35 @@ def substrings
end

def next
Lib.ubrk_next @iterator
Lib.ubrk_next(@iterator)
end

def previous
Lib.ubrk_next @iterator
Lib.ubrk_next(@iterator)
end

def first
Lib.ubrk_first @iterator
Lib.ubrk_first(@iterator)
end

def last
Lib.ubrk_last @iterator
Lib.ubrk_last(@iterator)
end

def preceding(offset)
Lib.ubrk_preceding @iterator, Integer(offset)
Lib.ubrk_preceding(@iterator, Integer(offset))
end

def following(offset)
Lib.ubrk_following @iterator, Integer(offset)
Lib.ubrk_following(@iterator, Integer(offset))
end

def current
Lib.ubrk_current @iterator
Lib.ubrk_current(@iterator)
end

def boundary?(offset)
Lib.ubrk_isBoundary(@iterator, Integer(offset)) != 0
end

end # BreakIterator
end # ICU
end
end
19 changes: 8 additions & 11 deletions lib/ffi-icu/chardet.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
module ICU
module CharDet

def self.detect(string)
Detector.new.detect string
Detector.new.detect(string)
end

class Detector
Match = Struct.new(:name, :confidence, :language)

def initialize
ptr = Lib.check_error { |err| Lib.ucsdet_open err }
ptr = Lib.check_error { |err| Lib.ucsdet_open(err) }
@detector = FFI::AutoPointer.new(ptr, Lib.method(:ucsdet_close))
end

def input_filter_enabled?
Lib.ucsdet_isInputFilterEnabled @detector
Lib.ucsdet_isInputFilterEnabled(@detector)
end

def input_filter_enabled=(bool)
Expand All @@ -37,7 +36,7 @@ def detect(str)
def detect_all(str)
set_text(str)

matches_found_ptr = FFI::MemoryPointer.new :int32_t
matches_found_ptr = FFI::MemoryPointer.new(:int32_t)
array_ptr = Lib.check_error do |status|
Lib.ucsdet_detectAll(@detector, matches_found_ptr, status)
end
Expand Down Expand Up @@ -71,14 +70,12 @@ def match_ptr_to_ruby(match_ptr)
result
end

def set_text(text)
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)
end
end

end # Detector
end # CharDet
end # ICU

end
end
end
Loading

0 comments on commit e03210d

Please sign in to comment.