Skip to content

Commit d13cac8

Browse files
authored
Merge pull request #7 from richcarl/string-common-fixes
Use modern Erlang string functions in Data.String.Common
2 parents df3cca5 + 0d68cf3 commit d13cac8

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/Data/String/Common.erl

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22
-export(['_localeCompare'/5, replace/3, replaceAll/3, split/2, toLower/1, toUpper/1, trim/1, joinWith/2]).
33

44
'_localeCompare'(_Lt,_Eq,_Gt,_S1,_S2) -> error("not implemented").
5-
replace(S1,S2,S3) -> iolist_to_binary(string:replace(S3, S1, S2)).
6-
replaceAll(S1,S2,S3) -> iolist_to_binary(string:replace(S3, S1, S2, all)).
5+
6+
replace(S1,S2,S3) -> unicode:characters_to_binary(string:replace(S3, S1, S2)).
7+
8+
replaceAll(S1,S2,S3) -> unicode:characters_to_binary(string:replace(S3, S1, S2, all)).
79

810
split(Sep,S) ->
9-
Split = string:split(S, Sep, all),
1011
Res = case {Sep,S} of
1112
{<<>>,<<>>} -> []; %% string:split says [<<>>] but JS says []
1213
{<<>>,_} ->
1314
%% string:split does not work on empty split pattern
1415
lists:map(fun (C) -> unicode:characters_to_binary([C], utf8) end, unicode:characters_to_list(S, utf8));
1516

1617
%% {_,<<>>} behaves the same in JS vs string:split
17-
_ -> Split
18+
_ -> string:split(S, Sep, all)
1819
end,
1920
array:from_list(Res).
2021

2122
toLower(S) -> string:lowercase(S).
2223

2324
toUpper(S) -> string:uppercase(S).
2425

25-
trim(S) -> re:replace(S, "^\\s*(.*?)\\s*$","\\1", [{return, binary}, dotall]).
26+
trim(S) -> string:trim(S).
2627

2728
joinWith(S, XS) ->
28-
XS1 = lists:map(fun unicode:characters_to_list/1, array:to_list(XS)),
29-
Res = string:join(XS1, unicode:characters_to_list(S)),
30-
unicode:characters_to_binary(Res).
29+
unicode:characters_to_binary(lists:join(S, array:to_list(XS))).

0 commit comments

Comments
 (0)