Skip to content

Commit 7748484

Browse files
feat: get string view
1 parent 220688e commit 7748484

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

include/binarystream/ReadOnlyBinaryStream.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class ReadOnlyBinaryStream {
113113
[[nodiscard]] BSAPI std::string getShortString();
114114
[[nodiscard]] BSAPI std::string getLongString();
115115

116+
[[nodiscard]] BSAPI std::string_view getStringView();
117+
[[nodiscard]] BSAPI std::string_view getShortStringView();
118+
[[nodiscard]] BSAPI std::string_view getLongStringView();
119+
116120
BSAPI void getRawBytes(std::string& rawBuffer, size_t length);
117121
[[nodiscard]] BSAPI std::string getRawBytes(size_t length);
118122
};

src/binarystream/BinaryStream.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ void BinaryStream::writeSignedBigEndianInt(int32_t value) { write(value, true);
123123
void BinaryStream::writeString(std::string_view value) {
124124
auto size = static_cast<uint32_t>(value.size());
125125
writeUnsignedVarInt(size);
126-
writeRawBytes(value, size);
126+
writeRawBytes(value, static_cast<size_t>(size));
127127
}
128128

129129
void BinaryStream::writeShortString(std::string_view value) {
130-
auto size = static_cast<uint16_t>(value.size());
130+
auto size = static_cast<int16_t>(value.size());
131131
writeSignedShort(size);
132-
writeRawBytes(value, size);
132+
writeRawBytes(value, static_cast<size_t>(size));
133133
}
134134

135135
void BinaryStream::writeLongString(std::string_view value) {
136136
auto size = static_cast<int>(value.size());
137137
writeSignedInt(size);
138-
writeRawBytes(value, size);
138+
writeRawBytes(value, static_cast<size_t>(size));
139139
}
140140

141141
void BinaryStream::writeUnsignedInt24(uint32_t value) {

src/binarystream/ReadOnlyBinaryStream.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ uint32_t ReadOnlyBinaryStream::getUnsignedVarInt() noexcept {
163163
}
164164

165165
byte = static_cast<uint8_t>(mBufferView[mReadPointer++]);
166-
value |= (byte & 0x7F) << shift;
166+
value |= static_cast<uint32_t>(byte & 0x7F) << shift;
167167
shift += 7;
168168

169169
} while (byte & 0x80);
@@ -218,17 +218,17 @@ int32_t ReadOnlyBinaryStream::getSignedBigEndianInt() noexcept {
218218

219219
void ReadOnlyBinaryStream::getString(std::string& outString) {
220220
uint32_t length = getUnsignedVarInt();
221-
getRawBytes(outString, length);
221+
getRawBytes(outString, static_cast<size_t>(length));
222222
}
223223

224224
void ReadOnlyBinaryStream::getShortString(std::string& outString) {
225225
short length = getSignedShort();
226-
getRawBytes(outString, length);
226+
getRawBytes(outString, static_cast<size_t>(length));
227227
}
228228

229229
void ReadOnlyBinaryStream::getLongString(std::string& outString) {
230230
int length = getSignedInt();
231-
getRawBytes(outString, length);
231+
getRawBytes(outString, static_cast<size_t>(length));
232232
}
233233

234234
std::string ReadOnlyBinaryStream::getString() {
@@ -249,14 +249,35 @@ std::string ReadOnlyBinaryStream::getLongString() {
249249
return result;
250250
}
251251

252+
std::string_view ReadOnlyBinaryStream::getStringView() {
253+
auto length = static_cast<size_t>(getUnsignedVarInt());
254+
auto result = mBufferView.substr(mReadPointer, length);
255+
mReadPointer += length;
256+
return result;
257+
}
258+
259+
std::string_view ReadOnlyBinaryStream::getShortStringView() {
260+
auto length = static_cast<size_t>(getSignedShort());
261+
auto result = mBufferView.substr(mReadPointer, length);
262+
mReadPointer += length;
263+
return result;
264+
}
265+
266+
std::string_view ReadOnlyBinaryStream::getLongStringView() {
267+
auto length = static_cast<size_t>(getSignedInt());
268+
auto result = mBufferView.substr(mReadPointer, length);
269+
mReadPointer += length;
270+
return result;
271+
}
272+
252273
uint32_t ReadOnlyBinaryStream::getUnsignedInt24() noexcept {
253274
if (mReadPointer + 3 > mBufferView.size()) {
254275
mHasOverflowed = true;
255276
return 0;
256277
}
257278
uint32_t value = 0;
258279
if (mBigEndian) {
259-
value = static_cast<uint8_t>(mBufferView[mReadPointer++]) << 16;
280+
value = static_cast<uint32_t>(static_cast<uint8_t>(mBufferView[mReadPointer++]) << 16);
260281
value |= static_cast<uint32_t>(static_cast<uint8_t>(mBufferView[mReadPointer++])) << 8;
261282
value |= static_cast<uint32_t>(static_cast<uint8_t>(mBufferView[mReadPointer++]));
262283
} else {

xmake.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ target("BinaryStream")
5050
add_cxflags(
5151
"-Wall",
5252
"-Wextra",
53+
"-Wconversion",
5354
"-pedantic",
5455
"-fexceptions",
5556
"-stdlib=libc++",

0 commit comments

Comments
 (0)