Add braces to C++ single-line loops and conditionals (NFC) (#2973)

This makes code easier to read and more consistent between C++ and Java.
Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
Peter Johnson
2020-12-28 12:58:06 -08:00
committed by GitHub
parent 0291a3ff56
commit 2aed432b4b
634 changed files with 10716 additions and 3938 deletions

View File

@@ -47,28 +47,44 @@ static const unsigned char dhtData[] = {
0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa};
bool IsJpeg(wpi::StringRef data) {
if (data.size() < 11) return false;
if (data.size() < 11) {
return false;
}
// Check for valid SOI
auto bytes = data.bytes_begin();
if (bytes[0] != 0xff || bytes[1] != 0xd8) return false;
if (bytes[0] != 0xff || bytes[1] != 0xd8) {
return false;
}
return true;
}
bool GetJpegSize(wpi::StringRef data, int* width, int* height) {
if (!IsJpeg(data)) return false;
if (!IsJpeg(data)) {
return false;
}
data = data.substr(2); // Get to the first block
auto bytes = data.bytes_begin();
for (;;) {
if (data.size() < 4) return false; // EOF
if (data.size() < 4) {
return false; // EOF
}
bytes = data.bytes_begin();
if (bytes[0] != 0xff) return false; // not a tag
if (bytes[1] == 0xd9) return false; // EOI without finding SOF?
if (bytes[1] == 0xda) return false; // SOS without finding SOF?
if (bytes[0] != 0xff) {
return false; // not a tag
}
if (bytes[1] == 0xd9) {
return false; // EOI without finding SOF?
}
if (bytes[1] == 0xda) {
return false; // SOS without finding SOF?
}
if (bytes[1] == 0xc0) {
// SOF contains the file size
if (data.size() < 9) return false;
if (data.size() < 9) {
return false;
}
*height = bytes[5] * 256 + bytes[6];
*width = bytes[7] * 256 + bytes[8];
return true;
@@ -80,7 +96,9 @@ bool GetJpegSize(wpi::StringRef data, int* width, int* height) {
bool JpegNeedsDHT(const char* data, size_t* size, size_t* locSOF) {
wpi::StringRef sdata(data, *size);
if (!IsJpeg(sdata)) return false;
if (!IsJpeg(sdata)) {
return false;
}
*locSOF = *size;
@@ -88,12 +106,22 @@ bool JpegNeedsDHT(const char* data, size_t* size, size_t* locSOF) {
sdata = sdata.substr(2); // Get to the first block
auto bytes = sdata.bytes_begin();
for (;;) {
if (sdata.size() < 4) return false; // EOF
if (sdata.size() < 4) {
return false; // EOF
}
bytes = sdata.bytes_begin();
if (bytes[0] != 0xff) return false; // not a tag
if (bytes[1] == 0xda) break; // SOS
if (bytes[1] == 0xc4) return false; // DHT
if (bytes[1] == 0xc0) *locSOF = sdata.data() - data; // SOF
if (bytes[0] != 0xff) {
return false; // not a tag
}
if (bytes[1] == 0xda) {
break; // SOS
}
if (bytes[1] == 0xc4) {
return false; // DHT
}
if (bytes[1] == 0xc0) {
*locSOF = sdata.data() - data; // SOF
}
// Go to the next block
sdata = sdata.substr(bytes[2] * 256 + bytes[3] + 2);
}
@@ -126,18 +154,26 @@ bool ReadJpeg(wpi::raw_istream& is, std::string& buf, int* width, int* height) {
// read SOI and first marker
buf.resize(4);
is.read(&(*buf.begin()), 4);
if (is.has_error()) return false;
if (is.has_error()) {
return false;
}
// Check for valid SOI
auto bytes = reinterpret_cast<const unsigned char*>(buf.data());
if (bytes[0] != 0xff || bytes[1] != 0xd8) return false;
if (bytes[0] != 0xff || bytes[1] != 0xd8) {
return false;
}
size_t pos = 2; // point to first marker
for (;;) {
bytes = reinterpret_cast<const unsigned char*>(buf.data() + pos);
if (bytes[0] != 0xff) return false; // not a marker
if (bytes[0] != 0xff) {
return false; // not a marker
}
unsigned char marker = bytes[1];
if (marker == 0xd9) return true; // EOI, we're done
if (marker == 0xd9) {
return true; // EOI, we're done
}
if (marker == 0xda) {
// SOS: need to keep reading until we reach a normal marker.
@@ -147,12 +183,15 @@ bool ReadJpeg(wpi::raw_istream& is, std::string& buf, int* width, int* height) {
bool maybeMarker = false;
for (;;) {
ReadInto(is, buf, 1);
if (is.has_error()) return false;
if (is.has_error()) {
return false;
}
bytes = reinterpret_cast<const unsigned char*>(buf.data() + pos);
if (maybeMarker) {
if (bytes[0] != 0x00 && bytes[0] != 0xff &&
(bytes[0] < 0xd0 || bytes[0] > 0xd7))
(bytes[0] < 0xd0 || bytes[0] > 0xd7)) {
break;
}
maybeMarker = false;
} else if (bytes[0] == 0xff) {
maybeMarker = true;
@@ -165,7 +204,9 @@ bool ReadJpeg(wpi::raw_istream& is, std::string& buf, int* width, int* height) {
// A normal block. Read the length
ReadInto(is, buf, 2); // read length
if (is.has_error()) return false;
if (is.has_error()) {
return false;
}
// Point to length
pos += 2;
@@ -174,7 +215,9 @@ bool ReadJpeg(wpi::raw_istream& is, std::string& buf, int* width, int* height) {
// Read the block and the next marker
size_t blockLength = bytes[0] * 256 + bytes[1];
ReadInto(is, buf, blockLength);
if (is.has_error()) return false;
if (is.has_error()) {
return false;
}
bytes = reinterpret_cast<const unsigned char*>(buf.data() + pos);
// Special block processing