Merge "Fix search for values in blkId output" am: 0fbc5af676 am: 56715503b5
am: 464ff37019
Change-Id: I5d47ddedc29cafa05424e6bfcfd6b99885614d7c
diff --git a/Utils.cpp b/Utils.cpp
index f9b61b6..92f25d9 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -202,12 +202,15 @@
bool FindValue(const std::string& raw, const std::string& key, std::string* value) {
auto qual = key + "=\"";
- auto start = raw.find(qual);
- if (start > 0 && raw[start - 1] != ' ') {
- start = raw.find(qual, start + 1);
+ size_t start = 0;
+ while (true) {
+ start = raw.find(qual, start);
+ if (start == std::string::npos) return false;
+ if (start == 0 || raw[start - 1] == ' ') {
+ break;
+ }
+ start += 1;
}
-
- if (start == std::string::npos) return false;
start += qual.length();
auto end = raw.find("\"", start);
diff --git a/tests/Utils_test.cpp b/tests/Utils_test.cpp
index e16cbac..d18dc67 100644
--- a/tests/Utils_test.cpp
+++ b/tests/Utils_test.cpp
@@ -27,6 +27,7 @@
std::string tmp;
ASSERT_FALSE(FindValue("", "KEY", &tmp));
+ ASSERT_FALSE(FindValue("NOTMATCH=\"VALUE\"", "KEY", &tmp));
ASSERT_FALSE(FindValue("BADKEY=\"VALUE\"", "KEY", &tmp));
ASSERT_TRUE(FindValue("KEY=\"VALUE\"", "KEY", &tmp));
@@ -37,6 +38,9 @@
ASSERT_TRUE(FindValue("BADKEY=\"VALUE\" KEY=\"BAZ\"", "KEY", &tmp));
ASSERT_EQ("BAZ", tmp);
+
+ ASSERT_TRUE(FindValue("BADKEY=\"VALUE\" NOTKEY=\"OTHER\" KEY=\"QUUX\"", "KEY", &tmp));
+ ASSERT_EQ("QUUX", tmp);
}
} // namespace vold