@@ -974,7 +974,7 @@ defmodule String do
974
974
@ doc """
975
975
Returns a substring starting at the offset given by the first, and
976
976
a length given by the second.
977
- If the offset is greater than string length, than it returns `nil `.
977
+ If the offset is greater than string length, than it returns `"" `.
978
978
979
979
## Examples
980
980
@@ -985,13 +985,13 @@ defmodule String do
985
985
"lixir"
986
986
987
987
iex> String.slice("elixir", 10, 3)
988
- nil
988
+ ""
989
989
990
990
iex> String.slice("elixir", -4, 4)
991
991
"ixir"
992
992
993
993
iex> String.slice("elixir", -10, 3)
994
- nil
994
+ ""
995
995
996
996
iex> String.slice("a", 0, 1500)
997
997
"a"
@@ -1000,16 +1000,13 @@ defmodule String do
1000
1000
""
1001
1001
1002
1002
iex> String.slice("a", 2, 1500)
1003
- nil
1003
+ ""
1004
1004
1005
1005
"""
1006
- @ spec slice ( t , integer , integer ) :: grapheme | nil
1006
+ @ spec slice ( t , integer , integer ) :: grapheme
1007
1007
1008
- def slice ( string , start , 0 ) do
1009
- case abs ( start ) <= length ( string ) do
1010
- true -> ""
1011
- false -> nil
1012
- end
1008
+ def slice ( _ , _ , 0 ) do
1009
+ ""
1013
1010
end
1014
1011
1015
1012
def slice ( string , start , len ) when start >= 0 and len >= 0 do
@@ -1020,7 +1017,7 @@ defmodule String do
1020
1017
real_start_pos = length ( string ) - abs ( start )
1021
1018
case real_start_pos >= 0 do
1022
1019
true -> do_slice ( next_grapheme ( string ) , real_start_pos , real_start_pos + len - 1 , 0 , "" )
1023
- false -> nil
1020
+ false -> ""
1024
1021
end
1025
1022
end
1026
1023
@@ -1029,7 +1026,7 @@ defmodule String do
1029
1026
range to the offset given by the end of the range.
1030
1027
1031
1028
If the start of the range is not a valid offset for the given
1032
- string or if the range is in reverse order, returns `nil `.
1029
+ string or if the range is in reverse order, returns `"" `.
1033
1030
1034
1031
## Examples
1035
1032
@@ -1040,7 +1037,7 @@ defmodule String do
1040
1037
"lixir"
1041
1038
1042
1039
iex> String.slice("elixir", 10..3)
1043
- nil
1040
+ ""
1044
1041
1045
1042
iex> String.slice("elixir", -4..-1)
1046
1043
"ixir"
@@ -1052,10 +1049,10 @@ defmodule String do
1052
1049
"ixir"
1053
1050
1054
1051
iex> String.slice("elixir", -1..-4)
1055
- nil
1052
+ ""
1056
1053
1057
1054
iex> String.slice("elixir", -10..-7)
1058
- nil
1055
+ ""
1059
1056
1060
1057
iex> String.slice("a", 0..1500)
1061
1058
"a"
@@ -1064,10 +1061,10 @@ defmodule String do
1064
1061
""
1065
1062
1066
1063
iex> String.slice("a", 2..1500)
1067
- nil
1064
+ ""
1068
1065
1069
1066
"""
1070
- @ spec slice ( t , Range . t ) :: t | nil
1067
+ @ spec slice ( t , Range . t ) :: t
1071
1068
1072
1069
def slice ( string , range )
1073
1070
@@ -1088,11 +1085,13 @@ defmodule String do
1088
1085
1089
1086
if first >= 0 do
1090
1087
do_slice ( next_grapheme ( string ) , first , last , 0 , "" )
1088
+ else
1089
+ ""
1091
1090
end
1092
1091
end
1093
1092
1094
1093
defp do_slice ( _ , start_pos , last_pos , _ , _ ) when start_pos > last_pos do
1095
- nil
1094
+ ""
1096
1095
end
1097
1096
1098
1097
defp do_slice ( { _ , rest } , start_pos , last_pos , current_pos , acc ) when current_pos < start_pos do
@@ -1106,16 +1105,9 @@ defmodule String do
1106
1105
defp do_slice ( { char , _ } , start_pos , last_pos , current_pos , acc ) when current_pos >= start_pos and current_pos == last_pos do
1107
1106
acc <> char
1108
1107
end
1109
-
1110
- defp do_slice ( nil , start_pos , _ , current_pos , acc ) when start_pos == current_pos do
1111
- acc
1112
- end
1113
-
1108
+
1114
1109
defp do_slice ( nil , _ , _ , _ , acc ) do
1115
- case acc do
1116
- "" -> nil
1117
- _ -> acc
1118
- end
1110
+ acc
1119
1111
end
1120
1112
1121
1113
@ doc """
0 commit comments