Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7107e8f

Browse files
committed
optimization of CPU intensive sanitizeAsciiString
1 parent 5396f13 commit 7107e8f

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

lib/core/common.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,10 +1027,19 @@ def safeStringFormat(formatStr, params):
10271027
return retVal
10281028

10291029
def sanitizeAsciiString(subject):
1030-
retVal = None
10311030
if subject:
1032-
retVal = "".join(char if ord(char) < 128 else '?' for char in subject)
1033-
return retVal
1031+
index = None
1032+
for i in xrange(len(subject)):
1033+
if ord(subject[i]) >= 128:
1034+
index = i
1035+
break
1036+
if not index:
1037+
return subject
1038+
else:
1039+
retVal = subject[:index] + "".join(subject[i] if ord(subject[i]) < 128 else '?' for i in xrange(index, len(subject)))
1040+
return retVal
1041+
else:
1042+
return None
10341043

10351044
def decloakToNamedTemporaryFile(filepath, name=None):
10361045
retVal = NamedTemporaryFile()

0 commit comments

Comments
 (0)