OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build
. | 5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build
. |
6 | 6 |
7 Design doc: http://www.chromium.org/developers/design-documents/idl-build | 7 Design doc: http://www.chromium.org/developers/design-documents/idl-build |
8 """ | 8 """ |
9 | 9 |
10 import os | 10 import os |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 idl_type = union_type | 400 idl_type = union_type |
401 if union_type.is_nullable: | 401 if union_type.is_nullable: |
402 idl_type = union_type.inner_type | 402 idl_type = union_type.inner_type |
403 name = idl_type.cpp_type or idl_type.name | 403 name = idl_type.cpp_type or idl_type.name |
404 alias = aliases.get(name) | 404 alias = aliases.get(name) |
405 if alias: | 405 if alias: |
406 return alias | 406 return alias |
407 return name | 407 return name |
408 | 408 |
409 | 409 |
| 410 def format_remove_duplicates(text, patterns): |
| 411 """Removes duplicated line-basis patterns. |
| 412 |
| 413 Based on simple pattern matching, removes duplicated lines in a block |
| 414 of lines. Lines that match with a same pattern are considered as |
| 415 duplicates. |
| 416 |
| 417 Designed to be used as a filter function for Jinja2. |
| 418 |
| 419 Args: |
| 420 text: A str of multi-line text. |
| 421 patterns: A list of str where each str represents a simple |
| 422 pattern. The patterns are not considered as regexp, and |
| 423 exact match is applied. |
| 424 |
| 425 Returns: |
| 426 A formatted str with duplicates removed. |
| 427 """ |
| 428 pattern_founds = [False] * len(patterns) |
| 429 output = [] |
| 430 for line in text.split('\n'): |
| 431 for i, pattern in enumerate(patterns): |
| 432 if pattern not in line: |
| 433 continue |
| 434 if pattern_founds[i]: |
| 435 line = '' |
| 436 else: |
| 437 pattern_founds[i] = True |
| 438 output.append(line) |
| 439 |
| 440 # Let |'\n'.join| emit the last newline. |
| 441 if output: |
| 442 output.append('') |
| 443 |
| 444 return '\n'.join(output) |
| 445 |
| 446 |
410 def format_blink_cpp_source_code(text): | 447 def format_blink_cpp_source_code(text): |
411 """Formats C++ source code. | 448 """Formats C++ source code. |
412 | 449 |
413 Supported modifications are: | 450 Supported modifications are: |
414 - Reduces successive empty lines into a single empty line. | 451 - Reduces successive empty lines into a single empty line. |
415 - Removes empty lines just after an open brace or before closing brace. | 452 - Removes empty lines just after an open brace or before closing brace. |
416 This rule does not apply to namespaces. | 453 This rule does not apply to namespaces. |
417 | 454 |
418 Designed to be used as a filter function for Jinja2. | 455 Designed to be used as a filter function for Jinja2. |
419 | 456 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 | 490 |
454 # Remember an open brace. | 491 # Remember an open brace. |
455 match = re_last_brace.search(line) | 492 match = re_last_brace.search(line) |
456 was_open_brace = (match and match.group('last') == '{' and 'namespace' n
ot in line) | 493 was_open_brace = (match and match.group('last') == '{' and 'namespace' n
ot in line) |
457 | 494 |
458 # Let |'\n'.join| emit the last newline. | 495 # Let |'\n'.join| emit the last newline. |
459 if output: | 496 if output: |
460 output.append('') | 497 output.append('') |
461 | 498 |
462 return '\n'.join(output) | 499 return '\n'.join(output) |
OLD | NEW |