1 //===-- StringConvert.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <cstdlib> 10 11 #include "StringConvert.h" 12 13 namespace StringConvert { 14 15 int64_t ToSInt64(const char *s, int64_t fail_value, int base, 16 bool *success_ptr) { 17 if (s && s[0]) { 18 char *end = nullptr; 19 int64_t uval = ::strtoll(s, &end, base); 20 if (*end == '\0') { 21 if (success_ptr) 22 *success_ptr = true; 23 return uval; // All characters were used, return the result 24 } 25 } 26 if (success_ptr) 27 *success_ptr = false; 28 return fail_value; 29 } 30 31 uint64_t ToUInt64(const char *s, uint64_t fail_value, int base, 32 bool *success_ptr) { 33 if (s && s[0]) { 34 char *end = nullptr; 35 uint64_t uval = ::strtoull(s, &end, base); 36 if (*end == '\0') { 37 if (success_ptr) 38 *success_ptr = true; 39 return uval; // All characters were used, return the result 40 } 41 } 42 if (success_ptr) 43 *success_ptr = false; 44 return fail_value; 45 } 46 47 double ToDouble(const char *s, double fail_value, bool *success_ptr) { 48 if (s && s[0]) { 49 char *end = nullptr; 50 double val = strtod(s, &end); 51 if (*end == '\0') { 52 if (success_ptr) 53 *success_ptr = true; 54 return val; // All characters were used, return the result 55 } 56 } 57 if (success_ptr) 58 *success_ptr = false; 59 return fail_value; 60 } 61 62 } // namespace StringConvert 63