diff --git a/changelog.txt b/changelog.txt index 68d5b27..c6cfc8d 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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 diff --git a/docsite/docs/03-parsing-options.md b/docsite/docs/03-parsing-options.md index a88f934..115abaa 100644 --- a/docsite/docs/03-parsing-options.md +++ b/docsite/docs/03-parsing-options.md @@ -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( @@ -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. @@ -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 +``` + diff --git a/src/core/zcl_text2tab_parser.clas.abap b/src/core/zcl_text2tab_parser.clas.abap index 1be0dde..63d5a20 100644 --- a/src/core/zcl_text2tab_parser.clas.abap +++ b/src/core/zcl_text2tab_parser.clas.abap @@ -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 @@ -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. @@ -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. @@ -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 +0(1) = mv_skip_lines_starting_with. + continue. " Skip comment lines + endif. + parse_line( exporting i_dataline = diff --git a/src/core/zcl_text2tab_parser.clas.testclasses.abap b/src/core/zcl_text2tab_parser.clas.testclasses.abap index f165a17..be2f082 100644 --- a/src/core/zcl_text2tab_parser.clas.testclasses.abap +++ b/src/core/zcl_text2tab_parser.clas.testclasses.abap @@ -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 === @@ -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. diff --git a/src/core/zcl_text2tab_utils.clas.abap b/src/core/zcl_text2tab_utils.clas.abap index 7415eda..a608020 100644 --- a/src/core/zcl_text2tab_utils.clas.abap +++ b/src/core/zcl_text2tab_utils.clas.abap @@ -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 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 . clear ls_rename. diff --git a/src/core/zcl_text2tab_utils.clas.testclasses.abap b/src/core/zcl_text2tab_utils.clas.testclasses.abap index 1e1c869..4552b4c 100644 --- a/src/core/zcl_text2tab_utils.clas.testclasses.abap +++ b/src/core/zcl_text2tab_utils.clas.testclasses.abap @@ -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.