Merge "Expose ParseBool from libbase"

This commit is contained in:
Daniel Colascione 2019-11-20 19:50:14 +00:00 committed by Gerrit Code Review
commit e8237b3517
5 changed files with 151 additions and 6 deletions

View file

@ -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",

View file

@ -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 <string_view>
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

34
base/parsebool.cpp Normal file
View file

@ -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 <errno.h>
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

48
base/parsebool_test.cpp Normal file
View file

@ -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 <errno.h>
#include <gtest/gtest.h>
#include <string_view>
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(""));
}

View file

@ -28,19 +28,22 @@
#include <map>
#include <string>
#include <android-base/parsebool.h>
#include <android-base/parseint.h>
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 <typename T>