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 <stdlib.h>
10 
11 #include "lldb/Host/StringConvert.h"
12 
13 namespace lldb_private {
14 namespace StringConvert {
15 
ToSInt32(const char * s,int32_t fail_value,int base,bool * success_ptr)16 int32_t ToSInt32(const char *s, int32_t fail_value, int base,
17                  bool *success_ptr) {
18   if (s && s[0]) {
19     char *end = nullptr;
20     const long sval = ::strtol(s, &end, base);
21     if (*end == '\0') {
22       if (success_ptr)
23         *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN));
24       return (int32_t)sval; // All characters were used, return the result
25     }
26   }
27   if (success_ptr)
28     *success_ptr = false;
29   return fail_value;
30 }
31 
ToUInt32(const char * s,uint32_t fail_value,int base,bool * success_ptr)32 uint32_t ToUInt32(const char *s, uint32_t fail_value, int base,
33                   bool *success_ptr) {
34   if (s && s[0]) {
35     char *end = nullptr;
36     const unsigned long uval = ::strtoul(s, &end, base);
37     if (*end == '\0') {
38       if (success_ptr)
39         *success_ptr = (uval <= UINT32_MAX);
40       return (uint32_t)uval; // All characters were used, return the result
41     }
42   }
43   if (success_ptr)
44     *success_ptr = false;
45   return fail_value;
46 }
47 
ToSInt64(const char * s,int64_t fail_value,int base,bool * success_ptr)48 int64_t ToSInt64(const char *s, int64_t fail_value, int base,
49                  bool *success_ptr) {
50   if (s && s[0]) {
51     char *end = nullptr;
52     int64_t uval = ::strtoll(s, &end, base);
53     if (*end == '\0') {
54       if (success_ptr)
55         *success_ptr = true;
56       return uval; // All characters were used, return the result
57     }
58   }
59   if (success_ptr)
60     *success_ptr = false;
61   return fail_value;
62 }
63 
ToUInt64(const char * s,uint64_t fail_value,int base,bool * success_ptr)64 uint64_t ToUInt64(const char *s, uint64_t fail_value, int base,
65                   bool *success_ptr) {
66   if (s && s[0]) {
67     char *end = nullptr;
68     uint64_t uval = ::strtoull(s, &end, base);
69     if (*end == '\0') {
70       if (success_ptr)
71         *success_ptr = true;
72       return uval; // All characters were used, return the result
73     }
74   }
75   if (success_ptr)
76     *success_ptr = false;
77   return fail_value;
78 }
79 
ToDouble(const char * s,double fail_value,bool * success_ptr)80 double ToDouble(const char *s, double fail_value, bool *success_ptr) {
81   if (s && s[0]) {
82     char *end = nullptr;
83     double val = strtod(s, &end);
84     if (*end == '\0') {
85       if (success_ptr)
86         *success_ptr = true;
87       return val; // All characters were used, return the result
88     }
89   }
90   if (success_ptr)
91     *success_ptr = false;
92   return fail_value;
93 }
94 }
95 }
96