Skip to content

Commit 52876f4

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

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

rosidl_generator_c/resource/full__description.c.em

Lines changed: 9 additions & 9 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
@@ -23,14 +24,13 @@ def static_seq_n(varname, n):
2324
def static_seq(varname, values):
2425
"""Statically define a runtime Sequence or String type."""
2526
if values:
26-
return f'{{{varname}, {len(values)}, {len(values)}}}'
27+
if isinstance(values, str):
28+
utf8_values = values.encode('utf-8')
29+
return f'{{{varname}, {len(utf8_values)}, {len(utf8_values)}}}'
30+
else:
31+
return f'{{{varname}, {len(values)}, {len(values)}}}'
2732
return '{NULL, 0, 0}'
2833

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-
3434
implicit_type_names = set(td['type_description']['type_name'] for td, _ in implicit_type_descriptions)
3535
includes = OrderedDict()
3636
toplevel_msg, _ = toplevel_type_description
@@ -98,7 +98,7 @@ ref_tds = msg['referenced_type_descriptions']
9898
@[ for field in itype_description['fields']]@
9999
static char @(td_c_typename)__FIELD_NAME__@(field['name'])[] = "@(field['name'])";
100100
@[ if field['default_value']]@
101-
static char @(td_c_typename)__DEFAULT_VALUE__@(field['name'])[] = "@(utf8_encode(field['default_value']))";
101+
static char @(td_c_typename)__DEFAULT_VALUE__@(field['name'])[] = "@(escape_utf8(escape_string(field['default_value'])))";
102102
@[ end if]@
103103
@[ end for]@
104104

@@ -167,9 +167,9 @@ c_typename = typename_to_c(ref_td['type_name'])
167167
@[if raw_source_content]@
168168
static char toplevel_type_raw_source[] =@
169169
@[ for line in raw_source_content.splitlines()[:-1]]
170-
"@(utf8_encode(line))\n"@
170+
"@(escape_utf8(escape_string(line)))\n"@
171171
@[ end for]
172-
"@(utf8_encode(raw_source_content.splitlines()[-1]))";
172+
"@(escape_utf8(escape_string(raw_source_content.splitlines()[-1])))";
173173
@[end if]@
174174

175175
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+
if c < 0x80:
236+
# ASCII characters are encoded as-is
237+
result += chr(c)
238+
else:
239+
# Non-ASCII characters are encoded as UTF-8
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)