Skip to content

Commit 2360fe6

Browse files
committed
Fix default wstring length
Signed-off-by: Anthony Welte <[email protected]>
1 parent 6c9bf0e commit 2360fe6

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

rosidl_generator_c/resource/full__description.c.em

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@{
33
from collections import OrderedDict
44
from rosidl_generator_c import escape_string
5+
from rosidl_generator_c import escape_utf8
56
from rosidl_generator_c import idl_structure_type_to_c_include_prefix
67
from rosidl_generator_c import type_hash_to_c_definition
78
from rosidl_parser.definition import NamespacedType
@@ -26,11 +27,6 @@ def static_seq(varname, values):
2627
return f'{{{varname}, {len(values)}, {len(values)}}}'
2728
return '{NULL, 0, 0}'
2829

29-
def utf8_encode(value_string):
30-
from rosidl_generator_c import escape_string
31-
# Slice removes the b'' from the representation.
32-
return escape_string(repr(value_string.encode('utf-8'))[2:-1])
33-
3430
implicit_type_names = set(td['type_description']['type_name'] for td, _ in implicit_type_descriptions)
3531
includes = OrderedDict()
3632
toplevel_msg, _ = toplevel_type_description
@@ -98,7 +94,7 @@ ref_tds = msg['referenced_type_descriptions']
9894
@[ for field in itype_description['fields']]@
9995
static char @(td_c_typename)__FIELD_NAME__@(field['name'])[] = "@(field['name'])";
10096
@[ if field['default_value']]@
101-
static char @(td_c_typename)__DEFAULT_VALUE__@(field['name'])[] = "@(utf8_encode(field['default_value']))";
97+
static char @(td_c_typename)__DEFAULT_VALUE__@(field['name'])[] = "@(escape_utf8(escape_string(field['default_value'])))";
10298
@[ end if]@
10399
@[ end for]@
104100

@@ -167,9 +163,9 @@ c_typename = typename_to_c(ref_td['type_name'])
167163
@[if raw_source_content]@
168164
static char toplevel_type_raw_source[] =@
169165
@[ for line in raw_source_content.splitlines()[:-1]]
170-
"@(utf8_encode(line))\n"@
166+
"@(escape_utf8(escape_string(line)))\n"@
171167
@[ end for]
172-
"@(utf8_encode(raw_source_content.splitlines()[-1]))";
168+
"@(escape_utf8(escape_string(raw_source_content.splitlines()[-1])))";
173169
@[end if]@
174170

175171
static char @(toplevel_encoding)_encoding[] = "@(toplevel_encoding)";

rosidl_generator_c/rosidl_generator_c/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ def escape_wstring(s):
229229
return escape_string(s)
230230

231231

232+
def escape_utf8(s):
233+
result = ''
234+
for c in s.encode('utf-8'):
235+
# ASCII characters are encoded as-is
236+
if c < 0x80:
237+
result += chr(c)
238+
# Non-ASCII characters are encoded as UTF-8
239+
else:
240+
result += '\\x%02x' % c
241+
return result
242+
243+
232244
def type_hash_to_c_definition(hash_string, *, indent=2):
233245
"""Generate empy for rosidl_type_hash_t instance with 8 bytes per line for readability."""
234246
bytes_per_row = 8

0 commit comments

Comments
 (0)