From f9ab424eec719cdacd8eae0addb712853e47c39e Mon Sep 17 00:00:00 2001 From: Lars Hvam Date: Tue, 2 Jan 2024 17:10:58 +0100 Subject: [PATCH] cleanup --- src/stream/zcl_protobuf_stream.clas.abap | 49 ++++++++++++++++--- .../zcl_protobuf_stream.clas.testclasses.abap | 11 +++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/stream/zcl_protobuf_stream.clas.abap b/src/stream/zcl_protobuf_stream.clas.abap index 8e2981e..9221e23 100644 --- a/src/stream/zcl_protobuf_stream.clas.abap +++ b/src/stream/zcl_protobuf_stream.clas.abap @@ -402,22 +402,57 @@ CLASS zcl_protobuf_stream IMPLEMENTATION. METHOD encode_int64. - DATA lv_bits TYPE string. - - IF iv_int > 0. + DATA lv_bits TYPE string. + DATA lv_bit TYPE c LENGTH 1. + DATA lv_hex TYPE x LENGTH 1. + DATA lv_index TYPE i. + DATA lv_offset TYPE i. + DATA lv_byteno TYPE i. + DATA lv_int TYPE int8. + + IF iv_int >= 0. ro_ref = encode_varint( iv_int ). ELSE. - DATA(lv_int) = abs( iv_int + 1 ). + " WRITE / iv_int. + lv_int = -1 * ( iv_int + 1 ). WHILE lv_int > 0. - DATA(lv_bit) = lv_int MOD 2. + " WRITE / lv_int. + lv_bit = lv_int MOD 2. CONCATENATE lv_bit lv_bits INTO lv_bits. lv_int = lv_int DIV 2. ENDWHILE. - WRITE / lv_bits. + WHILE strlen( lv_bits ) < 64. + CONCATENATE '0' lv_bits INTO lv_bits. + ENDWHILE. + + " WRITE / lv_bits. + TRANSLATE lv_bits USING '0110'. +* extra bits to fill the last byte + CONCATENATE '000000' lv_bits INTO lv_bits. + " WRITE / lv_bits. + + lv_offset = 70 - 7. +* 10 bytes to encode 64 bits + DO 10 TIMES. + lv_byteno = sy-index. + CLEAR lv_hex. +* add continuation bits, 1 for every 7 bits + IF lv_byteno <> 10. + SET BIT 1 OF lv_hex TO 1. + ENDIF. + + DO 7 TIMES. + lv_index = sy-index + 1. + SET BIT lv_index OF lv_hex TO lv_bits+lv_offset(1). + lv_offset = lv_offset + 1. + ENDDO. + " WRITE / lv_hex. + append( lv_hex ). + lv_offset = lv_offset - 14. + ENDDO. - ASSERT 1 = 'todo'. ENDIF. ENDMETHOD. diff --git a/src/stream/zcl_protobuf_stream.clas.testclasses.abap b/src/stream/zcl_protobuf_stream.clas.testclasses.abap index b030ebf..b6c189b 100644 --- a/src/stream/zcl_protobuf_stream.clas.testclasses.abap +++ b/src/stream/zcl_protobuf_stream.clas.testclasses.abap @@ -16,6 +16,7 @@ CLASS ltcl_test DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS FINAL. METHODS decode_int32_negative_six FOR TESTING RAISING cx_static_check. METHODS decode_int32_positive_one FOR TESTING RAISING cx_static_check. METHODS encode_int32_negative_six FOR TESTING RAISING cx_static_check. + METHODS encode_int32_negative_thousand FOR TESTING RAISING cx_static_check. ENDCLASS. CLASS ltcl_test IMPLEMENTATION. @@ -171,4 +172,14 @@ CLASS ltcl_test IMPLEMENTATION. ENDMETHOD. + METHOD encode_int32_negative_thousand. + + mo_cut->encode_int32( -1000 ). + + cl_abap_unit_assert=>assert_equals( + exp = '98F8FFFFFFFFFFFFFF01' + act = mo_cut->get( ) ). + + ENDMETHOD. + ENDCLASS.