drgn/libdrgn/nstring.h
Omar Sandoval d1745755f1 Fix some include-what-you-use warnings
Also:

* Rename struct string to struct nstring and move it to its own header.
* Fix scripts/iwyu.py, which was broken by commit 5541fad063 ("Fix
  some flake8 errors").
* Add workarounds for a few outstanding include-what-you-use issues.

There is still a false positive for
include-what-you-use/include-what-you-use#970, but hopefully that is
fixed soon.

Signed-off-by: Omar Sandoval <osandov@osandov.com>
2021-11-10 15:09:29 -08:00

38 lines
798 B
C

// Copyright (c) Facebook, Inc. and its affiliates.
// SPDX-License-Identifier: GPL-3.0-or-later
/**
* @file
*
* String with length.
*/
#ifndef DRGN_NSTRING_H
#define DRGN_NSTRING_H
#include <string.h>
/** A string with a stored length. */
struct nstring {
/**
* The string, which is not necessarily null-terminated and may have
* embedded null bytes.
*/
const char *str;
/** The length in bytes of the string. */
size_t len;
};
/** Compare two @ref nstring keys for equality. */
static inline bool nstring_eq(const struct nstring *a, const struct nstring *b)
{
/*
* len == 0 is a special case because memcmp(NULL, NULL, 0) is
* technically undefined.
*/
return (a->len == b->len &&
(a->len == 0 || memcmp(a->str, b->str, a->len) == 0));
}
#endif /* DRGN_NSTRING_H */