1 //===-- StringConvert.cpp ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // C Includes 11 #include <stdlib.h> 12 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Host/StringConvert.h" 17 18 namespace lldb_private { 19 20 namespace StringConvert { 21 22 int32_t 23 ToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr) 24 { 25 if (s && s[0]) 26 { 27 char *end = nullptr; 28 const long sval = ::strtol (s, &end, base); 29 if (*end == '\0') 30 { 31 if (success_ptr) 32 *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN)); 33 return (int32_t)sval; // All characters were used, return the result 34 } 35 } 36 if (success_ptr) *success_ptr = false; 37 return fail_value; 38 } 39 40 uint32_t 41 ToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr) 42 { 43 if (s && s[0]) 44 { 45 char *end = nullptr; 46 const unsigned long uval = ::strtoul (s, &end, base); 47 if (*end == '\0') 48 { 49 if (success_ptr) 50 *success_ptr = (uval <= UINT32_MAX); 51 return (uint32_t)uval; // All characters were used, return the result 52 } 53 } 54 if (success_ptr) *success_ptr = false; 55 return fail_value; 56 } 57 58 int64_t 59 ToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr) 60 { 61 if (s && s[0]) 62 { 63 char *end = nullptr; 64 int64_t uval = ::strtoll (s, &end, base); 65 if (*end == '\0') 66 { 67 if (success_ptr) *success_ptr = true; 68 return uval; // All characters were used, return the result 69 } 70 } 71 if (success_ptr) *success_ptr = false; 72 return fail_value; 73 } 74 75 uint64_t 76 ToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr) 77 { 78 if (s && s[0]) 79 { 80 char *end = nullptr; 81 uint64_t uval = ::strtoull (s, &end, base); 82 if (*end == '\0') 83 { 84 if (success_ptr) *success_ptr = true; 85 return uval; // All characters were used, return the result 86 } 87 } 88 if (success_ptr) *success_ptr = false; 89 return fail_value; 90 } 91 92 } 93 } 94