File: string_slice.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,225 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_STRINGS_STRING_SLICE_H_
#define BASE_STRINGS_STRING_SLICE_H_

#include <stddef.h>
#include <stdint.h>

#include <bit>
#include <string_view>

namespace base::internal {

// Determines the minimum unsigned integer type needed to hold `kSize`.
template <size_t kSize>
struct IndexTypeForSize {
 private:
  static constexpr auto Helper() {
    constexpr int kMinBits = std::bit_width(kSize);
    if constexpr (kMinBits <= 8) {
      return uint8_t();
    } else if constexpr (kMinBits <= 16) {
      return uint16_t();
    } else if constexpr (kMinBits <= 32) {
      return uint32_t();
    } else {
      return size_t();
    }
  }

 public:
  using Type = decltype(Helper());
};

}  // namespace base::internal

namespace base::subtle {

// This is intended for use in tables generated by scripts and should not be
// directly used. Consider a global constant like this:
//
// constexpr std::string_view kNames[] = {
//   "Alice",
//   "Bob",
//   "Eve",
// };
//
// "Alice", "Bob", and "Eve" are also constants, but they are not stored inline
// in `kNames`; `kNames[0]` points to "Alice", `kNames[1]` points to "Bob", and
// `kNames[2]` points to "Eve". However, images can be loaded at arbitrary base
// addresses, so the actual pointer values are unknown at build time.
//
// To solve this, the tooling stores `kNames` in `.data.rel.ro` and records 3
// relocations in `.rela.dyn`. When the image is loaded, the linker applies the
// relocations to fix up the addresses before marking the section read-only.
//
// Unfortunately, this has both a binary size cost (relocation entries are
// relatively large unless RELR is in use) and a runtime cost (to apply the
// relocations).
//
// StringSlice avoids relocations by only storing an offset and a length and
// dynamically resolving to a std::string_view at runtime. Using `StringSlice`,
// the above example might look like this instead:
//
// constexpr char kData[] = "AliceBobEve";
// constexpr StringSlice<sizeof(kData), kData> kNames[] = {
//   {0, 5},
//   {5, 3},
//   {8, 3},
// };
//
// While this has a small runtime cost (typically a PC-relative load), modern
// CPUs are quite good at this sort of math.
template <size_t N, const char (&kData)[N]>
struct StringSlice {
  using IndexType = typename internal::IndexTypeForSize<N>::Type;

  IndexType offset;
  IndexType length;

  friend constexpr bool operator==(StringSlice lhs, StringSlice rhs) {
    return std::string_view(lhs) == std::string_view(rhs);
  }
  friend constexpr auto operator<=>(StringSlice lhs, StringSlice rhs) {
    return std::string_view(lhs) <=> std::string_view(rhs);
  }
  constexpr operator std::string_view() const {
    // Note 1: using as_string_view() or span() can cause issues with constexpr
    // evaluation limits.
    // Note 2: Subtract 1 from kData since this is intended for use with string
    // literals, and the terminating nul should not be included.
    return std::string_view(kData, sizeof(kData) - 1).substr(offset, length);
  }
};

}  // namespace base::subtle

#endif  // BASE_STRINGS_STRING_SLICE_H_