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

Unified Diff: third_party/WebKit/Source/bindings/scripts/name_style_converter.py

Issue 2684773003: [WebAgent API]: Implement name style handling. (Closed)
Patch Set: Fixed the deps. Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/scripts/name_style_converter.py
diff --git a/third_party/WebKit/Source/bindings/scripts/name_style_converter.py b/third_party/WebKit/Source/bindings/scripts/name_style_converter.py
new file mode 100644
index 0000000000000000000000000000000000000000..5a8b3ce67be1adcaa74fc60e00d86321dc06bb36
--- /dev/null
+++ b/third_party/WebKit/Source/bindings/scripts/name_style_converter.py
@@ -0,0 +1,64 @@
+# 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.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# pylint: disable=import-error,print-statement,relative-import
+
+import re
+
+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 :)
+ 'WebGL',
+ 'SVG',
+]
+
+MATCHING_EXPRESSION = '((?:[A-Z][a-z]+)|[0-9]D?$)'
+
+
+class SmartTokenizer(object):
+ """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.
+ knowledge, such as recognizing that in SVGSVGElement, the first two SVGs
+ are separate tokens, but WebGL is one token."""
+
+ def __init__(self, name):
+ self.remaining = name
+
+ def detect_special_prefix(self):
+ for prefix in SPECIAL_PREFIXES:
+ if self.remaining.startswith(prefix):
+ result = self.remaining[:len(prefix)]
+ self.remaining = self.remaining[len(prefix):]
+ return result
+ return None
+
+ def tokenize(self):
+ prefix = self.detect_special_prefix()
+ return filter(None,
+ [prefix] + re.split(MATCHING_EXPRESSION, self.remaining))
+
+
+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
+ """Converts names from camelCase and other styles to various other styles.
+ """
+
+ def __init__(self, name):
+ self.tokens = self.tokenize(name)
+
+ def tokenize(self, name):
+ tokenizer = SmartTokenizer(name)
+ return tokenizer.tokenize()
+
+ def to_hacker_case(self):
+ return '_'.join([token.lower() for token in self.tokens])
+
+ def to_pascal_case(self):
+ return ''.join([token[0].upper() + token[1:] for token in self.tokens])
+
+ 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.
+ return '_'.join([token.upper() for token in self.tokens])
+
+ def to_all_cases(self):
+ return {
+ 'hacker': self.to_hacker_case(),
+ 'pascal': self.to_pascal_case(),
+ '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.
+ }

Powered by Google App Engine
This is Rietveld 408576698