2 #include <c10/util/Optional.h> 3 #include <torch/csrc/jit/script/error_report.h> 4 #include <torch/csrc/jit/script/lexer.h> 10 inline bool isCharCount(
char c,
const std::string& str,
size_t start,
int len) {
12 return start + len <= str.size() &&
13 std::count(str.begin() + start, str.begin() + start + len, c) == len;
16 inline static bool isOctal(
char c) {
17 return c >=
'0' && c <
'8';
22 if (pos + 3 >= str.size())
25 for (
size_t i = 1, b = 64; i < 4; ++i, b /= 8) {
27 if (d < '0' || d >
'7')
36 inline std::string parseStringLiteral(
37 const SourceRange& range,
38 const std::string& str) {
39 int quote_len = isCharCount(str[0], str, 0, 3) ? 3 : 1;
40 auto ret_str = str.substr(quote_len, str.size() - quote_len * 2);
41 size_t pos = ret_str.find(
'\\');
42 while (pos != std::string::npos) {
44 char c = ret_str[pos + 1];
46 switch (ret_str[pos + 1]) {
71 throw ErrorReport(range) <<
"unsupported hex specifier";
74 if (
auto v = parseOctal(str, pos + 1)) {
78 throw ErrorReport(range) <<
" ill formed octal specifier";
81 ret_str.replace(pos, to_erase, 1, c);
82 pos = ret_str.find(
'\\', pos + 1);