1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <errno.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include <limits> 24 #include <string> 25 #include <type_traits> 26 27 #define LIBBASE_ALWAYS_INLINE __attribute__((__always_inline__)) 28 29 namespace android { 30 namespace base { 31 32 // Parses the unsigned decimal or hexadecimal integer in the string 's' and sets 33 // 'out' to that value if it is specified. Optionally allows the caller to define 34 // a 'max' beyond which otherwise valid values will be rejected. Returns boolean 35 // success; 'out' is untouched if parsing fails. 36 template <typename T> 37 LIBBASE_ALWAYS_INLINE bool ParseUint(const char* s, T* out, T max = std::numeric_limits<T>::max(), 38 bool allow_suffixes = false) { 39 static_assert(std::is_unsigned<T>::value, "ParseUint can only be used with unsigned types"); 40 while (isspace(*s)) { 41 s++; 42 } 43 44 if (s[0] == '-') { 45 errno = EINVAL; 46 return false; 47 } 48 49 // This is never out of bounds. If string is zero-sized, s[0] == '\0' 50 // so the second condition is not checked. If string is "0", 51 // s[1] will compare against the '\0'. 52 int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; 53 errno = 0; 54 char* end; 55 unsigned long long int result = strtoull(s, &end, base); 56 if (errno != 0) return false; 57 if (end == s) { 58 errno = EINVAL; 59 return false; 60 } 61 if (*end != '\0') { 62 const char* suffixes = "bkmgtpe"; 63 const char* suffix; 64 if ((!allow_suffixes || (suffix = strchr(suffixes, tolower(*end))) == nullptr) || 65 __builtin_mul_overflow(result, 1ULL << (10 * (suffix - suffixes)), &result)) { 66 errno = EINVAL; 67 return false; 68 } 69 } 70 if (max < result) { 71 errno = ERANGE; 72 return false; 73 } 74 if (out != nullptr) { 75 *out = static_cast<T>(result); 76 } 77 return true; 78 } 79 80 // TODO: string_view 81 template <typename T> 82 LIBBASE_ALWAYS_INLINE bool ParseUint(const std::string& s, T* out, 83 T max = std::numeric_limits<T>::max(), 84 bool allow_suffixes = false) { 85 return ParseUint(s.c_str(), out, max, allow_suffixes); 86 } 87 88 template <typename T> 89 LIBBASE_ALWAYS_INLINE bool ParseByteCount(const char* s, T* out, 90 T max = std::numeric_limits<T>::max()) { 91 return ParseUint(s, out, max, true); 92 } 93 94 // TODO: string_view 95 template <typename T> 96 LIBBASE_ALWAYS_INLINE bool ParseByteCount(const std::string& s, T* out, 97 T max = std::numeric_limits<T>::max()) { 98 return ParseByteCount(s.c_str(), out, max); 99 } 100 101 // Parses the signed decimal or hexadecimal integer in the string 's' and sets 102 // 'out' to that value if it is specified. Optionally allows the caller to define 103 // a 'min' and 'max' beyond which otherwise valid values will be rejected. Returns 104 // boolean success; 'out' is untouched if parsing fails. 105 template <typename T> 106 LIBBASE_ALWAYS_INLINE bool ParseInt(const char* s, T* out, T min = std::numeric_limits<T>::min(), 107 T max = std::numeric_limits<T>::max()) { 108 static_assert(std::is_signed<T>::value, "ParseInt can only be used with signed types"); 109 while (isspace(*s)) { 110 s++; 111 } 112 113 int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; 114 errno = 0; 115 char* end; 116 long long int result = strtoll(s, &end, base); 117 if (errno != 0) { 118 return false; 119 } 120 if (s == end || *end != '\0') { 121 errno = EINVAL; 122 return false; 123 } 124 if (result < min || max < result) { 125 errno = ERANGE; 126 return false; 127 } 128 if (out != nullptr) { 129 *out = static_cast<T>(result); 130 } 131 return true; 132 } 133 134 // TODO: string_view 135 template <typename T> 136 LIBBASE_ALWAYS_INLINE bool ParseInt(const std::string& s, T* out, 137 T min = std::numeric_limits<T>::min(), 138 T max = std::numeric_limits<T>::max()) { 139 return ParseInt(s.c_str(), out, min, max); 140 } 141 142 } // namespace base 143 } // namespace android 144 145 #undef LIBBASE_ALWAYS_INLINE 146