src
unescape.cc
Go to the documentation of this file.
1 #include "src/parse/unescape.h"
2 
3 namespace re2c {
4 
5 // expected characters: [0-9a-zA-Z]
6 static inline uint32_t hex_digit (const char c)
7 {
8  switch (c)
9  {
10  case '0': return 0;
11  case '1': return 1;
12  case '2': return 2;
13  case '3': return 3;
14  case '4': return 4;
15  case '5': return 5;
16  case '6': return 6;
17  case '7': return 7;
18  case '8': return 8;
19  case '9': return 9;
20  case 'a':
21  case 'A': return 0xA;
22  case 'b':
23  case 'B': return 0xB;
24  case 'c':
25  case 'C': return 0xC;
26  case 'd':
27  case 'D': return 0xD;
28  case 'e':
29  case 'E': return 0xE;
30  case 'f':
31  case 'F': return 0xF;
32  default: return ~0u; // unexpected
33  }
34 }
35 
36 // expected string format: "\" [xXuU] [0-9a-zA-Z]*
37 uint32_t unesc_hex (const char * s, const char * s_end)
38 {
39  uint32_t n = 0;
40  for (s += 2; s != s_end; ++s)
41  {
42  n <<= 4;
43  n += hex_digit (*s);
44  }
45  return n;
46 }
47 
48 // expected string format: "\" [0-7]*
49 uint32_t unesc_oct (const char * s, const char * s_end)
50 {
51  uint32_t n = 0;
52  for (++s; s != s_end; ++s)
53  {
54  n <<= 3;
55  n += static_cast<uint8_t> (*s - '0');
56  }
57  return n;
58 }
59 
60 } // namespace re2c
uint32_t unesc_oct(const char *s, const char *s_end)
Definition: unescape.cc:49
Definition: bitmap.cc:10
uint32_t unesc_hex(const char *s, const char *s_end)
Definition: unescape.cc:37
static uint32_t hex_digit(const char c)
Definition: unescape.cc:6