From 9e3cbb64ee7fed0ef9d4d21d2b4b888efee32a6b Mon Sep 17 00:00:00 2001 From: Daniel Colascione Date: Thu, 14 Nov 2019 00:48:36 -0800 Subject: [PATCH] Expose ParseBool from libbase Also change the properties implementation to call the new API. We use this ParseBool API in the new SystemProperties implementation, with which we want the libbase property API to be consistent. Test: included Change-Id: I89cb3eb4e1203a6bb0da41914dad720e44c00303 --- base/Android.bp | 2 + base/include/android-base/parsebool.h | 58 +++++++++++++++++++++++++++ base/parsebool.cpp | 34 ++++++++++++++++ base/parsebool_test.cpp | 48 ++++++++++++++++++++++ base/properties.cpp | 15 ++++--- 5 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 base/include/android-base/parsebool.h create mode 100644 base/parsebool.cpp create mode 100644 base/parsebool_test.cpp diff --git a/base/Android.bp b/base/Android.bp index 25ae535a3..aeb8864db 100644 --- a/base/Android.bp +++ b/base/Android.bp @@ -58,6 +58,7 @@ cc_defaults { "file.cpp", "logging.cpp", "mapped_file.cpp", + "parsebool.cpp", "parsenetaddress.cpp", "process.cpp", "properties.cpp", @@ -149,6 +150,7 @@ cc_test { "macros_test.cpp", "mapped_file_test.cpp", "parsedouble_test.cpp", + "parsebool_test.cpp", "parseint_test.cpp", "parsenetaddress_test.cpp", "process_test.cpp", diff --git a/base/include/android-base/parsebool.h b/base/include/android-base/parsebool.h new file mode 100644 index 000000000..b2bd0217e --- /dev/null +++ b/base/include/android-base/parsebool.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +namespace android { +namespace base { + +// Parse the given string as yes or no inactivation of some sort. Return one of the +// ParseBoolResult enumeration values. +// +// The following values parse as true: +// +// 1 +// on +// true +// y +// yes +// +// +// The following values parse as false: +// +// 0 +// false +// n +// no +// off +// +// Anything else is a parse error. +// +// The purpose of this function is to have a single canonical parser for yes-or-no indications +// throughout the system. + +enum class ParseBoolResult { + kError, + kFalse, + kTrue, +}; + +ParseBoolResult ParseBool(std::string_view s); + +} // namespace base +} // namespace android diff --git a/base/parsebool.cpp b/base/parsebool.cpp new file mode 100644 index 000000000..ff96fe9a9 --- /dev/null +++ b/base/parsebool.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "android-base/parsebool.h" +#include + +namespace android { +namespace base { + +ParseBoolResult ParseBool(std::string_view s) { + if (s == "1" || s == "y" || s == "yes" || s == "on" || s == "true") { + return ParseBoolResult::kTrue; + } + if (s == "0" || s == "n" || s == "no" || s == "off" || s == "false") { + return ParseBoolResult::kFalse; + } + return ParseBoolResult::kError; +} + +} // namespace base +} // namespace android diff --git a/base/parsebool_test.cpp b/base/parsebool_test.cpp new file mode 100644 index 000000000..a0819940b --- /dev/null +++ b/base/parsebool_test.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "android-base/parsebool.h" + +#include + +#include +#include + +using android::base::ParseBool; +using android::base::ParseBoolResult; + +TEST(parsebool, true_) { + static const char* yes[] = { + "1", "on", "true", "y", "yes", + }; + for (const char* s : yes) { + ASSERT_EQ(ParseBoolResult::kTrue, ParseBool(s)); + } +} + +TEST(parsebool, false_) { + static const char* no[] = { + "0", "false", "n", "no", "off", + }; + for (const char* s : no) { + ASSERT_EQ(ParseBoolResult::kFalse, ParseBool(s)); + } +} + +TEST(parsebool, invalid) { + ASSERT_EQ(ParseBoolResult::kError, ParseBool("blarg")); + ASSERT_EQ(ParseBoolResult::kError, ParseBool("")); +} diff --git a/base/properties.cpp b/base/properties.cpp index d5a5918ce..4731bf249 100644 --- a/base/properties.cpp +++ b/base/properties.cpp @@ -28,19 +28,22 @@ #include #include +#include #include namespace android { namespace base { bool GetBoolProperty(const std::string& key, bool default_value) { - std::string value = GetProperty(key, ""); - if (value == "1" || value == "y" || value == "yes" || value == "on" || value == "true") { - return true; - } else if (value == "0" || value == "n" || value == "no" || value == "off" || value == "false") { - return false; + switch (ParseBool(GetProperty(key, ""))) { + case ParseBoolResult::kError: + return default_value; + case ParseBoolResult::kFalse: + return false; + case ParseBoolResult::kTrue: + return true; } - return default_value; + __builtin_unreachable(); } template