Skip to content

Commit 94028e2

Browse files
committed
Small performance tuning printable_name and normalize.
These utils show on top when profiling. Now they are a (very) little faster.
1 parent 126d20d commit 94028e2

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

src/robot/utils/misc.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,33 @@ def printable_name(string, code_style=False):
6666
if code_style and '_' in string:
6767
string = string.replace('_', ' ')
6868
parts = string.split()
69-
if not parts:
70-
return ''
7169
if code_style and len(parts) == 1 \
7270
and not (string.isalpha() and string.islower()):
73-
parts = _camelCaseSplit(list(parts[0]))
71+
parts = _split_camel_case(parts[0])
7472
return ' '.join(part[0].upper() + part[1:] for part in parts)
7573

7674

77-
def _camelCaseSplit(chars):
75+
def _split_camel_case(string):
76+
tokens = []
7877
token = []
79-
for prev, char, next in zip([''] + chars, chars, chars[1:] + ['']):
80-
if _isCamelCaseBoundary(prev, char, next):
78+
for prev, char, next in zip(' ' + string, string, string[1:] + ' '):
79+
if _is_camel_case_boundary(prev, char, next):
8180
if token:
82-
yield ''.join(token)
81+
tokens.append(''.join(token))
8382
token = [char]
8483
else:
8584
token.append(char)
8685
if token:
87-
yield ''.join(token)
86+
tokens.append(''.join(token))
87+
return tokens
8888

8989

90-
def _isCamelCaseBoundary(prev, char, next):
90+
def _is_camel_case_boundary(prev, char, next):
9191
if prev.isdigit():
9292
return not char.isdigit()
9393
if char.isupper():
9494
return next.islower() or prev.isalpha() and not prev.isupper()
95-
if char.isdigit():
96-
return not prev.isdigit()
97-
return False
95+
return char.isdigit()
9896

9997

10098
def plural_or_not(item):

src/robot/utils/normalizing.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sys
1717
from collections import MutableMapping
1818

19-
from .platform import PY3
19+
from .platform import PY3, IRONPYTHON
2020
from .robottypes import is_dict_like
2121

2222

@@ -35,14 +35,16 @@ def normalize(string, ignore=(), caseless=True, spaceless=True):
3535
if caseless:
3636
string = lower(string)
3737
ignore = [lower(i) for i in ignore]
38-
for ign in ignore:
39-
if ign in string: # performance optimization
40-
string = string.replace(ign, empty)
38+
# both if statements below enhance performance a little
39+
if ignore:
40+
for ign in ignore:
41+
if ign in string:
42+
string = string.replace(ign, empty)
4143
return string
4244

4345

4446
# http://ironpython.codeplex.com/workitem/33133
45-
if sys.platform == 'cli' and sys.version_info < (2, 7, 5):
47+
if IRONPYTHON and sys.version_info < (2, 7, 5):
4648
def lower(string):
4749
return ('A' + string).lower()[1:]
4850
else:

0 commit comments

Comments
 (0)