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)
| 1935 | |
| 1936 | |
| 1937 | def CleanseRawStrings(raw_lines): |
| 1938 | """Removes C++11 raw strings from lines. |
| 1939 | |
| 1940 | Before: |
| 1941 | static const char kData[] = R"( |
| 1942 | multi-line string |
| 1943 | )"; |
| 1944 | |
| 1945 | After: |
| 1946 | static const char kData[] = "" |
| 1947 | (replaced by blank line) |
| 1948 | ""; |
| 1949 | |
| 1950 | Args: |
| 1951 | raw_lines: list of raw lines. |
| 1952 | |
| 1953 | Returns: |
| 1954 | list of lines with C++11 raw strings replaced by empty strings. |
| 1955 | """ |
| 1956 | |
| 1957 | delimiter = None |
| 1958 | lines_without_raw_strings = [] |
| 1959 | for line in raw_lines: |
| 1960 | if delimiter: |
| 1961 | # Inside a raw string, look for the end |
| 1962 | end = line.find(delimiter) |
| 1963 | if end >= 0: |
| 1964 | # Found the end of the string, match leading space for this |
| 1965 | # line and resume copying the original lines, and also insert |
| 1966 | # a "" on the last line. |
| 1967 | leading_space = re.match(r"^(\s*)\S", line) |
| 1968 | line = leading_space.group(1) + '""' + line[end + len(delimiter) :] |
| 1969 | delimiter = None |
| 1970 | else: |
| 1971 | # Haven't found the end yet, append a blank line. |
| 1972 | line = '""' |
| 1973 | |
| 1974 | # Look for beginning of a raw string, and replace them with |
| 1975 | # empty strings. This is done in a loop to handle multiple raw |
| 1976 | # strings on the same line. |
| 1977 | while delimiter is None: |
| 1978 | # Look for beginning of a raw string. |
| 1979 | # See 2.14.15 [lex.string] for syntax. |
| 1980 | # |
| 1981 | # Once we have matched a raw string, we check the prefix of the |
| 1982 | # line to make sure that the line is not part of a single line |
| 1983 | # comment. It's done this way because we remove raw strings |
| 1984 | # before removing comments as opposed to removing comments |
| 1985 | # before removing raw strings. This is because there are some |
| 1986 | # cpplint checks that requires the comments to be preserved, but |
| 1987 | # we don't want to check comments that are inside raw strings. |
| 1988 | matched = re.match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line) |
| 1989 | if matched and not re.match( |
| 1990 | r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//', matched.group(1) |
| 1991 | ): |
| 1992 | delimiter = ")" + matched.group(2) + '"' |
| 1993 | |
| 1994 | end = matched.group(3).find(delimiter) |