erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

164 Zeilen
5.5 KiB

vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 4 Jahren
vor 5 Jahren
  1. -module(utTypeCast).
  2. -compile([export_all, nowarn_export_all]).
  3. toFloat(Value) when is_list(Value) -> list_to_float(Value);
  4. toFloat(Value) when is_binary(Value) -> binary_to_float(Value);
  5. toFloat(Value) when is_integer(Value) -> erlang:float(Value);
  6. toFloat(Value) when is_float(Value) -> Value.
  7. toList(undefined) -> "undefined";
  8. toList(null) -> "null";
  9. toList(Value) when is_tuple(Value) -> tuple_to_list(Value);
  10. toList(Value) when is_binary(Value) -> binary_to_list(Value);
  11. toList(Value) when is_bitstring(Value) -> bitstring_to_list(Value);
  12. toList(Value) when is_integer(Value) -> integer_to_list(Value);
  13. toList(Value) when is_float(Value) -> float_to_list(Value, [{decimals, 6}, compact]);
  14. toList(Value) when is_atom(Value) -> atom_to_list(Value);
  15. toList(Value) when is_list(Value) -> Value;
  16. toList([Tuple | PropList] = Value) when is_list(PropList) and is_tuple(Tuple) ->
  17. lists:map(fun({K, V}) -> {toList(K), toList(V)} end, [Value]).
  18. %% to_list(Term) when is_binary(Term) ->
  19. %% case unicode:characters_to_binary(Term, utf8, utf8) of
  20. %% Term ->
  21. %% unicode:characters_to_list(Term);
  22. %% _ ->
  23. %% binary_to_list(Term)
  24. %% end.
  25. toBinary(Value) when is_integer(Value) -> integer_to_binary(Value);
  26. toBinary(Value) when is_list(Value) -> list_to_binary(Value);
  27. toBinary(Value) when is_float(Value) -> float_to_binary(Value, [{decimals, 6}, compact]);
  28. toBinary(Value) when is_atom(Value) -> atom_to_binary(Value, utf8);
  29. toBinary(Value) when is_binary(Value) -> Value;
  30. toBinary([Tuple | PropList] = Value) when is_list(PropList) and is_tuple(Tuple) ->
  31. lists:map(fun({K, V}) -> {toBinary(K), toBinary(V)} end, Value);
  32. toBinary(Value) -> term_to_binary(Value).
  33. toInteger(undefined) -> undefined;
  34. toInteger(Value) when is_float(Value) -> trunc(Value);
  35. toInteger(Value) when is_list(Value) -> list_to_integer(Value);
  36. toInteger(Value) when is_binary(Value) -> binary_to_integer(Value);
  37. toInteger(Value) when is_tuple(Value) -> toInteger(tuple_to_list(Value));
  38. toInteger(Value) when is_integer(Value) -> Value.
  39. dataType(Data) when is_list(Data) -> list;
  40. dataType(Data) when is_integer(Data) -> integer;
  41. dataType(Data) when is_binary(Data) -> binary;
  42. dataType(Data) when is_function(Data) -> function;
  43. dataType(Data) when is_tuple(Data) -> tuple;
  44. dataType(Data) when is_map(Data) -> map;
  45. dataType(Data) when is_atom(Data) -> atom;
  46. %%dataType(Data) when is_boolean(Data) -> boolean;
  47. dataType(Data) when is_bitstring(Data) -> bitstring;
  48. dataType(Data) when is_float(Data) -> float;
  49. dataType(Data) when is_number(Data) -> number;
  50. dataType(Data) when is_reference(Data) -> reference;
  51. dataType(Data) when is_pid(Data) -> pid;
  52. dataType(Data) when is_port(Data) -> port;
  53. dataType(_Data) -> not_know.
  54. %% Trim the binary
  55. -spec trim(Bin :: binary()) -> binary().
  56. trim(Bin) when is_binary(Bin) -> trimHead(trimTail(Bin)).
  57. %% Trim head of binary
  58. -spec trimHead(Bin :: binary()) -> binary().
  59. trimHead(<<>>) -> <<>>;
  60. trimHead(<<C, BinTail/binary>> = Bin) ->
  61. case is_whitespace(C) of
  62. true -> trimHead(BinTail);
  63. false -> Bin
  64. end.
  65. %% Trim tail of binary
  66. -spec trimTail(Bin :: binary()) -> binary().
  67. trimTail(<<>>) -> <<>>;
  68. trimTail(Bin) ->
  69. Size = byte_size(Bin) - 1,
  70. <<BinHead:Size/binary, C>> = Bin,
  71. case is_whitespace(C) of
  72. true -> trimTail(BinHead);
  73. false -> Bin
  74. end.
  75. %% Check if the char is a whitespace
  76. -spec is_whitespace(char()) -> true | false.
  77. is_whitespace($\s) -> true;
  78. is_whitespace($\t) -> true;
  79. is_whitespace($\n) -> true;
  80. is_whitespace($\r) -> true;
  81. is_whitespace(_) -> false.
  82. %% term序列化, term转为string
  83. term_to_string(Bin) when is_binary(Bin) ->
  84. binary_to_list(Bin);
  85. term_to_string(Term) ->
  86. case catch io_lib:format("~w", [Term]) of
  87. {'EXIT', _} -> lists:flatten(io_lib:format("~p", [Term]));
  88. GoodString -> lists:flatten(GoodString)
  89. end.
  90. %% term反序列化, string转换为term
  91. string_to_term(String) ->
  92. case erl_scan:string(String ++ ".") of
  93. {ok, Tokens, _} ->
  94. case erl_parse:parse_term(Tokens) of
  95. {ok, Term} -> Term;
  96. _Err -> String
  97. end;
  98. _Error ->
  99. undefined
  100. end.
  101. %% term序列化,term转换为string格式,e.g., [{a},1] => "[{a},1]"
  102. term_to_string1(Term) ->
  103. binary_to_list(list_to_binary(io_lib:format("~w", [Term]))).
  104. %% term序列化,term转换为bitstring格式,e.g., [{a},1] => <<"[{a},1]">>
  105. term_to_bitstring(Term) ->
  106. erlang:list_to_bitstring(io_lib:format("~w", [Term])).
  107. %% term反序列化,string转换为term,e.g., "[{a},1]" => [{a},1]
  108. string_to_term1(String) ->
  109. case String of
  110. [] -> [];
  111. _ ->
  112. case erl_scan:string(String ++ ".") of
  113. {ok, Tokens, _} ->
  114. case erl_parse:parse_term(Tokens) of
  115. {ok, Term} -> Term;
  116. _Err -> undefined
  117. end;
  118. _Error ->
  119. undefined
  120. end
  121. end.
  122. %%将列表转换为string [a,b,c] -> "a,b,c"
  123. list_to_string(List) ->
  124. case List == [] orelse List == "" of
  125. true -> "";
  126. false ->
  127. F = fun(E) ->
  128. tool:to_list(E) ++ ","
  129. end,
  130. L1 = [F(E) || E <- List],
  131. L2 = lists:concat(L1),
  132. string:substr(L2, 1, length(L2) - 1)
  133. end.
  134. %% term反序列化,bitstring转换为term,e.g., <<"[{a},1]">> => [{a},1]
  135. bitstring_to_term(undefined) -> undefined;
  136. bitstring_to_term(BitString) ->
  137. string_to_term(tool:to_list(BitString)).
  138. termToBase64(Term) ->
  139. base64:encode(term_to_binary(Term)).
  140. base64ToTerm(Base64String) ->
  141. binary_to_term(base64:decode(Base64String)).