Project

General

Profile

Actions

Backport #6206

closed

encoding of empty string from String#split

Added by no6v (Nobuhiro IMAI) about 12 years ago. Updated almost 12 years ago.

Status:
Closed
[ruby-dev:45441]

Description

String#split が空文字列を返す場合に、エンコーディングが ASCII-8BIT になる時がありますが、
これは意図的でしょうか?

a = "a:".split(":", 2) # => ["a", ""]
a.map(&:encoding) # => [#Encoding:UTF-8, #Encoding:ASCII-8BIT]

関係あるかどうか分かりませんが、partition だと以下のようになります。

a = "a:".partition(":") # => ["a", ":", ""]
a.map(&:encoding) # => [#Encoding:UTF-8, #Encoding:UTF-8, #Encoding:UTF-8]

が、パターンが含まれていない場合は ASCII-8BIT になります。

a = "a:".partition("|") # => ["a:", "", ""]
a.map(&:encoding) # => [#Encoding:UTF-8, #Encoding:ASCII-8BIT, #Encoding:ASCII-8BIT]

SQLite3 が ASCII-8BIT の文字列を text なカラムに追加するときに、
勝手に blob になってしまって検索出来なくなるという問題を見かけました。

http://www.mew.org/pipermail/mew-dist/2012-March/029160.html

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0