Permalink
Browse files

Added encoding handling for ruby 1.9

Patch adds ability to transcode to and from
the user's encoding.

Also when the user tries to get suggestion from a multibyte word
he receives bytestream as answer. In ruby 1.9 we can present him with
a decent answer. In ruby 1.8.7 we do nothing, let the user handle it.

Signed-off-by: Potamianos Gregory <gregory@skroutz.gr>
Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
  • Loading branch information...
1 parent 25dfddc commit 3d5d4cd9d8569a5cb0ba55153bc923b8287ee644 Potamianos Gregory committed with Jul 6, 2012
Showing with 68 additions and 3 deletions.
  1. +39 −3 lib/ffi/aspell/speller.rb
  2. +12 −0 spec/ffi/aspell/suggestions.rb
  3. +17 −0 spec/helper.rb
View
@@ -118,7 +118,8 @@ def correct?(word)
end
speller = Aspell.speller_new(@config)
- correct = Aspell.speller_check(speller, word.to_s, word.bytesize)
+ correct = Aspell.speller_check(speller, handle_input(word.to_s),
+ word.bytesize)
Aspell.speller_delete(speller)
@@ -139,12 +140,13 @@ def suggestions(word)
end
speller = Aspell.speller_new(@config)
- list = Aspell.speller_suggest(speller, word, word.bytesize)
+ list = Aspell.speller_suggest(speller, handle_input(word),
+ word.bytesize)
suggestions = []
elements = Aspell.word_list_elements(list)
while word = Aspell.string_enumeration_next(elements)
- suggestions << word
+ suggestions << handle_output(word)
end
Aspell.string_enumeration_delete(elements)
@@ -267,6 +269,40 @@ def reset(key)
)
end
end
+
+ ##
+ # Converts word to encoding expected in aspell
+ # from current ruby encoding
+ #
+ # @param [String] word The word to convert
+ # return [String] word
+ #
+ def handle_input(word)
+ if defined?(Encoding)
+ enc = get('encoding')
+ word.encode!(enc)
+ end
+
+ word
+ end
+ private :handle_input
+
+ ##
+ # Converts word from aspell encoding to what ruby expects
+ #
+ # @param [String] word The word to convert
+ # return [String] word
+ #
+ def handle_output(word)
+ if defined?(Encoding)
+ enc = get('encoding')
+ word.force_encoding(enc).encode!
+ end
+
+ word
+ end
+ private :handle_output
+
end # Speller
end # Aspell
end # FFI
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
require File.expand_path('../../../helper', __FILE__)
describe 'FFI::Aspell::Speller#suggestions' do
@@ -10,6 +12,16 @@
suggestions.include?('cooked').should == true
end
+ it 'Return a list of UTF-8 words' do
+ with_internal_encoding('UTF-8') do
+ speller = FFI::Aspell::Speller.new('el')
+ suggestions = speller.suggestions('χταπίδι')
+
+ suggestions.include?('χταπόδι').should == true
+ suggestions.include?('απίδι').should == true
+ end
+ end
+
it 'Return a list of word suggestions using the "bad-spellers" mode' do
speller = FFI::Aspell::Speller.new
View
@@ -5,3 +5,20 @@
Bacon.summary_on_exit
FIXTURES = File.expand_path('../fixtures', __FILE__)
+
+##
+# Used to change default internal encoding for certain tests.
+# Ruby uses default_internal (amongst others) when presenting
+# strings and when calling encode! without argument.
+# Rails explicitly sets it to UTF-8 on bootup
+#
+# @param [String] enc The encoding to switch to
+#
+def with_internal_encoding(enc)
+ if defined?(Encoding)
+ old_enc = Encoding.default_internal
+ Encoding.default_internal = enc
+ yield
+ Encoding.default_internal = old_enc
+ end
+end

0 comments on commit 3d5d4cd

Please sign in to comment.