From: Lionel Landwerlin Date: Thu, 30 Apr 2020 12:43:01 +0000 (+0300) Subject: genxml: factor out utility functions X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=942d4538a46c3420570ccf5c5f5677380c75caf0;p=mesa.git genxml: factor out utility functions v2: Use the regexp version (Jordan) Also fix regexp that missed the ' character replacement (Lionel) Signed-off-by: Lionel Landwerlin Reviewed-by: Jordan Justen Part-of: --- diff --git a/src/intel/genxml/gen_bits_header.py b/src/intel/genxml/gen_bits_header.py index 3b0f9ab2f3f..2bc956f39dd 100644 --- a/src/intel/genxml/gen_bits_header.py +++ b/src/intel/genxml/gen_bits_header.py @@ -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): diff --git a/src/intel/genxml/gen_pack_header.py b/src/intel/genxml/gen_pack_header.py index 2795e5905be..0d95c4e9dd2 100644 --- a/src/intel/genxml/gen_pack_header.py +++ b/src/intel/genxml/gen_pack_header.py @@ -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 index 00000000000..91ef622e5ae --- /dev/null +++ b/src/intel/genxml/util.py @@ -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