1313 *
1414 */
1515
16- #include < stdlib.h>
16+ #include < cstdlib>
17+ #include < cassert>
1718
1819#include " cppvalue.h"
1920#include " constexp.h"
2021
21- CPPValue CPPValue::parseOctal (const std::string& token)
22+ CPPValue CPPValue::parseOctal (const std::string & token)
2223{
2324 long val = 0 ;
24- for (const char *p = token. c_str (); *p != 0 ; p++ )
25+ for (const char c : token)
2526 {
26- if (*p >= ' 0' && *p <= ' 7' ) val = val * 8 + *p - ' 0' ;
27+ if (c >= ' 0' && c <= ' 7' ) val = val * 8 + c - ' 0' ;
2728 }
2829 return CPPValue (val);
2930}
3031
31- CPPValue CPPValue::parseDecimal (const std::string& token)
32+ CPPValue CPPValue::parseDecimal (const std::string & token)
3233{
3334 long val = 0 ;
34- for (const char *p = token. c_str (); *p != 0 ; p++ )
35+ for (const char c : token)
3536 {
36- if (*p >= ' 0' && *p <= ' 9' ) val = val * 10 + *p - ' 0' ;
37+ if (c >= ' 0' && c <= ' 9' ) val = val * 10 + c - ' 0' ;
3738 }
3839 return CPPValue (val);
3940}
4041
41- CPPValue CPPValue::parseHexadecimal (const std::string& token)
42+ CPPValue CPPValue::parseHexadecimal (const std::string & token)
4243{
4344 long val = 0 ;
44- for (const char *p = token. c_str (); *p != 0 ; p++ )
45+ for (const char c : token)
4546 {
46- if (*p >= ' 0' && *p <= ' 9' ) val = val * 16 + *p - ' 0' ;
47- else if (*p >= ' a' && *p <= ' f' ) val = val * 16 + *p - ' a' + 10 ;
48- else if (*p >= ' A' && *p <= ' F' ) val = val * 16 + *p - ' A' + 10 ;
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 ;
4950 }
5051 // printf("parseHexadecimal %s->%x\n",qPrint(token),val);
5152 return CPPValue (val);
5253}
5354
54- CPPValue CPPValue::parseBinary (const std::string& token)
55+ CPPValue CPPValue::parseBinary (const std::string & token)
5556{
5657 long val = 0 ;
57- for (const char *p = token. c_str (); *p != 0 ; p++ )
58+ for (const char c : token)
5859 {
59- if (*p >= ' 0' && *p <= ' 1' ) val = val * 2 + *p - ' 0' ;
60+ if (c >= ' 0' && c <= ' 1' ) val = val * 2 + c - ' 0' ;
6061 }
6162 return CPPValue (val);
6263}
6364
64- CPPValue CPPValue::parseCharacter (const std::string& token) // does not work for '\n' and the alike
65+ CPPValue CPPValue::parseCharacter (const std::string & token) // does not work for '\n' and the alike
6566{
67+ assert (token.length ()>0 );
6668 if (token[1 ]==' \\ ' )
6769 {
70+ assert (token.length ()>1 );
6871 switch (token[2 ])
6972 {
7073 case ' n' : return CPPValue (' \n ' );
@@ -89,14 +92,14 @@ CPPValue CPPValue::parseCharacter(const std::string& token) // does not work for
8992 return parseOctal (token);
9093 case ' x' :
9194 case ' X' : return parseHexadecimal (token);
92- default : printf (" Invalid escape sequence %s found!\n " ,token.c_str ());
95+ default : printf (" Invalid escape sequence %s found!\n " ,std::string ( token) .c_str ());
9396 return CPPValue (0L );
9497 }
9598 }
9699 return CPPValue (token[1 ]);
97100}
98101
99- CPPValue CPPValue::parseFloat (const std::string& token)
102+ CPPValue CPPValue::parseFloat (const std::string & token)
100103{
101104 return CPPValue (std::stod (token));
102105}
0 commit comments