Skip to content

Commit fab9523

Browse files
committed
Initial nanopb_string
1 parent dec5712 commit fab9523

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_NANOPB_NANOPB_STRING_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_NANOPB_NANOPB_STRING_H_
19+
20+
#include <pb.h>
21+
22+
#include <cstdlib>
23+
#include <functional>
24+
#include <utility>
25+
26+
#include "Firestore/core/src/firebase/firestore/util/comparison.h"
27+
#include "absl/strings/string_view.h"
28+
29+
namespace firebase {
30+
namespace firestore {
31+
namespace nanopb {
32+
33+
class String : public util::Comparable<String> {
34+
public:
35+
static pb_bytes_array_t* MakeBytesArray(absl::string_view value) {
36+
auto size = static_cast<pb_size_t>(value.size());
37+
38+
// Allocate one extra byte for the null terminator that's not necessarily
39+
// there in a string_view. As long as we're making a copy, might as well
40+
// make a copy that can be used as a regular C string too.
41+
auto result = reinterpret_cast<pb_bytes_array_t*>(
42+
malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(size) + 1));
43+
result->size = size;
44+
memcpy(result->bytes, value.data(), size);
45+
result->bytes[size] = '\0';
46+
47+
return result;
48+
}
49+
50+
String() {
51+
}
52+
53+
explicit String(const char* value) : bytes_{MakeBytesArray(value)} {
54+
}
55+
56+
explicit String(const std::string& value) : bytes_{MakeBytesArray(value)} {
57+
}
58+
59+
explicit String(absl::string_view value) : bytes_{MakeBytesArray(value)} {
60+
}
61+
62+
String(const String& other)
63+
: bytes_{MakeBytesArray(absl::string_view{other})} {
64+
65+
}
66+
67+
String(String&& other) noexcept
68+
: String{} {
69+
swap(*this, other);
70+
}
71+
72+
~String() {
73+
delete bytes_;
74+
}
75+
76+
String& operator=(String other) {
77+
using std::swap;
78+
swap(*this, other);
79+
return *this;
80+
}
81+
82+
size_t Hash() const {
83+
std::hash<absl::string_view>{}.operator()(absl::string_view{*this});
84+
}
85+
86+
bool empty() const {
87+
return !bytes_ || bytes_->size == 0;
88+
}
89+
90+
explicit operator absl::string_view() const {
91+
const char* str = reinterpret_cast<const char*>(bytes_->bytes);
92+
return absl::string_view{str, bytes_->size};
93+
}
94+
95+
friend void swap(String& lhs, String& rhs) noexcept {
96+
using std::swap;
97+
swap(lhs.bytes_, rhs.bytes_);
98+
}
99+
100+
friend bool operator==(const String& lhs, const String& rhs) {
101+
return absl::string_view{lhs} == absl::string_view{rhs};
102+
}
103+
friend bool operator<(const String& lhs, const String& rhs) {
104+
return absl::string_view{lhs} < absl::string_view{rhs};
105+
}
106+
107+
friend bool operator==(const String& lhs, absl::string_view rhs) {
108+
return absl::string_view{lhs} == rhs;
109+
}
110+
friend bool operator!=(const String& lhs, absl::string_view rhs) {
111+
return !(lhs == rhs);
112+
}
113+
114+
private:
115+
pb_bytes_array_t* bytes_ = nullptr;
116+
117+
}; // namespace nanopb
118+
119+
} // namespace nanopb
120+
} // namespace firestore
121+
} // namespace firebase
122+
123+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_NANOPB_NANOPB_STRING_H_

0 commit comments

Comments
 (0)