[erlang-questions] Why MAXCS?

sand <>
Sat Mar 7 08:49:59 CET 2009


Down in the guts of stdlib-1.15.3/src/io_lib_pretty.erl, there's a
preprocessor definition:

  -define(MAXCS, 60). % Approx max number of characters on one line.

It's used in various places in the file to detect "long" lines and
split them when pretty-printing.  For example, in
io_lib_pretty:print/5, we do a one-line write if the term would exceed
the provided line length (variable Ll), *or* of the term does not
exceed MAXCS.

  print(Term, Col, Ll, D, RecDefFun) when is_tuple(Term); 
                                          is_list(Term) ->
      If = {_S, Len} = print_length(Term, D, RecDefFun),
      if
          Len < Ll - Col, Len =< ?MAXCS ->
              write(If);
          true ->
              TInd = while_fail([-1, 4], 
                                fun(I) -> cind(If, Col, Ll, I, 0, 0) end, 
                                1),
              pp(If, Col, Ll, TInd, indent(Col), 0, 0)
      end;
  print(<<_/bitstring>>=Term, Col, Ll, D, RecDefFun) ->
      If = {_S, Len} = print_length(Term, D, RecDefFun),
      if
          Len < Ll - Col, Len =< ?MAXCS ->
              write(If);
          true ->
              TInd = while_fail([-1, 4], 
                                fun(I) -> cind(If, Col, Ll, I, 0, 0) end, 
                                1),
              pp(If, Col, Ll, TInd, indent(Col), 0, 0)
      end;

This means that all you can realy do with the LineLength argument to
io_lib:print/4 is drop it to something smaller than 60.  Bumping it up
to 1000000 (to try to get everything printed on one line) doesn't
work.

%% Standard

4> io:put_chars(io_lib:print(["foo", "foo", "foo", "foo", "foo", "foo",
 "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo",
 "foo", "foo", "foo", "foo", "foo"])).
["foo","foo","foo","foo","foo","foo","foo","foo","foo",
 "foo","foo","foo","foo","foo","foo","foo","foo","foo","foo",
 "foo","foo"]ok

%% Small LineLength --- prints short lines

5> io:put_chars(io_lib:print(["foo", "foo", "foo", "foo", "foo", "foo",
 "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo",
 "foo", "foo", "foo", "foo", "foo"], 1, 20, -1)).
["foo","foo",
 "foo","foo",
 "foo","foo",
 "foo","foo",
 "foo","foo",
 "foo","foo",
 "foo","foo",
 "foo","foo",
 "foo","foo",
 "foo","foo",
 "foo"]ok

%% Big LineLength --- doesn't print long lines

6> io:put_chars(io_lib:print(["foo", "foo", "foo", "foo", "foo", "foo",
 "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo",
 "foo", "foo", "foo", "foo", "foo"], 1, 200000, -1)).
["foo","foo","foo","foo","foo","foo","foo","foo","foo",
 "foo","foo","foo","foo","foo","foo","foo","foo","foo","foo",
 "foo","foo"]ok

Is this behavior deliberate?  If so, it should be documented that line
lengths are locked to a maximum of 60 (in io_lib and io).  Otherwise,
it would be very helpful if we could get rid of MAXCS.

Derek

-- 
Derek Upham




More information about the erlang-questions mailing list