Skip to content

Commit 2b67a65

Browse files
bretambroseBret Ambrose
andauthored
Add API for a more compact (no dashes) UUID-to-str (#1212)
Co-authored-by: Bret Ambrose <[email protected]>
1 parent b54e189 commit 2b67a65

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

include/aws/common/uuid.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ struct aws_uuid {
1818

1919
/* 36 bytes for the UUID plus one more for the null terminator. */
2020
enum { AWS_UUID_STR_LEN = 37 };
21+
/* 32 bytes for the UUID (no dashes) plus one more for the null terminator. */
22+
enum { AWS_UUID_STR_COMPACT_LEN = 33 };
2123

2224
AWS_EXTERN_C_BEGIN
2325

2426
AWS_COMMON_API int aws_uuid_init(struct aws_uuid *uuid);
2527
AWS_COMMON_API int aws_uuid_init_from_str(struct aws_uuid *uuid, const struct aws_byte_cursor *uuid_str);
2628
AWS_COMMON_API int aws_uuid_to_str(const struct aws_uuid *uuid, struct aws_byte_buf *output);
29+
AWS_COMMON_API int aws_uuid_to_str_compact(const struct aws_uuid *uuid, struct aws_byte_buf *output);
2730
AWS_COMMON_API bool aws_uuid_equals(const struct aws_uuid *a, const struct aws_uuid *b);
2831

2932
AWS_EXTERN_C_END

source/uuid.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT "-" HEX_CHAR_FMT HEX_CHAR_FMT \
1818
"-" HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT
1919

20+
#define UUID_FORMAT_COMPACT \
21+
HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT \
22+
HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT HEX_CHAR_FMT
23+
2024
#include <stdio.h>
2125

2226
#ifdef _MSC_VER
@@ -66,14 +70,18 @@ int aws_uuid_init_from_str(struct aws_uuid *uuid, const struct aws_byte_cursor *
6670
return AWS_OP_SUCCESS;
6771
}
6872

69-
int aws_uuid_to_str(const struct aws_uuid *uuid, struct aws_byte_buf *output) {
73+
static int s_uuid_to_str(
74+
const struct aws_uuid *uuid,
75+
struct aws_byte_buf *output,
76+
const char *format_string,
77+
size_t padded_formatted_length) {
7078
size_t space_remaining = output->capacity - output->len;
71-
AWS_ERROR_PRECONDITION(space_remaining >= AWS_UUID_STR_LEN, AWS_ERROR_SHORT_BUFFER);
79+
AWS_ERROR_PRECONDITION(space_remaining >= padded_formatted_length, AWS_ERROR_SHORT_BUFFER);
7280

7381
snprintf(
7482
(char *)(output->buffer + output->len),
7583
space_remaining,
76-
UUID_FORMAT,
84+
format_string,
7785
uuid->uuid_data[0],
7886
uuid->uuid_data[1],
7987
uuid->uuid_data[2],
@@ -91,11 +99,19 @@ int aws_uuid_to_str(const struct aws_uuid *uuid, struct aws_byte_buf *output) {
9199
uuid->uuid_data[14],
92100
uuid->uuid_data[15]);
93101

94-
output->len += AWS_UUID_STR_LEN - 1;
102+
output->len += padded_formatted_length - 1;
95103

96104
return AWS_OP_SUCCESS;
97105
}
98106

107+
int aws_uuid_to_str(const struct aws_uuid *uuid, struct aws_byte_buf *output) {
108+
return s_uuid_to_str(uuid, output, UUID_FORMAT, AWS_UUID_STR_LEN);
109+
}
110+
111+
int aws_uuid_to_str_compact(const struct aws_uuid *uuid, struct aws_byte_buf *output) {
112+
return s_uuid_to_str(uuid, output, UUID_FORMAT_COMPACT, AWS_UUID_STR_COMPACT_LEN);
113+
}
114+
99115
bool aws_uuid_equals(const struct aws_uuid *a, const struct aws_uuid *b) {
100116
return 0 == memcmp(a->uuid_data, b->uuid_data, sizeof(a->uuid_data));
101117
}

tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,11 @@ add_test_case(device_rand_buffer_append_distribution)
384384
add_test_case(device_rand_buffer_append_short_buffer)
385385

386386
add_test_case(uuid_string)
387+
add_test_case(uuid_string_compact)
387388
add_test_case(prefilled_uuid_string)
389+
add_test_case(prefilled_uuid_string_compact)
388390
add_test_case(uuid_string_short_buffer)
391+
add_test_case(uuid_string_compact_short_buffer)
389392
add_test_case(uuid_string_parse)
390393
add_test_case(uuid_string_parse_too_short)
391394
add_test_case(uuid_string_parse_malformed)

tests/uuid_test.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ static int s_uuid_string_fn(struct aws_allocator *allocator, void *ctx) {
2929

3030
AWS_TEST_CASE(uuid_string, s_uuid_string_fn)
3131

32+
static int s_uuid_string_compact_fn(struct aws_allocator *allocator, void *ctx) {
33+
(void)allocator;
34+
(void)ctx;
35+
36+
struct aws_uuid uuid;
37+
ASSERT_SUCCESS(aws_uuid_init(&uuid));
38+
39+
uint8_t uuid_array[AWS_UUID_STR_COMPACT_LEN] = {0};
40+
struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
41+
uuid_buf.len = 0;
42+
43+
ASSERT_SUCCESS(aws_uuid_to_str_compact(&uuid, &uuid_buf));
44+
uint8_t zerod_buf[AWS_UUID_STR_COMPACT_LEN] = {0};
45+
ASSERT_UINT_EQUALS(AWS_UUID_STR_COMPACT_LEN - 1, uuid_buf.len);
46+
ASSERT_FALSE(0 == memcmp(zerod_buf, uuid_array, sizeof(uuid_array)));
47+
48+
return AWS_OP_SUCCESS;
49+
}
50+
51+
AWS_TEST_CASE(uuid_string_compact, s_uuid_string_compact_fn)
52+
3253
static int s_prefilled_uuid_string_fn(struct aws_allocator *allocator, void *ctx) {
3354
(void)allocator;
3455
(void)ctx;
@@ -52,6 +73,29 @@ static int s_prefilled_uuid_string_fn(struct aws_allocator *allocator, void *ctx
5273

5374
AWS_TEST_CASE(prefilled_uuid_string, s_prefilled_uuid_string_fn)
5475

76+
static int s_prefilled_uuid_string_compact_fn(struct aws_allocator *allocator, void *ctx) {
77+
(void)allocator;
78+
(void)ctx;
79+
80+
struct aws_uuid uuid = {
81+
.uuid_data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10},
82+
};
83+
84+
uint8_t uuid_array[AWS_UUID_STR_COMPACT_LEN] = {0};
85+
struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
86+
uuid_buf.len = 0;
87+
88+
ASSERT_SUCCESS(aws_uuid_to_str_compact(&uuid, &uuid_buf));
89+
90+
const char *expected_str = "0102030405060708090a0b0c0d0e0f10";
91+
struct aws_byte_buf expected = aws_byte_buf_from_c_str(expected_str);
92+
ASSERT_BIN_ARRAYS_EQUALS(expected.buffer, expected.len, uuid_buf.buffer, uuid_buf.len);
93+
94+
return AWS_OP_SUCCESS;
95+
}
96+
97+
AWS_TEST_CASE(prefilled_uuid_string_compact, s_prefilled_uuid_string_compact_fn)
98+
5599
static int s_uuid_string_short_buffer_fn(struct aws_allocator *allocator, void *ctx) {
56100
(void)allocator;
57101
(void)ctx;
@@ -70,6 +114,24 @@ static int s_uuid_string_short_buffer_fn(struct aws_allocator *allocator, void *
70114

71115
AWS_TEST_CASE(uuid_string_short_buffer, s_uuid_string_short_buffer_fn)
72116

117+
static int s_uuid_string_compact_short_buffer_fn(struct aws_allocator *allocator, void *ctx) {
118+
(void)allocator;
119+
(void)ctx;
120+
121+
struct aws_uuid uuid;
122+
ASSERT_SUCCESS(aws_uuid_init(&uuid));
123+
124+
uint8_t uuid_array[AWS_UUID_STR_COMPACT_LEN - 2] = {0};
125+
struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_array, sizeof(uuid_array));
126+
uuid_buf.len = 0;
127+
128+
ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_uuid_to_str_compact(&uuid, &uuid_buf));
129+
130+
return AWS_OP_SUCCESS;
131+
}
132+
133+
AWS_TEST_CASE(uuid_string_compact_short_buffer, s_uuid_string_compact_short_buffer_fn)
134+
73135
static int s_uuid_string_parse_fn(struct aws_allocator *allocator, void *ctx) {
74136
(void)allocator;
75137
(void)ctx;

0 commit comments

Comments
 (0)