Doxygen
Toggle main menu visibility
Loading...
Searching...
No Matches
cppvalue.cpp
Go to the documentation of this file.
1
/******************************************************************************
2
*
3
* Copyright (C) 1997-2021 by Dimitri van Heesch.
4
*
5
* Permission to use, copy, modify, and distribute this software and its
6
* documentation under the terms of the GNU General Public License is hereby
7
* granted. No representations are made about the suitability of this software
8
* for any purpose. It is provided "as is" without express or implied warranty.
9
* See the GNU General Public License for more details.
10
*
11
* Documents produced by Doxygen are derivative works derived from the
12
* input used in their production; they are not affected by this license.
13
*
14
*/
15
16
#include <cstdlib>
17
#include <cassert>
18
19
#include "
cppvalue.h
"
20
#include "
constexp.h
"
21
22
CPPValue
CPPValue::parseOctal
(
const
std::string &token)
23
{
24
long
val = 0;
25
for
(
const
char
c : token)
26
{
27
if
(c >=
'0'
&& c <=
'7'
) val = val * 8 + c -
'0'
;
28
}
29
return
CPPValue
(val);
30
}
31
32
CPPValue
CPPValue::parseDecimal
(
const
std::string &token)
33
{
34
long
val = 0;
35
for
(
const
char
c : token)
36
{
37
if
(c >=
'0'
&& c <=
'9'
) val = val * 10 + c -
'0'
;
38
}
39
return
CPPValue
(val);
40
}
41
42
CPPValue
CPPValue::parseHexadecimal
(
const
std::string &token)
43
{
44
long
val = 0;
45
for
(
const
char
c : token)
46
{
47
if
(c >=
'0'
&& c <=
'9'
) val = val * 16 + c -
'0'
;
48
else
if
(c >=
'a'
&& c <=
'f'
) val = val * 16 + c -
'a'
+ 10;
49
else
if
(c >=
'A'
&& c <=
'F'
) val = val * 16 + c -
'A'
+ 10;
50
}
51
//printf("parseHexadecimal %s->%x\n",qPrint(token),val);
52
return
CPPValue
(val);
53
}
54
55
CPPValue
CPPValue::parseBinary
(
const
std::string &token)
56
{
57
long
val = 0;
58
for
(
const
char
c : token)
59
{
60
if
(c >=
'0'
&& c <=
'1'
) val = val * 2 + c -
'0'
;
61
}
62
return
CPPValue
(val);
63
}
64
65
CPPValue
CPPValue::parseCharacter
(
const
std::string &token)
// does not work for '\n' and the alike
66
{
67
assert(token.length()>0);
68
if
(token[1]==
'\\'
)
69
{
70
assert(token.length()>1);
71
switch
(token[2])
72
{
73
case
'n'
:
return
CPPValue
(
'\n'
);
74
case
't'
:
return
CPPValue
(
'\t'
);
75
case
'v'
:
return
CPPValue
(
'\v'
);
76
case
'b'
:
return
CPPValue
(
'\b'
);
77
case
'r'
:
return
CPPValue
(
'\r'
);
78
case
'f'
:
return
CPPValue
(
'\f'
);
79
case
'a'
:
return
CPPValue
(
'\a'
);
80
case
'\\'
:
return
CPPValue
(
'\\'
);
81
case
'?'
:
return
CPPValue
(
'\?'
);
82
case
'\''
:
return
CPPValue
(
'\''
);
83
case
'"'
:
return
CPPValue
(
'"'
);
84
case
'0'
:
// fall through
85
case
'1'
:
// fall through
86
case
'2'
:
// fall through
87
case
'3'
:
// fall through
88
case
'4'
:
// fall through
89
case
'5'
:
// fall through
90
case
'6'
:
// fall through
91
case
'7'
:
// fall through
92
return
parseOctal
(token);
93
case
'x'
:
94
case
'X'
:
return
parseHexadecimal
(token);
95
default
: printf(
"Invalid escape sequence %s found!\n"
,std::string(token).c_str());
96
return
CPPValue
(0L);
97
}
98
}
99
return
CPPValue
(token[1]);
100
}
101
102
CPPValue
CPPValue::parseFloat
(
const
std::string &token)
103
{
104
return
CPPValue
(std::stod(token));
105
}
CPPValue::CPPValue
constexpr CPPValue(char c) noexcept
Definition
cppvalue.h:28
CPPValue::parseOctal
static CPPValue parseOctal(const std::string &token)
Definition
cppvalue.cpp:22
CPPValue::parseDecimal
static CPPValue parseDecimal(const std::string &token)
Definition
cppvalue.cpp:32
CPPValue::parseHexadecimal
static CPPValue parseHexadecimal(const std::string &token)
Definition
cppvalue.cpp:42
CPPValue::parseCharacter
static CPPValue parseCharacter(const std::string &token)
Definition
cppvalue.cpp:65
CPPValue::parseFloat
static CPPValue parseFloat(const std::string &token)
Definition
cppvalue.cpp:102
CPPValue::parseBinary
static CPPValue parseBinary(const std::string &token)
Definition
cppvalue.cpp:55
constexp.h
cppvalue.h
src
cppvalue.cpp
Generated by
1.17.0