Skip to content

Commit bd96999

Browse files
authored
Merge pull request #5 from sbcgua/case-insensitive
case insensitive support
2 parents 8935645 + 3d506e9 commit bd96999

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

src/zcl_abap_string_map.clas.abap

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ class zcl_abap_string_map definition
2121

2222
class-methods create
2323
importing
24+
!iv_case_insensitive type abap_bool default abap_false
2425
!iv_from type any optional
2526
returning
2627
value(ro_instance) type ref to zcl_abap_string_map .
2728
methods constructor
2829
importing
30+
!iv_case_insensitive type abap_bool default abap_false
2931
!iv_from type any optional.
3032

3133
methods get
@@ -80,6 +82,7 @@ class zcl_abap_string_map definition
8082
private section.
8183
data mv_is_strict type abap_bool.
8284
data mv_read_only type abap_bool.
85+
data mv_case_insensitive type abap_bool.
8386
ENDCLASS.
8487

8588

@@ -100,6 +103,7 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
100103

101104
method constructor.
102105
mv_is_strict = abap_true.
106+
mv_case_insensitive = iv_case_insensitive.
103107

104108
if iv_from is not initial.
105109
data lo_type type ref to cl_abap_typedescr.
@@ -130,7 +134,10 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
130134

131135

132136
method create.
133-
create object ro_instance exporting iv_from = iv_from.
137+
create object ro_instance
138+
exporting
139+
iv_case_insensitive = iv_case_insensitive
140+
iv_from = iv_from.
134141
endmethod.
135142

136143

@@ -201,8 +208,16 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
201208

202209
method get.
203210

211+
data lv_key like iv_key.
204212
field-symbols <entry> like line of mt_entries.
205-
read table mt_entries assigning <entry> with key k = iv_key.
213+
214+
if mv_case_insensitive = abap_true.
215+
lv_key = to_upper( iv_key ).
216+
else.
217+
lv_key = iv_key.
218+
endif.
219+
220+
read table mt_entries assigning <entry> with key k = lv_key.
206221
if sy-subrc = 0.
207222
rv_val = <entry>-v.
208223
endif.
@@ -236,17 +251,24 @@ CLASS ZCL_ABAP_STRING_MAP IMPLEMENTATION.
236251
method set.
237252

238253
data ls_entry like line of mt_entries.
254+
data lv_key like iv_key.
239255
field-symbols <entry> like line of mt_entries.
240256

241257
if mv_read_only = abap_true.
242258
lcx_error=>raise( 'String map is read only' ).
243259
endif.
244260

245-
read table mt_entries assigning <entry> with key k = iv_key.
261+
if mv_case_insensitive = abap_true.
262+
lv_key = to_upper( iv_key ).
263+
else.
264+
lv_key = iv_key.
265+
endif.
266+
267+
read table mt_entries assigning <entry> with key k = lv_key.
246268
if sy-subrc = 0.
247269
<entry>-v = iv_val.
248270
else.
249-
ls_entry-k = iv_key.
271+
ls_entry-k = lv_key.
250272
ls_entry-v = iv_val.
251273
insert ls_entry into table mt_entries.
252274
endif.

src/zcl_abap_string_map.clas.testclasses.abap

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ltcl_string_map definition
2424
methods from_entries for testing.
2525
methods freeze for testing.
2626
methods create_from for testing.
27+
methods case_insensitive for testing.
2728

2829
endclass.
2930

@@ -41,7 +42,7 @@ class ltcl_string_map implementation.
4142
iv_val = '1' ).
4243

4344
try.
44-
zcl_abap_string_map=>create( `abc` ).
45+
zcl_abap_string_map=>create( iv_from = `abc` ).
4546
cl_abap_unit_assert=>fail( ).
4647
catch cx_root into lx.
4748
cl_abap_unit_assert=>assert_equals(
@@ -50,7 +51,7 @@ class ltcl_string_map implementation.
5051
endtry.
5152

5253
try.
53-
zcl_abap_string_map=>create( me ).
54+
zcl_abap_string_map=>create( iv_from = me ).
5455
cl_abap_unit_assert=>fail( ).
5556
catch cx_root into lx.
5657
cl_abap_unit_assert=>assert_equals(
@@ -59,7 +60,7 @@ class ltcl_string_map implementation.
5960
endtry.
6061

6162
" From obj
62-
lo_cut = zcl_abap_string_map=>create( lo_src ).
63+
lo_cut = zcl_abap_string_map=>create( iv_from = lo_src ).
6364
cl_abap_unit_assert=>assert_equals(
6465
exp = 1
6566
act = lo_cut->size( ) ).
@@ -68,7 +69,7 @@ class ltcl_string_map implementation.
6869
act = lo_cut->get( 'A' ) ).
6970

7071
" From tab
71-
lo_cut = zcl_abap_string_map=>create( lo_src->mt_entries ).
72+
lo_cut = zcl_abap_string_map=>create( iv_from = lo_src->mt_entries ).
7273
cl_abap_unit_assert=>assert_equals(
7374
exp = 1
7475
act = lo_cut->size( ) ).
@@ -78,7 +79,7 @@ class ltcl_string_map implementation.
7879

7980
" From struc
8081
data: begin of ls_dummy, a type string value '1', end of ls_dummy.
81-
lo_cut = zcl_abap_string_map=>create( ls_dummy ).
82+
lo_cut = zcl_abap_string_map=>create( iv_from = ls_dummy ).
8283
cl_abap_unit_assert=>assert_equals(
8384
exp = 1
8485
act = lo_cut->size( ) ).
@@ -479,4 +480,42 @@ class ltcl_string_map implementation.
479480

480481
endmethod.
481482

483+
method case_insensitive.
484+
485+
data lo_cut type ref to zcl_abap_string_map.
486+
lo_cut = zcl_abap_string_map=>create( iv_case_insensitive = abap_true ).
487+
488+
lo_cut->set(
489+
iv_key = 'A'
490+
iv_val = 'avalue' ).
491+
lo_cut->set(
492+
iv_key = 'b'
493+
iv_val = 'bvalue' ).
494+
495+
cl_abap_unit_assert=>assert_equals(
496+
exp = 'avalue'
497+
act = lo_cut->get( 'A' ) ).
498+
499+
cl_abap_unit_assert=>assert_equals(
500+
exp = 'avalue'
501+
act = lo_cut->get( 'a' ) ).
502+
503+
cl_abap_unit_assert=>assert_equals(
504+
exp = 'bvalue'
505+
act = lo_cut->get( 'B' ) ).
506+
507+
cl_abap_unit_assert=>assert_equals(
508+
exp = 'bvalue'
509+
act = lo_cut->get( 'b' ) ).
510+
511+
data lt_exp_keys type string_table.
512+
append 'A' to lt_exp_keys.
513+
append 'B' to lt_exp_keys.
514+
515+
cl_abap_unit_assert=>assert_equals(
516+
exp = lt_exp_keys
517+
act = lo_cut->keys( ) ).
518+
519+
endmethod.
520+
482521
endclass.

0 commit comments

Comments
 (0)