Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

523 wiersze
18 KiB

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