11c3bbb01SEd Maste //===-- StringConvert.cpp ---------------------------------------*- C++ -*-===//
21c3bbb01SEd Maste //
31c3bbb01SEd Maste // The LLVM Compiler Infrastructure
41c3bbb01SEd Maste //
51c3bbb01SEd Maste // This file is distributed under the University of Illinois Open Source
61c3bbb01SEd Maste // License. See LICENSE.TXT for details.
71c3bbb01SEd Maste //
81c3bbb01SEd Maste //===----------------------------------------------------------------------===//
91c3bbb01SEd Maste
101c3bbb01SEd Maste #include <stdlib.h>
111c3bbb01SEd Maste
121c3bbb01SEd Maste #include "lldb/Host/StringConvert.h"
131c3bbb01SEd Maste
14435933ddSDimitry Andric namespace lldb_private {
15435933ddSDimitry Andric namespace StringConvert {
161c3bbb01SEd Maste
ToSInt32(const char * s,int32_t fail_value,int base,bool * success_ptr)17435933ddSDimitry Andric int32_t ToSInt32(const char *s, int32_t fail_value, int base,
18435933ddSDimitry Andric bool *success_ptr) {
19435933ddSDimitry Andric if (s && s[0]) {
201c3bbb01SEd Maste char *end = nullptr;
211c3bbb01SEd Maste const long sval = ::strtol(s, &end, base);
22435933ddSDimitry Andric if (*end == '\0') {
231c3bbb01SEd Maste if (success_ptr)
241c3bbb01SEd Maste *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN));
251c3bbb01SEd Maste return (int32_t)sval; // All characters were used, return the result
261c3bbb01SEd Maste }
271c3bbb01SEd Maste }
28b91a7dfcSDimitry Andric if (success_ptr)
29b91a7dfcSDimitry Andric *success_ptr = false;
301c3bbb01SEd Maste return fail_value;
311c3bbb01SEd Maste }
321c3bbb01SEd Maste
ToUInt32(const char * s,uint32_t fail_value,int base,bool * success_ptr)33435933ddSDimitry Andric uint32_t ToUInt32(const char *s, uint32_t fail_value, int base,
34435933ddSDimitry Andric bool *success_ptr) {
35435933ddSDimitry Andric if (s && s[0]) {
361c3bbb01SEd Maste char *end = nullptr;
371c3bbb01SEd Maste const unsigned long uval = ::strtoul(s, &end, base);
38435933ddSDimitry Andric if (*end == '\0') {
391c3bbb01SEd Maste if (success_ptr)
401c3bbb01SEd Maste *success_ptr = (uval <= UINT32_MAX);
411c3bbb01SEd Maste return (uint32_t)uval; // All characters were used, return the result
421c3bbb01SEd Maste }
431c3bbb01SEd Maste }
44b91a7dfcSDimitry Andric if (success_ptr)
45b91a7dfcSDimitry Andric *success_ptr = false;
461c3bbb01SEd Maste return fail_value;
471c3bbb01SEd Maste }
481c3bbb01SEd Maste
ToSInt64(const char * s,int64_t fail_value,int base,bool * success_ptr)49435933ddSDimitry Andric int64_t ToSInt64(const char *s, int64_t fail_value, int base,
50435933ddSDimitry Andric bool *success_ptr) {
51435933ddSDimitry Andric if (s && s[0]) {
521c3bbb01SEd Maste char *end = nullptr;
531c3bbb01SEd Maste int64_t uval = ::strtoll(s, &end, base);
54435933ddSDimitry Andric if (*end == '\0') {
55b91a7dfcSDimitry Andric if (success_ptr)
56b91a7dfcSDimitry Andric *success_ptr = true;
571c3bbb01SEd Maste return uval; // All characters were used, return the result
581c3bbb01SEd Maste }
591c3bbb01SEd Maste }
60b91a7dfcSDimitry Andric if (success_ptr)
61b91a7dfcSDimitry Andric *success_ptr = false;
621c3bbb01SEd Maste return fail_value;
631c3bbb01SEd Maste }
641c3bbb01SEd Maste
ToUInt64(const char * s,uint64_t fail_value,int base,bool * success_ptr)65435933ddSDimitry Andric uint64_t ToUInt64(const char *s, uint64_t fail_value, int base,
66435933ddSDimitry Andric bool *success_ptr) {
67435933ddSDimitry Andric if (s && s[0]) {
681c3bbb01SEd Maste char *end = nullptr;
691c3bbb01SEd Maste uint64_t uval = ::strtoull(s, &end, base);
70435933ddSDimitry Andric if (*end == '\0') {
71b91a7dfcSDimitry Andric if (success_ptr)
72b91a7dfcSDimitry Andric *success_ptr = true;
731c3bbb01SEd Maste return uval; // All characters were used, return the result
741c3bbb01SEd Maste }
751c3bbb01SEd Maste }
76435933ddSDimitry Andric if (success_ptr)
77435933ddSDimitry Andric *success_ptr = false;
781c3bbb01SEd Maste return fail_value;
791c3bbb01SEd Maste }
801c3bbb01SEd Maste
ToDouble(const char * s,double fail_value,bool * success_ptr)81435933ddSDimitry Andric double ToDouble(const char *s, double fail_value, bool *success_ptr) {
82435933ddSDimitry Andric if (s && s[0]) {
83b91a7dfcSDimitry Andric char *end = nullptr;
84b91a7dfcSDimitry Andric double val = strtod(s, &end);
85435933ddSDimitry Andric if (*end == '\0') {
86b91a7dfcSDimitry Andric if (success_ptr)
87b91a7dfcSDimitry Andric *success_ptr = true;
88b91a7dfcSDimitry Andric return val; // All characters were used, return the result
89b91a7dfcSDimitry Andric }
90b91a7dfcSDimitry Andric }
91b91a7dfcSDimitry Andric if (success_ptr)
92b91a7dfcSDimitry Andric *success_ptr = false;
93b91a7dfcSDimitry Andric return fail_value;
94b91a7dfcSDimitry Andric }
951c3bbb01SEd Maste }
961c3bbb01SEd Maste }
97