From 4ff9cf4e59b43fc4b751cea7df8c6f85599a0747 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Tue, 14 Mar 2023 23:02:50 +0100 Subject: [PATCH] Berry coc support unicode (#18195) --- lib/libesp32/berry/tools/coc/str_build.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/libesp32/berry/tools/coc/str_build.py b/lib/libesp32/berry/tools/coc/str_build.py index 82933abec..31caba1d3 100644 --- a/lib/libesp32/berry/tools/coc/str_build.py +++ b/lib/libesp32/berry/tools/coc/str_build.py @@ -1,6 +1,20 @@ import json from coc_string import * +# from https://stackoverflow.com/questions/14945095/how-to-escape-string-for-generated-c (simplified) +def escape_c(s, encoding='ascii'): + result = '' + for c in s: + if not (32 <= ord(c) < 127): + result += '\\%03o' % ord(c) + elif c == '\\': + result += "\\\\" + elif c == '"': + result += "\\\"" + else: + result += c + return '"' + result + '"' + class str_info: def __init__(self): self.hash = 0 @@ -91,7 +105,7 @@ class str_build: else: next = "NULL" istr += "be_define_const_str(" - istr += node + ", " + json.dumps(info.str) + ", " + istr += node + ", " + escape_c(info.str) + ", " istr += str(info.hash) + "u, " + str(info.extra) + ", " istr += str(len(info.str)) + ", " + next + ");\n" strings[info.str] = istr @@ -104,7 +118,7 @@ class str_build: ostr += "\n/* weak strings */\n" for k in self.str_weak: ostr += "be_define_const_str(" - ostr += escape_operator(k) + ", " + json.dumps(k) + ", " + ostr += escape_operator(k) + ", " + escape_c(k) + ", " ostr += "0u, 0, " + str(len(k)) + ", NULL);\n" ostr += "\n"