Skip to content

Commit

Permalink
Add new binary conversion bifs
Browse files Browse the repository at this point in the history
Added: binary_to_integer/1,2, integer_to_binary/1,2
  • Loading branch information
garazdawi committed Feb 14, 2013
1 parent e55aff9 commit b074099
Show file tree
Hide file tree
Showing 14 changed files with 736 additions and 218 deletions.
49 changes: 48 additions & 1 deletion erts/doc/src/erlang.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,32 @@
<p>Failure: <c>badarg</c> if the atom does not already exist.</p>
</desc>
</func>
<func>
<name name="binary_to_integer" arity="1"/>
<fsummary>Convert from text representation to an integer</fsummary>
<desc>
<p>Returns an integer whose text representation is
<c><anno>Binary</anno></c>.</p>
<pre>
> <input>binary_to_integer(&lt;&lt;"123"&gt;&gt;).</input>
123</pre>
<p>Failure: <c>badarg</c> if <c><anno>Binary</anno></c> contains a bad
representation of an integer.</p>
</desc>
</func>
<func>
<name name="binary_to_integer" arity="2"/>
<fsummary>Convert from text representation to an integer</fsummary>
<desc>
<p>Returns an integer whose text representation in base
<c><anno>Base</anno></c> is <c><anno>Binary</anno></c>.</p>
<pre>
> <input>binary_to_integer(&lt;&lt;"3FF"&gt;&gt;, 16).</input>
1023</pre>
<p>Failure: <c>badarg</c> if <c><anno>Binary</anno></c> contains a bad
representation of an integer.</p>
</desc>
</func>
<func>
<name name="binary_to_list" arity="1"/>
<fsummary>Convert a binary to a list</fsummary>
Expand Down Expand Up @@ -1429,7 +1455,28 @@ os_prompt% </pre>
{one,new,two,three}</pre>
</desc>
</func>

<func>
<name name="integer_to_binary" arity="1"/>
<fsummary>Text representation of an integer</fsummary>
<desc>
<p>Returns a binary which corresponds to the text
representation of <c><anno>Integer</anno></c>.</p>
<pre>
> <input>integer_to_binary(77).</input>
&lt;&lt;"77">></pre>
</desc>
</func>
<func>
<name name="integer_to_binary" arity="2"/>
<fsummary>Text representation of an integer</fsummary>
<desc>
<p>Returns a binary which corresponds to the text
representation of <c><anno>Integer</anno></c> in base <c><anno>Base</anno></c>.</p>
<pre>
> <input>integer_to_binary(1023, 16).</input>
&lt;&lt;"3FF">></pre>
</desc>
</func>
<func>
<name name="integer_to_list" arity="1"/>
<fsummary>Text representation of an integer</fsummary>
Expand Down
50 changes: 47 additions & 3 deletions erts/emulator/beam/bif.c
Original file line number Diff line number Diff line change
Expand Up @@ -2895,20 +2895,64 @@ BIF_RETTYPE string_to_integer_1(BIF_ALIST_1)
BIF_RET(TUPLE2(hp, res, tail));
}
}


BIF_RETTYPE list_to_integer_1(BIF_ALIST_1)
{
{
/* Using do_list_to_integer is about twice as fast as using
erts_chars_to_integer because we do not have to copy the
entire list */
Eterm res;
Eterm dummy;
/* must be a list */

if (do_list_to_integer(BIF_P,BIF_ARG_1,&res,&dummy) != LTI_ALL_INTEGER) {
BIF_ERROR(BIF_P,BADARG);
}
BIF_RET(res);
}

BIF_RETTYPE list_to_integer_2(BIF_ALIST_2)
{

/* Bif implementation is about 50% faster than pure erlang,
and since we have erts_chars_to_integer now it is simpler
as well. This could be optmized further if we did not have to
copy the list to buf. */
int i;
Eterm res;
char *buf = NULL;
int base;

i = list_length(BIF_ARG_1);
if (i < 0)
BIF_ERROR(BIF_P, BADARG);

base = signed_val(BIF_ARG_2);

if (base < 2 || base > 36)
BIF_ERROR(BIF_P, BADARG);

/* Take fast path if base it 10 */
if (base == 10)
return list_to_integer_1(BIF_P,&BIF_ARG_1);

buf = (char *) erts_alloc(ERTS_ALC_T_TMP, i + 1);

if (intlist_to_buf(BIF_ARG_1, buf, i) < 0)
goto list_to_integer_1_error;
buf[i] = '\0'; /* null terminal */

if ((res = erts_chars_to_integer(BIF_P,buf,i,base)) == THE_NON_VALUE)
goto list_to_integer_1_error;

erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_RET(res);

list_to_integer_1_error:
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_ERROR(BIF_P, BADARG);

}

/**********************************************************************/

/* convert a float to a list of ascii characters */
Expand Down
4 changes: 4 additions & 0 deletions erts/emulator/beam/bif.tab
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,10 @@ bif erlang:prepare_loading/2
bif erlang:finish_loading/1
bif erlang:insert_element/3
bif erlang:delete_element/2
bif erlang:binary_to_integer/1
bif erlang:binary_to_integer/2
bif erlang:integer_to_binary/1
bif erlang:list_to_integer/2

#
# Obsolete
Expand Down
Loading

0 comments on commit b074099

Please sign in to comment.