couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject couch commit: updated refs/heads/master to 4708e32
Date Tue, 28 Jul 2015 22:29:42 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/master a150e5d24 -> 4708e3271


Avoid arithmetic in `to_hex` function


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/4708e327
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/4708e327
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/4708e327

Branch: refs/heads/master
Commit: 4708e3271f96fc0d0e5e545aae3bba43ddd0cdca
Parents: a150e5d
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Mon Jul 27 10:13:45 2015 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Mon Jul 27 10:13:45 2015 -0700

----------------------------------------------------------------------
 src/couch_util.erl        | 31 +++++++++++++++++++++++--------
 test/couch_util_tests.erl |  8 ++++++++
 2 files changed, 31 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/4708e327/src/couch_util.erl
----------------------------------------------------------------------
diff --git a/src/couch_util.erl b/src/couch_util.erl
index 742ab70..2015244 100644
--- a/src/couch_util.erl
+++ b/src/couch_util.erl
@@ -133,15 +133,30 @@ validate_utf8_fast(B, O) ->
             false
     end.
 
-to_hex([]) ->
-    [];
-to_hex(Bin) when is_binary(Bin) ->
-    to_hex(binary_to_list(Bin));
-to_hex([H|T]) ->
-    [to_digit(H div 16), to_digit(H rem 16) | to_hex(T)].
 
-to_digit(N) when N < 10 -> $0 + N;
-to_digit(N)             -> $a + N-10.
+to_hex(<<Hi:4, Lo:4, Rest/binary>>) ->
+    [nibble_to_hex(Hi), nibble_to_hex(Lo) | to_hex(Rest)];
+to_hex(<<>>) ->
+    [];
+to_hex(List) when is_list(List) ->
+    to_hex(list_to_binary(List)).
+
+nibble_to_hex(0) -> $0;
+nibble_to_hex(1) -> $1;
+nibble_to_hex(2) -> $2;
+nibble_to_hex(3) -> $3;
+nibble_to_hex(4) -> $4;
+nibble_to_hex(5) -> $5;
+nibble_to_hex(6) -> $6;
+nibble_to_hex(7) -> $7;
+nibble_to_hex(8) -> $8;
+nibble_to_hex(9) -> $9;
+nibble_to_hex(10) -> $a;
+nibble_to_hex(11) -> $b;
+nibble_to_hex(12) -> $c;
+nibble_to_hex(13) -> $d;
+nibble_to_hex(14) -> $e;
+nibble_to_hex(15) -> $f.
 
 
 parse_term(Bin) when is_binary(Bin) ->

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/4708e327/test/couch_util_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_util_tests.erl b/test/couch_util_tests.erl
index b3c0702..a0e9238 100644
--- a/test/couch_util_tests.erl
+++ b/test/couch_util_tests.erl
@@ -160,3 +160,11 @@ should_fail_for_missing_cb() ->
                 {error, {undefined_callback, Name, MFA}},
                 couch_util:validate_callback_exists(M, F, A))}
         end, Cases).
+
+to_hex_test_() ->
+    [
+        ?_assertEqual("", couch_util:to_hex([])),
+        ?_assertEqual("010203faff", couch_util:to_hex([1, 2, 3, 250, 255])),
+        ?_assertEqual("", couch_util:to_hex(<<>>)),
+        ?_assertEqual("010203faff", couch_util:to_hex(<<1, 2, 3, 250, 255>>))
+    ].


Mime
View raw message