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 | #pragma once |
| 10 | |
| 11 | #include <stdbool.h> |
| 12 | #include <stddef.h> |
| 13 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 14 | #include "hf/memiter.h" |
| 15 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 16 | /** |
| 17 | * Maximum length of a string including the NULL terminator. |
| 18 | * This is an arbitrary number and can be adjusted to fit use cases. |
| 19 | */ |
| 20 | #define STRING_MAX_SIZE 32 |
| 21 | |
| 22 | enum string_return_code { |
| 23 | STRING_SUCCESS, |
| 24 | STRING_ERROR_INVALID_INPUT, |
| 25 | STRING_ERROR_TOO_LONG, |
| 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * Statically-allocated string data structure with input validation to ensure |
| 30 | * strings are properly NULL-terminated. |
| 31 | * |
| 32 | * This is intentionally kept as simple as possible and should not be extended |
| 33 | * to perform complex string operations without a good use case. |
| 34 | */ |
| 35 | struct string { |
| 36 | char data[STRING_MAX_SIZE]; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * Macro to initialize `struct string` from a string constant. |
| 41 | * Triggers a compilation error if the string does not fit into the buffer. |
| 42 | */ |
| 43 | #define STRING_INIT(str) ((struct string){.data = str}) |
| 44 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 45 | enum string_return_code string_init(struct string *str, |
| 46 | const struct memiter *data); |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 47 | void string_init_empty(struct string *str); |
| 48 | bool string_is_empty(const struct string *str); |
| 49 | const char *string_data(const struct string *str); |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 50 | bool string_eq(const struct string *str, const struct memiter *data); |