Skip to content

Commit 87fee37

Browse files
author
José Valim
committed
Change String.next_grapheme/1 and String.next_codepoint/1 to return nil on string end
This is more composable in the sense can use in in if/cond constructs and also makes it easy to build streams. Closes #1721.
1 parent 4870351 commit 87fee37

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ defmodule IO.ANSI.Docs do
264264

265265
defp length_without_escape(rest, count) do
266266
case String.next_grapheme(rest) do
267-
:no_grapheme -> count
268267
{ _, rest } -> length_without_escape(rest, count + 1)
268+
nil -> count
269269
end
270270
end
271271

lib/elixir/lib/string.ex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ defmodule String do
562562
do_reverse(next_grapheme(rest), [grapheme|acc])
563563
end
564564

565-
defp do_reverse(:no_grapheme, acc), do: iolist_to_binary(acc)
565+
defp do_reverse(nil, acc), do: iolist_to_binary(acc)
566566

567567
@doc """
568568
Returns a binary `subject` duplicated `n` times.
@@ -602,7 +602,7 @@ defmodule String do
602602
Returns the next codepoint in a String.
603603
604604
The result is a tuple with the codepoint and the
605-
remaining of the string or `:no_codepoint` in case
605+
remaining of the string or `nil` in case
606606
the string reached its end.
607607
608608
As with other functions in the String module, this
@@ -617,7 +617,7 @@ defmodule String do
617617
618618
"""
619619
@compile { :inline, next_codepoint: 1 }
620-
@spec next_codepoint(t) :: {codepoint, t} | :no_codepoint
620+
@spec next_codepoint(t) :: {codepoint, t} | nil
621621
defdelegate next_codepoint(string), to: String.Unicode
622622

623623
@doc %S"""
@@ -693,7 +693,7 @@ defmodule String do
693693
Returns the next grapheme in a String.
694694
695695
The result is a tuple with the grapheme and the
696-
remaining of the string or `:no_grapheme` in case
696+
remaining of the string or `nil` in case
697697
the String reached its end.
698698
699699
## Examples
@@ -703,7 +703,7 @@ defmodule String do
703703
704704
"""
705705
@compile { :inline, next_grapheme: 1 }
706-
@spec next_grapheme(t) :: { grapheme, t } | :no_grapheme
706+
@spec next_grapheme(t) :: { grapheme, t } | nil
707707
defdelegate next_grapheme(string), to: String.Graphemes
708708

709709
@doc """
@@ -722,7 +722,7 @@ defmodule String do
722722
def first(string) do
723723
case next_grapheme(string) do
724724
{ char, _ } -> char
725-
:no_grapheme -> nil
725+
nil -> nil
726726
end
727727
end
728728

@@ -747,7 +747,7 @@ defmodule String do
747747
do_last(next_grapheme(rest), char)
748748
end
749749

750-
defp do_last(:no_grapheme, last_char), do: last_char
750+
defp do_last(nil, last_char), do: last_char
751751

752752
@doc """
753753
Returns the number of unicode graphemes in an utf8 string.
@@ -769,7 +769,7 @@ defmodule String do
769769
1 + do_length(next_grapheme(rest))
770770
end
771771

772-
defp do_length(:no_grapheme), do: 0
772+
defp do_length(nil), do: 0
773773

774774
@doc """
775775
Returns the grapheme in the `position` of the given utf8 `string`.
@@ -811,7 +811,7 @@ defmodule String do
811811
char
812812
end
813813

814-
defp do_at(:no_grapheme, _, _), do: nil
814+
defp do_at(nil, _, _), do: nil
815815

816816
@doc """
817817
Returns a substring starting at the offset given by the first, and
@@ -934,11 +934,11 @@ defmodule String do
934934
acc <> char
935935
end
936936

937-
defp do_slice(:no_grapheme, start_pos, _, current_pos, acc) when start_pos == current_pos do
937+
defp do_slice(nil, start_pos, _, current_pos, acc) when start_pos == current_pos do
938938
acc
939939
end
940940

941-
defp do_slice(:no_grapheme, _, _, _, acc) do
941+
defp do_slice(nil, _, _, _, acc) do
942942
case acc do
943943
"" -> nil
944944
_ -> acc

lib/elixir/test/elixir/string_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule StringTest do
1212
test :next_codepoint do
1313
assert String.next_codepoint("ésoj") == { "é", "soj" }
1414
assert String.next_codepoint(<<255>>) == { <<255>>, "" }
15-
assert String.next_codepoint("") == :no_codepoint
15+
assert String.next_codepoint("") == nil
1616
end
1717

1818
# test cases described in http://mortoray.com/2013/11/27/the-string-type-is-broken/
@@ -229,7 +229,7 @@ defmodule StringTest do
229229

230230
test :next_grapheme do
231231
assert String.next_grapheme("Ā̀stute") == {"Ā̀", "stute"}
232-
assert String.next_grapheme("") == :no_grapheme
232+
assert String.next_grapheme("") == nil
233233
end
234234

235235
test :first do

lib/elixir/unicode/graphemes.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule String.Graphemes do
8080
end
8181

8282
def next_grapheme(<<>>) do
83-
:no_grapheme
83+
nil
8484
end
8585

8686
# Handle Hangul L
@@ -169,7 +169,7 @@ defmodule String.Graphemes do
169169
[c|do_graphemes(next_grapheme(rest))]
170170
end
171171

172-
defp do_graphemes(:no_grapheme) do
172+
defp do_graphemes(nil) do
173173
[]
174174
end
175175
end

lib/elixir/unicode/unicode.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ defmodule String.Unicode do
159159
end
160160

161161
def next_codepoint(<<>>) do
162-
:no_codepoint
162+
nil
163163
end
164164

165165
def codepoints(binary) when is_binary(binary) do
@@ -170,7 +170,7 @@ defmodule String.Unicode do
170170
[c|do_codepoints(next_codepoint(rest))]
171171
end
172172

173-
defp do_codepoints(:no_codepoint) do
173+
defp do_codepoints(nil) do
174174
[]
175175
end
176176
end

0 commit comments

Comments
 (0)