Skip to content

Commit e95569a

Browse files
author
Pierre-Alexandre Meyer
committed
utils: make ZZ_DECODE an inline function, to ensure cast
Signed-off-by: Pierre-Alexandre Meyer <pierre@ning.com>
1 parent 85abaee commit e95569a

File tree

5 files changed

+15
-29
lines changed

5 files changed

+15
-29
lines changed

c/src/smile_decode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void smile_decode_value(u8** orig_data, struct content_handler* handler)
210210
handler->start_value();
211211
(*orig_data)++;
212212
ip++;
213-
handler->number_value(ZZ_DECODE(*ip & 0x1F));
213+
handler->number_value(zz_decode(*ip & 0x1F));
214214
handler->end_value();
215215
// Misc; binary / text / structure markers
216216
} else {

c/src/smile_decode.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,6 @@
44
#include "api.h"
55

66
#define DEBUG_BYTE(byte) printf("0x%02x ", (u8) byte);
7-
#define PRINT(ip, length) while (length--) {putchar(*ip++);}
8-
#define PRINT_EOK putchar(':'); putchar(' ');
9-
#define PRINT_INDENT printf("%-*s", indent, " ");
10-
11-
#define PRINT_STRING_VALUE(ip, length) \
12-
putchar('"'); \
13-
PRINT(ip, length) \
14-
putchar('"');
15-
16-
#define PRINT_STRING_KEY(ip, length) \
17-
PRINT_INDENT \
18-
PRINT_STRING_VALUE(ip, length)
19-
20-
#define PRINT_INT_VALUE(ip) \
21-
printf("%d", ZZ_DECODE(ip));
22-
23-
#define PRINT_LONG_VALUE(ip) \
24-
printf("%lu", ZZ_DECODE(ip));
257

268
#define SUCCESS 0
279

c/src/smile_utils.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616

1717
#include "smile_utils.h"
1818

19+
inline long zz_decode(unsigned long n)
20+
{
21+
return ((n >> 1) ^ (-(n & 1)));
22+
}
23+
1924
long zzvarint_decode(u8 **msg)
2025
{
21-
return ZZ_DECODE(varint_decode(msg));
26+
return zz_decode(varint_decode(msg));
2227
}
2328

2429
unsigned long varint_decode(u8 **msg)

c/src/smile_utils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
#include "api.h"
55

6-
#define ZZ_DECODE(n) ((n >> 1) ^ (-(n & 1)))
7-
6+
inline long zz_decode(unsigned long);
87
long zzvarint_decode(u8**);
98
unsigned long varint_decode(u8**);
109
unsigned long varint_decode_buffer(u8*);

c/test/test_varint.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ void test_number_encoding()
4343
// 0x 0111 1100 1010 0000
4444
// 111 1100 010 0000
4545
// 11 1110 0010 0000
46-
ASSERT_EQUAL(8080, ZZ_DECODE(varint_decode_buffer(&buf)));
46+
ASSERT_EQUAL(8080, zz_decode(varint_decode_buffer(&buf)));
4747
}
4848

4949
void test_zigzag()
5050
{
51-
ASSERT_EQUAL(0, ZZ_DECODE(0))
52-
ASSERT_EQUAL(-1, ZZ_DECODE(1))
53-
ASSERT_EQUAL(1, ZZ_DECODE(2))
54-
ASSERT_EQUAL(-2, ZZ_DECODE(3))
55-
ASSERT_EQUAL(2147483647, ZZ_DECODE(4294967294))
56-
ASSERT_EQUAL(-2147483648, ZZ_DECODE(4294967295))
51+
ASSERT_EQUAL(0, zz_decode(0))
52+
ASSERT_EQUAL(-1, zz_decode(1))
53+
ASSERT_EQUAL(1, zz_decode(2))
54+
ASSERT_EQUAL(-2, zz_decode(3))
55+
ASSERT_EQUAL(2147483647, zz_decode(4294967294))
56+
ASSERT_EQUAL(-2147483648, zz_decode(4294967295))
5757
}
5858

5959
void test_varint()

0 commit comments

Comments
 (0)