scripts/dts/gen_defines.py: generalize string escape
Add str2str to make the conversion of a string into a C literal with all necessary escapes and enclosing double quotes available outside a function that emits a define. Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
parent
b914ba8831
commit
e9a173120f
1 changed files with 12 additions and 7 deletions
|
@ -597,18 +597,16 @@ def out_dev(dev, ident, val, name_alias=None):
|
|||
|
||||
|
||||
def out_dev_s(dev, ident, s):
|
||||
# Like out_dev(), but puts quotes around 's' and escapes any double quotes
|
||||
# and backslashes within it
|
||||
# Like out_dev(), but emits 's' as a string literal
|
||||
|
||||
# \ must be escaped before " to avoid double escaping
|
||||
out_dev(dev, ident, '"{}"'.format(escape(s)))
|
||||
out_dev(dev, ident, quote_str(s))
|
||||
|
||||
|
||||
def out_s(ident, val):
|
||||
# Like out(), but puts quotes around 's' and escapes any double quotes and
|
||||
# backslashes within it
|
||||
# Like out(), but puts quotes around 'val' and escapes any double
|
||||
# quotes and backslashes within it
|
||||
|
||||
out(ident, '"{}"'.format(escape(val)))
|
||||
out(ident, quote_str(val))
|
||||
|
||||
|
||||
def out(ident, val, aliases=()):
|
||||
|
@ -652,6 +650,13 @@ def escape(s):
|
|||
return s.replace("\\", "\\\\").replace('"', '\\"')
|
||||
|
||||
|
||||
def quote_str(s):
|
||||
# Puts quotes around 's' and escapes any double quotes and
|
||||
# backslashes within it
|
||||
|
||||
return '"{}"'.format(escape(s))
|
||||
|
||||
|
||||
def err(s):
|
||||
raise Exception(s)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue