guettli
guettli

Reputation: 27806

Make concat_lines_of( ) work for rawstring

I am converting some code. In the past string was used, now rawstring should get used.

This worked with string, but now fails with rawstring:

data my_table TYPE TABLE OF rawstring.
concat_lines_of( table = my_table sep = `, `)

How to make above lines work? In my case rawstring contains utf8 encoded data. The result should be a xstring (sequence of bytes)

It does not really matter here, but in Python this would look like this:

my_list = [my_byte_sequence1, my_byte_sequence2, my_byte_sequence3]
big_byte_sequence = b', '.join(my_list)

Upvotes: 3

Views: 1105

Answers (2)

Sandra Rossi
Sandra Rossi

Reputation: 13628

Given you have these inputs:

  • a table of byte strings containing characters encoded in a given code page
  • the name of the given code page
  • the separator character used to separate each line in the output

You want this output:

  • a string of bytes containing all lines joined and separated by the separator in the given code page

This can be achieved using this code:

TYPES ty_table TYPE STANDARD TABLE OF xstring WITH EMPTY KEY.
DATA: my_table   TYPE ty_table,
      my_xstring TYPE xstring.

my_table = VALUE #( ( CONV #( '01FF' ) ) ( CONV #( 'BEEF' ) ) ).
PERFORM proc USING my_table `, ` 'UTF-8' CHANGING my_xstring.
ASSERT my_xstring = '01FF2C20BEEF'.


FORM proc 
      USING
        itab       TYPE ty_table 
        sep        TYPE csequence 
        codepage   TYPE string
      CHANGING
        my_xstring TYPE xstring.

  DATA(xsep) = cl_abap_codepage=>convert_to( source = sep codepage = codepage ).

  my_xstring = REDUCE #(
      INIT aux TYPE xstring
      FOR <x> IN my_table
      NEXT aux = COND #( WHEN aux IS INITIAL THEN <x> ELSE |{ aux }{ xsep }{ <x> }| ) ).

ENDFORM.

Upvotes: 2

Sandra Rossi
Sandra Rossi

Reputation: 13628

The classic request is to just concatenate a table of bytes into a string of bytes, in that way adding a comma to identify the original lines is non-sense because it's impossible to store a comma as a character (alternative: encode it in a given code page).

If the question is just the classic request, then there's no equivalence of CONCAT_LINES_OF for the XSTRING type.

One workaround is to use REDUCE :

DATA my_table TYPE TABLE OF xstring.

my_table = VALUE #( ( CONV #( '01FF' ) ) ( CONV #( 'BEEF' ) ) ).

DATA(my_xstring) = REDUCE #(
      INIT aux TYPE xstring
      FOR <x> IN my_table
      NEXT aux = COND #( WHEN aux IS INITIAL THEN <x> ELSE aux && <x> ) ).

ASSERT my_xstring = '01FFBEEF'.

Upvotes: 5

Related Questions