/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/log/absl_check.h" |
26 | | #include "absl/strings/string_view.h" |
27 | | #include "absl/types/span.h" |
28 | | #include "s2/util/coding/coder.h" |
29 | | #include "s2/encoded_uint_vector.h" |
30 | | |
31 | | using absl::MakeSpan; |
32 | | using absl::Span; |
33 | | using absl::string_view; |
34 | | using std::string; |
35 | | using std::vector; |
36 | | |
37 | | namespace s2coding { |
38 | | |
39 | 0 | StringVectorEncoder::StringVectorEncoder() = default; |
40 | | |
41 | 0 | void StringVectorEncoder::Encode(Encoder* encoder) { |
42 | 0 | offsets_.push_back(data_.length()); |
43 | | // We don't encode the first element of "offsets_", which is always zero. |
44 | 0 | EncodeUintVector<uint64_t>( |
45 | 0 | MakeSpan(offsets_.data() + 1, offsets_.data() + offsets_.size()), |
46 | 0 | encoder); |
47 | 0 | encoder->Ensure(data_.length()); |
48 | 0 | encoder->putn(data_.base(), data_.length()); |
49 | 0 | } |
50 | | |
51 | 0 | void StringVectorEncoder::Encode(Span<const string> v, Encoder* encoder) { |
52 | 0 | StringVectorEncoder string_vector; |
53 | 0 | for (const auto& str : v) string_vector.Add(str); |
54 | 0 | string_vector.Encode(encoder); |
55 | 0 | } |
56 | | |
57 | 0 | bool EncodedStringVector::Init(Decoder* decoder) { |
58 | 0 | if (!offsets_.Init(decoder)) return false; |
59 | 0 | if (offsets_.size() > 0) { |
60 | 0 | uint64_t length = offsets_[offsets_.size() - 1]; |
61 | 0 | if (decoder->avail() < length) return false; |
62 | 0 | data_ = absl::string_view(decoder->skip(0), length); |
63 | 0 | decoder->skip(length); |
64 | 0 | } else { |
65 | 0 | data_ = absl::string_view(); |
66 | 0 | } |
67 | 0 | return true; |
68 | 0 | } |
69 | | |
70 | 0 | vector<string_view> EncodedStringVector::Decode() const { |
71 | 0 | size_t n = size(); |
72 | 0 | vector<string_view> result(n); |
73 | 0 | uint64_t start = 0; |
74 | 0 | for (size_t i = 0; i < n; ++i) { |
75 | 0 | uint64_t limit = offsets_[i]; |
76 | | // Check bounds, skip invalid values, but continue decoding to provide |
77 | | // consistency between Decode() and operator[]. |
78 | 0 | if (start <= limit && limit <= data_.size()) { |
79 | 0 | result[i] = data_.substr(start, limit - start); |
80 | 0 | } |
81 | 0 | start = limit; |
82 | 0 | } |
83 | 0 | return result; |
84 | 0 | } |
85 | | |
86 | | // The encoding must be identical to StringVectorEncoder::Encode(). |
87 | 0 | void EncodedStringVector::Encode(Encoder* encoder) const { |
88 | 0 | offsets_.Encode(encoder); |
89 | |
|
90 | 0 | if (offsets_.size() > 0) { |
91 | 0 | ABSL_DCHECK_EQ(data_.size(), offsets_[offsets_.size() - 1]); |
92 | 0 | encoder->Ensure(data_.size()); |
93 | 0 | encoder->putn(data_.data(), data_.size()); |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | } // namespace s2coding |