szaszm commented on a change in pull request #923:
URL: https://github.com/apache/nifi-minifi-cpp/pull/923#discussion_r503320203
##########
File path: libminifi/src/utils/Id.cpp
##########
@@ -115,18 +116,36 @@ std::string Identifier::to_string() const {
utils::optional<Identifier> Identifier::parse(const std::string &str) {
Identifier id;
- int assigned = sscanf(str.c_str(), UUID_FORMAT_STRING,
- &id.data_[0], &id.data_[1], &id.data_[2], &id.data_[3],
- &id.data_[4], &id.data_[5],
- &id.data_[6], &id.data_[7],
- &id.data_[8], &id.data_[9],
- &id.data_[10], &id.data_[11], &id.data_[12], &id.data_[13],
&id.data_[14], &id.data_[15]);
- if (assigned != 16) {
- return utils::nullopt;
+ if (str.length() != 36) return {};
+ int charIdx = 0;
+ int byteIdx = 0;
+ auto input = reinterpret_cast<const uint8_t*>(str.c_str());
+ while(byteIdx < 4) {
+ if (!parseByte(id.data_, input, charIdx, byteIdx)) return {};
+ }
+ if (input[charIdx++] != '-') return {};
+
+ for (size_t idx = 0; idx < 3; ++idx) {
+ if (!parseByte(id.data_, input, charIdx, byteIdx)) return {};
+ if (!parseByte(id.data_, input, charIdx, byteIdx)) return {};
+ if (input[charIdx++] != '-') return {};
+ }
+ while(byteIdx < 16) {
+ if(!parseByte(id.data_, input, charIdx, byteIdx)) return {};
Review comment:
I would add a few comments to explain what each part is parsing, because
it's inherently less readable than a format string.
```suggestion
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 36 long: 16 bytes * 2 hex
digits / byte + 4 hyphens
if (str.length() != 36) return {};
int charIdx = 0;
int byteIdx = 0;
auto input = reinterpret_cast<const uint8_t*>(str.c_str());
// [xxxxxxxx]-xxxx-xxxx-xxxx-xxxxxxxxxxxx
while(byteIdx < 4) {
if (!parseByte(id.data_, input, charIdx, byteIdx)) return {};
}
// xxxxxxxx[-]xxxx-xxxx-xxxx-xxxxxxxxxxxx
if (input[charIdx++] != '-') return {};
// xxxxxxxx-[xxxx-xxxx-xxxx-]xxxxxxxxxxxx - 3x 2 bytes and a hyphen
for (size_t idx = 0; idx < 3; ++idx) {
if (!parseByte(id.data_, input, charIdx, byteIdx)) return {};
if (!parseByte(id.data_, input, charIdx, byteIdx)) return {};
if (input[charIdx++] != '-') return {};
}
// xxxxxxxx-xxxx-xxxx-xxxx-[xxxxxxxxxxxx] - the rest, i.e. until byte 16
while(byteIdx < 16) {
if(!parseByte(id.data_, input, charIdx, byteIdx)) return {};
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]