From 886ac785c3428ea22857a031dd5f5b17df0336bd Mon Sep 17 00:00:00 2001 From: Alex Dolski Date: Mon, 22 Apr 2024 09:00:44 -0500 Subject: [PATCH] StringUtils.utf8() uses String.encode() instead of iconv --- Gemfile | 2 -- Gemfile.lock | 2 -- app/util/string_utils.rb | 2 +- test/util/string_utils_test.rb | 7 +++++++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index ec068691..9d55cc13 100644 --- a/Gemfile +++ b/Gemfile @@ -27,8 +27,6 @@ gem "delayed_job_active_record" gem "haml-rails" # Application HTTP client gem "httpclient" -# Assists in converting non-Unicode text during full text extraction -gem "iconv" # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem "jbuilder" # Use jquery as the JavaScript library diff --git a/Gemfile.lock b/Gemfile.lock index 161a099f..7b3a2232 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -203,7 +203,6 @@ GEM httpclient (2.8.3) i18n (1.14.4) concurrent-ruby (~> 1.0) - iconv (1.0.8) io-console (0.7.2) irb (1.12.0) rdoc @@ -427,7 +426,6 @@ DEPENDENCIES delayed_job_active_record haml-rails httpclient - iconv jbuilder jquery-rails js_cookie_rails diff --git a/app/util/string_utils.rb b/app/util/string_utils.rb index 930935f0..fd829306 100644 --- a/app/util/string_utils.rb +++ b/app/util/string_utils.rb @@ -38,7 +38,7 @@ def self.url_encode(string) # @return [String] UTF-8 string. # def self.utf8(string) - Iconv.conv('UTF-8//IGNORE', 'UTF-8', string) + string.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?") end ## diff --git a/test/util/string_utils_test.rb b/test/util/string_utils_test.rb index 08fdf578..df0a5805 100644 --- a/test/util/string_utils_test.rb +++ b/test/util/string_utils_test.rb @@ -22,6 +22,13 @@ class StringUtilsTest < ActiveSupport::TestCase assert_equal "word%20word", StringUtils.url_encode("word word") end + # utf8() + + test "utf8() converts a non-UTF-8 string to UTF-8" do + str = [0x5a, 0xfc, 0x72, 0x69, 0x63, 0x68].pack('c*') + assert_equal "Z?rich", StringUtils.utf8(str) + end + # valid_email?() test "valid_email?() returns true for a valid email" do