genxml: factor out utility functions
authorLionel Landwerlin <lionel.g.landwerlin@intel.com>
Thu, 30 Apr 2020 12:43:01 +0000 (15:43 +0300)
committerMarge Bot <eric+marge@anholt.net>
Sat, 9 May 2020 07:20:48 +0000 (07:20 +0000)
v2: Use the regexp version (Jordan)
    Also fix regexp that missed the ' character replacement (Lionel)

Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4938>

src/intel/genxml/gen_bits_header.py
src/intel/genxml/gen_pack_header.py
src/intel/genxml/util.py [new file with mode: 0644]

index 3b0f9ab2f3f72802d995d1c3a5060ca6ddb89de5..2bc956f39dd61c172a9355d7d62198dcad06bb52 100644 (file)
@@ -25,10 +25,10 @@ from __future__ import (
 
 import argparse
 import os
-import re
 import xml.parsers.expat
 
 from mako.template import Template
+from util import *
 
 TEMPLATE = Template("""\
 <%!
@@ -132,17 +132,6 @@ ${emit_per_gen_prop_func(field, 'start')}
 
 #endif /* ${guard} */""", output_encoding='utf-8')
 
-alphanum_nono = re.compile(r'[ /\[\]()\-:.,=>#&*"+\\]+')
-def to_alphanum(name):
-    global alphanum_nono
-    return alphanum_nono.sub('', name)
-
-def safe_name(name):
-    name = to_alphanum(name)
-    if not name[0].isalpha():
-        name = '_' + name
-    return name
-
 class Gen(object):
 
     def __init__(self, z):
index 2795e5905befd9f3a78186eb9f77ff145ab26339..0d95c4e9dd233cb330063a5245cf6db90e5acf99 100644 (file)
@@ -10,6 +10,7 @@ import re
 import sys
 import copy
 import textwrap
+from util import *
 
 license =  """/*
  * Copyright (C) 2016 Intel Corporation
@@ -182,40 +183,6 @@ __gen_ufixed(float v, uint32_t start, NDEBUG_UNUSED uint32_t end, uint32_t fract
 
 """
 
-def to_alphanum(name):
-    substitutions = {
-        ' ': '',
-        '/': '',
-        '[': '',
-        ']': '',
-        '(': '',
-        ')': '',
-        '-': '',
-        ':': '',
-        '.': '',
-        ',': '',
-        '=': '',
-        '>': '',
-        '#': '',
-        '&': '',
-        '*': '',
-        '"': '',
-        '+': '',
-        '\'': '',
-    }
-
-    for i, j in substitutions.items():
-        name = name.replace(i, j)
-
-    return name
-
-def safe_name(name):
-    name = to_alphanum(name)
-    if not name[0].isalpha():
-        name = '_' + name
-
-    return name
-
 def num_from_str(num_str):
     if num_str.lower().startswith('0x'):
         return int(num_str, base=16)
diff --git a/src/intel/genxml/util.py b/src/intel/genxml/util.py
new file mode 100644 (file)
index 0000000..91ef622
--- /dev/null
@@ -0,0 +1,38 @@
+#encoding=utf-8
+#
+# Copyright © 2020 Intel Corporation
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+#
+
+# A few utility functions reused across genxml scripts
+
+import re
+
+alphanum_nono = re.compile(r'[ /\[\]()\-:.,=>#&*\'"+\\]+')
+def to_alphanum(name):
+    global alphanum_nono
+    return alphanum_nono.sub('', name)
+
+def safe_name(name):
+    name = to_alphanum(name)
+    if not name[0].isalpha():
+        name = '_' + name
+    return name