From 727fe0994cf7cc8dfd7f9543d131fdb05cd0c5b2 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 13 Nov 2014 15:54:29 -0500 Subject: [PATCH] Fix string write --- barectf/cli.py | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/barectf/cli.py b/barectf/cli.py index 7c98d83..7050d86 100644 --- a/barectf/cli.py +++ b/barectf/cli.py @@ -1126,33 +1126,27 @@ class BarectfCodeGenerator: def _write_field_string(self, fname, src_name, string, scope_prefix=None): clines = [] - # string index variable declaration - iv = 'is_{}'.format(fname) - clines.append(_CLine('uint32_t {};'.format(iv))) - - # for loop; loop until the end of the source string is reached - fmt = "for ({iv} = 0; {src}[{iv}] != '\\0'; ++{iv}, {ctxat} += 8) {{" - line = fmt.format(iv=iv, src=src_name, ctxat=self._CTX_AT) + # get string length + sz_bytes_varname = 'slen_bytes_{}'.format(fname) + line = 'size_t {} = strlen({}) + 1;'.format(sz_bytes_varname, src_name) clines.append(_CLine(line)) - # for loop statements - for_block = _CBlock() - # check offset overflow - for_block.append(self._get_chk_offset_v_cline(8)) - - # write byte to the buffer - fmt = '{dst} = {src}[{iv}];' - line = fmt.format(dst=self._CTX_BUF_AT, iv=iv, src=src_name) - for_block.append(_CLine(line)) + sz_bits_varname = 'slen_bits_{}'.format(fname) + line = 'size_t {} = ({} << 3);'.format(sz_bits_varname, + sz_bytes_varname) + clines.append(_CLine(line)) + cline = self._get_chk_offset_v_cline(sz_bits_varname) + clines.append(cline) - # append for loop - clines.append(for_block) - clines.append(_CLine('}')) + # memcpy() + dst = self._CTX_BUF_AT_ADDR + line = 'memcpy({}, {}, {});'.format(dst, src_name, sz_bytes_varname) + clines.append(_CLine(line)) - # write NULL character to the buffer - clines.append(_CLine("{} = '\\0';".format(self._CTX_BUF_AT))) - clines.append(_CLine('{} += 8;'.format(self._CTX_AT))) + # update bit position + line = '{} += {};'.format(self._CTX_AT, sz_bits_varname) + clines.append(_CLine(line)) return clines -- 2.34.1