cpp-common/bt2c: add `bt2c::CStringView`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 5 Dec 2023 07:00:28 +0000 (02:00 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
commit339e8bc45d45a998c7d530ebe8d2bf0d579aa60a
tree04f3d71bc9ea73ca63979b9d554cdd83fdeb9900
parent09a4c5192efc12632c83df65f93f274399618838
cpp-common/bt2c: add `bt2c::CStringView`

A `bt2c::CStringView` is like a `bt2s::string_view`, but specialized for
strings that are known to be null-terminated (C strings).

This is useful when wrapping an existing API which has functions
returning `const char *`: return `bt2c::CStringView` from the wrappers
at no cost, but with improved usability:

    if (myObj.name().len() >= 16) {
        // ...
    }

vs.

    if (std::strlen(myObj.name()) >= 16) {
        // ...
    }

A `bt2c::CStringView` only computes the length of the underlying
C string with std::strlen() when needed. It can convert to `std::string`
and `bt2s::string_view`.

`c-string-view.hpp` includes an {fmt} formatter for convenience. It
writes `(null)` when the underlying pointer is null, just like the glibc
printf() does.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I522ce586e9966736dadeeabf30f02da5dc62c80c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11485
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
src/Makefile.am
src/cpp-common/bt2c/c-string-view.hpp [new file with mode: 0644]
This page took 0.040321 seconds and 4 git commands to generate.