String.strip/2 breaks for empty strings #643

alexrp opened this Issue Nov 14, 2012 · 1 comment


None yet

3 participants

iex(1)> String.strip("")
** (ArgumentError) argument error
    /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.

@josevalim josevalim closed this in 2104b29 Nov 14, 2012

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

 => ["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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment