mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
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:
@@ -23,10 +23,11 @@ StringRef UnescapeURI(const Twine& str, SmallVectorImpl<char>& buf,
|
||||
// pass non-escaped characters to output
|
||||
if (*i != '%') {
|
||||
// decode + to space
|
||||
if (*i == '+')
|
||||
if (*i == '+') {
|
||||
buf.push_back(' ');
|
||||
else
|
||||
} else {
|
||||
buf.push_back(*i);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -92,17 +93,23 @@ HttpQueryMap::HttpQueryMap(wpi::StringRef query) {
|
||||
bool err = false;
|
||||
auto name = wpi::UnescapeURI(nameEsc, nameBuf, &err);
|
||||
// note: ignores duplicates
|
||||
if (!err) m_elems.try_emplace(name, valueEsc);
|
||||
if (!err) {
|
||||
m_elems.try_emplace(name, valueEsc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<wpi::StringRef> HttpQueryMap::Get(
|
||||
wpi::StringRef name, wpi::SmallVectorImpl<char>& buf) const {
|
||||
auto it = m_elems.find(name);
|
||||
if (it == m_elems.end()) return {};
|
||||
if (it == m_elems.end()) {
|
||||
return {};
|
||||
}
|
||||
bool err = false;
|
||||
auto val = wpi::UnescapeURI(it->second, buf, &err);
|
||||
if (err) return {};
|
||||
if (err) {
|
||||
return {};
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -128,12 +135,16 @@ HttpPath::HttpPath(wpi::StringRef path) {
|
||||
}
|
||||
|
||||
bool HttpPath::startswith(size_t start, ArrayRef<StringRef> match) const {
|
||||
if (m_pathEnds.size() < (start + match.size())) return false;
|
||||
if (m_pathEnds.size() < (start + match.size())) {
|
||||
return false;
|
||||
}
|
||||
bool first = start == 0;
|
||||
auto p = m_pathEnds.begin() + start;
|
||||
for (auto m : match) {
|
||||
auto val = m_pathBuf.slice(first ? 0 : *(p - 1), *p);
|
||||
if (val != m) return false;
|
||||
if (val != m) {
|
||||
return false;
|
||||
}
|
||||
first = false;
|
||||
++p;
|
||||
}
|
||||
@@ -142,16 +153,24 @@ bool HttpPath::startswith(size_t start, ArrayRef<StringRef> match) const {
|
||||
|
||||
bool ParseHttpHeaders(raw_istream& is, SmallVectorImpl<char>* contentType,
|
||||
SmallVectorImpl<char>* contentLength) {
|
||||
if (contentType) contentType->clear();
|
||||
if (contentLength) contentLength->clear();
|
||||
if (contentType) {
|
||||
contentType->clear();
|
||||
}
|
||||
if (contentLength) {
|
||||
contentLength->clear();
|
||||
}
|
||||
|
||||
bool inContentType = false;
|
||||
bool inContentLength = false;
|
||||
SmallString<64> lineBuf;
|
||||
for (;;) {
|
||||
StringRef line = is.getline(lineBuf, 1024).rtrim();
|
||||
if (is.has_error()) return false;
|
||||
if (line.empty()) return true; // empty line signals end of headers
|
||||
if (is.has_error()) {
|
||||
return false;
|
||||
}
|
||||
if (line.empty()) {
|
||||
return true; // empty line signals end of headers
|
||||
}
|
||||
|
||||
// header fields start at the beginning of the line
|
||||
if (!std::isspace(line[0])) {
|
||||
@@ -160,22 +179,24 @@ bool ParseHttpHeaders(raw_istream& is, SmallVectorImpl<char>* contentType,
|
||||
StringRef field;
|
||||
std::tie(field, line) = line.split(':');
|
||||
field = field.rtrim();
|
||||
if (field.equals_lower("content-type"))
|
||||
if (field.equals_lower("content-type")) {
|
||||
inContentType = true;
|
||||
else if (field.equals_lower("content-length"))
|
||||
} else if (field.equals_lower("content-length")) {
|
||||
inContentLength = true;
|
||||
else
|
||||
} else {
|
||||
continue; // ignore other fields
|
||||
}
|
||||
}
|
||||
|
||||
// collapse whitespace
|
||||
line = line.ltrim();
|
||||
|
||||
// save field data
|
||||
if (inContentType && contentType)
|
||||
if (inContentType && contentType) {
|
||||
contentType->append(line.begin(), line.end());
|
||||
else if (inContentLength && contentLength)
|
||||
} else if (inContentLength && contentLength) {
|
||||
contentLength->append(line.begin(), line.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +212,9 @@ bool FindMultipartBoundary(raw_istream& is, StringRef boundary,
|
||||
if (!saveBuf) {
|
||||
do {
|
||||
is.read(searchBuf.data(), 1);
|
||||
if (is.has_error()) return false;
|
||||
if (is.has_error()) {
|
||||
return false;
|
||||
}
|
||||
} while (searchBuf[0] == '\r' || searchBuf[0] == '\n');
|
||||
searchPos = 1;
|
||||
}
|
||||
@@ -202,19 +225,26 @@ bool FindMultipartBoundary(raw_istream& is, StringRef boundary,
|
||||
// there's a bunch of continuous -'s in the output, but that's unlikely.
|
||||
for (;;) {
|
||||
is.read(searchBuf.data() + searchPos, searchBuf.size() - searchPos);
|
||||
if (is.has_error()) return false;
|
||||
if (is.has_error()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Did we find the boundary?
|
||||
if (searchBuf[0] == '-' && searchBuf[1] == '-' &&
|
||||
searchBuf.substr(2) == boundary)
|
||||
searchBuf.substr(2) == boundary) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fast-scan for '-'
|
||||
size_t pos = searchBuf.find('-', searchBuf[0] == '-' ? 1 : 0);
|
||||
if (pos == StringRef::npos) {
|
||||
if (saveBuf) saveBuf->append(searchBuf.data(), searchBuf.size());
|
||||
if (saveBuf) {
|
||||
saveBuf->append(searchBuf.data(), searchBuf.size());
|
||||
}
|
||||
} else {
|
||||
if (saveBuf) saveBuf->append(searchBuf.data(), pos);
|
||||
if (saveBuf) {
|
||||
saveBuf->append(searchBuf.data(), pos);
|
||||
}
|
||||
|
||||
// move '-' and following to start of buffer (next read will fill)
|
||||
std::memmove(searchBuf.data(), searchBuf.data() + pos,
|
||||
@@ -305,7 +335,9 @@ HttpLocation::HttpLocation(const Twine& url_, bool* error,
|
||||
// split out next param and value
|
||||
StringRef rawParam, rawValue;
|
||||
std::tie(rawParam, query) = query.split('&');
|
||||
if (rawParam.empty()) continue; // ignore "&&"
|
||||
if (rawParam.empty()) {
|
||||
continue; // ignore "&&"
|
||||
}
|
||||
std::tie(rawParam, rawValue) = rawParam.split('=');
|
||||
|
||||
// unescape param
|
||||
@@ -350,8 +382,9 @@ bool HttpConnection::Handshake(const HttpRequest& request,
|
||||
// send GET request
|
||||
os << "GET /" << request.path << " HTTP/1.1\r\n";
|
||||
os << "Host: " << request.host << "\r\n";
|
||||
if (!request.auth.empty())
|
||||
if (!request.auth.empty()) {
|
||||
os << "Authorization: Basic " << request.auth << "\r\n";
|
||||
}
|
||||
os << "\r\n";
|
||||
os.flush();
|
||||
|
||||
@@ -404,8 +437,12 @@ void HttpMultipartScanner::Reset(bool saveSkipped) {
|
||||
}
|
||||
|
||||
StringRef HttpMultipartScanner::Execute(StringRef in) {
|
||||
if (m_state == kDone) Reset(m_saveSkipped);
|
||||
if (m_saveSkipped) m_buf += in;
|
||||
if (m_state == kDone) {
|
||||
Reset(m_saveSkipped);
|
||||
}
|
||||
if (m_saveSkipped) {
|
||||
m_buf += in;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
if (m_state == kBoundary) {
|
||||
@@ -451,7 +488,9 @@ StringRef HttpMultipartScanner::Execute(StringRef in) {
|
||||
if (ch == '\n') {
|
||||
// Found the LF; return remaining input buffer (following it)
|
||||
m_state = kDone;
|
||||
if (m_saveSkipped) m_buf.resize(m_buf.size() - in.size() + pos);
|
||||
if (m_saveSkipped) {
|
||||
m_buf.resize(m_buf.size() - in.size() + pos);
|
||||
}
|
||||
return in.drop_front(pos);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user