Coverage Report

Created: 2025-11-08 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/s2geometry/src/s2/encoded_string_vector.cc
Line
Count
Source
1
// Copyright 2018 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
16
// Author: ericv@google.com (Eric Veach)
17
18
#include "s2/encoded_string_vector.h"
19
20
#include <cstddef>
21
#include <cstdint>
22
#include <string>
23
#include <vector>
24
25
#include "absl/strings/string_view.h"
26
#include "absl/types/span.h"
27
#include "s2/util/coding/coder.h"
28
#include "s2/encoded_uint_vector.h"
29
30
using absl::MakeSpan;
31
using absl::Span;
32
using absl::string_view;
33
using std::string;
34
using std::vector;
35
36
namespace s2coding {
37
38
0
StringVectorEncoder::StringVectorEncoder() = default;
39
40
0
void StringVectorEncoder::Encode(Encoder* encoder) {
41
0
  offsets_.push_back(data_.length());
42
  // We don't encode the first element of "offsets_", which is always zero.
43
0
  EncodeUintVector<uint64_t>(
44
0
      MakeSpan(offsets_.data() + 1, offsets_.data() + offsets_.size()),
45
0
      encoder);
46
0
  encoder->Ensure(data_.length());
47
0
  encoder->putn(data_.base(), data_.length());
48
0
}
49
50
0
void StringVectorEncoder::Encode(Span<const string> v, Encoder* encoder) {
51
0
  StringVectorEncoder string_vector;
52
0
  for (const auto& str : v) string_vector.Add(str);
53
0
  string_vector.Encode(encoder);
54
0
}
55
56
0
bool EncodedStringVector::Init(Decoder* decoder) {
57
0
  if (!offsets_.Init(decoder)) return false;
58
0
  data_ = decoder->skip(0);
59
0
  uint64_t length = 0;
60
0
  for (int i = 0, n = offsets_.size(); i < n; ++i) {
61
    // Strings are packed sequentially, offsets_[i] representing end of item i.
62
    // String length is diff of two sequential offsets, thus the offsets should
63
    // not decrease, otherwise we might read memory beyond the end of data.
64
0
    if (offsets_[i] < length) return false;
65
0
    length = offsets_[i];
66
0
  }
67
0
  if (decoder->avail() < length) return false;
68
0
  decoder->skip(length);
69
0
  return true;
70
0
}
71
72
0
vector<string_view> EncodedStringVector::Decode() const {
73
0
  size_t n = size();
74
0
  vector<string_view> result(n);
75
0
  for (size_t i = 0; i < n; ++i) {
76
0
    result[i] = (*this)[i];
77
0
  }
78
0
  return result;
79
0
}
80
81
// The encoding must be identical to StringVectorEncoder::Encode().
82
0
void EncodedStringVector::Encode(Encoder* encoder) const {
83
0
  offsets_.Encode(encoder);
84
85
0
  if (offsets_.size() > 0) {
86
0
    const uint64_t length = offsets_[offsets_.size() - 1];
87
0
    encoder->Ensure(length);
88
0
    encoder->putn(data_, length);
89
0
  }
90
0
}
91
92
}  // namespace s2coding