Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: tools/json_comment_eater/json_comment_eater.py

Issue 2797253011: Improve json_comment_eater.py performance. (Closed)
Patch Set: Improve JSON comment eater performance. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 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 '''Utility to remove comments from JSON files so that they can be parsed by 6 '''Utility to remove comments from JSON files so that they can be parsed by
7 json.loads. 7 json.loads.
8 ''' 8 '''
9 9
10 import sys 10 import sys
11 11
12 12
13 def _Rcount(string, chars): 13 def _Rcount(string, chars):
14 '''Returns the number of consecutive characters from |chars| that occur at the 14 '''Returns the number of consecutive characters from |chars| that occur at the
15 end of |string|. 15 end of |string|.
16 ''' 16 '''
17 return len(string) - len(string.rstrip(chars)) 17 return len(string) - len(string.rstrip(chars))
18 18
19 19
20 def _FindNextToken(string, tokens, start): 20 def _FindNextToken(string, tokens, start):
21 '''Finds the next token in |tokens| that occurs in |string| from |start|. 21 '''Finds the next token in |tokens| that occurs in |string| from |start|.
22 Returns a tuple (index, token key). 22 Returns a tuple (index, token key).
23 ''' 23 '''
24 min_index, min_key = (-1, None) 24 for index, item in enumerate(string, start):
25 for k in tokens: 25 for k in tokens:
26 index = string.find(k, start) 26 if (string[index:index + len(k)] == k):
27 if index != -1 and (min_index == -1 or index < min_index): 27 return (index, k)
28 min_index, min_key = (index, k)
29 return (min_index, min_key)
30 28
29 return (-1, None)
31 30
32 def _ReadString(input, start, output): 31 def _ReadString(input, start, output):
33 output.append('"') 32 output.append('"')
34 start_range, end_range = (start, input.find('"', start)) 33 start_range, end_range = (start, input.find('"', start))
35 # \" escapes the ", \\" doesn't, \\\" does, etc. 34 # \" escapes the ", \\" doesn't, \\\" does, etc.
36 while (end_range != -1 and 35 while (end_range != -1 and
37 _Rcount(input[start_range:end_range], '\\') % 2 == 1): 36 _Rcount(input[start_range:end_range], '\\') % 2 == 1):
38 start_range, end_range = (end_range, input.find('"', end_range + 1)) 37 start_range, end_range = (end_range, input.find('"', end_range + 1))
39 if end_range == -1: 38 if end_range == -1:
40 return start_range + 1 39 return start_range + 1
(...skipping 29 matching lines...) Expand all
70 if token is None: 69 if token is None:
71 output.append(input[pos:]) 70 output.append(input[pos:])
72 break 71 break
73 output.append(input[pos:token_index]) 72 output.append(input[pos:token_index])
74 pos = token_actions[token](input, token_index + len(token), output) 73 pos = token_actions[token](input, token_index + len(token), output)
75 return ''.join(output) 74 return ''.join(output)
76 75
77 76
78 if __name__ == '__main__': 77 if __name__ == '__main__':
79 sys.stdout.write(Nom(sys.stdin.read())) 78 sys.stdout.write(Nom(sys.stdin.read()))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698