Internal refactor

PiperOrigin-RevId: 727057179
This commit is contained in:
Guogang Li
2025-02-14 14:07:18 -08:00
committed by Copybara-Service
parent 60d5fe440e
commit eea3441da9
12 changed files with 116 additions and 142 deletions
+3 -3
View File
@@ -82,13 +82,13 @@ cc_library(
cc_library(
name = "util",
srcs = [
"base_input_stream.cc",
"byte_utils.cc",
"stream_reader.cc",
"stream_writer.cc",
],
hdrs = [
"base_input_stream.h",
"byte_utils.h",
"stream_reader.h",
"stream_writer.h",
],
visibility = [
@@ -399,8 +399,8 @@ cc_test(
cc_test(
name = "platform_util_test",
srcs = [
"base_input_stream_test.cc",
"byte_utils_test.cc",
"stream_reader_test.cc",
"stream_writer_test.cc",
],
deps = [
+4 -5
View File
@@ -14,13 +14,12 @@
#include "internal/platform/byte_utils.h"
#include <cstdint>
#include <cstdlib>
#include <string>
#include "absl/strings/str_format.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/stream_reader.h"
namespace nearby {
@@ -28,9 +27,9 @@ std::string ByteUtils::ToFourDigitString(ByteArray& bytes) {
int multiplier = 1;
int hashCode = 0;
BaseInputStream base_input_stream{bytes};
while (base_input_stream.IsAvailable(1)) {
auto byte = base_input_stream.ReadInt8().value_or(0);
StreamReader stream_reader{bytes};
while (stream_reader.IsAvailable(1)) {
auto byte = stream_reader.ReadInt8().value_or(0);
hashCode = (hashCode + byte * multiplier) % kHashBasePrime;
multiplier = multiplier * kHashBaseMultiplier % kHashBasePrime;
}
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/base_input_stream.h"
#include "internal/platform/stream_reader.h"
#include <cstddef>
#include <cstdint>
@@ -23,22 +23,7 @@
namespace nearby {
ExceptionOr<ByteArray> BaseInputStream::Read(std::int64_t size) {
if (!IsAvailable(size)) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
ByteArray read_bytes{static_cast<size_t>(size)};
if (read_bytes.CopyAt(/*offset=*/0, buffer_,
/*source_offset=*/position_)) {
position_ += size;
return ExceptionOr<ByteArray>{read_bytes};
} else {
return ExceptionOr<ByteArray>{Exception::kIo};
}
}
std::optional<std::uint8_t> BaseInputStream::ReadBits(int bits) {
std::optional<std::uint8_t> StreamReader::ReadBits(int bits) {
if (bits > 8 || bits <= 0) {
return std::nullopt;
}
@@ -59,7 +44,7 @@ std::optional<std::uint8_t> BaseInputStream::ReadBits(int bits) {
return value;
}
std::optional<std::uint8_t> BaseInputStream::ReadUint8() {
std::optional<std::uint8_t> StreamReader::ReadUint8() {
constexpr int byte_size = sizeof(std::uint8_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -70,7 +55,7 @@ std::optional<std::uint8_t> BaseInputStream::ReadUint8() {
return static_cast<std::uint8_t>(data[0]);
}
std::optional<std::int8_t> BaseInputStream::ReadInt8() {
std::optional<std::int8_t> StreamReader::ReadInt8() {
constexpr int byte_size = sizeof(std::int8_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -81,7 +66,7 @@ std::optional<std::int8_t> BaseInputStream::ReadInt8() {
return static_cast<std::int8_t>(data[0]);
}
std::optional<std::uint16_t> BaseInputStream::ReadUint16() {
std::optional<std::uint16_t> StreamReader::ReadUint16() {
constexpr int byte_size = sizeof(std::uint16_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -94,7 +79,7 @@ std::optional<std::uint16_t> BaseInputStream::ReadUint16() {
return static_cast<uint16_t>(data[0] << 8 | data[1]);
}
std::optional<std::int16_t> BaseInputStream::ReadInt16() {
std::optional<std::int16_t> StreamReader::ReadInt16() {
constexpr int byte_size = sizeof(std::int16_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -107,7 +92,7 @@ std::optional<std::int16_t> BaseInputStream::ReadInt16() {
return static_cast<int16_t>(data[0] << 8 | data[1]);
}
std::optional<std::uint32_t> BaseInputStream::ReadUint32() {
std::optional<std::uint32_t> StreamReader::ReadUint32() {
constexpr int byte_size = sizeof(std::uint32_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -121,7 +106,7 @@ std::optional<std::uint32_t> BaseInputStream::ReadUint32() {
data[3]);
}
std::optional<std::int32_t> BaseInputStream::ReadInt32() {
std::optional<std::int32_t> StreamReader::ReadInt32() {
constexpr int byte_size = sizeof(std::uint32_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -135,7 +120,7 @@ std::optional<std::int32_t> BaseInputStream::ReadInt32() {
data[3]);
}
std::optional<std::uint64_t> BaseInputStream::ReadUint64() {
std::optional<std::uint64_t> StreamReader::ReadUint64() {
constexpr int byte_size = sizeof(std::uint64_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -154,7 +139,7 @@ std::optional<std::uint64_t> BaseInputStream::ReadUint64() {
static_cast<uint64_t>(data[6]) << 8 | static_cast<uint64_t>(data[7]);
}
std::optional<std::int64_t> BaseInputStream::ReadInt64() {
std::optional<std::int64_t> StreamReader::ReadInt64() {
constexpr int byte_size = sizeof(std::int64_t);
std::optional<ByteArray> read_bytes = ReadBytes(byte_size);
if (!read_bytes.has_value()) {
@@ -173,7 +158,7 @@ std::optional<std::int64_t> BaseInputStream::ReadInt64() {
static_cast<int64_t>(data[6]) << 8 | static_cast<int64_t>(data[7]);
}
std::optional<ByteArray> BaseInputStream::ReadBytes(int size) {
std::optional<ByteArray> StreamReader::ReadBytes(int size) {
if (bits_unused_ != 0) {
return std::nullopt;
}
@@ -186,4 +171,19 @@ std::optional<ByteArray> BaseInputStream::ReadBytes(int size) {
return read_bytes_result.result();
}
ExceptionOr<ByteArray> StreamReader::Read(std::int64_t size) {
if (!IsAvailable(size)) {
return ExceptionOr<ByteArray>{Exception::kIo};
}
ByteArray read_bytes{static_cast<size_t>(size)};
if (read_bytes.CopyAt(/*offset=*/0, buffer_,
/*source_offset=*/position_)) {
position_ += size;
return ExceptionOr<ByteArray>{read_bytes};
} else {
return ExceptionOr<ByteArray>{Exception::kIo};
}
}
} // namespace nearby
@@ -12,40 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_BASE_BASE_INPUT_STREAM_H_
#define PLATFORM_BASE_BASE_INPUT_STREAM_H_
#ifndef PLATFORM_BASE_STREAM_READERH_
#define PLATFORM_BASE_STREAM_READERH_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/input_stream.h"
namespace nearby {
// A base {@link InputStream } for reading the contents of a byte array.
class BaseInputStream : public InputStream {
class StreamReader {
public:
explicit BaseInputStream(ByteArray &buffer) : buffer_{buffer} {}
BaseInputStream(const BaseInputStream &) = delete;
BaseInputStream &operator=(const BaseInputStream &) = delete;
~BaseInputStream() override { Close(); }
ExceptionOr<ByteArray> Read(std::int64_t size) override;
ExceptionOr<size_t> Skip(size_t offset) override {
size_t real_offset = std::min(offset, buffer_.size() - position_);
position_ += real_offset;
return ExceptionOr<size_t>(real_offset);
}
Exception Close() override {
// Do nothing.
return {Exception::kSuccess};
}
explicit StreamReader(ByteArray &buffer) : buffer_{buffer} {}
StreamReader(const StreamReader &) = delete;
StreamReader &operator=(const StreamReader &) = delete;
~StreamReader() = default;
// Reads less than 8 bits from the stream, returning the value if available.
// The read bits cannot across a byte boundary.
@@ -65,6 +50,8 @@ class BaseInputStream : public InputStream {
}
private:
ExceptionOr<ByteArray> Read(std::int64_t size);
uint8_t bits_unused_{0};
uint8_t bits_buffer_{0};
ByteArray &buffer_;
@@ -73,4 +60,4 @@ class BaseInputStream : public InputStream {
} // namespace nearby
#endif // PLATFORM_BASE_BASE_INPUT_STREAM_H_
#endif // PLATFORM_BASE_STREAM_READERH_
@@ -11,7 +11,7 @@
// 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 "internal/platform/base_input_stream.h"
#include "internal/platform/stream_reader.h"
#include <cstdint>
#include <string>
@@ -22,10 +22,10 @@
namespace nearby {
namespace {
TEST(BaseInputStreamTest, ReadBits) {
TEST(StreamReaderTest, ReadBits) {
std::string data{static_cast<char>(0b01011100)};
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_EQ(stream.ReadBits(1), 0);
EXPECT_EQ(stream.ReadBits(2), 2);
EXPECT_EQ(stream.ReadBits(3), 7);
@@ -34,28 +34,28 @@ TEST(BaseInputStreamTest, ReadBits) {
EXPECT_FALSE(stream.ReadBits(1).has_value());
}
TEST(BaseInputStreamTest, ReadBitsExceedsByteBoundary) {
TEST(StreamReaderTest, ReadBitsExceedsByteBoundary) {
std::string data = "ab";
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_FALSE(stream.ReadBits(9).has_value());
EXPECT_EQ(stream.ReadBits(1), 0);
EXPECT_FALSE(stream.ReadInt16().has_value());
}
TEST(BaseInputStreamTest, ReadUintValues) {
TEST(StreamReaderTest, ReadUintValues) {
std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06";
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_EQ(stream.ReadUint16(), 0xfff1);
EXPECT_EQ(stream.ReadUint32(), 0x0f0e0102);
EXPECT_EQ(stream.ReadUint64(), 0x0101010203040506);
}
TEST(BaseInputStreamTest, ReadIntValues) {
TEST(StreamReaderTest, ReadIntValues) {
std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06";
ByteArray byte_array(data);
BaseInputStream stream{byte_array};
StreamReader stream{byte_array};
EXPECT_EQ(stream.ReadInt16(), static_cast<int16_t>(0xfff1));
EXPECT_EQ(stream.ReadInt32(), static_cast<int32_t>(0x0f0e0102));
EXPECT_EQ(stream.ReadInt64(), static_cast<int64_t>(0x0101010203040506));