Advertisement
rplantiko

Regex Object vs. Regex String

Oct 26th, 2012
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 2.89 KB | None | 0 0
  1. *&---------------------------------------------------------------------*
  2. *& Report  ZZ_TEST_REPLACE_IRREGULAR
  3. *&---------------------------------------------------------------------*
  4. *& In runtime terms:
  5. *& Using a precompiled regular expression repeatedly,
  6. *& equals using the same regular expression STRING repeatedly
  7. *& (shows that there is an internal buffering of the last regex)
  8. *&---------------------------------------------------------------------*
  9.  
  10. report  zz_test_replace_irregular.
  11.  
  12. parameters: p_num type i default 1000,
  13.             p_len type i default 100,
  14.             p_seed type i default 14527.
  15.  
  16. start-of-selection.
  17.   perform start.
  18.  
  19. * ---
  20. form start.
  21.  
  22.   data: lt_text type stringtab,
  23.         lv_text type string.
  24.  
  25.   field-symbols: <lv_text> type string.
  26.  
  27.   perform make_sample using p_num p_len p_seed
  28.                       changing lt_text.
  29.  
  30.   loop at lt_text assigning <lv_text>.
  31.     lv_text = <lv_text>.
  32.     perform replace1 using '.' lv_text.
  33.     lv_text = <lv_text>.
  34.     perform replace2 using '.' lv_text.
  35.   endloop.
  36.  
  37. endform.                    "start
  38.  
  39. *  ---
  40. form replace1 using    iv_default type char01
  41.               changing cv_text    type csequence.
  42.  
  43.   statics: sv_pattern type string.
  44.   if  sv_pattern  is  initial.
  45.     perform get_pattern changing sv_pattern.
  46.   endif.
  47.  
  48.   replace all occurrences of regex sv_pattern in cv_text with iv_default.
  49.  
  50. endform.                    "replace1
  51.  
  52. *
  53. form get_pattern changing cv_pattern type string.
  54.   data: lv_allowed_char type string.
  55.   call function 'RSKC_ALLOWED_CHAR_GET'
  56.     importing
  57.       e_allowed_char = lv_allowed_char.
  58.  
  59.   concatenate '([^' lv_allowed_char '])' into cv_pattern.
  60.  
  61. endform.                    "get_pattern
  62.  
  63.  
  64. * ---
  65. form replace2 using    iv_default type char01
  66.               changing cv_text    type csequence.
  67.  
  68.   statics: so_regex type ref to cl_abap_regex.
  69.   data: lv_pattern type string.
  70.  
  71.   if  so_regex  is  initial.
  72.     perform get_pattern changing lv_pattern.
  73.     create object so_regex
  74.       exporting
  75.         pattern = lv_pattern.
  76.   endif.
  77.  
  78.   replace all occurrences of regex so_regex in cv_text with iv_default.
  79.  
  80. endform.                    "replace2
  81.  
  82. *
  83. form make_sample using    iv_num type i
  84.                           iv_len type i
  85.                           iv_seed type i
  86.                  changing ct_text type stringtab.
  87.  
  88.   data:
  89.      lo_ran  type ref to cl_abap_random_int,
  90.      lv_x    type x length 2,
  91.      lv_c    type c,
  92.      lv_seed type i,
  93.      lv_text type string.
  94.  
  95.   lo_ran = cl_abap_random_int=>create( min = 32 max = 255 seed = iv_seed ).
  96.  
  97.   do iv_num times.
  98.     clear lv_text.
  99.     do iv_len times.
  100.       lv_x = lo_ran->get_next( ).
  101.       lv_c = cl_abap_conv_in_ce=>uccp( lv_x ).
  102.       concatenate lv_text lv_c into lv_text respecting blanks.
  103.     enddo.
  104.     append lv_text to ct_text.
  105.   enddo.
  106.  
  107. endform.                    "make_sample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement