This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Split out varint code. * Add tests.
- Loading branch information
Showing
9 changed files
with
219 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2019, OpenCensus Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
#include "absl/strings/string_view.h" | ||
#include "opencensus/common/internal/varint.h" | ||
|
||
namespace opencensus { | ||
namespace common { | ||
|
||
void AppendVarint32(uint32_t i, std::string* out) { | ||
do { | ||
// Encode 7 bits. | ||
uint8_t c = i & 0x7F; | ||
i = i >> 7; | ||
if (i != 0) { | ||
c |= 0x80; | ||
} | ||
out->push_back(c); | ||
} while (i != 0); | ||
} | ||
|
||
bool ParseVarint32(absl::string_view* input, uint32_t* out) { | ||
absl::string_view s = *input; | ||
uint32_t i = 0; | ||
uint8_t c; | ||
int shift = 0; | ||
do { | ||
if (s.empty()) { | ||
return false; // Too short. | ||
} | ||
c = s[0]; | ||
s = s.substr(1); | ||
if (shift == 28 && c > 0x0f) { | ||
return false; // Out of range for uint32_t. | ||
} | ||
i |= (c & 0x7F) << shift; | ||
shift += 7; | ||
} while (c & 0x80); | ||
*input = s; | ||
*out = i; | ||
return true; | ||
} | ||
|
||
} // namespace common | ||
} // namespace opencensus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2019, OpenCensus Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef OPENCENSUS_COMMON_INTERNAL_VARINT_H_ | ||
#define OPENCENSUS_COMMON_INTERNAL_VARINT_H_ | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
#include "absl/strings/string_view.h" | ||
|
||
namespace opencensus { | ||
namespace common { | ||
|
||
// Appends a variable-length encoded integer to the destination string. | ||
void AppendVarint32(uint32_t i, std::string* out); | ||
|
||
// Parses a variable-length encoded integer from the input. Returns false on | ||
// failure. Returns true and consumes the bytes from the input, on success. | ||
bool ParseVarint32(absl::string_view* input, uint32_t* out); | ||
|
||
} // namespace common | ||
} // namespace opencensus | ||
|
||
#endif // OPENCENSUS_COMMON_INTERNAL_VARINT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2019, OpenCensus Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "opencensus/common/internal/varint.h" | ||
|
||
#include <iostream> | ||
|
||
#include "absl/strings/escaping.h" | ||
#include "absl/strings/string_view.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace opencensus { | ||
namespace common { | ||
namespace { | ||
|
||
TEST(Varint, EncodeDecode) { | ||
auto test = [](uint32_t i) { | ||
std::string s; | ||
AppendVarint32(i, &s); | ||
std::cout << " int " << i << " encoded to hex " << absl::BytesToHexString(s) | ||
<< "\n"; | ||
absl::string_view sv(s); | ||
uint32_t j = i + 1; | ||
EXPECT_TRUE(ParseVarint32(&sv, &j)); | ||
EXPECT_EQ(i, j); | ||
}; | ||
test(0); | ||
test(1); | ||
test(10); | ||
test(100); | ||
|
||
test(127); | ||
test(128); | ||
test(129); | ||
|
||
test(255); | ||
test(256); | ||
test(257); | ||
|
||
test(16383); | ||
test(16384); | ||
test(16385); | ||
|
||
test(2097151); | ||
test(2097152); | ||
test(2097153); | ||
|
||
test(268435455); | ||
test(268435456); | ||
test(268435457); | ||
|
||
test(4294967295); | ||
} | ||
|
||
TEST(Varint, DecodeOutOfRange) { | ||
constexpr uint8_t input[] = {0xff, 0xff, 0xff, 0xff, 0x10}; | ||
absl::string_view sv(reinterpret_cast<const char*>(input), sizeof(input)); | ||
uint32_t i; | ||
EXPECT_FALSE(ParseVarint32(&sv, &i)); | ||
} | ||
|
||
} // namespace | ||
} // namespace common | ||
} // namespace opencensus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ opencensus_lib( | |
internal/grpc_tags_bin.cc | ||
DEPS | ||
tags | ||
common_varint | ||
absl::strings) | ||
|
||
opencensus_lib( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters