Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 A Deterministic acyclic finite state automaton (DAFSA) is a compact | 7 A Deterministic acyclic finite state automaton (DAFSA) is a compact |
| 8 representation of an unordered word list (dictionary). | 8 representation of an unordered word list (dictionary). |
| 9 | 9 |
| 10 http://en.wikipedia.org/wiki/Deterministic_acyclic_finite_state_automaton | 10 http://en.wikipedia.org/wiki/Deterministic_acyclic_finite_state_automaton |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 441 def parse_gperf(infile): | 441 def parse_gperf(infile): |
| 442 """Parses gperf file and extract strings and return code""" | 442 """Parses gperf file and extract strings and return code""" |
| 443 lines = [line.strip() for line in infile] | 443 lines = [line.strip() for line in infile] |
| 444 # Extract strings after the first '%%' and before the second '%%'. | 444 # Extract strings after the first '%%' and before the second '%%'. |
| 445 begin = lines.index('%%') + 1 | 445 begin = lines.index('%%') + 1 |
| 446 end = lines.index('%%', begin) | 446 end = lines.index('%%', begin) |
| 447 lines = lines[begin:end] | 447 lines = lines[begin:end] |
| 448 for line in lines: | 448 for line in lines: |
| 449 if line[-3:-1] != ', ': | 449 if line[-3:-1] != ', ': |
| 450 raise InputError('Expected "domainname, <digit>", found "%s"' % line) | 450 raise InputError('Expected "domainname, <digit>", found "%s"' % line) |
| 451 # Technically the DAFSA format could support return values in range [0-31], | 451 # Technically the DAFSA format can support return values in the range |
| 452 # but the values below are the only with a defined meaning. | 452 # [0-31], but only the first three bits have any defined meaning. |
| 453 if line[-1] not in '0124': | 453 if line[-1] not in map(str, range(0, 7)): |
|
M-A Ruel
2016/03/03 22:19:43
That's an relatively expensive call to do at every
| |
| 454 raise InputError('Expected value to be one of {0,1,2,4}, found "%s"' % | 454 raise InputError('Expected value to be in the range of 0-7, found "%s"' % |
| 455 line[-1]) | 455 line[-1]) |
| 456 return [line[:-3] + line[-1] for line in lines] | 456 return [line[:-3] + line[-1] for line in lines] |
| 457 | 457 |
| 458 | 458 |
| 459 def main(): | 459 def main(): |
| 460 if len(sys.argv) != 3: | 460 if len(sys.argv) != 3: |
| 461 print('usage: %s infile outfile' % sys.argv[0]) | 461 print('usage: %s infile outfile' % sys.argv[0]) |
| 462 return 1 | 462 return 1 |
| 463 with open(sys.argv[1], 'r') as infile, open(sys.argv[2], 'w') as outfile: | 463 with open(sys.argv[1], 'r') as infile, open(sys.argv[2], 'w') as outfile: |
| 464 outfile.write(words_to_cxx(parse_gperf(infile))) | 464 outfile.write(words_to_cxx(parse_gperf(infile))) |
| 465 return 0 | 465 return 0 |
| 466 | 466 |
| 467 | 467 |
| 468 if __name__ == '__main__': | 468 if __name__ == '__main__': |
| 469 sys.exit(main()) | 469 sys.exit(main()) |
| OLD | NEW |