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> |
| 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 | |
| 29 | // Parses the unsigned decimal integer in the string 's' and sets 'out' to |
| 30 | // that value. Optionally allows the caller to define a 'max' beyond which |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 31 | // otherwise valid values will be rejected. Returns boolean success; 'out' |
| 32 | // is untouched if parsing fails. |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 33 | template <typename T> |
| 34 | bool ParseUint(const char* s, T* out, |
| 35 | T max = std::numeric_limits<T>::max()) { |
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 | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 40 | if (errno != 0 || s == end || *end != '\0') { |
| 41 | return false; |
| 42 | } |
| 43 | if (max < result) { |
| 44 | return false; |
| 45 | } |
| 46 | *out = static_cast<T>(result); |
| 47 | return true; |
| 48 | } |
| 49 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 50 | // TODO: string_view |
| 51 | template <typename T> |
| 52 | bool ParseUint(const std::string& s, T* out, |
| 53 | T max = std::numeric_limits<T>::max()) { |
| 54 | return ParseUint(s.c_str(), out, max); |
| 55 | } |
| 56 | |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 57 | // Parses the signed decimal integer in the string 's' and sets 'out' to |
| 58 | // that value. Optionally allows the caller to define a 'min' and 'max |
| 59 | // beyond which otherwise valid values will be rejected. Returns boolean |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 60 | // success; 'out' is untouched if parsing fails. |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 61 | template <typename T> |
| 62 | bool ParseInt(const char* s, T* out, |
| 63 | T min = std::numeric_limits<T>::min(), |
| 64 | T max = std::numeric_limits<T>::max()) { |
Yifan Hong | 6b1e33b | 2016-10-07 16:48:17 -0700 | [diff] [blame] | 65 | 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] | 66 | errno = 0; |
| 67 | char* end; |
Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 68 | long long int result = strtoll(s, &end, base); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 69 | if (errno != 0 || s == end || *end != '\0') { |
| 70 | return false; |
| 71 | } |
| 72 | if (result < min || max < result) { |
| 73 | return false; |
| 74 | } |
| 75 | *out = static_cast<T>(result); |
| 76 | return true; |
| 77 | } |
| 78 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 79 | // TODO: string_view |
| 80 | template <typename T> |
| 81 | bool ParseInt(const std::string& s, T* out, |
| 82 | T min = std::numeric_limits<T>::min(), |
| 83 | T max = std::numeric_limits<T>::max()) { |
| 84 | return ParseInt(s.c_str(), out, min, max); |
| 85 | } |
| 86 | |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 87 | } // namespace base |
| 88 | } // namespace android |
| 89 | |
Elliott Hughes | f522443 | 2016-03-23 15:04:52 -0700 | [diff] [blame] | 90 | #endif // ANDROID_BASE_PARSEINT_H |