Project

General

Profile

Actions

Patch #44734

closed

Use String#scrub to replace invalid UTF-8 byte sequences in Redmine::CodesetUtil.replace_invalid_utf8

Added by Go MAEDA 1 day ago. Updated about 22 hours ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:

Description

The attached patch improves the performance of Redmine::CodesetUtil.replace_invalid_utf8 by reimplementing it with String#scrub.

The method currently replaces invalid byte sequences with "?" by transcoding the string to UTF-16LE and back to UTF-8. This is a workaround from the time before String#scrub was added in Ruby 2.1:

str = str.encode("UTF-16LE", :invalid => :replace,
      :undef => :replace, :replace => '?').encode("UTF-8")

String#scrub does the same thing in a single pass:

str.dup.force_encoding('UTF-8').scrub('?')

On Ruby 3.3, both implementations returned the same result for 200,000 random strings containing various invalid byte sequences. String#scrub was about 7 times faster: 0.05 s compared with 0.36 s for 2,000 iterations on a 30KB string.


Files

Actions

Also available in: Atom PDF