bux API Reference 1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
ScannerBase.cpp
Go to the documentation of this file.
1#include "ScannerBase.h"
2#include <charconv> // std::from_chars()
3
4namespace bux {
5
6std::string escseq2str(std::string s)
7{
8 for (size_t pos =0; pos < s.size();)
9 {
10 uint32_t c;
11 const size_t n = parseEscapeChar(s, c, pos);
12 if (n > 1)
13 {
14 const auto ucstr = to_utf8(c);
15 s.replace(pos, n, ucstr);
16 pos += ucstr.size();
17 }
18 else
19 ++pos;
20 }
21 return s;
22}
23
24bool isIdentifier(std::string_view s) noexcept
25{
26 const size_t end = skipIdentifier(s, 0);
27 return end && end == s.size();
28}
29
30size_t parseEscapeChar(std::string_view s, uint32_t &c, size_t pos)
31{
32 const size_t n = s.size();
33 if (pos >= n)
34 RUNTIME_ERROR("Pos {} passes end of string", pos);
35
36 auto i = pos;
37 c = static_cast<unsigned char>(s[i++]);
38 if (c == '\\' && i < n)
39 {
40 const unsigned char cc = static_cast<unsigned char>(s[i++]);
41 int radix;
42 switch (cc)
43 {
44 case 'a':
45 c = '\a';
46 break;
47 case 'b':
48 c = '\b';
49 break;
50 case 'f':
51 c = '\f';
52 break;
53 case 'n':
54 c = '\n';
55 break;
56 case 'r':
57 c = '\r';
58 break;
59 case 't':
60 c = '\t';
61 break;
62 case 'v':
63 c = '\v';
64 break;
65 case 'x':
66 case 'u':
67 case 'U':
68 radix = 16;
69 goto DecodeDigits;
70 default:
71 if (cc < '0' || '7' < cc)
72 goto WastedBackslash;
73 radix = 8;
74 --i;
75 DecodeDigits:
76 if (const auto start = s.data() + i; auto off = std::from_chars(start, s.data()+s.size(), c, radix).ptr - start)
77 {
78 i += size_t(off);
79 break;
80 }
81 WastedBackslash:
82 c = cc;
83 }
84 }
85 return i - pos;
86}
87
88size_t skipIdentifier(std::string_view s, size_t pos) noexcept
89{
90 for (bool first = true; pos < s.size(); ++pos, first = false)
91 {
92 auto c = s[pos];
93 if (!isascii(c) || c != '_' && !(first? isalpha(c): isalnum(c)))
94 break;
95 }
96 return pos;
97}
98
99} //namespace bux
#define RUNTIME_ERROR(fmtStr,...)
Wrap FILE(DATE)#__LINE__ FUNCTION: msg into std::runtime_error.
Definition XException.h:32
THE common namespace of bux library.
Definition AtomiX.cpp:3
std::string_view to_utf8(T_Utf32 uc)
bool isIdentifier(std::string_view s) noexcept
std::string escseq2str(std::string s)
size_t parseEscapeChar(std::string_view s, uint32_t &c, size_t pos)
size_t skipIdentifier(std::string_view s, size_t pos) noexcept