LCOV - code coverage report
Current view: top level - datadog - string_view.h (source / functions) Hit Total Coverage
Test: filtered.info Lines: 8 8 100.0 %
Date: 2024-01-03 20:30:12 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : // One of the clients of this library is Envoy, a service (HTTP) proxy.
       4             : //
       5             : // Envoy uses Abseil as its base C++ library, and additionally builds in C++17
       6             : // mode.  Abseil has a build option to forward its `std::string_view` and
       7             : // `std::optional` equivalents to the actual standard types when C++17 is
       8             : // available.
       9             : //
      10             : // Envoy does not use this Abseil build option, due to incomplete support for
      11             : // the C++17 standard library on iOS 11.
      12             : //
      13             : // As a result, Envoy forbids use of `std::string_view` and `std::optional`,
      14             : // instead preferring Abseil's `absl::string_view` and `absl::optional`.
      15             : //
      16             : // This presents a problem for this library, since we use `std::string_view`
      17             : // and `std::optional` in the exported interface, i.e. in header files.
      18             : //
      19             : // As a workaround, Bazel (the build tool used by Envoy) builds of this library
      20             : // will define the `DD_USE_ABSEIL_FOR_ENVOY` preprocessor macro.  When this
      21             : // macro is defined, the library-specific `StringView` and `Optional` aliases
      22             : // will refer to the Abseil types.  When the macro is not defined, the
      23             : // library-specific aliases will refer to the standard types.
      24             : //
      25             : // This file defines `datadog::tracing::StringView`, a type that is an alias
      26             : // for either `std::string_view` or `absl::string_view`.
      27             : 
      28             : #include <string>
      29             : 
      30             : #ifdef DD_USE_ABSEIL_FOR_ENVOY
      31             : // Abseil examples, including usage in Envoy, include Abseil headers in quoted
      32             : // style instead of angle bracket style, per Bazel's default build behavior.
      33             : #include "absl/strings/string_view.h"
      34             : #else
      35             : #include <string_view>
      36             : #endif  // defined DD_USE_ABSEIL_FOR_ENVOY
      37             : 
      38             : namespace datadog {
      39             : namespace tracing {
      40             : 
      41             : #ifdef DD_USE_ABSEIL_FOR_ENVOY
      42             : using StringView = absl::string_view;
      43             : #else
      44             : using StringView = std::string_view;
      45             : #endif  // defined DD_USE_ABSEIL_FOR_ENVOY
      46             : 
      47             : // When `StringView` is not the same as `std::string_view`,
      48             : // `operator+=(string&, StringView)` isn't defined.  To work around this, use
      49             : // `append` everywhere.
      50       62672 : inline void append(std::string& destination, StringView text) {
      51       62672 :   destination.append(text.data(), text.size());
      52       62672 : }
      53             : 
      54             : // When `StringView` is not the same as `std::string_view`,
      55             : // `operator=(string&, StringView)` isn't defined.  To work around this, use
      56             : // `assign` everywhere.
      57          80 : inline void assign(std::string& destination, StringView text) {
      58          80 :   destination.assign(text.data(), text.size());
      59          80 : }
      60             : 
      61          11 : inline bool contains(StringView text, StringView pattern) {
      62          11 :   return text.find(pattern) != text.npos;
      63             : }
      64             : 
      65             : }  // namespace tracing
      66             : }  // namespace datadog

Generated by: LCOV version 1.16