Skip to content

Commit e8ea196

Browse files
author
José Valim
committed
Merge pull request #2096 from devinus/stringio
StringIO improvements
2 parents f669e2b + 9fc7a1c commit e8ea196

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

lib/elixir/lib/string_io.ex

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ defmodule StringIO do
8484

8585
def init({ string, options }) do
8686
capture_prompt = options[:capture_prompt] || false
87-
8887
{ :ok, state(input: string, capture_prompt: capture_prompt) }
8988
end
9089

@@ -161,11 +160,11 @@ defmodule StringIO do
161160
end
162161

163162
defp io_request({ :setopts, _opts }, s) do
164-
{ :ok, s }
163+
{ { :error, :enotsup }, s }
165164
end
166165

167166
defp io_request(:getopts, s) do
168-
{ { :error, :enotsup }, s }
167+
{ [ binary: true, encoding: :unicode ], s }
169168
end
170169

171170
defp io_request({ :get_geometry, :columns }, s) do
@@ -200,20 +199,17 @@ defmodule StringIO do
200199
end
201200
end
202201

203-
defp do_get_chars("", _, _encoding) do
202+
defp do_get_chars("", _encoding, _n) do
204203
{ :eof, "" }
205204
end
206205

207-
defp do_get_chars(input, n, :latin1) when byte_size(input) < n do
206+
defp do_get_chars(input, :latin1, n) when byte_size(input) < n do
208207
{ input, "" }
209208
end
210209

211210
defp do_get_chars(input, :latin1, n) do
212-
if byte_size(input) < n do
213-
{ input, "" }
214-
else
215-
:erlang.split_binary(input, n)
216-
end
211+
<< chars :: [ binary, size(n) ], rest :: binary >> = input
212+
{ chars, rest }
217213
end
218214

219215
defp do_get_chars(input, encoding, n) do
@@ -222,7 +218,8 @@ defmodule StringIO do
222218
{ buf_count, split_pos } when buf_count < n or split_pos == :none ->
223219
{ input, "" }
224220
{ _buf_count, split_pos } ->
225-
:erlang.split_binary(input, split_pos)
221+
<< chars :: [ binary, size(split_pos) ], rest :: binary >> = input
222+
{ chars, rest }
226223
end
227224
catch
228225
:exit, :invalid_unicode ->
@@ -266,9 +263,9 @@ defmodule StringIO do
266263
state(input: input, output: output, capture_prompt: capture_prompt) = s) do
267264
case :unicode.characters_to_list(input, encoding) do
268265
{ :error, _, _ } ->
269-
{ :error, fun }
266+
{ :error, s }
270267
{ :incomplete, _, _ } ->
271-
{ :error, fun }
268+
{ :error, s }
272269
chars ->
273270
{ result, input, count } = do_get_until(chars, encoding, mod, fun, args)
274271

lib/elixir/test/elixir/string_io_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ defmodule StringIOTest do
8585
assert contents(pid) == { "", "" }
8686
end
8787

88-
test "IO.binread :line with \\rn" do
88+
test "IO.binread :line with \\r\\n" do
8989
pid = start("abc\r\n")
9090
assert IO.binread(pid, :line) == "abc\n"
9191
assert IO.binread(pid, :line) == :eof

0 commit comments

Comments
 (0)