Skip to content

Commit

Permalink
StringUtils.utf8() uses String.encode() instead of iconv
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Dolski committed Apr 22, 2024
1 parent 25b242a commit 886ac78
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -427,7 +426,6 @@ DEPENDENCIES
delayed_job_active_record
haml-rails
httpclient
iconv
jbuilder
jquery-rails
js_cookie_rails
Expand Down
2 changes: 1 addition & 1 deletion app/util/string_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down
7 changes: 7 additions & 0 deletions test/util/string_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 886ac78

Please sign in to comment.