blob: 2c8570ead78f3be5c9e6c0446c8f5cf7d722eaec [file] [log] [blame]
Elliott Hughes7dd8e202015-09-04 16:26:51 -07001/*
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 Hughesf5224432016-03-23 15:04:52 -070017#ifndef ANDROID_BASE_PARSEINT_H
18#define ANDROID_BASE_PARSEINT_H
Elliott Hughes7dd8e202015-09-04 16:26:51 -070019
20#include <errno.h>
21#include <stdlib.h>
22
23#include <limits>
Elliott Hughes31d04db2016-10-11 17:09:00 -070024#include <string>
Elliott Hughes7dd8e202015-09-04 16:26:51 -070025
26namespace android {
27namespace 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 Hughes31d04db2016-10-11 17:09:00 -070031// otherwise valid values will be rejected. Returns boolean success; 'out'
32// is untouched if parsing fails.
Elliott Hughes7dd8e202015-09-04 16:26:51 -070033template <typename T>
34bool ParseUint(const char* s, T* out,
35 T max = std::numeric_limits<T>::max()) {
Yifan Hong6b1e33b2016-10-07 16:48:17 -070036 int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;
Elliott Hughes7dd8e202015-09-04 16:26:51 -070037 errno = 0;
38 char* end;
Elliott Hughesefa901e2015-11-02 09:01:53 -080039 unsigned long long int result = strtoull(s, &end, base);
Elliott Hughes7dd8e202015-09-04 16:26:51 -070040 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 Hughes31d04db2016-10-11 17:09:00 -070050// TODO: string_view
51template <typename T>
52bool 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 Hughes7dd8e202015-09-04 16:26:51 -070057// 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 Hughes31d04db2016-10-11 17:09:00 -070060// success; 'out' is untouched if parsing fails.
Elliott Hughes7dd8e202015-09-04 16:26:51 -070061template <typename T>
62bool ParseInt(const char* s, T* out,
63 T min = std::numeric_limits<T>::min(),
64 T max = std::numeric_limits<T>::max()) {
Yifan Hong6b1e33b2016-10-07 16:48:17 -070065 int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;
Elliott Hughes7dd8e202015-09-04 16:26:51 -070066 errno = 0;
67 char* end;
Elliott Hughesefa901e2015-11-02 09:01:53 -080068 long long int result = strtoll(s, &end, base);
Elliott Hughes7dd8e202015-09-04 16:26:51 -070069 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 Hughes31d04db2016-10-11 17:09:00 -070079// TODO: string_view
80template <typename T>
81bool 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 Hughes7dd8e202015-09-04 16:26:51 -070087} // namespace base
88} // namespace android
89
Elliott Hughesf5224432016-03-23 15:04:52 -070090#endif // ANDROID_BASE_PARSEINT_H