Debian Codegraph @

crt/aws-c-common/include/aws/common/string.inl

raw
1 #ifndef AWS_COMMON_STRING_INL
2 #define AWS_COMMON_STRING_INL
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/common/string.h>
8 #include <ctype.h>
9
10 AWS_EXTERN_C_BEGIN
11 /**
12 * Equivalent to str->bytes.
13 */
14 AWS_STATIC_IMPL
15 const uint8_t *aws_string_bytes(const struct aws_string *str) {
16 AWS_PRECONDITION(aws_string_is_valid(str));
17 return str->bytes;
18 }
19
20 /**
21 * Equivalent to `(const char *)str->bytes`.
22 */
23 AWS_STATIC_IMPL
24 const char *aws_string_c_str(const struct aws_string *str) {
25 AWS_PRECONDITION(aws_string_is_valid(str));
26 return (const char *)str->bytes;
27 }
28
29 /**
30 * Evaluates the set of properties that define the shape of all valid aws_string structures.
31 * It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion).
32 */
33 AWS_STATIC_IMPL
34 bool aws_string_is_valid(const struct aws_string *str) {
35 return str && AWS_MEM_IS_READABLE(&str->bytes[0], str->len + 1) && str->bytes[str->len] == 0;
36 }
37
38 /**
39 * Best-effort checks aws_string invariants, when the str->len is unknown
40 */
41 AWS_STATIC_IMPL
42 bool aws_c_string_is_valid(const char *str) {
43 /* Knowing the actual length to check would require strlen(), which is
44 * a) linear time in the length of the string
45 * b) could already cause a memory violation for a non-zero-terminated string.
46 * But we know that a c-string must have at least one character, to store the null terminator
47 */
48 return str && AWS_MEM_IS_READABLE(str, 1);
49 }
50
51 /**
52 * Evaluates if a char is a white character.
53 */
54 AWS_STATIC_IMPL
55 bool aws_char_is_space(uint8_t c) {
56 return aws_isspace(c);
57 }
58
59 AWS_EXTERN_C_END
60 #endif /* AWS_COMMON_STRING_INL */