Skip to content

Commit 4d99c2a

Browse files
committed
Use std::pair to return multiple values
1 parent 4ee9c35 commit 4d99c2a

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/jpgimage.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,15 @@ bool markerHasLength(byte m) {
7575
return !markerWithoutLength;
7676
}
7777

78-
void readSegmentSize(const byte marker, BasicIo& io, std::array<byte, 2>& buf, std::uint16_t& size) {
78+
std::pair<std::array<byte, 2>, uint16_t> readSegmentSize(const byte marker, BasicIo& io) {
79+
std::array<byte, 2> buf{0, 0}; // 2-byte buffer for reading the size.
80+
uint16_t size{0}; // Size of the segment, including the 2-byte size field
7981
if (markerHasLength(marker)) {
8082
io.readOrThrow(buf.data(), buf.size(), ErrorCode::kerFailedToReadImageData);
8183
size = getUShort(buf.data(), bigEndian);
8284
enforce(size >= 2, ErrorCode::kerFailedToReadImageData);
8385
}
86+
return {buf, size};
8487
}
8588
} // namespace
8689

@@ -143,9 +146,7 @@ void JpegBase::readMetadata() {
143146
byte marker = advanceToMarker(ErrorCode::kerNotAJpeg);
144147

145148
while (marker != sos_ && marker != eoi_ && search > 0) {
146-
std::array<byte, 2> sizebuf; // 2-byte buffer for reading the size.
147-
uint16_t size = 0; // Size of the segment, including the 2-byte size field
148-
readSegmentSize(marker, *io_, sizebuf, size);
149+
auto [sizebuf, size] = readSegmentSize(marker, *io_);
149150

150151
// Read the rest of the segment.
151152
DataBuf buf(size);
@@ -345,10 +346,7 @@ void JpegBase::printStructure(std::ostream& out, PrintStructureOption option, in
345346
first = false;
346347
bool bLF = bPrint;
347348

348-
// 2-byte buffer for reading the size.
349-
std::array<byte, 2> sizebuf;
350-
uint16_t size = 0;
351-
readSegmentSize(marker, *io_, sizebuf, size);
349+
auto [sizebuf, size] = readSegmentSize(marker, *io_);
352350

353351
// Read the rest of the segment.
354352
DataBuf buf(size);
@@ -592,10 +590,7 @@ void JpegBase::writeMetadata() {
592590
}
593591

594592
DataBuf JpegBase::readNextSegment(byte marker) {
595-
// 2-byte buffer for reading the size.
596-
std::array<byte, 2> sizebuf;
597-
uint16_t size = 0;
598-
readSegmentSize(marker, *io_, sizebuf, size);
593+
auto [sizebuf, size] = readSegmentSize(marker, *io_);
599594

600595
// Read the rest of the segment.
601596
DataBuf buf(size);

0 commit comments

Comments
 (0)