OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 """ Utility to remove comments from JSON files so that they can be parsed by | |
6 json.loads.""" | |
7 | |
8 def _ReadString(input, start, output): | |
9 output.append('"') | |
10 in_escape = False | |
11 for pos in xrange(start, len(input)): | |
12 output.append(input[pos]) | |
13 if in_escape: | |
14 in_escape = False | |
15 else: | |
16 if input[pos] == '\\': | |
17 in_escape = True | |
18 elif input[pos] == '"': | |
19 return pos + 1 | |
20 return pos | |
21 | |
22 def _ReadComment(input, start, output): | |
23 for pos in xrange(start, len(input)): | |
24 if input[pos] in ['\r', '\n']: | |
25 output.append(input[pos]) | |
26 return pos + 1 | |
27 return pos | |
28 | |
29 def Nom(input): | |
30 output = [] | |
31 pos = 0 | |
32 while pos < len(input): | |
33 if input[pos] == '"': | |
34 pos = _ReadString(input, pos + 1, output) | |
35 elif input[pos:pos+2] == '//': | |
36 pos = _ReadComment(input, pos + 2, output) | |
37 else: | |
38 output.append(input[pos]) | |
39 pos += 1 | |
40 return ''.join(output) | |
OLD | NEW |