Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 : : * Author: Thun Lu <thun.lu@zohomail.cn>
4 : : * Repo: https://github.com/thun-res/vlink
5 : : * _ __ __ _ __
6 : : * | | / / / / (_) ____ / /__
7 : : * | | / / / / / / / __ \ / //_/
8 : : * | |/ / / /___ / / / / / / / ,<
9 : : * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 : : *
11 : : * Licensed under the Apache License, Version 2.0 (the "License");
12 : : * you may not use this file except in compliance with the License.
13 : : * You may obtain a copy of the License at
14 : : *
15 : : * http://www.apache.org/licenses/LICENSE-2.0
16 : : *
17 : : * Unless required by applicable law or agreed to in writing, software
18 : : * distributed under the License is distributed on an "AS IS" BASIS,
19 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 : : * See the License for the specific language governing permissions and
21 : : * limitations under the License.
22 : : */
23 : :
24 : : #include "./impl/types.h"
25 : :
26 : : #include <charconv>
27 : : #include <sstream>
28 : : #include <string>
29 : :
30 : : namespace vlink {
31 : :
32 : : // SampleLostInfo
33 : 3 : std::ostream& operator<<(std::ostream& ostream, const SampleLostInfo& info) noexcept {
34 : : ostream << "SampleLostInfo:"
35 : 3 : << "[total]" << info.total << "[lost]" << info.lost;
36 : :
37 : 3 : return ostream;
38 : : }
39 : :
40 : : // SchemaData
41 : 10797 : bool SchemaData::is_valid_type(SchemaType schema_type) noexcept {
42 [ + - ]: 10797 : switch (schema_type) {
43 : 10797 : case SchemaType::kUnknown:
44 : : case SchemaType::kProtobuf:
45 : : case SchemaType::kFlatbuffers:
46 : : case SchemaType::kRaw:
47 : : case SchemaType::kZeroCopy:
48 : : case SchemaType::kCdr:
49 : 10797 : return true;
50 : : default: // LCOV_EXCL_LINE GCOVR_EXCL_LINE
51 : : return false; // LCOV_EXCL_LINE GCOVR_EXCL_LINE
52 : : }
53 : : }
54 : :
55 : 127 : bool SchemaData::is_real_type(SchemaType schema_type) noexcept {
56 [ + + ]: 127 : switch (schema_type) {
57 : 95 : case SchemaType::kProtobuf:
58 : : case SchemaType::kFlatbuffers:
59 : : case SchemaType::kZeroCopy:
60 : 95 : return true;
61 : 32 : default:
62 : 32 : return false;
63 : : }
64 : : }
65 : :
66 : 7715 : std::string_view SchemaData::convert_type(SchemaType schema_type) noexcept {
67 [ + + + - : 7715 : switch (schema_type) {
- + ]
68 : 465 : case SchemaType::kProtobuf:
69 : 465 : return "protobuf";
70 : 4 : case SchemaType::kFlatbuffers:
71 : 4 : return "flatbuffers";
72 : 7117 : case SchemaType::kRaw:
73 : 7117 : return "raw";
74 : 0 : case SchemaType::kZeroCopy:
75 : 0 : return "zerocopy";
76 : 0 : case SchemaType::kCdr:
77 : 0 : return "cdr";
78 : 129 : default:
79 : 129 : return "";
80 : : }
81 : : }
82 : :
83 : 3842 : SchemaType SchemaData::convert_encoding(std::string_view encoding) noexcept {
84 : 4038 : constexpr auto kIsAsciiSpace = [](char ch) noexcept {
85 [ + - + - : 4038 : return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\f' || ch == '\v';
+ - + - +
- - + ]
86 : : };
87 : :
88 : 17062 : constexpr auto kAsciiLower = [](char ch) noexcept {
89 [ + - - + ]: 17062 : return ch >= 'A' && ch <= 'Z' ? static_cast<char>(ch - 'A' + 'a') : ch;
90 : : };
91 : :
92 : 44490 : const auto iequals = [kAsciiLower](std::string_view lhs, std::string_view rhs) noexcept {
93 [ + + ]: 44490 : if (lhs.size() != rhs.size()) {
94 : 40568 : return false;
95 : : }
96 : :
97 [ + + ]: 10544 : for (size_t i = 0; i < lhs.size(); ++i) {
98 [ + + ]: 8531 : if (kAsciiLower(lhs[i]) != kAsciiLower(rhs[i])) {
99 : 1909 : return false;
100 : : }
101 : : }
102 : :
103 : 2013 : return true;
104 : : };
105 : :
106 [ + + - + : 3842 : while (!encoding.empty() && kIsAsciiSpace(encoding.front())) {
- + ]
107 : 0 : encoding.remove_prefix(1);
108 : : }
109 : :
110 [ + + - + : 3842 : while (!encoding.empty() && kIsAsciiSpace(encoding.back())) {
- + ]
111 : 0 : encoding.remove_suffix(1);
112 : : }
113 : :
114 [ + + - + : 3842 : if (iequals(encoding, "protobuf") || iequals(encoding, "proto")) {
+ + ]
115 : 116 : return SchemaType::kProtobuf;
116 : : }
117 : :
118 [ + - + - : 7452 : if (iequals(encoding, "flatbuffers") || iequals(encoding, "flatbuffer") || iequals(encoding, "fbs") ||
+ - + + ]
119 [ + + ]: 7452 : iequals(encoding, "bfbs")) {
120 : 1 : return SchemaType::kFlatbuffers;
121 : : }
122 : :
123 [ + + + - : 3725 : if (iequals(encoding, "raw") || iequals(encoding, "json") || iequals(encoding, "text") || iequals(encoding, "blob")) {
+ - + + +
+ ]
124 : 1896 : return SchemaType::kRaw;
125 : : }
126 : :
127 [ + - + - : 3658 : if (iequals(encoding, "string") || iequals(encoding, "std::string") || iequals(encoding, "application/json") ||
+ - - + ]
128 [ - + ]: 3658 : iequals(encoding, "text/json")) {
129 : 0 : return SchemaType::kRaw;
130 : : }
131 : :
132 [ + - - + : 1829 : if (iequals(encoding, "zerocopy") || iequals(encoding, "vlink_msg")) {
- + ]
133 : 0 : return SchemaType::kZeroCopy;
134 : : }
135 : :
136 [ - + ]: 1829 : if (iequals(encoding, "cdr")) {
137 : 0 : return SchemaType::kCdr;
138 : : }
139 : :
140 : 1829 : return SchemaType::kUnknown;
141 : : }
142 : :
143 : 10367 : SchemaType SchemaData::resolve_type(SchemaType schema_type, std::string_view ser_type,
144 : : std::string_view encoding) noexcept {
145 [ + - ]: 10367 : const auto normalized_schema_type = is_valid_type(schema_type) ? schema_type : SchemaType::kUnknown;
146 : :
147 [ + + ]: 10367 : if (normalized_schema_type != SchemaType::kUnknown) {
148 : 8530 : return normalized_schema_type;
149 : : }
150 : :
151 : 1837 : const auto inferred_encoding_type = convert_encoding(encoding);
152 : :
153 [ + + ]: 1837 : if (inferred_encoding_type != SchemaType::kUnknown) {
154 : 52 : return inferred_encoding_type;
155 : : }
156 : :
157 : 1785 : return infer_ser_type(ser_type);
158 : : }
159 : :
160 : : // Version
161 : 1156 : bool Version::operator==(const Version& target) const noexcept {
162 [ + + + - : 1156 : return major == target.major && minor == target.minor && patch == target.patch;
+ + ]
163 : : }
164 : :
165 : 1154 : bool Version::operator!=(const Version& target) const noexcept { return !(*this == target); }
166 : :
167 : 15 : bool Version::operator<(const Version& target) const noexcept {
168 [ + + ]: 15 : if (major != target.major) {
169 : 6 : return major < target.major;
170 : : }
171 : :
172 [ + + ]: 9 : if (minor != target.minor) {
173 : 3 : return minor < target.minor;
174 : : }
175 : :
176 : 6 : return patch < target.patch;
177 : : }
178 : :
179 : 3 : bool Version::operator>(const Version& target) const noexcept { return target < *this; }
180 : :
181 : 828 : Version Version::from_string(const std::string& version_str) noexcept {
182 : 828 : Version version;
183 : :
184 [ + + ]: 828 : thread_local std::stringstream ss;
185 : 828 : ss.clear();
186 : 828 : ss.str(version_str);
187 : :
188 : 828 : std::string token;
189 : :
190 [ + + ]: 828 : if (std::getline(ss, token, '.')) {
191 : 827 : const auto* end = token.data() + token.size();
192 : 827 : auto result = std::from_chars(token.data(), end, version.major);
193 : :
194 [ + + ]: 827 : if VUNLIKELY (result.ptr != end) {
195 : 1 : version.major = -1;
196 : : }
197 : : }
198 : :
199 [ + + ]: 828 : if (std::getline(ss, token, '.')) {
200 : 825 : const auto* end = token.data() + token.size();
201 : 825 : auto result = std::from_chars(token.data(), end, version.minor);
202 : :
203 [ - + ]: 825 : if VUNLIKELY (result.ptr != end) {
204 : 0 : version.minor = -1;
205 : : }
206 : : }
207 : :
208 [ + + ]: 828 : if (std::getline(ss, token, '.')) {
209 : 824 : const auto* end = token.data() + token.size();
210 : 824 : auto result = std::from_chars(token.data(), end, version.patch);
211 : :
212 [ - + ]: 824 : if VUNLIKELY (result.ptr != end) {
213 : 0 : version.patch = -1;
214 : : }
215 : : }
216 : :
217 : 1656 : return version;
218 : 828 : }
219 : :
220 : 44 : std::string Version::to_string() const noexcept {
221 : 44 : return std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch);
222 : : }
223 : :
224 [ + + + + : 812 : bool Version::is_valid() const noexcept { return major >= 0 && minor >= 0 && patch >= 0; }
+ + ]
225 : :
226 : : } // namespace vlink
|