• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 LIBTEXTCLASSIFIER_UTIL_STRINGS_NUMBERS_H_
18 #define LIBTEXTCLASSIFIER_UTIL_STRINGS_NUMBERS_H_
19 
20 #include <string>
21 
22 #include "util/base/integral_types.h"
23 
24 namespace libtextclassifier {
25 
26 // Parses an int32 from a C-style string.
27 //
28 // c_str should point to a zero-terminated array of chars that contains the
29 // number representation as (a) "<radix-10-number>" (e.g., "721"), (b)
30 // "0x<radix-16-number>" (e.g., "0xa1"), or (c) "0<radix-8-number>" (e.g.,
31 // "017201").
32 //
33 // Stores parsed number into *value.  Returns true on success, false on error.
34 // Note: presence of extra characters after the number counts as an error: e.g.,
35 // parsing "123a" will return false due to the extra "a" (which is not a valid
36 // radix-10 digit).  Parsing a string that does not contain any digit (e.g., "")
37 // is treated as an error: this function returns false.
38 bool ParseInt32(const char *c_str, int32 *value);
39 
40 // Like ParseInt32, but for int64.
41 bool ParseInt64(const char *c_str, int64 *value);
42 
43 // Like ParseInt32, but for double.
44 bool ParseDouble(const char *c_str, double *value);
45 
46 // Converts an integer to string.  Accepts (via implicit conversions) all common
47 // int types.
48 std::string IntToString(int64 input);
49 
50 
51 }  // namespace libtextclassifier
52 
53 #endif  // LIBTEXTCLASSIFIER_UTIL_STRINGS_NUMBERS_H_
54