Skip to content

Commit

Permalink
Small performance tuning printable_name and normalize.
Browse files Browse the repository at this point in the history
These utils show on top when profiling. Now they are a (very) little
faster.
  • Loading branch information
pekkaklarck committed Jan 19, 2017
1 parent 126d20d commit 94028e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
22 changes: 10 additions & 12 deletions src/robot/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,33 @@ def printable_name(string, code_style=False):
if code_style and '_' in string:
string = string.replace('_', ' ')
parts = string.split()
if not parts:
return ''
if code_style and len(parts) == 1 \
and not (string.isalpha() and string.islower()):
parts = _camelCaseSplit(list(parts[0]))
parts = _split_camel_case(parts[0])
return ' '.join(part[0].upper() + part[1:] for part in parts)


def _camelCaseSplit(chars):
def _split_camel_case(string):
tokens = []
token = []
for prev, char, next in zip([''] + chars, chars, chars[1:] + ['']):
if _isCamelCaseBoundary(prev, char, next):
for prev, char, next in zip(' ' + string, string, string[1:] + ' '):
if _is_camel_case_boundary(prev, char, next):
if token:
yield ''.join(token)
tokens.append(''.join(token))
token = [char]
else:
token.append(char)
if token:
yield ''.join(token)
tokens.append(''.join(token))
return tokens


def _isCamelCaseBoundary(prev, char, next):
def _is_camel_case_boundary(prev, char, next):
if prev.isdigit():
return not char.isdigit()
if char.isupper():
return next.islower() or prev.isalpha() and not prev.isupper()
if char.isdigit():
return not prev.isdigit()
return False
return char.isdigit()


def plural_or_not(item):
Expand Down
12 changes: 7 additions & 5 deletions src/robot/utils/normalizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys
from collections import MutableMapping

from .platform import PY3
from .platform import PY3, IRONPYTHON
from .robottypes import is_dict_like


Expand All @@ -35,14 +35,16 @@ def normalize(string, ignore=(), caseless=True, spaceless=True):
if caseless:
string = lower(string)
ignore = [lower(i) for i in ignore]
for ign in ignore:
if ign in string: # performance optimization
string = string.replace(ign, empty)
# both if statements below enhance performance a little
if ignore:
for ign in ignore:
if ign in string:
string = string.replace(ign, empty)
return string


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

0 comments on commit 94028e2

Please sign in to comment.