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)
| 1763 | |
| 1764 | |
| 1765 | def CleanseRawStrings(raw_lines): |
| 1766 | """Removes C++11 raw strings from lines. |
| 1767 | |
| 1768 | Before: |
| 1769 | static const char kData[] = R"( |
| 1770 | multi-line string |
| 1771 | )"; |
| 1772 | |
| 1773 | After: |
| 1774 | static const char kData[] = "" |
| 1775 | (replaced by blank line) |
| 1776 | ""; |
| 1777 | |
| 1778 | Args: |
| 1779 | raw_lines: list of raw lines. |
| 1780 | |
| 1781 | Returns: |
| 1782 | list of lines with C++11 raw strings replaced by empty strings. |
| 1783 | """ |
| 1784 | |
| 1785 | delimiter = None |
| 1786 | lines_without_raw_strings = [] |
| 1787 | for line in raw_lines: |
| 1788 | if delimiter: |
| 1789 | # Inside a raw string, look for the end |
| 1790 | end = line.find(delimiter) |
| 1791 | if end >= 0: |
| 1792 | # Found the end of the string, match leading space for this |
| 1793 | # line and resume copying the original lines, and also insert |
| 1794 | # a "" on the last line. |
| 1795 | leading_space = Match(r'^(\s*)\S', line) |
| 1796 | line = leading_space.group(1) + '""' + line[end + len(delimiter):] |
| 1797 | delimiter = None |
| 1798 | else: |
| 1799 | # Haven't found the end yet, append a blank line. |
| 1800 | line = '""' |
| 1801 | |
| 1802 | # Look for beginning of a raw string, and replace them with |
| 1803 | # empty strings. This is done in a loop to handle multiple raw |
| 1804 | # strings on the same line. |
| 1805 | while delimiter is None: |
| 1806 | # Look for beginning of a raw string. |
| 1807 | # See 2.14.15 [lex.string] for syntax. |
| 1808 | # |
| 1809 | # Once we have matched a raw string, we check the prefix of the |
| 1810 | # line to make sure that the line is not part of a single line |
| 1811 | # comment. It's done this way because we remove raw strings |
| 1812 | # before removing comments as opposed to removing comments |
| 1813 | # before removing raw strings. This is because there are some |
| 1814 | # cpplint checks that requires the comments to be preserved, but |
| 1815 | # we don't want to check comments that are inside raw strings. |
| 1816 | matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line) |
| 1817 | if (matched and |
| 1818 | not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//', |
| 1819 | matched.group(1))): |
| 1820 | delimiter = ')' + matched.group(2) + '"' |
| 1821 | |
| 1822 | end = matched.group(3).find(delimiter) |