Skip to content

Fix default wstring length #862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions rosidl_generator_c/resource/full__description.c.em
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@{
from collections import OrderedDict
from rosidl_generator_c import escape_string
from rosidl_generator_c import escape_utf8
from rosidl_generator_c import idl_structure_type_to_c_include_prefix
from rosidl_generator_c import type_hash_to_c_definition
from rosidl_parser.definition import NamespacedType
Expand All @@ -23,14 +24,13 @@ def static_seq_n(varname, n):
def static_seq(varname, values):
"""Statically define a runtime Sequence or String type."""
if values:
return f'{{{varname}, {len(values)}, {len(values)}}}'
if isinstance(values, str):
utf8_values = values.encode('utf-8')
return f'{{{varname}, {len(utf8_values)}, {len(utf8_values)}}}'
else:
return f'{{{varname}, {len(values)}, {len(values)}}}'
return '{NULL, 0, 0}'

def utf8_encode(value_string):
from rosidl_generator_c import escape_string
# Slice removes the b'' from the representation.
return escape_string(repr(value_string.encode('utf-8'))[2:-1])

implicit_type_names = set(td['type_description']['type_name'] for td, _ in implicit_type_descriptions)
includes = OrderedDict()
toplevel_msg, _ = toplevel_type_description
Expand Down Expand Up @@ -98,7 +98,7 @@ ref_tds = msg['referenced_type_descriptions']
@[ for field in itype_description['fields']]@
static char @(td_c_typename)__FIELD_NAME__@(field['name'])[] = "@(field['name'])";
@[ if field['default_value']]@
static char @(td_c_typename)__DEFAULT_VALUE__@(field['name'])[] = "@(utf8_encode(field['default_value']))";
static char @(td_c_typename)__DEFAULT_VALUE__@(field['name'])[] = "@(escape_utf8(escape_string(field['default_value'])))";
@[ end if]@
@[ end for]@

Expand Down Expand Up @@ -167,9 +167,9 @@ c_typename = typename_to_c(ref_td['type_name'])
@[if raw_source_content]@
static char toplevel_type_raw_source[] =@
@[ for line in raw_source_content.splitlines()[:-1]]
"@(utf8_encode(line))\n"@
"@(escape_utf8(escape_string(line)))\n"@
@[ end for]
"@(utf8_encode(raw_source_content.splitlines()[-1]))";
"@(escape_utf8(escape_string(raw_source_content.splitlines()[-1])))";
@[end if]@

static char @(toplevel_encoding)_encoding[] = "@(toplevel_encoding)";
Expand Down
12 changes: 12 additions & 0 deletions rosidl_generator_c/rosidl_generator_c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ def escape_wstring(s):
return escape_string(s)


def escape_utf8(s):
result = ''
for c in s.encode('utf-8'):
if c < 0x80:
# ASCII characters are encoded as-is
result += chr(c)
else:
# Non-ASCII characters are encoded as UTF-8
result += '\\x%02x' % c
return result


def type_hash_to_c_definition(hash_string, *, indent=2):
"""Generate empy for rosidl_type_hash_t instance with 8 bytes per line for readability."""
bytes_per_row = 8
Expand Down