diff options
author | unril <unril@yandex-team.ru> | 2022-02-10 16:46:05 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:05 +0300 |
commit | 3b241dd57cf58f20bbbd63fa6a0a758dbec09b68 (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /contrib/restricted/aws/aws-c-common/source/string.c | |
parent | 11ae9eca250d0188b7962459cbc6706719e7dca9 (diff) | |
download | ydb-3b241dd57cf58f20bbbd63fa6a0a758dbec09b68.tar.gz |
Restoring authorship annotation for <unril@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/restricted/aws/aws-c-common/source/string.c')
-rw-r--r-- | contrib/restricted/aws/aws-c-common/source/string.c | 130 |
1 files changed, 65 insertions, 65 deletions
diff --git a/contrib/restricted/aws/aws-c-common/source/string.c b/contrib/restricted/aws/aws-c-common/source/string.c index 4bd67ca7b2..d1abf0dbff 100644 --- a/contrib/restricted/aws/aws-c-common/source/string.c +++ b/contrib/restricted/aws/aws-c-common/source/string.c @@ -1,41 +1,41 @@ /** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. - */ -#include <aws/common/string.h> - -struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, const char *c_str) { + */ +#include <aws/common/string.h> + +struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, const char *c_str) { AWS_PRECONDITION(allocator && c_str); - return aws_string_new_from_array(allocator, (const uint8_t *)c_str, strlen(c_str)); -} - -struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { + return aws_string_new_from_array(allocator, (const uint8_t *)c_str, strlen(c_str)); +} + +struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); - size_t malloc_size; - if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { - return NULL; - } - struct aws_string *str = aws_mem_acquire(allocator, malloc_size); - if (!str) { - return NULL; - } - - /* Fields are declared const, so we need to copy them in like this */ - *(struct aws_allocator **)(&str->allocator) = allocator; - *(size_t *)(&str->len) = len; + size_t malloc_size; + if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { + return NULL; + } + struct aws_string *str = aws_mem_acquire(allocator, malloc_size); + if (!str) { + return NULL; + } + + /* Fields are declared const, so we need to copy them in like this */ + *(struct aws_allocator **)(&str->allocator) = allocator; + *(size_t *)(&str->len) = len; if (len > 0) { memcpy((void *)str->bytes, bytes, len); } - *(uint8_t *)&str->bytes[len] = '\0'; + *(uint8_t *)&str->bytes[len] = '\0'; AWS_RETURN_WITH_POSTCONDITION(str, aws_string_is_valid(str)); -} - -struct aws_string *aws_string_new_from_string(struct aws_allocator *allocator, const struct aws_string *str) { +} + +struct aws_string *aws_string_new_from_string(struct aws_allocator *allocator, const struct aws_string *str) { AWS_PRECONDITION(allocator && aws_string_is_valid(str)); - return aws_string_new_from_array(allocator, str->bytes, str->len); -} - + return aws_string_new_from_array(allocator, str->bytes, str->len); +} + struct aws_string *aws_string_new_from_cursor(struct aws_allocator *allocator, const struct aws_byte_cursor *cursor) { AWS_PRECONDITION(allocator && aws_byte_cursor_is_valid(cursor)); return aws_string_new_from_array(allocator, cursor->ptr, cursor->len); @@ -46,24 +46,24 @@ struct aws_string *aws_string_new_from_buf(struct aws_allocator *allocator, cons return aws_string_new_from_array(allocator, buf->buffer, buf->len); } -void aws_string_destroy(struct aws_string *str) { +void aws_string_destroy(struct aws_string *str) { AWS_PRECONDITION(!str || aws_string_is_valid(str)); - if (str && str->allocator) { - aws_mem_release(str->allocator, str); - } -} - -void aws_string_destroy_secure(struct aws_string *str) { + if (str && str->allocator) { + aws_mem_release(str->allocator, str); + } +} + +void aws_string_destroy_secure(struct aws_string *str) { AWS_PRECONDITION(!str || aws_string_is_valid(str)); - if (str) { - aws_secure_zero((void *)aws_string_bytes(str), str->len); - if (str->allocator) { - aws_mem_release(str->allocator, str); - } - } -} - -int aws_string_compare(const struct aws_string *a, const struct aws_string *b) { + if (str) { + aws_secure_zero((void *)aws_string_bytes(str), str->len); + if (str->allocator) { + aws_mem_release(str->allocator, str); + } + } +} + +int aws_string_compare(const struct aws_string *a, const struct aws_string *b) { AWS_PRECONDITION(!a || aws_string_is_valid(a)); AWS_PRECONDITION(!b || aws_string_is_valid(b)); if (a == b) { @@ -76,26 +76,26 @@ int aws_string_compare(const struct aws_string *a, const struct aws_string *b) { return 1; } - size_t len_a = a->len; - size_t len_b = b->len; - size_t min_len = len_a < len_b ? len_a : len_b; - - int ret = memcmp(aws_string_bytes(a), aws_string_bytes(b), min_len); + size_t len_a = a->len; + size_t len_b = b->len; + size_t min_len = len_a < len_b ? len_a : len_b; + + int ret = memcmp(aws_string_bytes(a), aws_string_bytes(b), min_len); AWS_POSTCONDITION(aws_string_is_valid(a)); AWS_POSTCONDITION(aws_string_is_valid(b)); - if (ret) { - return ret; /* overlapping characters differ */ - } - if (len_a == len_b) { - return 0; /* strings identical */ - } - if (len_a > len_b) { - return 1; /* string b is first n characters of string a */ - } - return -1; /* string a is first n characters of string b */ -} - -int aws_array_list_comparator_string(const void *a, const void *b) { + if (ret) { + return ret; /* overlapping characters differ */ + } + if (len_a == len_b) { + return 0; /* strings identical */ + } + if (len_a > len_b) { + return 1; /* string b is first n characters of string a */ + } + return -1; /* string a is first n characters of string b */ +} + +int aws_array_list_comparator_string(const void *a, const void *b) { if (a == b) { return 0; /* strings identical */ } @@ -105,10 +105,10 @@ int aws_array_list_comparator_string(const void *a, const void *b) { if (b == NULL) { return 1; } - const struct aws_string *str_a = *(const struct aws_string **)a; - const struct aws_string *str_b = *(const struct aws_string **)b; - return aws_string_compare(str_a, str_b); -} + const struct aws_string *str_a = *(const struct aws_string **)a; + const struct aws_string *str_b = *(const struct aws_string **)b; + return aws_string_compare(str_a, str_b); +} /** * Returns true if bytes of string are the same, false otherwise. |