changeset 38:eb004c1b9579

Update base-n code from https://github.com/azawadzki/base-n
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 22 Aug 2016 17:16:11 +0100
parents f8332b1acfc2
children 183be3bc9d7b
files json/base-n/example/basen_example.cpp json/base-n/include/basen.hpp
diffstat 2 files changed, 14 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/json/base-n/example/basen_example.cpp	Wed May 25 14:49:44 2016 +0100
+++ b/json/base-n/example/basen_example.cpp	Mon Aug 22 17:16:11 2016 +0100
@@ -70,7 +70,7 @@
                return 3;
             }
 
-            static char encode(int index)
+            static char encode(unsigned int index)
             {
                 const char* const dictionary = "01234567";
                 assert(index < strlen(dictionary));
--- a/json/base-n/include/basen.hpp	Wed May 25 14:49:44 2016 +0100
+++ b/json/base-n/include/basen.hpp	Mon Aug 22 17:16:11 2016 +0100
@@ -52,13 +52,17 @@
 namespace impl
 {
 
+const int ERROR = -1;
+
 namespace {
 
 char extract_partial_bits(char value, unsigned int start_bit, unsigned int bits_count)
 {
     assert(start_bit + bits_count < 8);
+    // shift extracted bits to the beginning of the byte
     char t1 = value >> (8 - bits_count - start_bit);
-    char t2 = t1 & ~(-1 << bits_count);
+    // mask out bits on the left
+    char t2 = t1 & ~(-1U << bits_count);
     return t2;
 }
 
@@ -68,8 +72,8 @@
     int bits_count_in_previous = 8 - start_bit;
     int bits_count_in_next = bits_count - bits_count_in_previous;
     char t1 = previous << bits_count_in_next;
-    char t2 = next >> (8 - bits_count_in_next) & ~(-1 << bits_count_in_next) ;
-    return (t1 | t2) & ~(-1 << bits_count);
+    char t2 = next >> (8 - bits_count_in_next) & ~(-1U << bits_count_in_next) ;
+    return (t1 | t2) & ~(-1U << bits_count);
 }
 
 }
@@ -95,7 +99,7 @@
         } else if (c >= 'A' && c <= 'F') {
             return c - 'A' + 10;
         }
-        return -1;
+        return ERROR;
     }
 };
 
@@ -120,7 +124,7 @@
         } else if (c >= '2' && c <= '7') {
             return c - '2' + 26;
         }
-        return -1;
+        return ERROR;
     }
 };
 
@@ -152,7 +156,7 @@
         } else if (c == '/') {
             return c - '/' + alph_len * 2 + 11;
         }
-        return -1;
+        return ERROR;
     }
 };
 
@@ -169,7 +173,7 @@
             continue;
         }
         char value = ConversionTraits::decode(*iter);
-        if (value == -1) {
+        if (value == ERROR) {
             // malformed data, but let's go on...
             ++iter;
             continue;
@@ -186,9 +190,9 @@
                 output_current_bit = 0;
             }
         } else {
-            // the value span across current and next byte
+            // the value spans across the current and the next byte
             int bits_in_next_byte = ConversionTraits::group_length() - bits_in_current_byte;
-            // fill current byte and flush it to our output
+            // fill the current byte and flush it to our output
             buffer |= value >> bits_in_next_byte;
             *out++ = buffer;
             buffer = 0;