1 /*
2 * Copyright (C) 2018 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 NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_NUMBERS_H_
18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_NUMBERS_H_
19
20 #include <string>
21
22 #include "lang_id/common/lite_strings/stringpiece.h"
23
24 namespace libtextclassifier3 {
25 namespace mobile {
26
27 // Parses an int from a C-style string; similar to absl::SimpleAtoi.
28 //
29 // c_str should point to a zero-terminated array of chars that contains the
30 // number representation as (a) "<radix-10-number>" (e.g., "721"), (b)
31 // "0x<radix-16-number>" (e.g., "0xa1"), or (c) "0<radix-8-number>" (e.g.,
32 // "017201"). Whitespaces (as determined by isspace()) are allowed before and
33 // after the number representation (but obviously not in the middle).
34 //
35 // Stores parsed number into *value. Returns true on success, false on error.
36 // Note: presence of extra non-whitespace characters after the number counts as
37 // an error: e.g., parsing "123a" will return false due to the extra "a" (which
38 // is not a valid radix-10 digit). This function also returns false for strings
39 // that do not contain any digit (e.g., ""), as well as for overflows /
40 // underflows.
41 bool LiteAtoi(const char *c_str, int *value);
42
LiteAtoi(const string & s,int * value)43 inline bool LiteAtoi(const string &s, int *value) {
44 return LiteAtoi(s.c_str(), value);
45 }
46
LiteAtoi(StringPiece sp,int * value)47 inline bool LiteAtoi(StringPiece sp, int *value) {
48 // Unfortunately, we can't directly call LiteAtoi(sp.data()): LiteAtoi(const
49 // char *) needs a zero-terminated string.
50 const string temp(sp.data(), sp.size());
51 return LiteAtoi(temp.c_str(), value);
52 }
53
54 // Like LiteAtoi, but for float; similar to absl::SimpleAtof.
55 //
56 // NOTE: currently, does not properly handle overflow / underflow.
57 // TODO(salcianu): fix that.
58 bool LiteAtof(const char *c_str, float *value);
59
LiteAtof(const string & s,float * value)60 inline bool LiteAtof(const string &s, float *value) {
61 return LiteAtof(s.c_str(), value);
62 }
63
LiteAtof(StringPiece sp,float * value)64 inline bool LiteAtof(StringPiece sp, float *value) {
65 // Unfortunately, we can't directly call LiteAtoi(sp.data()): LiteAtoi(const
66 // char *) needs a zero-terminated string.
67 const string temp(sp.data(), sp.size());
68 return LiteAtof(temp.c_str(), value);
69 }
70
71 } // namespace mobile
72 } // namespace nlp_saft
73
74 #endif // NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_NUMBERS_H_
75