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. Ret
(raw_lines)
| 1980 | |
| 1981 | |
| 1982 | def CleanseRawStrings(raw_lines): |
| 1983 | """Removes C++11 raw strings from lines. |
| 1984 | |
| 1985 | Before: |
| 1986 | static const char kData[] = R"( |
| 1987 | multi-line string |
| 1988 | )"; |
| 1989 | |
| 1990 | After: |
| 1991 | static const char kData[] = "" |
| 1992 | (replaced by blank line) |
| 1993 | ""; |
| 1994 | |
| 1995 | Args: |
| 1996 | raw_lines: list of raw lines. |
| 1997 | |
| 1998 | Returns: |
| 1999 | list of lines with C++11 raw strings replaced by empty strings. |
| 2000 | """ |
| 2001 | |
| 2002 | delimiter = None |
| 2003 | lines_without_raw_strings = [] |
| 2004 | for line in raw_lines: |
| 2005 | if delimiter: |
| 2006 | # Inside a raw string, look for the end |
| 2007 | end = line.find(delimiter) |
| 2008 | if end >= 0: |
| 2009 | # Found the end of the string, match leading space for this |
| 2010 | # line and resume copying the original lines, and also insert |
| 2011 | # a "" on the last line. |
| 2012 | leading_space = re.match(r"^(\s*)\S", line) |
| 2013 | line = leading_space.group(1) + '""' + line[end + len(delimiter) :] |
| 2014 | delimiter = None |
| 2015 | else: |
| 2016 | # Haven't found the end yet, append a blank line. |
| 2017 | line = '""' |
| 2018 | |
| 2019 | # Look for beginning of a raw string, and replace them with |
| 2020 | # empty strings. This is done in a loop to handle multiple raw |
| 2021 | # strings on the same line. |
| 2022 | while delimiter is None: |
| 2023 | # Look for beginning of a raw string. |
| 2024 | # See 2.14.15 [lex.string] for syntax. |
| 2025 | # |
| 2026 | # Once we have matched a raw string, we check the prefix of the |
| 2027 | # line to make sure that the line is not part of a single line |
| 2028 | # comment. It's done this way because we remove raw strings |
| 2029 | # before removing comments as opposed to removing comments |
| 2030 | # before removing raw strings. This is because there are some |
| 2031 | # cpplint checks that requires the comments to be preserved, but |
| 2032 | # we don't want to check comments that are inside raw strings. |
| 2033 | matched = re.match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line) |
| 2034 | if matched and not re.match( |
| 2035 | r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//', matched.group(1) |
| 2036 | ): |
| 2037 | delimiter = ")" + matched.group(2) + '"' |
| 2038 | |
| 2039 | end = matched.group(3).find(delimiter) |