rewrite from lager
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

544 рядки
19 KiB

4 роки тому
  1. %%
  2. %% %CopyrightBegin%
  3. %%
  4. %% Copyright Ericsson AB 1996-2011-2012. All Rights Reserved.
  5. %%
  6. %% The contents of this file are subject to the Erlang Public License,
  7. %% Version 1.1, (the "License"); you may not use this file except in
  8. %% compliance with the License. You should have received a copy of the
  9. %% Erlang Public License along with this software. If not, it can be
  10. %% retrieved online at http://www.erlang.org/.
  11. %%
  12. %% Software distributed under the License is distributed on an "AS IS"
  13. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  14. %% the License for the specific language governing rights and limitations
  15. %% under the License.
  16. %%
  17. %% %CopyrightEnd%
  18. %%
  19. -module(lager_format).
  20. %% fork of io_lib_format that uses trunc_io to protect against large terms
  21. -export([format/3, format/4]).
  22. -record(options, {
  23. chomp = false :: boolean()
  24. }).
  25. format(FmtStr, Args, MaxLen) ->
  26. format(FmtStr, Args, MaxLen, []).
  27. format([], [], _, _) ->
  28. "";
  29. format(FmtStr, Args, MaxLen, Opts) when is_atom(FmtStr) ->
  30. format(atom_to_list(FmtStr), Args, MaxLen, Opts);
  31. format(FmtStr, Args, MaxLen, Opts) when is_binary(FmtStr) ->
  32. format(binary_to_list(FmtStr), Args, MaxLen, Opts);
  33. format(FmtStr, Args, MaxLen, Opts) when is_list(FmtStr) ->
  34. case io_lib:deep_char_list(FmtStr) of
  35. true ->
  36. Options = make_options(Opts, #options{}),
  37. Cs = collect(FmtStr, Args),
  38. {Cs2, MaxLen2} = build(Cs, [], MaxLen, Options),
  39. %% count how many terms remain
  40. {Count, StrLen} = lists:foldl(
  41. fun({_C, _As, _F, _Adj, _P, _Pad, _Enc}, {Terms, Chars}) ->
  42. {Terms + 1, Chars};
  43. (_, {Terms, Chars}) ->
  44. {Terms, Chars + 1}
  45. end, {0, 0}, Cs2),
  46. build2(Cs2, Count, MaxLen2 - StrLen);
  47. false ->
  48. erlang:error(badarg)
  49. end;
  50. format(_FmtStr, _Args, _MaxLen, _Opts) ->
  51. erlang:error(badarg).
  52. collect([$~|Fmt0], Args0) ->
  53. {C,Fmt1,Args1} = collect_cseq(Fmt0, Args0),
  54. [C|collect(Fmt1, Args1)];
  55. collect([C|Fmt], Args) ->
  56. [C|collect(Fmt, Args)];
  57. collect([], []) -> [].
  58. collect_cseq(Fmt0, Args0) ->
  59. {F,Ad,Fmt1,Args1} = field_width(Fmt0, Args0),
  60. {P,Fmt2,Args2} = precision(Fmt1, Args1),
  61. {Pad,Fmt3,Args3} = pad_char(Fmt2, Args2),
  62. {Encoding,Fmt4,Args4} = encoding(Fmt3, Args3),
  63. {C,As,Fmt5,Args5} = collect_cc(Fmt4, Args4),
  64. {{C,As,F,Ad,P,Pad,Encoding},Fmt5,Args5}.
  65. encoding([$t|Fmt],Args) ->
  66. {unicode,Fmt,Args};
  67. encoding(Fmt,Args) ->
  68. {latin1,Fmt,Args}.
  69. field_width([$-|Fmt0], Args0) ->
  70. {F,Fmt,Args} = field_value(Fmt0, Args0),
  71. field_width(-F, Fmt, Args);
  72. field_width(Fmt0, Args0) ->
  73. {F,Fmt,Args} = field_value(Fmt0, Args0),
  74. field_width(F, Fmt, Args).
  75. field_width(F, Fmt, Args) when F < 0 ->
  76. {-F,left,Fmt,Args};
  77. field_width(F, Fmt, Args) when F >= 0 ->
  78. {F,right,Fmt,Args}.
  79. precision([$.|Fmt], Args) ->
  80. field_value(Fmt, Args);
  81. precision(Fmt, Args) ->
  82. {none,Fmt,Args}.
  83. field_value([$*|Fmt], [A|Args]) when is_integer(A) ->
  84. {A,Fmt,Args};
  85. field_value([C|Fmt], Args) when is_integer(C), C >= $0, C =< $9 ->
  86. field_value([C|Fmt], Args, 0);
  87. field_value(Fmt, Args) ->
  88. {none,Fmt,Args}.
  89. field_value([C|Fmt], Args, F) when is_integer(C), C >= $0, C =< $9 ->
  90. field_value(Fmt, Args, 10*F + (C - $0));
  91. field_value(Fmt, Args, F) -> %Default case
  92. {F,Fmt,Args}.
  93. pad_char([$.,$*|Fmt], [Pad|Args]) -> {Pad,Fmt,Args};
  94. pad_char([$.,Pad|Fmt], Args) -> {Pad,Fmt,Args};
  95. pad_char(Fmt, Args) -> {$\s,Fmt,Args}.
  96. %% collect_cc([FormatChar], [Argument]) ->
  97. %% {Control,[ControlArg],[FormatChar],[Arg]}.
  98. %% Here we collect the argments for each control character.
  99. %% Be explicit to cause failure early.
  100. collect_cc([$w|Fmt], [A|Args]) -> {$w,[A],Fmt,Args};
  101. collect_cc([$p|Fmt], [A|Args]) -> {$p,[A],Fmt,Args};
  102. collect_cc([$W|Fmt], [A,Depth|Args]) -> {$W,[A,Depth],Fmt,Args};
  103. collect_cc([$P|Fmt], [A,Depth|Args]) -> {$P,[A,Depth],Fmt,Args};
  104. collect_cc([$s|Fmt], [A|Args]) -> {$s,[A],Fmt,Args};
  105. collect_cc([$e|Fmt], [A|Args]) -> {$e,[A],Fmt,Args};
  106. collect_cc([$f|Fmt], [A|Args]) -> {$f,[A],Fmt,Args};
  107. collect_cc([$g|Fmt], [A|Args]) -> {$g,[A],Fmt,Args};
  108. collect_cc([$b|Fmt], [A|Args]) -> {$b,[A],Fmt,Args};
  109. collect_cc([$B|Fmt], [A|Args]) -> {$B,[A],Fmt,Args};
  110. collect_cc([$x|Fmt], [A,Prefix|Args]) -> {$x,[A,Prefix],Fmt,Args};
  111. collect_cc([$X|Fmt], [A,Prefix|Args]) -> {$X,[A,Prefix],Fmt,Args};
  112. collect_cc([$+|Fmt], [A|Args]) -> {$+,[A],Fmt,Args};
  113. collect_cc([$#|Fmt], [A|Args]) -> {$#,[A],Fmt,Args};
  114. collect_cc([$c|Fmt], [A|Args]) -> {$c,[A],Fmt,Args};
  115. collect_cc([$~|Fmt], Args) when is_list(Args) -> {$~,[],Fmt,Args};
  116. collect_cc([$n|Fmt], Args) when is_list(Args) -> {$n,[],Fmt,Args};
  117. collect_cc([$i|Fmt], [A|Args]) -> {$i,[A],Fmt,Args}.
  118. %% build([Control], Pc, Indentation) -> [Char].
  119. %% Interpret the control structures. Count the number of print
  120. %% remaining and only calculate indentation when necessary. Must also
  121. %% be smart when calculating indentation for characters in format.
  122. build([{$n, _, _, _, _, _, _}], Acc, MaxLen, #options{chomp=true}) ->
  123. %% trailing ~n, ignore
  124. {lists:reverse(Acc), MaxLen};
  125. build([{C,As,F,Ad,P,Pad,Enc}|Cs], Acc, MaxLen, O) ->
  126. {S, MaxLen2} = control(C, As, F, Ad, P, Pad, Enc, MaxLen),
  127. build(Cs, [S|Acc], MaxLen2, O);
  128. build([$\n], Acc, MaxLen, #options{chomp=true}) ->
  129. %% trailing \n, ignore
  130. {lists:reverse(Acc), MaxLen};
  131. build([$\n|Cs], Acc, MaxLen, O) ->
  132. build(Cs, [$\n|Acc], MaxLen - 1, O);
  133. build([$\t|Cs], Acc, MaxLen, O) ->
  134. build(Cs, [$\t|Acc], MaxLen - 1, O);
  135. build([C|Cs], Acc, MaxLen, O) ->
  136. build(Cs, [C|Acc], MaxLen - 1, O);
  137. build([], Acc, MaxLen, _O) ->
  138. {lists:reverse(Acc), MaxLen}.
  139. build2([{C,As,F,Ad,P,Pad,Enc}|Cs], Count, MaxLen) ->
  140. {S, Len} = control2(C, As, F, Ad, P, Pad, Enc, MaxLen div Count),
  141. [S|build2(Cs, Count - 1, MaxLen - Len)];
  142. build2([C|Cs], Count, MaxLen) ->
  143. [C|build2(Cs, Count, MaxLen)];
  144. build2([], _, _) -> [].
  145. %% control(FormatChar, [Argument], FieldWidth, Adjust, Precision, PadChar,
  146. %% Indentation) -> [Char]
  147. %% This is the main dispatch function for the various formatting commands.
  148. %% Field widths and precisions have already been calculated.
  149. control($e, [A], F, Adj, P, Pad, _Enc, L) when is_float(A) ->
  150. Res = fwrite_e(A, F, Adj, P, Pad),
  151. {Res, L - lists:flatlength(Res)};
  152. control($f, [A], F, Adj, P, Pad, _Enc, L) when is_float(A) ->
  153. Res = fwrite_f(A, F, Adj, P, Pad),
  154. {Res, L - lists:flatlength(Res)};
  155. control($g, [A], F, Adj, P, Pad, _Enc, L) when is_float(A) ->
  156. Res = fwrite_g(A, F, Adj, P, Pad),
  157. {Res, L - lists:flatlength(Res)};
  158. control($b, [A], F, Adj, P, Pad, _Enc, L) when is_integer(A) ->
  159. Res = unprefixed_integer(A, F, Adj, base(P), Pad, true),
  160. {Res, L - lists:flatlength(Res)};
  161. control($B, [A], F, Adj, P, Pad, _Enc, L) when is_integer(A) ->
  162. Res = unprefixed_integer(A, F, Adj, base(P), Pad, false),
  163. {Res, L - lists:flatlength(Res)};
  164. control($x, [A,Prefix], F, Adj, P, Pad, _Enc, L) when is_integer(A),
  165. is_atom(Prefix) ->
  166. Res = prefixed_integer(A, F, Adj, base(P), Pad, atom_to_list(Prefix), true),
  167. {Res, L - lists:flatlength(Res)};
  168. control($x, [A,Prefix], F, Adj, P, Pad, _Enc, L) when is_integer(A) ->
  169. true = io_lib:deep_char_list(Prefix), %Check if Prefix a character list
  170. Res = prefixed_integer(A, F, Adj, base(P), Pad, Prefix, true),
  171. {Res, L - lists:flatlength(Res)};
  172. control($X, [A,Prefix], F, Adj, P, Pad, _Enc, L) when is_integer(A),
  173. is_atom(Prefix) ->
  174. Res = prefixed_integer(A, F, Adj, base(P), Pad, atom_to_list(Prefix), false),
  175. {Res, L - lists:flatlength(Res)};
  176. control($X, [A,Prefix], F, Adj, P, Pad, _Enc, L) when is_integer(A) ->
  177. true = io_lib:deep_char_list(Prefix), %Check if Prefix a character list
  178. Res = prefixed_integer(A, F, Adj, base(P), Pad, Prefix, false),
  179. {Res, L - lists:flatlength(Res)};
  180. control($+, [A], F, Adj, P, Pad, _Enc, L) when is_integer(A) ->
  181. Base = base(P),
  182. Prefix = [integer_to_list(Base), $#],
  183. Res = prefixed_integer(A, F, Adj, Base, Pad, Prefix, true),
  184. {Res, L - lists:flatlength(Res)};
  185. control($#, [A], F, Adj, P, Pad, _Enc, L) when is_integer(A) ->
  186. Base = base(P),
  187. Prefix = [integer_to_list(Base), $#],
  188. Res = prefixed_integer(A, F, Adj, Base, Pad, Prefix, false),
  189. {Res, L - lists:flatlength(Res)};
  190. control($c, [A], F, Adj, P, Pad, unicode, L) when is_integer(A) ->
  191. Res = char(A, F, Adj, P, Pad),
  192. {Res, L - lists:flatlength(Res)};
  193. control($c, [A], F, Adj, P, Pad, _Enc, L) when is_integer(A) ->
  194. Res = char(A band 255, F, Adj, P, Pad),
  195. {Res, L - lists:flatlength(Res)};
  196. control($~, [], F, Adj, P, Pad, _Enc, L) ->
  197. Res = char($~, F, Adj, P, Pad),
  198. {Res, L - lists:flatlength(Res)};
  199. control($n, [], F, Adj, P, Pad, _Enc, L) ->
  200. Res = newline(F, Adj, P, Pad),
  201. {Res, L - lists:flatlength(Res)};
  202. control($i, [_A], _F, _Adj, _P, _Pad, _Enc, L) ->
  203. {[], L};
  204. control($s, [A], F, Adj, P, Pad, _Enc, L) when is_atom(A) ->
  205. Res = string(atom_to_list(A), F, Adj, P, Pad),
  206. {Res, L - lists:flatlength(Res)};
  207. control(C, A, F, Adj, P, Pad, Enc, L) ->
  208. %% save this for later - these are all the 'large' terms
  209. {{C, A, F, Adj, P, Pad, Enc}, L}.
  210. control2($w, [A], F, Adj, P, Pad, _Enc, L) ->
  211. Term = lager_trunc_io:fprint(A, L, [{lists_as_strings, false}]),
  212. Res = term(Term, F, Adj, P, Pad),
  213. {Res, lists:flatlength(Res)};
  214. control2($p, [A], _F, _Adj, _P, _Pad, _Enc, L) ->
  215. Term = lager_trunc_io:fprint(A, L, [{lists_as_strings, true}]),
  216. {Term, lists:flatlength(Term)};
  217. control2($W, [A,Depth], F, Adj, P, Pad, _Enc, L) when is_integer(Depth) ->
  218. Term = lager_trunc_io:fprint(A, L, [{depth, Depth}, {lists_as_strings, false}]),
  219. Res = term(Term, F, Adj, P, Pad),
  220. {Res, lists:flatlength(Res)};
  221. control2($P, [A,Depth], _F, _Adj, _P, _Pad, _Enc, L) when is_integer(Depth) ->
  222. Term = lager_trunc_io:fprint(A, L, [{depth, Depth}, {lists_as_strings, true}]),
  223. {Term, lists:flatlength(Term)};
  224. control2($s, [L0], F, Adj, P, Pad, latin1, L) ->
  225. List = lager_trunc_io:fprint(iolist_to_chars(L0), L, [{force_strings, true}]),
  226. Res = string(List, F, Adj, P, Pad),
  227. {Res, lists:flatlength(Res)};
  228. control2($s, [L0], F, Adj, P, Pad, unicode, L) ->
  229. List = lager_trunc_io:fprint(cdata_to_chars(L0), L, [{force_strings, true}]),
  230. Res = uniconv(string(List, F, Adj, P, Pad)),
  231. {Res, lists:flatlength(Res)}.
  232. iolist_to_chars([C|Cs]) when is_integer(C), C >= $\000, C =< $\377 ->
  233. [C | iolist_to_chars(Cs)];
  234. iolist_to_chars([I|Cs]) ->
  235. [iolist_to_chars(I) | iolist_to_chars(Cs)];
  236. iolist_to_chars([]) ->
  237. [];
  238. iolist_to_chars(B) when is_binary(B) ->
  239. binary_to_list(B).
  240. cdata_to_chars([C|Cs]) when is_integer(C), C >= $\000 ->
  241. [C | cdata_to_chars(Cs)];
  242. cdata_to_chars([I|Cs]) ->
  243. [cdata_to_chars(I) | cdata_to_chars(Cs)];
  244. cdata_to_chars([]) ->
  245. [];
  246. cdata_to_chars(B) when is_binary(B) ->
  247. case catch unicode:characters_to_list(B) of
  248. L when is_list(L) -> L;
  249. _ -> binary_to_list(B)
  250. end.
  251. make_options([], Options) ->
  252. Options;
  253. make_options([{chomp, Bool}|T], Options) when is_boolean(Bool) ->
  254. make_options(T, Options#options{chomp=Bool}).
  255. -ifdef(UNICODE_AS_BINARIES).
  256. uniconv(C) ->
  257. unicode:characters_to_binary(C,unicode).
  258. -else.
  259. uniconv(C) ->
  260. C.
  261. -endif.
  262. %% Default integer base
  263. base(none) ->
  264. 10;
  265. base(B) when is_integer(B) ->
  266. B.
  267. %% term(TermList, Field, Adjust, Precision, PadChar)
  268. %% Output the characters in a term.
  269. %% Adjust the characters within the field if length less than Max padding
  270. %% with PadChar.
  271. term(T, none, _Adj, none, _Pad) -> T;
  272. term(T, none, Adj, P, Pad) -> term(T, P, Adj, P, Pad);
  273. term(T, F, Adj, P0, Pad) ->
  274. L = lists:flatlength(T),
  275. P = case P0 of none -> erlang:min(L, F); _ -> P0 end,
  276. if
  277. L > P ->
  278. adjust(chars($*, P), chars(Pad, F-P), Adj);
  279. F >= P ->
  280. adjust(T, chars(Pad, F-L), Adj)
  281. end.
  282. %% fwrite_e(Float, Field, Adjust, Precision, PadChar)
  283. fwrite_e(Fl, none, Adj, none, Pad) -> %Default values
  284. fwrite_e(Fl, none, Adj, 6, Pad);
  285. fwrite_e(Fl, none, _Adj, P, _Pad) when P >= 2 ->
  286. float_e(Fl, float_data(Fl), P);
  287. fwrite_e(Fl, F, Adj, none, Pad) ->
  288. fwrite_e(Fl, F, Adj, 6, Pad);
  289. fwrite_e(Fl, F, Adj, P, Pad) when P >= 2 ->
  290. term(float_e(Fl, float_data(Fl), P), F, Adj, F, Pad).
  291. float_e(Fl, Fd, P) when Fl < 0.0 -> %Negative numbers
  292. [$-|float_e(-Fl, Fd, P)];
  293. float_e(_Fl, {Ds,E}, P) ->
  294. case float_man(Ds, 1, P-1) of
  295. {[$0|Fs],true} -> [[$1|Fs]|float_exp(E)];
  296. {Fs,false} -> [Fs|float_exp(E-1)]
  297. end.
  298. %% float_man([Digit], Icount, Dcount) -> {[Chars],CarryFlag}.
  299. %% Generate the characters in the mantissa from the digits with Icount
  300. %% characters before the '.' and Dcount decimals. Handle carry and let
  301. %% caller decide what to do at top.
  302. float_man(Ds, 0, Dc) ->
  303. {Cs,C} = float_man(Ds, Dc),
  304. {[$.|Cs],C};
  305. float_man([D|Ds], I, Dc) ->
  306. case float_man(Ds, I-1, Dc) of
  307. {Cs,true} when D =:= $9 -> {[$0|Cs],true};
  308. {Cs,true} -> {[D+1|Cs],false};
  309. {Cs,false} -> {[D|Cs],false}
  310. end;
  311. float_man([], I, Dc) -> %Pad with 0's
  312. {string:chars($0, I, [$.|string:chars($0, Dc)]),false}.
  313. float_man([D|_], 0) when D >= $5 -> {[],true};
  314. float_man([_|_], 0) -> {[],false};
  315. float_man([D|Ds], Dc) ->
  316. case float_man(Ds, Dc-1) of
  317. {Cs,true} when D =:= $9 -> {[$0|Cs],true};
  318. {Cs,true} -> {[D+1|Cs],false};
  319. {Cs,false} -> {[D|Cs],false}
  320. end;
  321. float_man([], Dc) -> {string:chars($0, Dc),false}. %Pad with 0's
  322. %% float_exp(Exponent) -> [Char].
  323. %% Generate the exponent of a floating point number. Always include sign.
  324. float_exp(E) when E >= 0 ->
  325. [$e,$+|integer_to_list(E)];
  326. float_exp(E) ->
  327. [$e|integer_to_list(E)].
  328. %% fwrite_f(FloatData, Field, Adjust, Precision, PadChar)
  329. fwrite_f(Fl, none, Adj, none, Pad) -> %Default values
  330. fwrite_f(Fl, none, Adj, 6, Pad);
  331. fwrite_f(Fl, none, _Adj, P, _Pad) when P >= 1 ->
  332. float_f(Fl, float_data(Fl), P);
  333. fwrite_f(Fl, F, Adj, none, Pad) ->
  334. fwrite_f(Fl, F, Adj, 6, Pad);
  335. fwrite_f(Fl, F, Adj, P, Pad) when P >= 1 ->
  336. term(float_f(Fl, float_data(Fl), P), F, Adj, F, Pad).
  337. float_f(Fl, Fd, P) when Fl < 0.0 ->
  338. [$-|float_f(-Fl, Fd, P)];
  339. float_f(Fl, {Ds,E}, P) when E =< 0 ->
  340. float_f(Fl, {string:chars($0, -E+1, Ds),1}, P); %Prepend enough 0's
  341. float_f(_Fl, {Ds,E}, P) ->
  342. case float_man(Ds, E, P) of
  343. {Fs,true} -> "1" ++ Fs; %Handle carry
  344. {Fs,false} -> Fs
  345. end.
  346. %% float_data([FloatChar]) -> {[Digit],Exponent}
  347. float_data(Fl) ->
  348. float_data(float_to_list(Fl), []).
  349. float_data([$e|E], Ds) ->
  350. {lists:reverse(Ds),list_to_integer(E)+1};
  351. float_data([D|Cs], Ds) when D >= $0, D =< $9 ->
  352. float_data(Cs, [D|Ds]);
  353. float_data([_|Cs], Ds) ->
  354. float_data(Cs, Ds).
  355. %% fwrite_g(Float, Field, Adjust, Precision, PadChar)
  356. %% Use the f form if Float is >= 0.1 and < 1.0e4,
  357. %% and the prints correctly in the f form, else the e form.
  358. %% Precision always means the # of significant digits.
  359. fwrite_g(Fl, F, Adj, none, Pad) ->
  360. fwrite_g(Fl, F, Adj, 6, Pad);
  361. fwrite_g(Fl, F, Adj, P, Pad) when P >= 1 ->
  362. A = abs(Fl),
  363. E = if A < 1.0e-1 -> -2;
  364. A < 1.0e0 -> -1;
  365. A < 1.0e1 -> 0;
  366. A < 1.0e2 -> 1;
  367. A < 1.0e3 -> 2;
  368. A < 1.0e4 -> 3;
  369. true -> fwrite_f
  370. end,
  371. if P =< 1, E =:= -1;
  372. P-1 > E, E >= -1 ->
  373. fwrite_f(Fl, F, Adj, P-1-E, Pad);
  374. P =< 1 ->
  375. fwrite_e(Fl, F, Adj, 2, Pad);
  376. true ->
  377. fwrite_e(Fl, F, Adj, P, Pad)
  378. end.
  379. %% string(String, Field, Adjust, Precision, PadChar)
  380. string(S, none, _Adj, none, _Pad) -> S;
  381. string(S, F, Adj, none, Pad) ->
  382. string_field(S, F, Adj, lists:flatlength(S), Pad);
  383. string(S, none, _Adj, P, Pad) ->
  384. string_field(S, P, left, lists:flatlength(S), Pad);
  385. string(S, F, Adj, P, Pad) when F >= P ->
  386. N = lists:flatlength(S),
  387. if F > P ->
  388. if N > P ->
  389. adjust(flat_trunc(S, P), chars(Pad, F-P), Adj);
  390. N < P ->
  391. adjust([S|chars(Pad, P-N)], chars(Pad, F-P), Adj);
  392. true -> % N == P
  393. adjust(S, chars(Pad, F-P), Adj)
  394. end;
  395. true -> % F == P
  396. string_field(S, F, Adj, N, Pad)
  397. end.
  398. string_field(S, F, _Adj, N, _Pad) when N > F ->
  399. flat_trunc(S, F);
  400. string_field(S, F, Adj, N, Pad) when N < F ->
  401. adjust(S, chars(Pad, F-N), Adj);
  402. string_field(S, _, _, _, _) -> % N == F
  403. S.
  404. %% unprefixed_integer(Int, Field, Adjust, Base, PadChar, Lowercase)
  405. %% -> [Char].
  406. unprefixed_integer(Int, F, Adj, Base, Pad, Lowercase)
  407. when Base >= 2, Base =< 1+$Z-$A+10 ->
  408. if Int < 0 ->
  409. S = cond_lowercase(erlang:integer_to_list(-Int, Base), Lowercase),
  410. term([$-|S], F, Adj, none, Pad);
  411. true ->
  412. S = cond_lowercase(erlang:integer_to_list(Int, Base), Lowercase),
  413. term(S, F, Adj, none, Pad)
  414. end.
  415. %% prefixed_integer(Int, Field, Adjust, Base, PadChar, Prefix, Lowercase)
  416. %% -> [Char].
  417. prefixed_integer(Int, F, Adj, Base, Pad, Prefix, Lowercase)
  418. when Base >= 2, Base =< 1+$Z-$A+10 ->
  419. if Int < 0 ->
  420. S = cond_lowercase(erlang:integer_to_list(-Int, Base), Lowercase),
  421. term([$-,Prefix|S], F, Adj, none, Pad);
  422. true ->
  423. S = cond_lowercase(erlang:integer_to_list(Int, Base), Lowercase),
  424. term([Prefix|S], F, Adj, none, Pad)
  425. end.
  426. %% char(Char, Field, Adjust, Precision, PadChar) -> [Char].
  427. char(C, none, _Adj, none, _Pad) -> [C];
  428. char(C, F, _Adj, none, _Pad) -> chars(C, F);
  429. char(C, none, _Adj, P, _Pad) -> chars(C, P);
  430. char(C, F, Adj, P, Pad) when F >= P ->
  431. adjust(chars(C, P), chars(Pad, F - P), Adj).
  432. %% newline(Field, Adjust, Precision, PadChar) -> [Char].
  433. newline(none, _Adj, _P, _Pad) -> "\n";
  434. newline(F, right, _P, _Pad) -> chars($\n, F).
  435. %%
  436. %% Utilities
  437. %%
  438. adjust(Data, [], _) -> Data;
  439. adjust(Data, Pad, left) -> [Data|Pad];
  440. adjust(Data, Pad, right) -> [Pad|Data].
  441. %% Flatten and truncate a deep list to at most N elements.
  442. flat_trunc(List, N) when is_integer(N), N >= 0 ->
  443. flat_trunc(List, N, []).
  444. flat_trunc(L, 0, R) when is_list(L) ->
  445. lists:reverse(R);
  446. flat_trunc([H|T], N, R) ->
  447. flat_trunc(T, N-1, [H|R]);
  448. flat_trunc([], _, R) ->
  449. lists:reverse(R).
  450. %% A deep version of string:chars/2,3
  451. chars(_C, 0) ->
  452. [];
  453. chars(C, 1) ->
  454. [C];
  455. chars(C, 2) ->
  456. [C,C];
  457. chars(C, 3) ->
  458. [C,C,C];
  459. chars(C, N) when is_integer(N), (N band 1) =:= 0 ->
  460. S = chars(C, N bsr 1),
  461. [S|S];
  462. chars(C, N) when is_integer(N) ->
  463. S = chars(C, N bsr 1),
  464. [C,S|S].
  465. %chars(C, N, Tail) ->
  466. % [chars(C, N)|Tail].
  467. %% Lowercase conversion
  468. cond_lowercase(String, true) ->
  469. lowercase(String);
  470. cond_lowercase(String,false) ->
  471. String.
  472. lowercase([H|T]) when is_integer(H), H >= $A, H =< $Z ->
  473. [(H-$A+$a)|lowercase(T)];
  474. lowercase([H|T]) ->
  475. [H|lowercase(T)];
  476. lowercase([]) ->
  477. [].