Removes C++11 raw strings from lines. Before: static const char kData[] = R"( multi-line string )"; After: static const char kData[] = "" (replaced by blank line) ""; Args: raw_lines: list of raw lines. Returns: list of line
(raw_lines)
| 1064 | |
| 1065 | |
| 1066 | def CleanseRawStrings(raw_lines): |
| 1067 | """Removes C++11 raw strings from lines. |
| 1068 | |
| 1069 | Before: |
| 1070 | static const char kData[] = R"( |
| 1071 | multi-line string |
| 1072 | )"; |
| 1073 | |
| 1074 | After: |
| 1075 | static const char kData[] = "" |
| 1076 | (replaced by blank line) |
| 1077 | ""; |
| 1078 | |
| 1079 | Args: |
| 1080 | raw_lines: list of raw lines. |
| 1081 | |
| 1082 | Returns: |
| 1083 | list of lines with C++11 raw strings replaced by empty strings. |
| 1084 | """ |
| 1085 | |
| 1086 | delimiter = None |
| 1087 | lines_without_raw_strings = [] |
| 1088 | for line in raw_lines: |
| 1089 | if delimiter: |
| 1090 | # Inside a raw string, look for the end |
| 1091 | end = line.find(delimiter) |
| 1092 | if end >= 0: |
| 1093 | # Found the end of the string, match leading space for this |
| 1094 | # line and resume copying the original lines, and also insert |
| 1095 | # a "" on the last line. |
| 1096 | leading_space = Match(r'^(\s*)\S', line) |
| 1097 | line = leading_space.group(1) + '""' + line[end + len(delimiter):] |
| 1098 | delimiter = None |
| 1099 | else: |
| 1100 | # Haven't found the end yet, append a blank line. |
| 1101 | line = '' |
| 1102 | |
| 1103 | else: |
| 1104 | # Look for beginning of a raw string. |
| 1105 | # See 2.14.15 [lex.string] for syntax. |
| 1106 | matched = Match(r'^(.*)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line) |
| 1107 | if matched: |
| 1108 | delimiter = ')' + matched.group(2) + '"' |
| 1109 | |
| 1110 | end = matched.group(3).find(delimiter) |
| 1111 | if end >= 0: |
| 1112 | # Raw string ended on same line |
| 1113 | line = (matched.group(1) + '""' + |
| 1114 | matched.group(3)[end + len(delimiter):]) |
| 1115 | delimiter = None |
| 1116 | else: |
| 1117 | # Start of a multi-line raw string |
| 1118 | line = matched.group(1) + '""' |
| 1119 | |
| 1120 | lines_without_raw_strings.append(line) |
| 1121 | |
| 1122 | # TODO(unknown): if delimiter is not None here, we might want to |
| 1123 | # emit a warning for unterminated string. |