Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce12543e authored by Daniel Colascione's avatar Daniel Colascione Committed by android-build-merger
Browse files

Merge "Expose ParseBool from libbase"

am: e8237b35

Change-Id: I8081a0fdb1a2b40fdb411642037f277fd9245206
parents 06a0f824 e8237b35
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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",
+58 −0
Original line number Diff line number Diff line
/*
 * 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

base/parsebool.cpp

0 → 100644
+34 −0
Original line number Diff line number Diff line
/*
 * 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 −0
Original line number Diff line number Diff line
/*
 * 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(""));
}
+9 −6
Original line number Diff line number Diff line
@@ -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") {
  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>