You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.6 KiB

  1. %%% @doc Compatibility module for string functionality
  2. %%% for pre- and post-unicode support.
  3. -module(rebar_string).
  4. -export([join/2, split/2, lexemes/2, trim/3, uppercase/1, lowercase/1, chr/2]).
  5. -ifdef(unicode_str).
  6. %% string:join/2 copy; string:join/2 is getting obsoleted
  7. %% and replaced by lists:join/2, but lists:join/2 is too new
  8. %% for version support (only appeared in 19.0) so it cannot be
  9. %% used. Instead we just adopt join/2 locally and hope it works
  10. %% for most unicode use cases anyway.
  11. join([], Sep) when is_list(Sep) ->
  12. [];
  13. join([H|T], Sep) ->
  14. H ++ lists:append([Sep ++ X || X <- T]).
  15. split(Str, SearchPattern) -> string:split(Str, SearchPattern).
  16. lexemes(Str, SepList) -> string:lexemes(Str, SepList).
  17. trim(Str, Direction, Cluster=[_]) -> string:trim(Str, Direction, Cluster).
  18. uppercase(Str) -> string:uppercase(Str).
  19. lowercase(Str) -> string:lowercase(Str).
  20. chr(S, C) when is_integer(C) -> chr(S, C, 1).
  21. chr([C|_Cs], C, I) -> I;
  22. chr([_|Cs], C, I) -> chr(Cs, C, I+1);
  23. chr([], _C, _I) -> 0.
  24. -else.
  25. join(Strings, Separator) -> string:join(Strings, Separator).
  26. split(Str, SearchPattern) when is_list(Str) -> string:split(Str, SearchPattern);
  27. split(Str, SearchPattern) when is_binary(Str) -> binary:split(Str, SearchPattern).
  28. lexemes(Str, SepList) -> string:tokens(Str, SepList).
  29. trim(Str, Direction, [Char]) ->
  30. Dir = case Direction of
  31. both -> both;
  32. leading -> left;
  33. trailing -> right
  34. end,
  35. string:strip(Str, Dir, Char).
  36. uppercase(Str) -> string:to_upper(Str).
  37. lowercase(Str) -> string:to_lower(Str).
  38. chr(Str, Char) -> string:chr(Str, Char).
  39. -endif.