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