Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
strtoint.h
Go to the documentation of this file.
1// Copyright 2010-2024 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"
49
50namespace operations_research {
51
52int32_t strtoint32(absl::string_view word);
53int64_t strtoint64(absl::string_view word);
54
55// Convenience versions of the above that take a string argument.
56inline int32_t atoi32(absl::string_view word) { return strtoint32(word); }
57inline int64_t atoi64(absl::string_view word) { return strtoint64(word); }
58
59} // namespace operations_research
60
61#endif // OR_TOOLS_BASE_STRTOINT_H_
In SWIG mode, we don't want anything besides these top-level includes.
int32_t atoi32(absl::string_view word)
Convenience versions of the above that take a string argument.
Definition strtoint.h:56
int64_t atoi64(absl::string_view word)
Definition strtoint.h:57
int64_t strtoint64(absl::string_view word)
Definition strtoint.cc:32
int32_t strtoint32(absl::string_view word)
Definition strtoint.cc:26