摘自 memcache 源码
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
using namespace std;
/* Avoid warnings on solaris, where isspace() is an index into an array, and gcc uses signed chars */
#define xisspace(c) isspace((unsigned char)c)
bool safe_strtoull(const char *str, uint64_t *out) {
assert(out != NULL);
errno = 0;
*out = 0;
char *endptr;
unsigned long long ull = strtoull(str, &endptr, 10);
if ((errno == ERANGE) || (str == endptr)) {
return false;
}
if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
if ((long long) ull < 0) {
/* only check for negative signs in the uncommon case when
* the unsigned number is so big that it's negative as a
* signed number. */
if (strchr(str, '-') != NULL) {
return false;
}
}
*out = ull;
return true;
}
return false;
}
bool safe_strtoll(const char *str, int64_t *out) {
assert(out != NULL);
errno = 0;
*out = 0;
char *endptr;
long long ll = strtoll(str, &endptr, 10);
if ((errno == ERANGE) || (str == endptr)) {
return false;
}
if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
*out = ll;
return true;
}
return false;
}
bool safe_strtoul(const char *str, uint32_t *out) {
char *endptr = NULL;
unsigned long l = 0;
assert(out);
assert(str);
*out = 0;
errno = 0;
l = strtoul(str, &endptr, 10);
if ((errno == ERANGE) || (str == endptr)) {
return false;
}
if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
if ((long) l < 0) {
/* only check for negative signs in the uncommon case when
* the unsigned number is so big that it's negative as a
* signed number. */
if (strchr(str, '-') != NULL) {
return false;
}
}
*out = l;
return true;
}
return false;
}
bool safe_strtol(const char *str, int32_t *out) {
assert(out != NULL);
errno = 0;
*out = 0;
char *endptr;
long l = strtol(str, &endptr, 10);
if ((errno == ERANGE) || (str == endptr)) {
return false;
}
if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
*out = l;
return true;
}
return false;
}
int main()
{
string s1 = "12343434";
uint64_t s1_value;
if (safe_strtoull(s1.c_str(), &s1_value)) {
cout << s1_value << endl;
}
cout << "Hello world!" << endl;
return 0;
}