diff options
Diffstat (limited to 'src/core/lib/support')
-rw-r--r-- | src/core/lib/support/string.c | 9 | ||||
-rw-r--r-- | src/core/lib/support/string.h | 3 |
2 files changed, 12 insertions, 0 deletions
diff --git a/src/core/lib/support/string.c b/src/core/lib/support/string.c index 85b915f118..0dc55c2ca3 100644 --- a/src/core/lib/support/string.c +++ b/src/core/lib/support/string.c @@ -34,7 +34,9 @@ #include "src/core/lib/support/string.h" #include <ctype.h> +#include <limits.h> #include <stddef.h> +#include <stdlib.h> #include <string.h> #include <grpc/support/alloc.h> @@ -189,6 +191,13 @@ int int64_ttoa(int64_t value, char *string) { return i; } +int gpr_parse_nonnegative_int(const char *value) { + char *end; + long result = strtol(value, &end, 0); + if (*end != '\0' || result < 0 || result > INT_MAX) return -1; + return (int)result; +} + char *gpr_leftpad(const char *str, char flag, size_t length) { const size_t str_length = strlen(str); const size_t out_length = str_length > length ? str_length : length; diff --git a/src/core/lib/support/string.h b/src/core/lib/support/string.h index 81a12ae476..43ab4dc1be 100644 --- a/src/core/lib/support/string.h +++ b/src/core/lib/support/string.h @@ -77,6 +77,9 @@ NOTE: This function ensures sufficient bit width even on Win x64, where long is 32bit is size.*/ int int64_ttoa(int64_t value, char *output); +// Parses a non-negative number from a value string. Returns -1 on error. +int gpr_parse_nonnegative_int(const char *value); + /* Reverse a run of bytes */ void gpr_reverse_bytes(char *str, int len); |