blob: 6ab6323d5f4ce370f17fcf63392b7be581c383f7 [file] [log] [blame]
David Brazdil136f2942019-09-23 14:11:03 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdbool.h>
20#include <stddef.h>
21
David Brazdilb856be62020-03-25 10:14:55 +000022#include "hf/memiter.h"
23
David Brazdil136f2942019-09-23 14:11:03 +010024/**
25 * Maximum length of a string including the NULL terminator.
26 * This is an arbitrary number and can be adjusted to fit use cases.
27 */
28#define STRING_MAX_SIZE 32
29
30enum string_return_code {
31 STRING_SUCCESS,
32 STRING_ERROR_INVALID_INPUT,
33 STRING_ERROR_TOO_LONG,
34};
35
36/**
37 * Statically-allocated string data structure with input validation to ensure
38 * strings are properly NULL-terminated.
39 *
40 * This is intentionally kept as simple as possible and should not be extended
41 * to perform complex string operations without a good use case.
42 */
43struct string {
44 char data[STRING_MAX_SIZE];
45};
46
47/**
48 * Macro to initialize `struct string` from a string constant.
49 * Triggers a compilation error if the string does not fit into the buffer.
50 */
51#define STRING_INIT(str) ((struct string){.data = str})
52
David Brazdilb856be62020-03-25 10:14:55 +000053enum string_return_code string_init(struct string *str,
54 const struct memiter *data);
David Brazdil136f2942019-09-23 14:11:03 +010055void string_init_empty(struct string *str);
56bool string_is_empty(const struct string *str);
57const char *string_data(const struct string *str);
David Brazdilb856be62020-03-25 10:14:55 +000058bool string_eq(const struct string *str, const struct memiter *data);