OR-Tools  9.6
strtoint.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // Architecture-neutral plug compatible replacements for strtol() friends.
15 //
16 // Long's have different lengths on ILP-32 and LP-64 platforms, and so overflow
17 // behavior across the two varies when strtol() and similar are used to parse
18 // 32-bit integers. Similar problems exist with atoi(), because although it
19 // has an all-integer interface, it uses strtol() internally, and so suffers
20 // from the same narrowing problems on assignments to int.
21 //
22 // Examples:
23 // errno = 0;
24 // i = strtol("3147483647", nullptr, 10);
25 // printf("%d, errno %d\n", i, errno);
26 // // 32-bit platform: 2147483647, errno 34
27 // // 64-bit platform: -1147483649, errno 0
28 //
29 // printf("%d\n", atoi("3147483647"));
30 // // 32-bit platform: 2147483647
31 // // 64-bit platform: -1147483649
32 //
33 // A way round this is to define local replacements for these, and use them
34 // instead of the standard libc functions.
35 //
36 // In most 32-bit cases the replacements can be inlined away to a call to the
37 // libc function. In a couple of 64-bit cases, however, adapters are required,
38 // to provide the right overflow and errno behavior.
39 //
40 
41 #ifndef OR_TOOLS_BASE_STRTOINT_H_
42 #define OR_TOOLS_BASE_STRTOINT_H_
43 
44 #include <cstdint>
45 #include <string>
46 
47 #include "absl/base/port.h" // disable some warnings on Windows
48 #include "absl/strings/string_view.h"
50 
51 namespace operations_research {
52 
53 int32_t strtoint32(absl::string_view word);
54 int64_t strtoint64(absl::string_view word);
55 
56 // Convenience versions of the above that take a string argument.
57 inline int32_t atoi32(const std::string& word) { return strtoint32(word); }
58 inline int64_t atoi64(const std::string& word) { return strtoint64(word); }
59 
60 } // namespace operations_research
61 
62 #endif // OR_TOOLS_BASE_STRTOINT_H_
Collection of objects used to extend the Constraint Solver library.
int64_t atoi64(const std::string &word)
Definition: strtoint.h:58
int32_t atoi32(const std::string &word)
Definition: strtoint.h:57
int32_t strtoint32(absl::string_view word)
Definition: strtoint.cc:26
int64_t strtoint64(absl::string_view word)
Definition: strtoint.cc:32