| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
/** |
| 5 |
* @file |
| 6 |
* |
| 7 |
* String with length. |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef DRGN_NSTRING_H |
| 11 |
#define DRGN_NSTRING_H |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
/** A string with a stored length. */ |
| 16 |
struct nstring { |
| 17 |
/** |
| 18 |
* The string, which is not necessarily null-terminated and may have |
| 19 |
* embedded null bytes. |
| 20 |
*/ |
| 21 |
const char *str; |
| 22 |
/** The length in bytes of the string. */ |
| 23 |
size_t len; |
| 24 |
}; |
| 25 |
|
| 26 |
/** Compare two @ref nstring keys for equality. */ |
| 27 |
static inline bool nstring_eq(const struct nstring *a, const struct nstring *b) |
| 28 |
{ |
| 29 |
/* |
| 30 |
* len == 0 is a special case because memcmp(NULL, NULL, 0) is |
| 31 |
* technically undefined. |
| 32 |
*/ |
| 33 |
return (a->len == b->len && |
| 34 |
(a->len == 0 || memcmp(a->str, b->str, a->len) == 0)); |
| 35 |
} |
| 36 |
|
| 37 |
#endif /* DRGN_NSTRING_H */ |