string_ref_sso.h
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef STRING_REF_SSO_H
8 #define STRING_REF_SSO_H
9 
10 #include <iostream>
11 #include <string.h>
12 
13 namespace PJ
14 {
21 class StringRef
22 {
23 private:
24  static const uint64_t TYPE_BIT = uint64_t(1) << (sizeof(size_t) * 8 - 1);
25 
26  struct noSSO
27  {
28  const char* data;
29  size_t size;
30  };
31 
32  static const uint8_t SSO_SIZE = sizeof(noSSO) - 1;
33 
34  struct SSO
35  {
36  char data[sizeof(noSSO)];
37  };
38 
39  union
40  {
43  } _storage;
44 
45 public:
46  bool isSSO() const
47  {
48  return !(_storage.no_sso.size & TYPE_BIT);
49  }
50 
52  {
53  }
54 
55  StringRef(const std::string& str) : StringRef(str.data(), str.size())
56  {
57  }
58 
59  StringRef(const char* str) : StringRef(str, strlen(str))
60  {
61  }
62 
63  explicit StringRef(const char* data_ptr, size_t length)
64  {
65  _storage.no_sso.data = nullptr;
66  _storage.no_sso.size = 0;
67 
68  if (length <= SSO_SIZE)
69  {
70  memcpy(_storage.sso.data, data_ptr, length);
71  _storage.sso.data[SSO_SIZE] = SSO_SIZE - length;
72  }
73  else
74  {
75  _storage.no_sso.data = data_ptr;
76  _storage.no_sso.size = length;
77  _storage.no_sso.size |= TYPE_BIT;
78  }
79  }
80 
81  const char* data() const
82  {
83  return isSSO() ? _storage.sso.data : _storage.no_sso.data;
84  }
85 
86  size_t size() const
87  {
88  return isSSO() ? (SSO_SIZE - _storage.sso.data[SSO_SIZE]) :
89  _storage.no_sso.size & ~TYPE_BIT;
90  }
91 };
92 
93 } // namespace PJ
94 
95 #endif // STRING_REF_SSO_H
PJ::StringRef::SSO
Definition: string_ref_sso.h:34
PJ::StringRef::sso
SSO sso
Definition: string_ref_sso.h:42
nonstd::span_lite::size_t
span_CONFIG_SIZE_TYPE size_t
Definition: span.hpp:576
PJ::StringRef::SSO::data
char data[sizeof(noSSO)]
Definition: string_ref_sso.h:36
PJ::StringRef
Super simple, unmutable, string_view with small string optimization. If the string is 15 bytes or les...
Definition: string_ref_sso.h:21
PJ::StringRef::noSSO::size
size_t size
Definition: string_ref_sso.h:29
PJ::StringRef::StringRef
StringRef(const char *str)
Definition: string_ref_sso.h:59
PJ::StringRef::isSSO
bool isSSO() const
Definition: string_ref_sso.h:46
PJ::StringRef::noSSO
Definition: string_ref_sso.h:26
PJ::StringRef::noSSO::data
const char * data
Definition: string_ref_sso.h:28
PJ::StringRef::SSO_SIZE
static const uint8_t SSO_SIZE
Definition: string_ref_sso.h:32
PJ::StringRef::size
size_t size() const
Definition: string_ref_sso.h:86
PJ::StringRef::StringRef
StringRef()
Definition: string_ref_sso.h:51
PJ::StringRef::data
const char * data() const
Definition: string_ref_sso.h:81
PJ::StringRef::StringRef
StringRef(const char *data_ptr, size_t length)
Definition: string_ref_sso.h:63
PJ
Definition: dataloader_base.h:16
PJ::StringRef::StringRef
StringRef(const std::string &str)
Definition: string_ref_sso.h:55
PJ::StringRef::_storage
union PJ::StringRef::@56 _storage
PJ::StringRef::TYPE_BIT
static const uint64_t TYPE_BIT
Definition: string_ref_sso.h:24
nullptr
#define nullptr
Definition: backward.hpp:386
PJ::StringRef::no_sso
noSSO no_sso
Definition: string_ref_sso.h:41


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:26