Skip to content
Merged
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
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Legend
+ : added
- : removed

v2.5.1, 2025-?
------------------
+ new parameter skip_lines_starting_with to allow skipping "commented" lines
+ string i_renames now also support `,` delimiter

v2.5.0, 2025-06-10
------------------
! BREAKING: move types to zif_text2tab interface #27
Expand Down
25 changes: 23 additions & 2 deletions docsite/docs/03-parsing-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ zcl_text2tab_parser=>create( lt_container )->parse(
* JOHN 02.02.1995
```

Renames can be also passed as a string in this format: `'field_to_rename:new_name;another_field:another_new_name'`. Coding convenience is important ;)
Renames can be also passed as a string in this format: `'field_to_rename:new_name, another_field:another_new_name'`. `;` and `,` are supported as separators. Coding convenience is important ;)

```abap
zcl_text2tab_parser=>create( lt_container )->parse(
Expand Down Expand Up @@ -134,7 +134,9 @@ JOHN 01.01.1990
Now we should call the factory method like this and the first line is interpreted as a comment:

```abap
zcl_text2tab_parser=>create( i_pattern = ls_birthday i_begin_comment = '*' ).
zcl_text2tab_parser=>create(
i_pattern = ls_birthday
i_begin_comment = '*' ).
```

The char '*' must have the first position in the text line. Otherwise it isn't interpreted as a comment.
Expand All @@ -146,3 +148,22 @@ Name Date of birth <<< containes spaces
NAME BIRTHDAY
JOHN 01.01.1990
```

### Within-data comments

Also the data can contain commented lines. They can be skipped if started with a char passed with `i_skip_lines_starting_with` param. This can be useful for human-friendly data grouping.

```abap
zcl_text2tab_parser=>create(
i_pattern = ls_birthday
i_skip_lines_starting_with = '#' ).
```

```text
NAME BIRTHDAY
# Managers <<< will be ignored
John 01.01.1990
# Employees <<< will be ignored
Anna 01.01.1990
```

7 changes: 7 additions & 0 deletions src/core/zcl_text2tab_parser.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class zcl_text2tab_parser definition
!i_amount_format type zif_text2tab=>ty_amount_format optional
!i_date_format type zif_text2tab=>ty_date_format optional
!i_begin_comment type zif_text2tab=>ty_begin_comment optional
!i_skip_lines_starting_with type zif_text2tab=>ty_begin_comment optional
!i_deep_provider type ref to zif_text2tab_deep_provider optional
returning
value(ro_parser) type ref to zcl_text2tab_parser
Expand Down Expand Up @@ -60,6 +61,7 @@ class zcl_text2tab_parser definition
data mv_line_index type i.
data mv_is_typeless type abap_bool.
data mv_begin_comment type zif_text2tab=>ty_begin_comment.
data mv_skip_lines_starting_with type zif_text2tab=>ty_begin_comment.
data mt_ignore_exits type sorted table of abap_editmask with unique key table_line.

data mt_components type zif_text2tab=>tt_comp_descr.
Expand Down Expand Up @@ -244,6 +246,7 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
endif.

ro_parser->mv_begin_comment = i_begin_comment.
ro_parser->mv_skip_lines_starting_with = i_skip_lines_starting_with.

endmethod.

Expand Down Expand Up @@ -418,6 +421,10 @@ CLASS ZCL_TEXT2TAB_PARSER IMPLEMENTATION.
raise_error( i_msg = 'Empty line cannot be parsed' i_code = 'LE' ). "#EC NOTEXT
endif.

if mv_skip_lines_starting_with is not initial and <dataline>+0(1) = mv_skip_lines_starting_with.
continue. " Skip comment lines
endif.

parse_line(
exporting
i_dataline = <dataline>
Expand Down
67 changes: 67 additions & 0 deletions src/core/zcl_text2tab_parser.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class ltcl_text2tab_parser_test definition for testing
methods deep_structures for testing.
methods parse_corresponding for testing raising zcx_text2tab_error.

methods begin_comments for testing raising zcx_text2tab_error.
methods line_comments for testing raising zcx_text2tab_error.

* ==== HELPERS ===

Expand Down Expand Up @@ -1655,4 +1657,69 @@ class ltcl_text2tab_parser_test implementation.

endmethod.

method begin_comments.

data lv_text_orig type string.
data lt_exp type tt_dummy.
data lt_act like lt_exp.

get_dummy_data(
importing
e_dummy_tab = lt_exp
e_dummy_string = lv_text_orig ).

zcl_text2tab_parser=>create(
i_pattern = lt_act
i_begin_comment = '#'
)->parse(
exporting
i_data = |#comment{ c_crlf }{ lv_text_orig }|
importing
e_container = lt_act ).

cl_abap_unit_assert=>assert_equals(
act = lt_act
exp = lt_exp ).

zcl_text2tab_parser=>create(
i_pattern = lt_act
i_begin_comment = zif_text2tab=>c_auto_detect_by_space
)->parse(
exporting
i_data = |field descr with space\tanother one...{ c_crlf }{ lv_text_orig }|
importing
e_container = lt_act ).

cl_abap_unit_assert=>assert_equals(
act = lt_act
exp = lt_exp ).

endmethod.

method line_comments.

data lv_text_orig type string.
data lt_exp type tt_dummy.
data lt_act like lt_exp.

get_dummy_data(
importing
e_dummy_tab = lt_exp
e_dummy_string = lv_text_orig ).

zcl_text2tab_parser=>create(
i_pattern = lt_act
i_skip_lines_starting_with = '#'
)->parse(
exporting
i_data = |{ lv_text_orig }#comment| " CRLF is already at the end, no need to add
importing
e_container = lt_act ).

cl_abap_unit_assert=>assert_equals(
act = lt_act
exp = lt_exp ).

endmethod.

endclass.
6 changes: 5 additions & 1 deletion src/core/zcl_text2tab_utils.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ CLASS ZCL_TEXT2TAB_UTILS IMPLEMENTATION.
elseif lo_type->type_kind = cl_abap_typedescr=>typekind_char or lo_type->type_kind = cl_abap_typedescr=>typekind_string.
data lt_renames type string_table.
field-symbols <str> type string.
split i_rename_fields at ';' into table lt_renames.
if find( val = i_rename_fields sub = ',' ) >= 0.
split i_rename_fields at ',' into table lt_renames. " TODO maybe make comma the only separator
else.
split i_rename_fields at ';' into table lt_renames.
endif.
delete lt_renames where table_line is initial.
loop at lt_renames assigning <str>.
clear ls_rename.
Expand Down
10 changes: 10 additions & 0 deletions src/core/zcl_text2tab_utils.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ class ltcl_text2tab_utils_test implementation.
endtry.
cl_abap_unit_assert=>assert_equals( act = lt_map_act exp = lt_map_exp ).

" with comma
lv_fields = 'some_field:tstring,some_field2:tstring2,,'.

try.
lt_map_act = zcl_text2tab_utils=>build_rename_map( lv_fields ).
catch zcx_text2tab_error into lx.
cl_abap_unit_assert=>fail( lx->get_text( ) ).
endtry.
cl_abap_unit_assert=>assert_equals( act = lt_map_act exp = lt_map_exp ).

endmethod.

method break_to_lines.
Expand Down