Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTF-16LEな文字列でgetsが正しく動かない #68

Closed
ttdoda opened this issue Nov 4, 2023 · 0 comments · Fixed by #69
Closed

UTF-16LEな文字列でgetsが正しく動かない #68

ttdoda opened this issue Nov 4, 2023 · 0 comments · Fixed by #69

Comments

@ttdoda
Copy link

ttdoda commented Nov 4, 2023

概要

UTF-16LEな文字列から生成したStringIOで、getsが正しく動きません。

再現手順

require 'stringio'

str = "line1\nline2\nline3\n".encode("utf-16le")
sio = StringIO.new(str)

p sio.gets
p sio.gets
p sio.getbyte
p sio.gets

結果

"line1\x0A"
"\u6C00\u6900\u6E00\u6500\u3200\u0A00"
0
"line3\x0A"

1行目は改行を表すバイト列 \x0A \x00 の内、1バイト目までが読み込まれています。
2行目は前の行の残った \x00 から読まれるので、壊れた文字列になります。
3行目で余計な \x00 を読み飛ばすとそれ以降は改行の手前までは正しく読まれます。

kou added a commit that referenced this issue Nov 6, 2023
The conversion logic is borrowed from ruby/ruby's io.c:
https://github.com/ruby/ruby/blob/40391faeab608665da87a05c686c074f91a5a206/io.c#L4059-L4079

Fix #68

Reported by IWAMOTO Kouichi. Thanks!!!
@kou kou closed this as completed in #69 Nov 8, 2023
kou added a commit that referenced this issue Nov 8, 2023
matzbot pushed a commit to ruby/ruby that referenced this issue Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant