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