]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/StringConvert.cpp
Merge ^/head r312968 through r313054.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / StringConvert.cpp
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 namespace StringConvert {
20
21 int32_t ToSInt32(const char *s, int32_t fail_value, int base,
22                  bool *success_ptr) {
23   if (s && s[0]) {
24     char *end = nullptr;
25     const long sval = ::strtol(s, &end, base);
26     if (*end == '\0') {
27       if (success_ptr)
28         *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN));
29       return (int32_t)sval; // All characters were used, return the result
30     }
31   }
32   if (success_ptr)
33     *success_ptr = false;
34   return fail_value;
35 }
36
37 uint32_t ToUInt32(const char *s, uint32_t fail_value, int base,
38                   bool *success_ptr) {
39   if (s && s[0]) {
40     char *end = nullptr;
41     const unsigned long uval = ::strtoul(s, &end, base);
42     if (*end == '\0') {
43       if (success_ptr)
44         *success_ptr = (uval <= UINT32_MAX);
45       return (uint32_t)uval; // All characters were used, return the result
46     }
47   }
48   if (success_ptr)
49     *success_ptr = false;
50   return fail_value;
51 }
52
53 int64_t ToSInt64(const char *s, int64_t fail_value, int base,
54                  bool *success_ptr) {
55   if (s && s[0]) {
56     char *end = nullptr;
57     int64_t uval = ::strtoll(s, &end, base);
58     if (*end == '\0') {
59       if (success_ptr)
60         *success_ptr = true;
61       return uval; // All characters were used, return the result
62     }
63   }
64   if (success_ptr)
65     *success_ptr = false;
66   return fail_value;
67 }
68
69 uint64_t ToUInt64(const char *s, uint64_t fail_value, int base,
70                   bool *success_ptr) {
71   if (s && s[0]) {
72     char *end = nullptr;
73     uint64_t uval = ::strtoull(s, &end, base);
74     if (*end == '\0') {
75       if (success_ptr)
76         *success_ptr = true;
77       return uval; // All characters were used, return the result
78     }
79   }
80   if (success_ptr)
81     *success_ptr = false;
82   return fail_value;
83 }
84
85 double ToDouble(const char *s, double fail_value, bool *success_ptr) {
86   if (s && s[0]) {
87     char *end = nullptr;
88     double val = strtod(s, &end);
89     if (*end == '\0') {
90       if (success_ptr)
91         *success_ptr = true;
92       return val; // All characters were used, return the result
93     }
94   }
95   if (success_ptr)
96     *success_ptr = false;
97   return fail_value;
98 }
99 }
100 }