David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "hf/string.h" |
| 10 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 11 | #include "hf/std.h" |
| 12 | |
| 13 | void string_init_empty(struct string *str) |
| 14 | { |
| 15 | static_assert(sizeof(str->data) >= 1, "String buffer too small"); |
| 16 | str->data[0] = '\0'; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Caller must guarantee that `data` points to a NULL-terminated string. |
| 21 | * The constructor checks that it fits into the internal buffer and copies |
| 22 | * the string there. |
| 23 | */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 24 | enum string_return_code string_init(struct string *str, |
| 25 | const struct memiter *data) |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 26 | { |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 27 | const char *base = memiter_base(data); |
| 28 | size_t size = memiter_size(data); |
| 29 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 30 | /* |
| 31 | * Require that the value contains exactly one NULL character and that |
| 32 | * it is the last byte. |
| 33 | */ |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 34 | if (size < 1 || memchr(base, '\0', size) != &base[size - 1]) { |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 35 | return STRING_ERROR_INVALID_INPUT; |
| 36 | } |
| 37 | |
| 38 | if (size > sizeof(str->data)) { |
| 39 | return STRING_ERROR_TOO_LONG; |
| 40 | } |
| 41 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 42 | memcpy_s(str->data, sizeof(str->data), base, size); |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 43 | return STRING_SUCCESS; |
| 44 | } |
| 45 | |
| 46 | bool string_is_empty(const struct string *str) |
| 47 | { |
| 48 | return str->data[0] == '\0'; |
| 49 | } |
| 50 | |
| 51 | const char *string_data(const struct string *str) |
| 52 | { |
| 53 | return str->data; |
| 54 | } |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Returns true if the iterator `data` contains string `str`. |
| 58 | * Only characters until the first null terminator are compared. |
| 59 | */ |
| 60 | bool string_eq(const struct string *str, const struct memiter *data) |
| 61 | { |
| 62 | const char *base = memiter_base(data); |
| 63 | size_t len = memiter_size(data); |
| 64 | |
| 65 | return (len <= sizeof(str->data)) && |
| 66 | (strncmp(str->data, base, len) == 0); |
| 67 | } |