/* * Copyright (c) 2023 Huawei Device Co., Ltd. * 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. */ #ifndef MAPLE_UTIL_INCLUDE_UTILS_H #define MAPLE_UTIL_INCLUDE_UTILS_H #include #include #include #include "mpl_logging.h" namespace maple { namespace utils { // Operations on char constexpr bool IsDigit(char c) { return (c >= '0' && c <= '9'); } constexpr bool IsLower(char c) { return (c >= 'a' && c <= 'z'); } constexpr bool IsUpper(char c) { return (c >= 'A' && c <= 'Z'); } constexpr bool IsAlpha(char c) { return (IsLower(c) || IsUpper(c)); } constexpr bool IsAlnum(char c) { return (IsAlpha(c) || IsDigit(c)); } namespace __ToDigitImpl { template struct ToDigitImpl { }; constexpr uint8_t kOctalNum = 8; constexpr uint8_t kDecimalNum = 10; constexpr uint8_t kHexadecimalNum = 16; template struct ToDigitImpl { static T DoIt(char c) { if (utils::IsDigit(c)) { return c - '0'; } return std::numeric_limits::max(); } }; template struct ToDigitImpl { static T DoIt(char c) { if (c >= '0' && c < '8') { return c - '0'; } return std::numeric_limits::max(); } }; template struct ToDigitImpl { static T DoIt(char c) { if (utils::IsDigit(c)) { return c - '0'; } if (c >= 'a' && c <= 'f') { return c - 'a' + kDecimalNum; } if (c >= 'A' && c <= 'F') { return c - 'A' + kDecimalNum; } return std::numeric_limits::max(); } }; } // namespace __ToDigitImpl template constexpr T ToDigit(char c) { return __ToDigitImpl::ToDigitImpl::DoIt(c); } // Operations on pointer template inline T &ToRef(T *ptr) { CHECK_NULL_FATAL(ptr); return *ptr; } template inline typename T::element_type &ToRef(T &ptr) { return ToRef(ptr.get()); } template > struct bit_field { enum { value = 1U << pos }; }; template constexpr uint32_t bit_field_v = bit_field::value; template > struct lbit_field { enum { value = 1ULL << pos }; }; template constexpr uint64_t lbit_field_v = bit_field::value; template bool Contains(const std::vector &container, const T &data) { return (std::find(std::begin(container), std::end(container), data) != container.end()); } } // namespace utils } // namespace maple #endif // MAPLE_UTIL_INCLUDE_UTILS_H