mirror of
https://github.com/abapGit/abapGit.git
synced 2025-04-30 11:46:38 +08:00

- Changed: Since X_TO_BITBYTE only converts a single byte to a bitstring, it is faster to use direct addressing instead of a loop/concatenate pair, since concatenate on characters has to process the whole char array in each run.
173 lines
4.0 KiB
ABAP
173 lines
4.0 KiB
ABAP
CLASS zcl_abapgit_convert DEFINITION
|
|
PUBLIC
|
|
CREATE PUBLIC .
|
|
|
|
PUBLIC SECTION.
|
|
|
|
CLASS-METHODS bitbyte_to_int
|
|
IMPORTING
|
|
!iv_bits TYPE clike
|
|
RETURNING
|
|
VALUE(rv_int) TYPE i .
|
|
CLASS-METHODS x_to_bitbyte
|
|
IMPORTING
|
|
!iv_x TYPE x
|
|
RETURNING
|
|
VALUE(rv_bitbyte) TYPE zif_abapgit_definitions=>ty_bitbyte .
|
|
CLASS-METHODS string_to_xstring_utf8
|
|
IMPORTING
|
|
!iv_string TYPE string
|
|
RETURNING
|
|
VALUE(rv_xstring) TYPE xstring .
|
|
CLASS-METHODS xstring_to_string_utf8
|
|
IMPORTING
|
|
!iv_data TYPE xstring
|
|
RETURNING
|
|
VALUE(rv_string) TYPE string .
|
|
CLASS-METHODS xstring_to_int
|
|
IMPORTING
|
|
!iv_xstring TYPE xstring
|
|
RETURNING
|
|
VALUE(rv_i) TYPE i
|
|
RAISING
|
|
zcx_abapgit_exception .
|
|
CLASS-METHODS int_to_xstring4
|
|
IMPORTING
|
|
!iv_i TYPE i
|
|
RETURNING
|
|
VALUE(rv_xstring) TYPE xstring .
|
|
CLASS-METHODS split_string
|
|
IMPORTING
|
|
!iv_string TYPE string
|
|
RETURNING
|
|
VALUE(rt_lines) TYPE string_table .
|
|
ENDCLASS.
|
|
|
|
|
|
|
|
CLASS ZCL_ABAPGIT_CONVERT IMPLEMENTATION.
|
|
|
|
|
|
METHOD bitbyte_to_int.
|
|
|
|
DATA: lv_bits TYPE string.
|
|
|
|
|
|
lv_bits = iv_bits.
|
|
|
|
rv_int = 0.
|
|
WHILE strlen( lv_bits ) > 0.
|
|
rv_int = rv_int * 2.
|
|
IF lv_bits(1) = '1'.
|
|
rv_int = rv_int + 1.
|
|
ENDIF.
|
|
lv_bits = lv_bits+1.
|
|
ENDWHILE.
|
|
|
|
ENDMETHOD. "bitbyte_to_int
|
|
|
|
|
|
METHOD int_to_xstring4.
|
|
* returns xstring of length 4 containing the integer value iv_i
|
|
|
|
DATA: lv_x TYPE x LENGTH 4.
|
|
|
|
|
|
lv_x = iv_i.
|
|
rv_xstring = lv_x.
|
|
|
|
ENDMETHOD. "int_to_xstring
|
|
|
|
|
|
METHOD split_string.
|
|
|
|
FIND FIRST OCCURRENCE OF cl_abap_char_utilities=>cr_lf IN iv_string.
|
|
|
|
" Convert string into table depending on separator type CR_LF vs. LF
|
|
IF sy-subrc = 0.
|
|
SPLIT iv_string AT cl_abap_char_utilities=>cr_lf INTO TABLE rt_lines.
|
|
ELSE.
|
|
SPLIT iv_string AT cl_abap_char_utilities=>newline INTO TABLE rt_lines.
|
|
ENDIF.
|
|
|
|
ENDMETHOD. "split_string
|
|
|
|
|
|
METHOD string_to_xstring_utf8.
|
|
|
|
DATA: lo_obj TYPE REF TO cl_abap_conv_out_ce.
|
|
|
|
|
|
TRY.
|
|
lo_obj = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
|
|
|
|
lo_obj->convert( EXPORTING data = iv_string
|
|
IMPORTING buffer = rv_xstring ).
|
|
|
|
CATCH cx_parameter_invalid_range
|
|
cx_sy_codepage_converter_init
|
|
cx_sy_conversion_codepage
|
|
cx_parameter_invalid_type. "#EC NO_HANDLER
|
|
ENDTRY.
|
|
|
|
ENDMETHOD. "string_to_xstring_utf8
|
|
|
|
|
|
METHOD xstring_to_int.
|
|
|
|
DATA: lv_xstring TYPE xstring,
|
|
lv_x TYPE x.
|
|
|
|
|
|
lv_xstring = iv_xstring.
|
|
WHILE xstrlen( lv_xstring ) > 0.
|
|
lv_x = lv_xstring(1).
|
|
rv_i = rv_i * 256 + lv_x.
|
|
lv_xstring = lv_xstring+1.
|
|
ENDWHILE.
|
|
|
|
ENDMETHOD. "xstring_to_int
|
|
|
|
|
|
METHOD xstring_to_string_utf8.
|
|
|
|
DATA: lv_len TYPE i,
|
|
lo_obj TYPE REF TO cl_abap_conv_in_ce.
|
|
|
|
|
|
TRY.
|
|
lo_obj = cl_abap_conv_in_ce=>create(
|
|
input = iv_data
|
|
encoding = 'UTF-8' ).
|
|
lv_len = xstrlen( iv_data ).
|
|
|
|
lo_obj->read( EXPORTING n = lv_len
|
|
IMPORTING data = rv_string ).
|
|
|
|
CATCH cx_parameter_invalid_range
|
|
cx_sy_codepage_converter_init
|
|
cx_sy_conversion_codepage
|
|
cx_parameter_invalid_type. "#EC NO_HANDLER
|
|
ENDTRY.
|
|
|
|
ENDMETHOD. "xstring_to_string_utf8
|
|
|
|
|
|
METHOD x_to_bitbyte.
|
|
|
|
DATA: lv_b TYPE n.
|
|
|
|
CLEAR rv_bitbyte.
|
|
|
|
GET BIT 1 OF iv_x INTO rv_bitbyte+0(1).
|
|
GET BIT 2 OF iv_x INTO rv_bitbyte+1(1).
|
|
GET BIT 3 OF iv_x INTO rv_bitbyte+2(1).
|
|
GET BIT 4 OF iv_x INTO rv_bitbyte+3(1).
|
|
GET BIT 5 OF iv_x INTO rv_bitbyte+4(1).
|
|
GET BIT 6 OF iv_x INTO rv_bitbyte+5(1).
|
|
GET BIT 7 OF iv_x INTO rv_bitbyte+6(1).
|
|
GET BIT 8 OF iv_x INTO rv_bitbyte+7(1).
|
|
|
|
ENDMETHOD. "x_to_bitbyte
|
|
ENDCLASS.
|