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 #include "lang_id/common/lite_strings/numbers.h"
18
19 #include <ctype.h>
20 #include <stdlib.h>
21 #include <climits>
22
23 namespace libtextclassifier3 {
24 namespace mobile {
25
26 // Returns true if the characters that start at address ptr (inclusive) and stop
27 // at the first '\0' consist of only whitespaces, as determined by isspace().
28 // Note: this function returns false if ptr is nullptr.
OnlyWhitespaces(const char * ptr)29 static bool OnlyWhitespaces(const char *ptr) {
30 if (ptr == nullptr) {
31 return false;
32 }
33 for (; *ptr != '\0'; ++ptr) {
34 if (!isspace(*ptr)) {
35 return false;
36 }
37 }
38 return true;
39 }
40
LiteAtoi(const char * c_str,int * value)41 bool LiteAtoi(const char *c_str, int *value) {
42 if (c_str == nullptr) {
43 return false;
44 }
45
46 // Short version of man strtol:
47 //
48 // strtol parses some optional whitespaces, an optional +/- sign, and next a
49 // succession of digits. If it finds some digits, it sets temp to point to
50 // the first character after that succession of digits and returns the parsed
51 // integer.
52 //
53 // If there were no digits at all, strtol() sets temp to be c_str (the start
54 // address) and returns 0.
55 char *temp = nullptr;
56 const long int parsed_value = strtol(c_str, &temp, 0); // NOLINT
57
58 // Check for overflow. Note: to simplify the code, we assume that LONG_MIN /
59 // LONG_MAX means that strtol encountered an overflow (normally, in that case,
60 // one should also inspect errno). Hence, we maybe give up the possibility to
61 // parse one extreme value on each side (min/max). That should be ok.
62 if ((parsed_value == LONG_MIN) || (parsed_value == LONG_MAX) ||
63 (parsed_value < INT_MIN) || (parsed_value > INT_MAX)) {
64 return false;
65 }
66 *value = static_cast<int>(parsed_value);
67
68 // First part of the expression below means that the input string contained at
69 // least one digit. The other part checks that what remains after the number
70 // (if anything) consists only of whitespaces.
71 return (temp != c_str) && OnlyWhitespaces(temp);
72 }
73
LiteAtof(const char * c_str,float * value)74 bool LiteAtof(const char *c_str, float *value) {
75 if (c_str == nullptr) {
76 return false;
77 }
78
79 // strtof is similar to strtol, see more detailed comments inside LiteAtoi.
80 char *temp = nullptr;
81 *value = strtof(c_str, &temp);
82 return (temp != c_str) && OnlyWhitespaces(temp);
83 }
84
85 } // namespace mobile
86 } // namespace nlp_saft
87