Skip to content

Commit 233d9a4

Browse files
committed
Tuning utils.printable_name.
- Fixed `printable_name('42FOO', code_style=True)` to yield '42 FOO' and not '42FOO'. '42Foo' already yielded '42 Foo'. - Small performance enhancements.
1 parent 97ba23b commit 233d9a4

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

src/robot/utils/misc.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,35 @@ def printable_name(string, code_style=False):
6767
parts = string.split()
6868
if not parts:
6969
return ''
70-
if code_style and len(parts) == 1:
71-
parts = _splitCamelCaseString(parts[0])
72-
return ' '.join(part[0].upper() + part[1:] for part in parts if part != '')
73-
74-
75-
def _splitCamelCaseString(string):
76-
parts = []
77-
current_part = []
78-
string = ' ' + string + ' ' # extra spaces make going through string easier
79-
for i in range(1, len(string)-1):
80-
# on 1st/last round prev/next is ' ' and char is 1st/last real char
81-
prev, char, next = string[i-1:i+2]
82-
if _isWordBoundary(prev, char, next):
83-
parts.append(''.join(current_part))
84-
current_part = [char]
70+
if code_style and len(parts) == 1 \
71+
and not (string.isalpha() and string.islower()):
72+
parts = _camelCaseSplit(list(string))
73+
return ' '.join(part[0].upper() + part[1:] for part in parts)
74+
75+
76+
def _camelCaseSplit(chars):
77+
token = []
78+
for prev, char, next in zip([''] + chars, chars, chars[1:] + ['']):
79+
if _isCamelCaseBoundary(prev, char, next):
80+
if token:
81+
yield ''.join(token)
82+
token = [char]
8583
else:
86-
current_part.append(char)
87-
parts.append(''.join(current_part)) # append last part
88-
return parts
84+
token.append(char)
85+
if token:
86+
yield ''.join(token)
8987

9088

91-
def _isWordBoundary(prev, char, next):
89+
def _isCamelCaseBoundary(prev, char, next):
90+
if prev.isdigit():
91+
return not char.isdigit()
92+
if not prev.isalpha():
93+
return False
9294
if char.isupper():
93-
return (prev.islower() or next.islower()) and prev.isalnum()
95+
return not (prev.isupper() and (not next or next.isupper()))
9496
if char.isdigit():
95-
return prev.isalpha()
96-
return prev.isdigit()
97+
return not prev.isdigit()
98+
return False
9799

98100

99101
def plural_or_not(item):

utest/utils/test_misc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def test_printable_name(self):
7575
('camelCaseName', 'CamelCaseName'),
7676
('with89numbers', 'With89numbers'),
7777
('with 89 numbers', 'With 89 Numbers'),
78+
('with 89_numbers', 'With 89_numbers'),
7879
('', '')]:
7980
assert_equal(printable_name(inp), exp)
8081

@@ -84,12 +85,17 @@ def test_printable_name_with_code_style(self):
8485
('under_score_name', 'Under Score Name'),
8586
('under_score and spaces', 'Under Score And Spaces'),
8687
('miXed_CAPS_nAMe', 'MiXed CAPS NAMe'),
88+
('with 89_numbers', 'With 89 Numbers'),
8789
('camelCaseName', 'Camel Case Name'),
90+
('mixedCAPSCamelName', 'Mixed CAPS Camel Name'),
8891
('camelCaseWithDigit1', 'Camel Case With Digit 1'),
92+
('teamX', 'Team X'),
8993
('name42WithNumbers666', 'Name 42 With Numbers 666'),
94+
('name42WITHNumbers666', 'Name 42 WITH Numbers 666'),
9095
('12more34numbers', '12 More 34 Numbers'),
91-
('mixedCAPSCamelName', 'Mixed CAPS Camel Name'),
9296
('foo-bar', 'Foo-bar'),
97+
('Foo-b:a;r!', 'Foo-b:a;r!'),
98+
('Foo-B:A;R!', 'Foo-B:A;R!'),
9399
('', '')]:
94100
assert_equal(printable_name(inp, code_style=True), exp)
95101

0 commit comments

Comments
 (0)