elixir icon indicating copy to clipboard operation
elixir copied to clipboard

String.strip/2 breaks for empty strings

Open alexrp opened this issue 14 years ago • 1 comments

iex(1)> String.strip("")
** (ArgumentError) argument error
    :binary.last("")
    /home/alexrp/Projects/elixir/lib/elixir/lib/string.ex:194: String.rstrip/2
    erl_eval.erl:572: :erl_eval.do_apply/6
    src/elixir.erl:113: :elixir.eval_forms/3
    /home/alexrp/Projects/elixir/lib/iex/lib/iex/loop.ex:18: IEx.Loop.do_loop/1

The reason seems to be that the rstrip code unconditionally calls :binary.last/1 which will fail if the binary is empty.

alexrp avatar Nov 14 '12 03:11 alexrp

I also believe that it makes sense to ensure that the split doesn't break if you type an empty string as the second parameter String.split("elixir", "")

iex(3)> String.split("elixir","") 
** (ArgumentError) argument error
    binary.erl:84: :binary.split/3
    erl_eval.erl:572: :erl_eval.do_apply/6
    src/elixir.erl:113: :elixir.eval_forms/3
    /desenv/elixir/lib/iex/lib/iex/loop.ex:18: IEx.Loop.do_loop/1

I suggest to adopt the same solution of split that ruby has

"elixir".split("")
 => ["e", "l", "i", "x", "i", "r"] 

edit: I'm sorry guys, I read it wrong... this issue is applied for strip, the error I found is for split. should another issue be open?

pedrosnk avatar Nov 14 '12 18:11 pedrosnk