@@ -183,7 +183,7 @@ defmodule StringIO do
183183    _  =  Process . monitor ( pid ) 
184184    capture_prompt  =  options [ :capture_prompt ]  ||  false 
185185    encoding  =  options [ :encoding ]  ||  :unicode 
186-     { :ok ,  % { encoding:  encoding ,  input:  string ,  output:  "" ,  capture_prompt:  capture_prompt } } 
186+     { :ok ,  % { encoding:  encoding ,  input:  string ,  output:  [ ] ,  capture_prompt:  capture_prompt } } 
187187  end 
188188
189189  @ impl  true 
@@ -208,18 +208,18 @@ defmodule StringIO do
208208
209209  @ impl  true 
210210  def  handle_call ( :contents ,  _from ,  % { input:  input ,  output:  output }  =  state )  do 
211-     # output is built as iodata, and converted before we need to cross the process boundary 
212-     output   =   IO . iodata_to_binary ( output ) 
213-     { :reply ,  { input ,  output } ,  % { state  |  output:  output } } 
211+     string_output   =   reverse_join ( output ) 
212+     # update the state too to avoid having to do the same joining in future calls 
213+     { :reply ,  { input ,  string_output } ,  % { state  |  output:  [ string_output ] } } 
214214  end 
215215
216216  def  handle_call ( :flush ,  _from ,  % { output:  output }  =  state )  do 
217-     { :reply ,  IO . iodata_to_binary ( output ) ,  % { state  |  output:  "" } } 
217+     { :reply ,  reverse_join ( output ) ,  % { state  |  output:  [ ] } } 
218218  end 
219219
220220  def  handle_call ( :close ,  _from ,  % { input:  input ,  output:  output }  =  state )  do 
221-     output  =  IO . iodata_to_binary ( output ) 
222-     { :stop ,  :normal ,  { :ok ,  { input ,  output } } ,  % { state  |  output:  output } } 
221+     string_output  =  reverse_join ( output ) 
222+     { :stop ,  :normal ,  { :ok ,  { input ,  string_output } } ,  % { state  |  output:  [ string_output ] } } 
223223  end 
224224
225225  defp  io_request ( from ,  reply_as ,  req ,  state )  do 
@@ -305,8 +305,7 @@ defmodule StringIO do
305305  defp  put_chars ( encoding ,  chars ,  req ,  state )  do 
306306    case  :unicode . characters_to_binary ( chars ,  encoding ,  state . encoding )  do 
307307      string  when  is_binary ( string )  -> 
308-         # we build output as iodata internally 
309-         { :ok ,  % { state  |  output:  [ state . output  |  string ] } } 
308+         { :ok ,  % { state  |  output:  [ string  |  state . output ] } } 
310309
311310      { _ ,  _ ,  _ }  -> 
312311        { { :error ,  { :no_translation ,  encoding ,  state . encoding } } ,  state } 
@@ -454,12 +453,14 @@ defmodule StringIO do
454453
455454  ## helpers 
456455
456+   defp  reverse_join ( output ) ,  do:  output  |>  :lists . reverse ( )  |>  IO . iodata_to_binary ( ) 
457+ 
457458  defp  state_after_read ( % { capture_prompt:  false }  =  state ,  remainder ,  _prompt ,  _count )  do 
458459    % { state  |  input:  remainder } 
459460  end 
460461
461462  defp  state_after_read ( % { capture_prompt:  true ,  output:  output }  =  state ,  remainder ,  prompt ,  count )  do 
462-     output  =  IO . iodata_to_binary ( [ output   |   :binary . copy ( IO . chardata_to_string ( prompt ) ,  count ) ] ) 
463+     output  =  [ :binary . copy ( IO . chardata_to_string ( prompt ) ,  count )   |   output ] 
463464    % { state  |  input:  remainder ,  output:  output } 
464465  end 
465466
0 commit comments