Página 1 de 1

Função nova muito util, HB_STRXCHG()

Enviado: 15 Fev 2013 22:45
por Itamar M. Lins Jr.
Ola!

Código: Selecionar todos

2013-02-15 01:53 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)
  * harbour/include/harbour.hbx
  * harbour/src/rtl/Makefile
  + harbour/src/rtl/strxchg.c
    + added new PRG function:
         hb_strXChg( <cString>, <cSource> | <acSource>, ;
                                <cDest> | <acDest> ] ) -> <cResult>
      This function allows to easy replace different substrings in
      given string.
      If 2-nd is string then each character in <cString> which exists
      in <cSource> at <n> position is replaced by corresponding character
      at <n> position in <cDest> or string from <acDest>[ <n> ]
      If 2-nd parameter is array then each <cString> substring which exists
      in <acSource> at <n> position is replaced by corresponding character
      at <n> position in <cDest> or string from <acDest>[ <n> ].
      If <n> is longer then LEN() of <cDest> or <acDest> then given
      character/substring is removed from result.
      This function should help in code which wrongly uses repeated
      StrTran() calls all regex which can change also substituted values.
      Examples:
         // encode XML value
         cXmlText := hb_strXChg( cText, "<>&", { "<", ">", "&" } )
         // now decode it to raw text
         cText := hb_strXChg( cXmlText, { "<", ">", "&" }, "<>&" )
         // strip all digits from string
         cNoDigit := hb_strXChg( cText, "0123456789" )
         // extract all digits from string
         cDigits := hb_strXChg( cText, cNoDigit )
         // convert chosen letters to upper case
         ? hb_strXChg( "hello world, "hlwd", "HLWD" )

Código: Selecionar todos

2013-02-15 12:14 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)
  * harbour/src/rtl/strxchg.c
    + added support for using hash arrays as replace pairs in second
      argument of hb_strXChg() - hash arrays are accepted if 3-rd parameter
      is ommitted or is NIL. Now this alternative syntax:
         hb_strXChg( <cString>, <hChanges> ) -> <cResult>
      is also supported, i.e.:
         cData := hb_strXChg( cSource, { "{LI}" => cLIValue, ;
                                         "{FI}" => cFIValue } )
      Please remember that single hash layer is much lighter data type
      then array of subarrays because it needs only one GC item when
      each subarray is new GC item.
Saudações,
Itamar M. Lins Jr.