Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
|
haraken
2017/02/09 01:00:11
2017
dglazkov
2017/02/09 04:15:28
Ooops! Fixed.
| |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 # pylint: disable=import-error,print-statement,relative-import | |
| 6 | |
| 7 import re | |
| 8 | |
| 9 SPECIAL_PREFIXES = [ | |
|
bashi
2017/02/09 00:15:07
Probably we should use v8_utilities.ACRONYMS (or m
dglazkov
2017/02/09 04:15:29
These are surprisingly intersecting, but not mutua
bashi
2017/02/09 04:20:45
I see. I'm fine with this then :)
| |
| 10 'WebGL', | |
| 11 'SVG', | |
| 12 ] | |
| 13 | |
| 14 MATCHING_EXPRESSION = '((?:[A-Z][a-z]+)|[0-9]D?$)' | |
| 15 | |
| 16 | |
| 17 class SmartTokenizer(object): | |
| 18 """Detects special cases that aren not easily discernible without additional | |
|
haraken
2017/02/09 01:00:11
are not
dglazkov
2017/02/09 04:15:29
Fixed.
| |
| 19 knowledge, such as recognizing that in SVGSVGElement, the first two SVGs | |
| 20 are separate tokens, but WebGL is one token.""" | |
| 21 | |
| 22 def __init__(self, name): | |
| 23 self.remaining = name | |
| 24 | |
| 25 def detect_special_prefix(self): | |
| 26 for prefix in SPECIAL_PREFIXES: | |
| 27 if self.remaining.startswith(prefix): | |
| 28 result = self.remaining[:len(prefix)] | |
| 29 self.remaining = self.remaining[len(prefix):] | |
| 30 return result | |
| 31 return None | |
| 32 | |
| 33 def tokenize(self): | |
| 34 prefix = self.detect_special_prefix() | |
| 35 return filter(None, | |
| 36 [prefix] + re.split(MATCHING_EXPRESSION, self.remaining)) | |
| 37 | |
| 38 | |
| 39 class NameStyleConverter(object): | |
|
haraken
2017/02/09 01:00:11
Add a comment and mention what hacker/pascal/macro
dglazkov
2017/02/09 04:15:28
Yep. Added comments and changed to snake_case/uppe
| |
| 40 """Converts names from camelCase and other styles to various other styles. | |
| 41 """ | |
| 42 | |
| 43 def __init__(self, name): | |
| 44 self.tokens = self.tokenize(name) | |
| 45 | |
| 46 def tokenize(self, name): | |
| 47 tokenizer = SmartTokenizer(name) | |
| 48 return tokenizer.tokenize() | |
| 49 | |
| 50 def to_hacker_case(self): | |
| 51 return '_'.join([token.lower() for token in self.tokens]) | |
| 52 | |
| 53 def to_pascal_case(self): | |
| 54 return ''.join([token[0].upper() + token[1:] for token in self.tokens]) | |
| 55 | |
| 56 def to_macro_case(self): | |
|
haraken
2017/02/09 01:00:11
Where is the macro case used?
dglazkov
2017/02/09 04:15:28
In the header guard.
| |
| 57 return '_'.join([token.upper() for token in self.tokens]) | |
| 58 | |
| 59 def to_all_cases(self): | |
| 60 return { | |
| 61 'hacker': self.to_hacker_case(), | |
| 62 'pascal': self.to_pascal_case(), | |
| 63 'macro': self.to_macro_case(), | |
|
bashi
2017/02/09 00:15:07
How about adding "_case" or "_style" prefix? When
dglazkov
2017/02/09 04:15:28
Fixed.
| |
| 64 } | |
| OLD | NEW |