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 #ifndef ANDROID_BASE_PARSEINT_H
18 #define ANDROID_BASE_PARSEINT_H
19 
20 #include <errno.h>
21 #include <stdlib.h>
22 
23 #include <limits>
24 #include <string>
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
31 // otherwise valid values will be rejected. Returns boolean success; 'out'
32 // is untouched if parsing fails.
33 template <typename T>
34 bool ParseUint(const char* s, T* out,
35                T max = std::numeric_limits<T>::max()) {
36   int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;
37   errno = 0;
38   char* end;
39   unsigned long long int result = strtoull(s, &end, base);
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 
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 
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
60 // success; 'out' is untouched if parsing fails.
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()) {
65   int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;
66   errno = 0;
67   char* end;
68   long long int result = strtoll(s, &end, base);
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 
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 
87 }  // namespace base
88 }  // namespace android
89 
90 #endif  // ANDROID_BASE_PARSEINT_H
91