OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 """Return name with first letter or initial acronym uppercased. | 62 """Return name with first letter or initial acronym uppercased. |
63 The acronym must have a capital letter following it to be considered. | 63 The acronym must have a capital letter following it to be considered. |
64 """ | 64 """ |
65 for acronym in ACRONYMS: | 65 for acronym in ACRONYMS: |
66 if name.startswith(acronym.lower()): | 66 if name.startswith(acronym.lower()): |
67 if len(name) == len(acronym) or name[len(acronym)].isupper(): | 67 if len(name) == len(acronym) or name[len(acronym)].isupper(): |
68 return name.replace(acronym.lower(), acronym, 1) | 68 return name.replace(acronym.lower(), acronym, 1) |
69 return upper_first_letter(name) | 69 return upper_first_letter(name) |
70 | 70 |
71 | 71 |
| 72 def lower_first_letter(name): |
| 73 """Return name with first letter lowercased.""" |
| 74 if not name: |
| 75 return '' |
| 76 return name[0].lower() + name[1:] |
| 77 |
| 78 |
72 def upper_first_letter(name): | 79 def upper_first_letter(name): |
73 """Return name with first letter uppercased.""" | 80 """Return name with first letter uppercased.""" |
74 if not name: | 81 if not name: |
75 return '' | 82 return '' |
76 return name[0].upper() + name[1:] | 83 return name[0].upper() + name[1:] |
77 | 84 |
78 | 85 |
79 def camel_case(css_name): | |
80 """Convert hyphen-separated-name to UpperCamelCase. | |
81 | |
82 E.g., '-foo-bar' becomes 'FooBar'. | |
83 """ | |
84 return ''.join(upper_first_letter(word) for word in css_name.split('-')) | |
85 | |
86 | |
87 def to_macro_style(name): | 86 def to_macro_style(name): |
88 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) | 87 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) |
89 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper() | 88 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper() |
90 | 89 |
91 | 90 |
92 def script_name(entry): | 91 def script_name(entry): |
93 return os.path.basename(entry['name']) | 92 return os.path.basename(entry['name']) |
94 | 93 |
95 | 94 |
96 def cpp_bool(value): | 95 def cpp_bool(value): |
97 if value is True: | 96 if value is True: |
98 return 'true' | 97 return 'true' |
99 if value is False: | 98 if value is False: |
100 return 'false' | 99 return 'false' |
101 # Return value as is, which for example may be a platform-dependent constant | 100 # Return value as is, which for example may be a platform-dependent constant |
102 # such as "defaultSelectTrailingWhitespaceEnabled". | 101 # such as "defaultSelectTrailingWhitespaceEnabled". |
103 return value | 102 return value |
104 | 103 |
105 | 104 |
106 def cpp_name(entry): | 105 def cpp_name(entry): |
107 return entry['ImplementedAs'] or script_name(entry) | 106 return entry['ImplementedAs'] or script_name(entry) |
108 | 107 |
109 | 108 |
110 def enum_for_css_keyword(keyword): | 109 def enum_for_css_keyword(keyword): |
111 return 'CSSValue' + ''.join(camel_case(keyword)) | 110 return 'CSSValue' + upper_camel_case(keyword) |
112 | 111 |
113 | 112 |
114 def enum_for_css_property(property_name): | 113 def enum_for_css_property(property_name): |
115 return 'CSSProperty' + ''.join(camel_case(property_name)) | 114 return 'CSSProperty' + upper_camel_case(property_name) |
116 | 115 |
117 | 116 |
118 def enum_for_css_property_alias(property_name): | 117 def enum_for_css_property_alias(property_name): |
119 return 'CSSPropertyAlias' + camel_case(property_name) | 118 return 'CSSPropertyAlias' + upper_camel_case(property_name) |
| 119 |
| 120 # TODO(shend): Merge these with the above methods. |
| 121 # and update all the generators to use these ones. |
| 122 # TODO(shend): Switch external callers of these methods to use the high level |
| 123 # API below instead. |
| 124 |
| 125 |
| 126 def split_name(name): |
| 127 """Splits a name in some format to a list of words""" |
| 128 return re.findall(r'(?:[A-Z][a-z]*)|[a-z]+|(?:\d+[a-z]*)', upper_first_lette
r(name)) |
| 129 |
| 130 |
| 131 def upper_camel_case(name): |
| 132 return ''.join(upper_first_letter(word) for word in split_name(name)) |
| 133 |
| 134 |
| 135 def lower_camel_case(name): |
| 136 return lower_first_letter(upper_camel_case(name)) |
| 137 |
| 138 # Use these high level naming functions which describe the semantics of the name
, |
| 139 # rather than a particular style. |
| 140 |
| 141 |
| 142 def enum_type_name(name): |
| 143 return upper_camel_case(name) |
| 144 |
| 145 |
| 146 def enum_value_name(name): |
| 147 return 'k' + upper_camel_case(name) |
| 148 |
| 149 |
| 150 def class_member_name(name): |
| 151 return 'm_' + lower_camel_case(name) |
| 152 |
| 153 |
| 154 def method_name(name): |
| 155 return lower_camel_case(name) |
OLD | NEW |