1 // go-encode-id.cc -- Go identifier encoding hooks
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #include "go-location.h"
8 #include "go-linemap.h"
9 #include "go-encode-id.h"
11 // Return whether the character c is OK to use in the assembler.
14 char_needs_encoding(char c
)
18 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
19 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
20 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
21 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
23 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
24 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
25 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
26 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
28 case '0': case '1': case '2': case '3': case '4':
29 case '5': case '6': case '7': case '8': case '9':
30 case '_': case '.': case '$': case '/':
37 // Return whether the identifier needs to be translated because it
38 // contains non-ASCII characters.
41 go_id_needs_encoding(const std::string
& str
)
43 for (std::string::const_iterator p
= str
.begin();
46 if (char_needs_encoding(*p
))
51 // Pull the next UTF-8 character out of P and store it in *PC. Return
52 // the number of bytes read.
55 fetch_utf8_char(const char* p
, unsigned int* pc
)
64 while ((c
& 0x80) != 0)
69 unsigned int rc
= *p
& ((1 << (7 - len
)) - 1);
70 for (size_t i
= 1; i
< len
; i
++)
72 unsigned int u
= p
[i
];
80 // Encode an identifier using ASCII characters.
83 go_encode_id(const std::string
&id
)
86 const char* p
= id
.c_str();
87 const char* pend
= p
+ id
.length();
91 size_t len
= fetch_utf8_char(p
, &c
);
92 if (len
== 1 && !char_needs_encoding(c
))
98 snprintf(buf
, sizeof buf
, "%x", c
);
108 go_selectively_encode_id(const std::string
&id
)
110 if (go_id_needs_encoding(id
))
111 return go_encode_id(id
);
112 return std::string();