Skip to content

Commit

Permalink
Add append and update function to common (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
bretambrose authored Nov 21, 2019
1 parent cf3518d commit a0d6ea5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/aws/common/byte_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,15 @@ int aws_byte_buf_append_with_lookup(
AWS_COMMON_API
int aws_byte_buf_append_dynamic(struct aws_byte_buf *to, const struct aws_byte_cursor *from);

/**
* Copy contents of cursor to buffer, then update cursor to reference the memory stored in the buffer.
* If buffer is too small, AWS_ERROR_DEST_COPY_TOO_SMALL will be returned.
*
* The cursor is permitted to reference memory from earlier in the buffer.
*/
AWS_COMMON_API
int aws_byte_buf_append_and_update(struct aws_byte_buf *to, struct aws_byte_cursor *from_and_update);

/**
* Attempts to increase the capacity of a buffer to the requested capacity
*
Expand Down
12 changes: 12 additions & 0 deletions source/byte_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1349,3 +1349,15 @@ bool aws_byte_buf_write_float_be64(struct aws_byte_buf *buf, double x) {
x = aws_htonf64(x);
return aws_byte_buf_write(buf, (uint8_t *)&x, 8);
}

int aws_byte_buf_append_and_update(struct aws_byte_buf *to, struct aws_byte_cursor *from_and_update) {
AWS_PRECONDITION(aws_byte_buf_is_valid(to));
AWS_PRECONDITION(aws_byte_cursor_is_valid(from_and_update));

if (aws_byte_buf_append(to, from_and_update)) {
return AWS_OP_ERR;
}

from_and_update->ptr = to->buffer + (to->len - from_and_update->len);
return AWS_OP_SUCCESS;
}
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ add_test_case(test_byte_buf_reserve_relative)
add_test_case(test_byte_buf_reset)
add_test_case(test_byte_cursor_compare_lexical)
add_test_case(test_byte_cursor_compare_lookup)
add_test_case(test_byte_buf_append_and_update_fail)
add_test_case(test_byte_buf_append_and_update_success)

add_test_case(test_byte_cursor_find_str)
add_test_case(test_byte_cursor_find_str_not_found)
Expand Down
48 changes: 48 additions & 0 deletions tests/byte_buf_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,51 @@ static int s_test_byte_cursor_compare_lookup(struct aws_allocator *allocator, vo
return 0;
}
AWS_TEST_CASE(test_byte_cursor_compare_lookup, s_test_byte_cursor_compare_lookup)

static int s_test_byte_buf_append_and_update_fail(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
(void)allocator;

struct aws_byte_buf buffer;
aws_byte_buf_init(&buffer, allocator, 10);

struct aws_byte_cursor test_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TOOOOOOOO LONG");
struct aws_byte_cursor test_cursor_copy = test_cursor;

ASSERT_FAILS(aws_byte_buf_append_and_update(&buffer, &test_cursor));
ASSERT_TRUE((test_cursor.ptr == test_cursor_copy.ptr) && (test_cursor.len == test_cursor_copy.len));

aws_byte_buf_clean_up(&buffer);

return 0;
}
AWS_TEST_CASE(test_byte_buf_append_and_update_fail, s_test_byte_buf_append_and_update_fail)

static int s_test_byte_buf_append_and_update_success(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
(void)allocator;

struct aws_byte_buf buffer;
aws_byte_buf_init(&buffer, allocator, 12);

struct aws_byte_cursor test_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("SHORT");
struct aws_byte_cursor test_cursor_copy = test_cursor;

ASSERT_SUCCESS(aws_byte_buf_append_and_update(&buffer, &test_cursor));
ASSERT_TRUE(test_cursor.ptr == buffer.buffer);
ASSERT_TRUE(aws_byte_cursor_eq(&test_cursor, &test_cursor_copy));

struct aws_byte_cursor test_cursor2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STOP");
struct aws_byte_cursor test_cursor_copy2 = test_cursor2;

ASSERT_SUCCESS(aws_byte_buf_append_and_update(&buffer, &test_cursor2));
ASSERT_TRUE(test_cursor2.ptr == buffer.buffer + test_cursor.len);
ASSERT_TRUE(aws_byte_cursor_eq(&test_cursor2, &test_cursor_copy2));

ASSERT_TRUE(buffer.len == test_cursor.len + test_cursor2.len);

aws_byte_buf_clean_up(&buffer);

return 0;
}
AWS_TEST_CASE(test_byte_buf_append_and_update_success, s_test_byte_buf_append_and_update_success)

0 comments on commit a0d6ea5

Please sign in to comment.