Gaga about bifs (Was: Complexity Shock Horror II: the Sequel

Raimo Niskanen raimo.niskanen@REDACTED
Wed Apr 9 10:59:45 CEST 2003


I have just added something like that to io_lib:fwrite. I did not have 
your code available when I did it, but I had an example from Happi that 
sounds just like your suggestion.

This was in the thread "MD5 in erlang.", by the way.

Here is how it will be in R9C unless anyone has strong objections:
     io_lib:format("~.16x", [-31,"0x"]) -> "-0x1f"
     io_lib:format("~.16X", [-31,"0x"]) -> "-0x1F"
~b/~B is just ~x/~X with empty prefix:
     io_lib:format("~.16b", [-31]) -> "-1f"
     io_lib:format("~.16B", [-31]) -> "-1F"
and ~u/~U is the same as ~b/~B with the argument integer 'band'ed with 
((1<<WordSize)-1):
     io_lib:format("~.16U", [31,32]) -> "1F"
     io_lib:format("~.16u", [-31,32]) -> "ffffffe1"
     io_lib:format("~8.16.0u", [31,32]) -> "0000001f"
     io_lib:format("~8.16.0U", [-31,32]) -> "FFFFFFE1"

And for io:fread I added:
     io_lib:fread("~16u", "FFFF") -> {ok,[65535],[]}
     io_lib:fread("~-~16u", "-FFFF") -> {ok,[-1,65535],[]}
~u scans unsigned integers in specified base.
~- scans an optional sign character, so you can write:
     io_lib:fread("~-0x~16u", "-0xFFFF") -> {ok,[-1,65535],[]}
     io_lib:fread("~-0x~16u", "+0xFFFF") -> {ok,[1,65535],[]}
     io_lib:fread("~-0x~16u", "0xFFFF") -> {ok,[1,65535],[]}
Therefore ~- and ~u does not skip leading whitespace.

/ Raimo Niskanen, Erlang/OTP



Robert Virding wrote:
> Why is everyone so completely gaga about adding new bifs to do relatively
> trivial things? Especially seeing you can already to do it without adding
> new stuff. Use erl_scan:string and io_lib:fwrite. I have sent a modified
> io_lib to erlang_questions a  couple of times but it hasn't seemed to make
> it into the distribution. If Björn sends the sgml manpage I can add it.
> 
> My io_lib contained a ~# to write signed based integers and ~b and ~x for
> binary and hex bitfields. You usually need these diferent styles of writing
> a number.
> 
> If someone wants my io_lib I will post it  again.
> 
> Robert
> 
> ----- Original Message -----
> From: "Raimo Niskanen" <raimo.niskanen@REDACTED>
> To: <erlang-questions@REDACTED>
> Sent: Tuesday, April 01, 2003 2:35 PM
> Subject: Re: Complexity Shock Horror II: the Sequel (was Re: MD5 in erlang
> 
> 
> 
>>Alright then, I will add integer_to_list(Integer, Base) and
>>list_to_integer(List, Base) to the module 'erlang', and to the
>>documentation for R9C. We will see if they become real BIFs to R9C.
>>
>>integer(Base), 2 =< Base, Base =< 36.
>>
>>integer_to_list/2 allows leading '-' or '+' but no prefix. Both
>>uppercase or lowercase digits. No internal '_' since integer_to_list/1
>>does not allow it, and neither do the compiler. (yet :-)
>>
>>list_to_integer/2 produces uppercase digits and a leading '-' for
>>negative numbers.
>>
>>/ Raimo Niskanen, Erlang/OTP
>>
>>
>>
>>integer_to_list(I, Base)
>>   when integer(I), integer(Base), Base >= 2, Base =< 1+$Z-$A+10 ->
>>     if I < 0 ->
>>    [$-|integer_to_list(-I, Base, [])];
>>        true ->
>>    integer_to_list(I, Base, [])
>>     end;
>>integer_to_list(I, Base) ->
>>     erlang:fault(badarg, [I, Base]).
>>
>>integer_to_list(I0, Base, R0) ->
>>     D = I0 rem Base,
>>     I1 = I0 div Base,
>>     R1 = if D >= 10 ->
>>[D-10+$A|R0];
>>    true ->
>>[D+$0|R0]
>>end,
>>     if I1 == 0 ->
>>    R1;
>>        true ->
>>    integer_to_list(I1, Base, R1)
>>     end.
>>
>>
>>
>>list_to_integer(L, Base)
>>   when list(L), integer(Base), Base >= 2, Base =< 1+$Z-$A+10 ->
>>     case list_to_integer_sign(L, Base) of
>>I when integer(I) ->
>>    I;
>>Fault ->
>>    erlang:fault(Fault, [L,Base])
>>     end;
>>list_to_integer(L, Base) ->
>>     erlang:fault(badarg, [L,Base]).
>>
>>list_to_integer_sign([$-|[_|_]=L], Base) ->
>>     case list_to_integer(L, Base, 0) of
>>I when integer(I) ->
>>    -I;
>>I ->
>>    I
>>     end;
>>list_to_integer_sign([$+|[_|_]=L], Base) ->
>>     list_to_integer(L, Base, 0);
>>list_to_integer_sign([_|_]=L, Base) ->
>>     list_to_integer(L, Base, 0);
>>list_to_integer_sign(_, _) ->
>>     badarg.
>>
>>list_to_integer([D|L], Base, I)
>>   when integer(D), D >= $0, D =< $9, D < Base+$0 ->
>>     list_to_integer(L, Base, I*Base + D-$0);
>>list_to_integer([D|L], Base, I)
>>   when integer(D), D >= $A, D < Base+$A-10 ->
>>     list_to_integer(L, Base, I*Base + D-$A+10);
>>list_to_integer([D|L], Base, I)
>>   when integer(D), D >= $a, D < Base+$a-10 ->
>>     list_to_integer(L, Base, I*Base + D-$a+10);
>>list_to_integer([], _, I) ->
>>     I;
>>list_to_integer(_, _, _) ->
>>     badarg.
>>
>>
>>
>>Tony Rogvall wrote:
>>
>>>Raimo Niskanen wrote:
>>>
>>>
>>>>Some comments and some problems. Since Erlang supports the
>>>>Base#Integer syntax i think that Base in:
>>>>    integer_to_list(Integer, Base)
>>>>should be 2..16, not atoms.
>>>>
>>>
>>>I just thought it would look nice, but I can buy 2..16 (btw do you know
>>>any one using base 13 for output? and since we only alow 2..16 its kind
>>>of crippled anyway)
>>>
>>>
>>>
>>>>The same for:
>>>>    list_to_integer(List, Base)
>>>>
>>>
>>>Oh well.
>>>
>>>
>>>>But what should happen if you call list_to_integer("16#1f", 8), and
>>>>how should you specify any base. I guess the answers are: badarg on
>>>>the first and list_to_integer("16#1f", undefined) on the second
>>>
> question.
> 
>>>Like you said, do NOT allow base syntax in the list_to_integer/2 (why
>>>should you?) but maybe in list_to_integer/1 (if that does not break
>>>anything, it could!)
>>>
>>>
>>>>And to add the prefix in the Right(tm) way is a bit awkward:
>>>>    List = case integer_to_list(Integer, 16) of
>>>>            [$-|L] -> "-16#"++L;
>>>>            L -> "16#"++L
>>>>        end
>>>
>>>
>>>My oppinon is that we ignore the prefix stuff completly just to get
>>>things on it's way. I myself have never sent any output in base syntax
>>>(anyone?).
>>>
>>>/Tony
>>
>>
> 




More information about the erlang-questions mailing list