MCPcopy Create free account
hub / github.com/alibaba/graph-learn / SafeStringTo32

Function SafeStringTo32

graphlearn/src/common/string/numeric.cc:97–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95} // anonymous namespace
96
97bool SafeStringTo32(LiteString str, int32_t* value) {
98 SkipSpaces(&str);
99
100 int64_t vmax = kInt32Max;
101 int sign = 1;
102 if (str.Consume("-")) {
103 sign = -1;
104 ++vmax;
105 }
106
107 if (!isdigit(SafeFirstChar(str))) {
108 return false;
109 }
110
111 int64_t result = 0;
112 do {
113 result = result * 10 + SafeFirstChar(str) - '0';
114 if (result > vmax) {
115 return false;
116 }
117 str.remove_prefix(1);
118 } while (isdigit(SafeFirstChar(str)));
119
120 SkipSpaces(&str);
121
122 if (!str.empty()) {
123 return false;
124 }
125
126 *value = static_cast<int32_t>(result * sign);
127 return true;
128}
129
130bool SafeStringTo64(LiteString str, int64_t* value) {
131 SkipSpaces(&str);

Callers

nothing calls this directly

Calls 5

SkipSpacesFunction · 0.85
SafeFirstCharFunction · 0.85
ConsumeMethod · 0.80
remove_prefixMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected