summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <ben.c.schubert@gmail.com>2019-07-15 17:47:28 +0100
committerbst-marge-bot <marge-bot@buildstream.build>2019-07-17 09:04:08 +0000
commitf3bdb4de710eccd021aaa5424860c88d03473aef (patch)
tree66fbb7c9f33746ada76cc10a0ab75ca3aa43b62a
parente8ddeef1ce55076a08106f4b4ffb074618f66b19 (diff)
downloadbuildstream-f3bdb4de710eccd021aaa5424860c88d03473aef.tar.gz
_utils: Refactor 'url_directory_name' for more efficient C operations
This reduces further the runtime of he method by more than 50%
-rw-r--r--src/buildstream/_utils.pyx24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/buildstream/_utils.pyx b/src/buildstream/_utils.pyx
index 797794eba..3a3a97017 100644
--- a/src/buildstream/_utils.pyx
+++ b/src/buildstream/_utils.pyx
@@ -22,10 +22,6 @@
This module contains utilities that have been optimized in Cython
"""
-import string
-
-cdef str VALID_DIRECTORY_CHARS = string.digits + string.ascii_letters + "%_"
-
def url_directory_name(str url):
"""Normalizes a url into a directory name
@@ -36,7 +32,21 @@ def url_directory_name(str url):
Returns:
A string which can be used as a directory name
"""
- def transl(x):
- return x if x in VALID_DIRECTORY_CHARS else '_'
+ return ''.join([_transl(x) for x in url])
+
+
+#############################################################
+# Module local helper Methods #
+#############################################################
- return ''.join([transl(x) for x in url]) \ No newline at end of file
+
+# _transl(x)
+#
+# Helper for `url_directory_name`
+#
+# This transforms the value to "_" if is it not a ascii letter, a digit or "%" or "_"
+#
+cdef Py_UNICODE _transl(Py_UNICODE x):
+ if ("a" <= x <= "z") or ("A" <= x <= "Z") or ("0" <= x <= "9") or x == "%":
+ return x
+ return "_"