Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | f522443 | 2016-03-23 15:04:52 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_BASE_PARSEINT_H |
| 18 | #define ANDROID_BASE_PARSEINT_H |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame^] | 22 | #include <string.h> |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 23 | |
| 24 | #include <limits> |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 25 | #include <string> |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace base { |
| 29 | |
| 30 | // Parses the unsigned decimal integer in the string 's' and sets 'out' to |
| 31 | // that value. Optionally allows the caller to define a 'max' beyond which |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 32 | // otherwise valid values will be rejected. Returns boolean success; 'out' |
| 33 | // is untouched if parsing fails. |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 34 | template <typename T> |
Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame^] | 35 | bool ParseUint(const char* s, T* out, T max = std::numeric_limits<T>::max(), |
| 36 | bool allow_suffixes = false) { |
Yifan Hong | 6b1e33b | 2016-10-07 16:48:17 -0700 | [diff] [blame] | 37 | int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 38 | errno = 0; |
| 39 | char* end; |
Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 40 | unsigned long long int result = strtoull(s, &end, base); |
Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame^] | 41 | if (errno != 0 || end == s) return false; |
| 42 | if (*end != '\0') { |
| 43 | const char* suffixes = "bkmgtpe"; |
| 44 | const char* suffix; |
| 45 | if (!allow_suffixes || (suffix = strchr(suffixes, tolower(*end))) == nullptr) return false; |
| 46 | #if __clang__ // TODO: win32 still builds with GCC :-( |
| 47 | if (__builtin_mul_overflow(result, 1ULL << (10 * (suffix - suffixes)), &result)) return false; |
| 48 | #endif |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 49 | } |
| 50 | if (max < result) { |
| 51 | return false; |
| 52 | } |
| 53 | *out = static_cast<T>(result); |
| 54 | return true; |
| 55 | } |
| 56 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 57 | // TODO: string_view |
| 58 | template <typename T> |
Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame^] | 59 | bool ParseUint(const std::string& s, T* out, T max = std::numeric_limits<T>::max(), |
| 60 | bool allow_suffixes = false) { |
| 61 | return ParseUint(s.c_str(), out, max, allow_suffixes); |
| 62 | } |
| 63 | |
| 64 | template <typename T> |
| 65 | bool ParseByteCount(const char* s, T* out, T max = std::numeric_limits<T>::max()) { |
| 66 | return ParseUint(s, out, max, true); |
| 67 | } |
| 68 | |
| 69 | // TODO: string_view |
| 70 | template <typename T> |
| 71 | bool ParseByteCount(const std::string& s, T* out, T max = std::numeric_limits<T>::max()) { |
| 72 | return ParseByteCount(s.c_str(), out, max); |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 75 | // Parses the signed decimal integer in the string 's' and sets 'out' to |
| 76 | // that value. Optionally allows the caller to define a 'min' and 'max |
| 77 | // beyond which otherwise valid values will be rejected. Returns boolean |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 78 | // success; 'out' is untouched if parsing fails. |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 79 | template <typename T> |
| 80 | bool ParseInt(const char* s, T* out, |
| 81 | T min = std::numeric_limits<T>::min(), |
| 82 | T max = std::numeric_limits<T>::max()) { |
Yifan Hong | 6b1e33b | 2016-10-07 16:48:17 -0700 | [diff] [blame] | 83 | int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 84 | errno = 0; |
| 85 | char* end; |
Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 86 | long long int result = strtoll(s, &end, base); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 87 | if (errno != 0 || s == end || *end != '\0') { |
| 88 | return false; |
| 89 | } |
| 90 | if (result < min || max < result) { |
| 91 | return false; |
| 92 | } |
| 93 | *out = static_cast<T>(result); |
| 94 | return true; |
| 95 | } |
| 96 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 97 | // TODO: string_view |
| 98 | template <typename T> |
| 99 | bool ParseInt(const std::string& s, T* out, |
| 100 | T min = std::numeric_limits<T>::min(), |
| 101 | T max = std::numeric_limits<T>::max()) { |
| 102 | return ParseInt(s.c_str(), out, min, max); |
| 103 | } |
| 104 | |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 105 | } // namespace base |
| 106 | } // namespace android |
| 107 | |
Elliott Hughes | f522443 | 2016-03-23 15:04:52 -0700 | [diff] [blame] | 108 | #endif // ANDROID_BASE_PARSEINT_H |