'===========================================================================
' Subject: KEEP MASKED CHARACTERS             Date: 06-26-00 (07:45)       
'  Author: Don Schullian                      Code: PBCC                   
'  Origin: d83@DASoftVSS.com                Packet: PBCC.ABC
'===========================================================================
$IF 0
    ----------------------------                      PowerBASIC/cc v2.0
 ---|          DASoft          |------------------------------------------
    ----------------------------    Copyright 2000      DATE: 2000-02-02
    | FILE NAME   Keep    .bas |          by
    | DIRECTORY   NBcc         |  Don Schullian, Jr.
    ----------------------------
   A license is hereby granted to the holder to use this source code in
   any program, commercial or otherwise,  without receiving the express
   permission of the copyright holder and without paying any royalties.
 -------------------------------------------------------------------------
  PURPOSE: Retain only the characters from Text$ that are found in Mask$
   PARAMS: Text$ the working text
           Mask$ the list of characters to be kept
  RETURNS: string containing only the characters found in Mask$
     NOTE:
 -------------------------------------------------------------------------
$ENDIF



FUNCTION fKeep ( BYVAL Text AS STRING, _
                 BYVAL Mask AS STRING  ) EXPORT AS STRING

  DIM Char  AS LOCAL BYTE
  DIM M_ptr AS LOCAL BYTE PTR
  DIM Mlen  AS LOCAL LONG
  DIM Moff  AS LOCAL LONG
  DIM Noff  AS LOCAL LONG
  DIM T_ptr AS LOCAL BYTE PTR
  DIM Tlen  AS LOCAL LONG
  DIM Toff  AS LOCAL LONG

  Mlen  = LEN(Mask) - 1 : IF Mlen < 0 THEN EXIT FUNCTION
  Tlen  = LEN(Text) - 1 : IF Tlen < 0 THEN EXIT FUNCTION
  M_ptr = STRPTR(Mask)
  T_ptr = STRPTR(Text)

  FOR Toff = 0 TO Tlen
    Char = @T_ptr[Toff]
    FOR Moff = Mlen TO 0 STEP -1
      IF Char = @M_ptr[Moff] THEN
        @T_ptr[Noff] = Char
        INCR Noff
        EXIT FOR
      END IF
    NEXT
  NEXT

  FUNCTION = LEFT$(Text,Noff)

END FUNCTION
