Skip to content

Commit 10967f2

Browse files
committed
Replace *strip with trim* calls and optimize around
1 parent 5c7b53f commit 10967f2

File tree

10 files changed

+29
-35
lines changed

10 files changed

+29
-35
lines changed

lib/elixir/lib/io/ansi/docs.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ defmodule IO.ANSI.Docs do
5858
options = Keyword.merge(default_options, options)
5959
doc
6060
|> String.split(["\r\n", "\n"], trim: false)
61-
|> Enum.map(&String.rstrip/1)
61+
|> Enum.map(&String.trim_trailing/1)
6262
|> process([], "", options)
6363
end
6464

@@ -68,19 +68,19 @@ defmodule IO.ANSI.Docs do
6868

6969
defp process(["# " <> heading | rest], text, indent, options) do
7070
write_text(text, indent, options)
71-
write_h1(String.strip(heading), options)
71+
write_h1(String.trim(heading), options)
7272
process(rest, [], "", options)
7373
end
7474

7575
defp process(["## " <> heading | rest], text, indent, options) do
7676
write_text(text, indent, options)
77-
write_h2(String.strip(heading), options)
77+
write_h2(String.trim(heading), options)
7878
process(rest, [], "", options)
7979
end
8080

8181
defp process(["### " <> heading | rest], text, indent, options) do
8282
write_text(text, indent, options)
83-
write_h3(String.strip(heading), indent, options)
83+
write_h3(String.trim(heading), indent, options)
8484
process(rest, [], "", options)
8585
end
8686

@@ -285,8 +285,8 @@ defmodule IO.ANSI.Docs do
285285

286286
defp split_into_columns(line, options) do
287287
line
288-
|> String.strip(?|)
289-
|> String.strip()
288+
|> String.trim("|")
289+
|> String.trim()
290290
|> String.split(~r/\s\|\s/)
291291
|> Enum.map(&render_column(&1, options))
292292
end

lib/elixir/lib/option_parser.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ defmodule OptionParser do
374374
"""
375375
@spec split(String.t) :: argv
376376
def split(string) do
377-
do_split(strip_leading_spaces(string), "", [], nil)
377+
do_split(String.trim_leading(string, " "), "", [], nil)
378378
end
379379

380380
# If we have an escaped quote, simply remove the escape
@@ -395,7 +395,7 @@ defmodule OptionParser do
395395

396396
# If we have space and we are outside of a quote, start new segment
397397
defp do_split(<<?\s, t::binary>>, buffer, acc, nil),
398-
do: do_split(strip_leading_spaces(t), "", [buffer | acc], nil)
398+
do: do_split(String.trim_leading(t, " "), "", [buffer | acc], nil)
399399

400400
# All other characters are moved to buffer
401401
defp do_split(<<h, t::binary>>, buffer, acc, quote) do
@@ -414,9 +414,6 @@ defmodule OptionParser do
414414
raise "argv string did not terminate properly, a #{<<marker>>} was opened but never closed"
415415
end
416416

417-
defp strip_leading_spaces(" " <> t), do: strip_leading_spaces(t)
418-
defp strip_leading_spaces(t), do: t
419-
420417
## Helpers
421418

422419
defp compile_config(opts) do
@@ -618,7 +615,7 @@ defmodule OptionParser do
618615
end
619616

620617
defp get_type(option, opts, types) do
621-
option_key = option |> String.lstrip(?-) |> get_option()
618+
option_key = option |> String.trim_leading("-") |> get_option()
622619

623620
if option_alias = opts[:aliases][option_key] do
624621
types[option_alias]

lib/elixir/test/elixir/io/ansi/docs_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ defmodule IO.ANSI.DocsTest do
55
import ExUnit.CaptureIO
66

77
def format_heading(str) do
8-
capture_io(fn -> IO.ANSI.Docs.print_heading(str, []) end) |> String.rstrip
8+
capture_io(fn -> IO.ANSI.Docs.print_heading(str, []) end) |> String.trim_trailing
99
end
1010

1111
def format(str) do
12-
capture_io(fn -> IO.ANSI.Docs.print(str, []) end) |> String.rstrip
12+
capture_io(fn -> IO.ANSI.Docs.print(str, []) end) |> String.trim_trailing
1313
end
1414

1515
test "heading is formatted" do

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ defmodule Kernel.WarningTest do
273273
output = capture_err(fn ->
274274
Code.compile_string """
275275
defmodule Sample do
276-
import String, only: [upcase: 1, downcase: 1, strip: 1]
276+
import String, only: [upcase: 1, downcase: 1, trim: 1]
277277
def a, do: upcase("hello")
278278
end
279279
"""
280280
end)
281281
assert output =~ "unused import String.downcase/1"
282-
assert output =~ "unused import String.strip/1"
282+
assert output =~ "unused import String.trim/1"
283283
after
284284
purge Sample
285285
end

lib/elixir/test/elixir/system_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule SystemTest do
1818

1919
version_file = Path.join([__DIR__, "../../../..", "VERSION"]) |> Path.expand
2020
{:ok, version} = File.read(version_file)
21-
assert build_info[:version] == String.strip(version)
21+
assert build_info[:version] == String.trim(version)
2222
assert build_info[:build] != ""
2323
end
2424

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ defmodule ExUnit.DocTest do
300300
actual ->
301301
reraise ExUnit.AssertionError,
302302
[message: "Doctest failed",
303-
expr: "#{unquote(String.strip(expr))} === #{unquote(String.strip(expected))}",
303+
expr: "#{unquote(String.trim(expr))} === #{unquote(String.trim(expected))}",
304304
left: actual],
305305
unquote(stack)
306306
end
@@ -318,7 +318,7 @@ defmodule ExUnit.DocTest do
318318
actual ->
319319
reraise ExUnit.AssertionError,
320320
[message: "Doctest failed",
321-
expr: "inspect(#{unquote(String.strip(expr))}) === #{unquote(String.strip(expected))}",
321+
expr: "inspect(#{unquote(String.trim(expr))}) === #{unquote(String.trim(expected))}",
322322
left: actual],
323323
unquote(stack)
324324
end
@@ -330,7 +330,7 @@ defmodule ExUnit.DocTest do
330330

331331
quote do
332332
stack = unquote(stack)
333-
expr = unquote(String.strip(expr))
333+
expr = unquote(String.trim(expr))
334334
spec = inspect(unquote(exception)) <> " with message " <> inspect(unquote(message))
335335

336336
try do
@@ -369,7 +369,7 @@ defmodule ExUnit.DocTest do
369369
quote do
370370
reraise ExUnit.AssertionError,
371371
[message: "Doctest did not compile, got: #{unquote(message)}",
372-
expr: unquote(String.strip(expr))],
372+
expr: unquote(String.trim(expr))],
373373
unquote(stack)
374374
end
375375
end
@@ -429,7 +429,7 @@ defmodule ExUnit.DocTest do
429429
end
430430

431431
defp adjust_indent(:text, [line | rest], line_no, adjusted_lines, indent, module) do
432-
case String.starts_with?(String.lstrip(line), @iex_prompt) do
432+
case String.starts_with?(String.trim_leading(line), @iex_prompt) do
433433
true ->
434434
adjust_indent(:prompt, [line | rest], line_no, adjusted_lines, get_indent(line, indent), module)
435435
false ->
@@ -440,7 +440,7 @@ defmodule ExUnit.DocTest do
440440
defp adjust_indent(kind, [line | rest], line_no, adjusted_lines, indent, module) when kind in [:prompt, :after_prompt] do
441441
stripped_line = strip_indent(line, indent)
442442

443-
case String.lstrip(line) do
443+
case String.trim_leading(line) do
444444
"" ->
445445
raise Error, line: line_no, module: module,
446446
message: "expected non-blank line to follow iex> prompt"
@@ -472,7 +472,7 @@ defmodule ExUnit.DocTest do
472472
cond do
473473
stripped_line == "" ->
474474
adjust_indent(:text, rest, line_no + 1, [{stripped_line, line_no} | adjusted_lines], 0, module)
475-
String.starts_with?(String.lstrip(line), @iex_prompt) ->
475+
String.starts_with?(String.trim_leading(line), @iex_prompt) ->
476476
adjust_indent(:prompt, [line | rest], line_no, adjusted_lines, indent, module)
477477
true ->
478478
adjust_indent(:code, rest, line_no + 1, [{stripped_line, line_no} | adjusted_lines], indent, module)
@@ -587,7 +587,7 @@ defmodule ExUnit.DocTest do
587587
case string do
588588
"** (" <> error ->
589589
[mod, message] = :binary.split(error, ")")
590-
{:error, Module.concat([mod]), String.lstrip(message)}
590+
{:error, Module.concat([mod]), String.trim_leading(message)}
591591
_ ->
592592
if string =~ ~r/^#[A-Z][\w\.]*<.*>$/ do
593593
{:inspect, inspect(string)}

lib/iex/lib/iex/helpers.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ defmodule IEx.Helpers do
399399
info = ["Term": inspect(term)] ++ IEx.Info.info(term)
400400

401401
for {subject, info} <- info do
402-
info = info |> to_string() |> String.strip() |> String.replace("\n", "\n ")
402+
info = info |> to_string() |> String.trim() |> String.replace("\n", "\n ")
403403
IO.puts IEx.color(:eval_result, to_string(subject))
404404
IO.puts IEx.color(:eval_info, " #{info}")
405405
end

lib/iex/test/iex/helpers_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ defmodule IEx.HelpersTest do
217217
File.cd! iex_path, fn ->
218218
paths = capture_io(fn -> ls end)
219219
|> String.split
220-
|> Enum.map(&String.strip(&1))
220+
|> Enum.map(&String.trim/1)
221221

222222
assert "ebin" in paths
223223
assert "mix.exs" in paths
@@ -444,7 +444,7 @@ defmodule IEx.HelpersTest do
444444

445445
test "i helper" do
446446
output = capture_iex ~s[i(:ok)]
447-
assert output == String.rstrip("""
447+
assert output == String.trim_trailing("""
448448
Term
449449
:ok
450450
Data type

lib/iex/test/test_helper.exs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,9 @@ defmodule IEx.Case do
6565

6666
defp strip_iex(string) do
6767
string
68-
|> strip_line # strip the greeting
69-
|> String.strip
70-
end
71-
72-
defp strip_line(string) do
73-
Regex.replace ~r/\A.+?$/ms, string, ""
68+
|> String.split("\n", parts: 2) # trim the greeting
69+
|> Enum.at(1)
70+
|> String.trim
7471
end
7572
end
7673

lib/mix/lib/mix/tasks/compile.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,6 @@ defmodule Mix.Tasks.Compile do
127127
end
128128

129129
defp first_line(doc) do
130-
String.split(doc, "\n", parts: 2) |> hd |> String.strip |> String.rstrip(?.)
130+
String.split(doc, "\n", parts: 2) |> hd |> String.trim |> String.trim_trailing(".")
131131
end
132132
end

0 commit comments

Comments
 (0)