Chromium Code Reviews| Index: third_party/WebKit/Source/bindings/scripts/utilities.py |
| diff --git a/third_party/WebKit/Source/bindings/scripts/utilities.py b/third_party/WebKit/Source/bindings/scripts/utilities.py |
| index 2d812434f9be78697ee4a71d9e49fde892635fba..fe8429c2a262929d71176e8cde88a5f62f8a5dcb 100644 |
| --- a/third_party/WebKit/Source/bindings/scripts/utilities.py |
| +++ b/third_party/WebKit/Source/bindings/scripts/utilities.py |
| @@ -407,6 +407,43 @@ def shorten_union_name(union_type): |
| return name |
| +def format_remove_duplicates(text, patterns): |
| + """Removes duplicated line-basis patterns. |
| + |
| + Based on simple pattern matching, removes duplicated lines in a block |
| + of lines. Lines that match with a same pattern are considered as |
| + duplicates. |
| + |
| + Designed to be used as a filter function for Jinja2. |
| + |
| + Args: |
| + text: A str of multi-line text. |
| + patterns: A list of str where each str represents a simple |
| + pattern. The patterns are not considered as regexp, and |
| + exact match is applied. |
| + |
| + Returns: |
| + A formatted str with duplicates removed. |
| + """ |
| + pattern_founds = [False] * len(patterns) |
| + output = [] |
| + for line in text.split('\n'): |
| + for i, pattern in enumerate(patterns): |
| + if pattern not in line: |
| + continue |
| + if pattern_founds[i]: |
| + line = '' |
|
peria
2016/09/09 02:15:24
Do we have to add an empty line?
Yuki
2016/09/14 07:10:59
Done.
|
| + else: |
| + pattern_founds[i] = True |
| + output.append(line) |
| + |
| + # Let |'\n'.join| emit the last newline. |
| + if output: |
| + output.append('') |
| + |
| + return '\n'.join(output) |
| + |
| + |
| def format_blink_cpp_source_code(text): |
| """Formats C++ source code. |
| @@ -423,6 +460,7 @@ def format_blink_cpp_source_code(text): |
| Returns: |
| A formatted str of the source code. |
| """ |
| + re_empty_line = re.compile(r'^\s*$') |
| re_first_brace = re.compile(r'(?P<first>[{}])') |
| re_last_brace = re.compile(r'.*(?P<last>[{}]).*?$') |
| was_open_brace = True # Trick to remove the empty lines at the beginning. |
| @@ -430,7 +468,7 @@ def format_blink_cpp_source_code(text): |
| output = [] |
| for line in text.split('\n'): |
| # Skip empty lines. |
| - if not line: # empty line |
| + if re_empty_line.match(line): |
| was_empty_line = True |
| continue |