• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

arangodb / velocypack / 3998645281

pending completion
3998645281

Pull #148

github

GitHub
Merge b1e3c924b into 5a28b6413
Pull Request #148: use separate namespace for xxh functions

0 of 5107 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/HashedStringRef.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
/// DISCLAIMER
3
///
4
/// Copyright 2014-2020 ArangoDB GmbH, Cologne, Germany
5
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
6
///
7
/// Licensed under the Apache License, Version 2.0 (the "License");
8
/// you may not use this file except in compliance with the License.
9
/// You may obtain a copy of the License at
10
///
11
///     http://www.apache.org/licenses/LICENSE-2.0
12
///
13
/// Unless required by applicable law or agreed to in writing, software
14
/// distributed under the License is distributed on an "AS IS" BASIS,
15
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
/// See the License for the specific language governing permissions and
17
/// limitations under the License.
18
///
19
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
20
///
21
/// @author Jan Steemann
22
////////////////////////////////////////////////////////////////////////////////
23

24
#include <cstring>
25
#include <iostream>
26
#include <limits>
27

28
#include "velocypack/Exception.h"
29
#include "velocypack/Slice.h"
30
#include "velocypack/HashedStringRef.h"
31
#include "velocypack/StringRef.h"
32

33
using namespace arangodb::velocypack;
34

35
namespace arangodb::velocypack {
36
extern void* memrchr(void const* block, int c, std::size_t size);
37
}  // namespace arangodb::velocypack
38

39
HashedStringRef::HashedStringRef(Slice slice) {
×
40
  VELOCYPACK_ASSERT(slice.isString());
×
41
  ValueLength l;
42
  _data = slice.getString(l);
×
43
  if (l > std::numeric_limits<uint32_t>::max()) {
×
44
    throw Exception(Exception::IndexOutOfBounds,
×
45
                    "string value too long for HashedStringRef");
×
46
  }
47
  _length = static_cast<uint32_t>(l);
×
48
  _hash = hash(_data, _length);
×
49
}
×
50

51
/// @brief create a HashedStringRef from a VPack slice of type String
52
HashedStringRef& HashedStringRef::operator=(Slice slice) {
×
53
  VELOCYPACK_ASSERT(slice.isString());
×
54
  ValueLength l;
55
  _data = slice.getString(l);
×
56
  if (l > std::numeric_limits<uint32_t>::max()) {
×
57
    throw Exception(Exception::IndexOutOfBounds,
×
58
                    "string value too long for HashedStringRef");
×
59
  }
60
  _length = static_cast<uint32_t>(l);
×
61
  _hash = hash(_data, _length);
×
62
  return *this;
×
63
}
64

65
HashedStringRef HashedStringRef::substr(std::size_t pos,
×
66
                                        std::size_t count) const {
67
  if (VELOCYPACK_UNLIKELY(pos > _length)) {
×
68
    throw Exception(Exception::IndexOutOfBounds, "substr index out of bounds");
×
69
  }
70
  if (count == std::string::npos || (count + pos >= _length)) {
×
71
    count = _length - pos;
×
72
  }
73
  return HashedStringRef(_data + pos, static_cast<uint32_t>(count));
×
74
}
75

76
char HashedStringRef::at(std::size_t index) const {
×
77
  if (index >= _length) {
×
78
    throw Exception(Exception::IndexOutOfBounds, "index out of bounds");
×
79
  }
80
  return operator[](index);
×
81
}
82

83
std::size_t HashedStringRef::find(char c, std::size_t offset) const noexcept {
×
84
  if (offset > _length) {
×
85
    offset = _length;
×
86
  }
87

88
  char const* p = static_cast<char const*>(
89
      memchr(static_cast<void const*>(_data + offset), c, _length - offset));
×
90

91
  if (p == nullptr) {
×
92
    return std::string::npos;
×
93
  }
94

95
  return (p - _data);
×
96
}
97

98
std::size_t HashedStringRef::rfind(char c, std::size_t offset) const noexcept {
×
99
  std::size_t length;
100
  if (offset >= _length + 1) {
×
101
    length = _length;
×
102
  } else {
103
    length = offset + 1;
×
104
  }
105

106
  char const* p = static_cast<char const*>(arangodb::velocypack::memrchr(
×
107
      static_cast<void const*>(_data), c, length));
×
108

109
  if (p == nullptr) {
×
110
    return std::string::npos;
×
111
  }
112

113
  return (p - _data);
×
114
}
115

116
int HashedStringRef::compare(HashedStringRef const& other) const noexcept {
×
117
  int res = std::memcmp(_data, other._data, (std::min)(_length, other._length));
×
118

119
  if (res != 0) {
×
120
    return res;
×
121
  }
122

123
  return static_cast<int>(_length) - static_cast<int>(other._length);
×
124
}
125

126
int HashedStringRef::compare(char const* data,
×
127
                             std::size_t length) const noexcept {
128
  int res = std::memcmp(_data, data,
×
129
                        (std::min)(static_cast<std::size_t>(_length), length));
×
130

131
  if (res != 0) {
×
132
    return res;
×
133
  }
134

135
  if (VELOCYPACK_UNLIKELY(length > std::numeric_limits<uint32_t>::max())) {
×
136
    return -1;
×
137
  }
138
  return static_cast<int>(_length) - static_cast<int>(length);
×
139
}
140

141
bool HashedStringRef::equals(HashedStringRef const& other) const noexcept {
×
142
  // if the tag is equal, then the size is equal too!
143
  return (tag() == other.tag() &&
×
144
          std::memcmp(data(), other.data(), size()) == 0);
×
145
}
146

147
bool HashedStringRef::equals(char const* data,
×
148
                             std::size_t length) const noexcept {
149
  // it does not matter that the std::string can be longer in size here,
150
  // as we are upcasting our own size to size_t and compare. only for
151
  // equal lengths we do the memory comparison
152
  return (static_cast<std::size_t>(size()) == length &&
×
153
          std::memcmp(_data, data, length) == 0);
×
154
}
155

156
std::string_view HashedStringRef::stringView() const noexcept {
×
157
  return std::string_view(data(), size());
×
158
}
159

160
#ifdef VELOCYPACK_64BIT
161
static_assert(sizeof(HashedStringRef) == 16,
162
              "unexpected size of HashedStringRef");
163
#endif
164

165
namespace arangodb::velocypack {
166

167
std::ostream& operator<<(std::ostream& stream, HashedStringRef const& ref) {
×
168
  stream.write(ref.data(), ref.length());
×
169
  return stream;
×
170
}
171

172
}  // namespace arangodb::velocypack
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc