revert bsstring length transmit type
This commit is contained in:
@@ -44,29 +44,25 @@ namespace basalt::shared::pipe_operator {
|
||||
write(&buffer, sizeof(TPod));
|
||||
}
|
||||
void read_string(std::string& buffer) {
|
||||
std::uint32_t raw_length = 0;
|
||||
read_pod(raw_length);
|
||||
auto length = static_cast<std::size_t>(raw_length);
|
||||
std::size_t length = 0;
|
||||
read_pod(length);
|
||||
buffer.resize(length);
|
||||
read(buffer.data(), length * sizeof(std::string::value_type));
|
||||
}
|
||||
void write_string(const std::string_view& buffer) {
|
||||
auto length = buffer.size();
|
||||
auto raw_length = static_cast<std::uint32_t>(length);
|
||||
write_pod(raw_length);
|
||||
write_pod(length);
|
||||
write(buffer.data(), length * sizeof(std::string_view::value_type));
|
||||
}
|
||||
void read_bsstring(char_types::BSString& buffer) {
|
||||
std::uint32_t raw_length = 0;
|
||||
read_pod(raw_length);
|
||||
auto length = static_cast<std::size_t>(raw_length);
|
||||
std::size_t length = 0;
|
||||
read_pod(length);
|
||||
buffer.resize(length);
|
||||
read(buffer.data(), length * sizeof(char_types::BSString::value_type));
|
||||
}
|
||||
void write_bsstring(const char_types::BSStringView& buffer) {
|
||||
auto length = buffer.size();
|
||||
auto raw_length = static_cast<std::uint32_t>(length);
|
||||
write_pod(raw_length);
|
||||
write_pod(length);
|
||||
write(buffer.data(), length * sizeof(char_types::BSStringView::value_type));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import sys
|
||||
import struct
|
||||
from typing import Optional, Any, ClassVar
|
||||
from typing import Optional, Any
|
||||
|
||||
IS_WINDOWS: bool = sys.platform == "win32"
|
||||
IS_LITTLE_ENDIAN: bool = sys.byteorder == "little"
|
||||
@@ -13,6 +13,9 @@ if IS_WINDOWS:
|
||||
else:
|
||||
import errno
|
||||
|
||||
BSSTR_LEN_PACKER: struct.Struct = struct.Struct("N")
|
||||
BSCHAR_SIZE: int = struct.calcsize("H") if IS_WINDOWS else struct.calcsize("c")
|
||||
|
||||
|
||||
class PipeOperator:
|
||||
"""
|
||||
@@ -160,26 +163,24 @@ class PipeOperator:
|
||||
def write_pod(self, pattern: struct.Struct, *args) -> None:
|
||||
self.write(pattern.pack(*args))
|
||||
|
||||
STR_LEN_PACKER: ClassVar[struct.Struct] = struct.Struct("=I")
|
||||
|
||||
def read_string(self) -> str:
|
||||
(length,) = self.read_pod(PipeOperator.STR_LEN_PACKER)
|
||||
(length,) = self.read_pod(BSSTR_LEN_PACKER)
|
||||
str_bytes = self.read(length)
|
||||
return str_bytes.decode("utf-8")
|
||||
|
||||
def write_string(self, s: str) -> None:
|
||||
str_bytes = s.encode("utf-8")
|
||||
self.write_pod(PipeOperator.STR_LEN_PACKER, len(str_bytes))
|
||||
self.write_pod(BSSTR_LEN_PACKER, len(str_bytes))
|
||||
self.write(str_bytes)
|
||||
|
||||
def read_bsstring(self) -> str:
|
||||
(length,) = self.read_pod(PipeOperator.STR_LEN_PACKER)
|
||||
str_bytes = self.read(length * 2)
|
||||
(length,) = self.read_pod(BSSTR_LEN_PACKER)
|
||||
str_bytes = self.read(length * BSCHAR_SIZE)
|
||||
return self.__decode_bsstring(str_bytes)
|
||||
|
||||
def write_bsstring(self, s: str) -> None:
|
||||
str_bytes = self.__encode_bsstring(s)
|
||||
self.write_pod(PipeOperator.STR_LEN_PACKER, len(str_bytes) // 2)
|
||||
self.write_pod(BSSTR_LEN_PACKER, len(str_bytes) // BSCHAR_SIZE)
|
||||
self.write(str_bytes)
|
||||
|
||||
def __encode_bsstring(self, s: str) -> bytes:
|
||||
|
||||
Reference in New Issue
Block a user