| 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 | e3c5a2a | 2018-06-26 17:17:41 -0700 | [diff] [blame] | 17 | #pragma once |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <stdlib.h> |
| Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame] | 21 | #include <string.h> |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 22 | |
| 23 | #include <limits> |
| Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 24 | #include <string> |
| Tom Cherry | 06518bc | 2018-08-22 15:26:20 -0700 | [diff] [blame] | 25 | #include <type_traits> |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace base { |
| 29 | |
| Inseob Kim | 06c19d0 | 2018-06-29 18:07:00 +0900 | [diff] [blame] | 30 | // Parses the unsigned decimal or hexadecimal integer in the string 's' and sets |
| Steven Moreland | 4d39661 | 2018-08-08 16:39:35 -0700 | [diff] [blame] | 31 | // 'out' to that value if it is specified. Optionally allows the caller to define |
| 32 | // a 'max' beyond which otherwise valid values will be rejected. Returns boolean |
| 33 | // success; 'out' 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) { |
| Tom Cherry | 06518bc | 2018-08-22 15:26:20 -0700 | [diff] [blame] | 37 | static_assert(std::is_unsigned<T>::value, "ParseUint can only be used with unsigned types"); |
| Tom Cherry | 9e58f1b | 2018-08-22 14:38:02 -0700 | [diff] [blame] | 38 | while (isspace(*s)) { |
| 39 | s++; |
| 40 | } |
| 41 | |
| 42 | if (s[0] == '-') { |
| Christopher Ferris | 3ac4085 | 2018-08-28 14:35:38 -0700 | [diff] [blame] | 43 | errno = EINVAL; |
| Tom Cherry | 9e58f1b | 2018-08-22 14:38:02 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
| Florian Mayer | b7c6e5a | 2022-08-11 22:58:22 +0000 | [diff] [blame] | 47 | // This is never out of bounds. If string is zero-sized, s[0] == '\0' |
| 48 | // so the second condition is not checked. If string is "0", |
| 49 | // s[1] will compare against the '\0'. |
| Yifan Hong | 6b1e33b | 2016-10-07 16:48:17 -0700 | [diff] [blame] | 50 | 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] | 51 | errno = 0; |
| 52 | char* end; |
| Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 53 | unsigned long long int result = strtoull(s, &end, base); |
| Christopher Ferris | 3ac4085 | 2018-08-28 14:35:38 -0700 | [diff] [blame] | 54 | if (errno != 0) return false; |
| 55 | if (end == s) { |
| 56 | errno = EINVAL; |
| 57 | return false; |
| 58 | } |
| Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame] | 59 | if (*end != '\0') { |
| 60 | const char* suffixes = "bkmgtpe"; |
| 61 | const char* suffix; |
| Christopher Ferris | 3ac4085 | 2018-08-28 14:35:38 -0700 | [diff] [blame] | 62 | if ((!allow_suffixes || (suffix = strchr(suffixes, tolower(*end))) == nullptr) || |
| 63 | __builtin_mul_overflow(result, 1ULL << (10 * (suffix - suffixes)), &result)) { |
| 64 | errno = EINVAL; |
| 65 | return false; |
| 66 | } |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 67 | } |
| 68 | if (max < result) { |
| Christopher Ferris | 3ac4085 | 2018-08-28 14:35:38 -0700 | [diff] [blame] | 69 | errno = ERANGE; |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 70 | return false; |
| 71 | } |
| Steven Moreland | c4f40bf | 2018-07-20 11:02:47 -0700 | [diff] [blame] | 72 | if (out != nullptr) { |
| 73 | *out = static_cast<T>(result); |
| 74 | } |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 78 | // TODO: string_view |
| 79 | template <typename T> |
| Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame] | 80 | bool ParseUint(const std::string& s, T* out, T max = std::numeric_limits<T>::max(), |
| 81 | bool allow_suffixes = false) { |
| 82 | return ParseUint(s.c_str(), out, max, allow_suffixes); |
| 83 | } |
| 84 | |
| 85 | template <typename T> |
| 86 | bool ParseByteCount(const char* s, T* out, T max = std::numeric_limits<T>::max()) { |
| 87 | return ParseUint(s, out, max, true); |
| 88 | } |
| 89 | |
| 90 | // TODO: string_view |
| 91 | template <typename T> |
| 92 | bool ParseByteCount(const std::string& s, T* out, T max = std::numeric_limits<T>::max()) { |
| 93 | return ParseByteCount(s.c_str(), out, max); |
| Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| Inseob Kim | 06c19d0 | 2018-06-29 18:07:00 +0900 | [diff] [blame] | 96 | // Parses the signed decimal or hexadecimal integer in the string 's' and sets |
| Steven Moreland | 4d39661 | 2018-08-08 16:39:35 -0700 | [diff] [blame] | 97 | // 'out' to that value if it is specified. Optionally allows the caller to define |
| 98 | // a 'min' and 'max' beyond which otherwise valid values will be rejected. Returns |
| 99 | // boolean success; 'out' is untouched if parsing fails. |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 100 | template <typename T> |
| 101 | bool ParseInt(const char* s, T* out, |
| 102 | T min = std::numeric_limits<T>::min(), |
| 103 | T max = std::numeric_limits<T>::max()) { |
| Tom Cherry | 06518bc | 2018-08-22 15:26:20 -0700 | [diff] [blame] | 104 | static_assert(std::is_signed<T>::value, "ParseInt can only be used with signed types"); |
| Tom Cherry | 9e58f1b | 2018-08-22 14:38:02 -0700 | [diff] [blame] | 105 | while (isspace(*s)) { |
| 106 | s++; |
| 107 | } |
| 108 | |
| Yifan Hong | 6b1e33b | 2016-10-07 16:48:17 -0700 | [diff] [blame] | 109 | 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] | 110 | errno = 0; |
| 111 | char* end; |
| Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 112 | long long int result = strtoll(s, &end, base); |
| Christopher Ferris | 3ac4085 | 2018-08-28 14:35:38 -0700 | [diff] [blame] | 113 | if (errno != 0) { |
| 114 | return false; |
| 115 | } |
| 116 | if (s == end || *end != '\0') { |
| 117 | errno = EINVAL; |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | if (result < min || max < result) { |
| Christopher Ferris | 3ac4085 | 2018-08-28 14:35:38 -0700 | [diff] [blame] | 121 | errno = ERANGE; |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| Steven Moreland | c4f40bf | 2018-07-20 11:02:47 -0700 | [diff] [blame] | 124 | if (out != nullptr) { |
| 125 | *out = static_cast<T>(result); |
| 126 | } |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 130 | // TODO: string_view |
| 131 | template <typename T> |
| 132 | bool ParseInt(const std::string& s, T* out, |
| 133 | T min = std::numeric_limits<T>::min(), |
| 134 | T max = std::numeric_limits<T>::max()) { |
| 135 | return ParseInt(s.c_str(), out, min, max); |
| 136 | } |
| 137 | |
| Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 138 | } // namespace base |
| 139 | } // namespace android |