forked from awslabs/aws-c-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
73 lines (66 loc) · 2.52 KB
/
string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <aws/common/string.h>
struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, const char *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) {
struct aws_string *hdr = aws_mem_acquire(allocator, sizeof(struct aws_string) + len + 1);
if (!hdr) {
return NULL;
}
*(struct aws_allocator **)(&hdr->allocator) = allocator;
*(size_t *)(&hdr->len) = len;
memcpy((void *)aws_string_bytes(hdr), bytes, len);
uint8_t *extra_byte = (uint8_t *)aws_string_bytes(hdr) + len;
*extra_byte = '\0';
return hdr;
}
void aws_string_destroy(void *str) {
struct aws_string *self = str;
if (self && self->allocator) {
aws_mem_release(self->allocator, self);
}
}
void aws_string_destroy_secure(void *str) {
struct aws_string *self = str;
if (self) {
aws_secure_zero((void *)aws_string_bytes(self), self->len);
if (self->allocator) {
aws_mem_release(self->allocator, self);
}
}
}
int aws_string_compare(const struct aws_string *a, const struct aws_string *b) {
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);
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) {
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);
}