|
2 | 2 | -export(['_localeCompare'/5, replace/3, replaceAll/3, split/2, toLower/1, toUpper/1, trim/1, joinWith/2]).
|
3 | 3 |
|
4 | 4 | '_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)). |
7 | 9 |
|
8 | 10 | split(Sep,S) ->
|
9 |
| - Split = string:split(S, Sep, all), |
10 | 11 | Res = case {Sep,S} of
|
11 | 12 | {<<>>,<<>>} -> []; %% string:split says [<<>>] but JS says []
|
12 | 13 | {<<>>,_} ->
|
13 | 14 | %% string:split does not work on empty split pattern
|
14 | 15 | lists:map(fun (C) -> unicode:characters_to_binary([C], utf8) end, unicode:characters_to_list(S, utf8));
|
15 | 16 |
|
16 | 17 | %% {_,<<>>} behaves the same in JS vs string:split
|
17 |
| - _ -> Split |
| 18 | + _ -> string:split(S, Sep, all) |
18 | 19 | end,
|
19 | 20 | array:from_list(Res).
|
20 | 21 |
|
21 | 22 | toLower(S) -> string:lowercase(S).
|
22 | 23 |
|
23 | 24 | toUpper(S) -> string:uppercase(S).
|
24 | 25 |
|
25 |
| -trim(S) -> re:replace(S, "^\\s*(.*?)\\s*$","\\1", [{return, binary}, dotall]). |
| 26 | +trim(S) -> string:trim(S). |
26 | 27 |
|
27 | 28 | 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