home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / AT2.PRG < prev    next >
Text File  |  1991-08-16  |  4KB  |  142 lines

  1. /*
  2.  * File......: AT2.prg
  3.  * Author....: Ralph Oliver,  TRANSCOM SYSTEMS
  4.  * CIS ID....: 74030,703
  5.  * Date......: $Date:   15 Aug 1991 23:05:58  $
  6.  * Revision..: $Revision:   1.2  $
  7.  * Log file..: $Logfile:   E:/nanfor/src/at2.prv  $
  8.  * 
  9.  * This is an original work by Ralph Oliver and is placed in the
  10.  * public domain.
  11.  *
  12.  * Modification history:
  13.  * ---------------------
  14.  *
  15.  * $Log:   E:/nanfor/src/at2.prv  $
  16.  * 
  17.  *    Rev 1.2   15 Aug 1991 23:05:58   GLENN
  18.  * Forest Belt proofread/edited/cleaned up doc
  19.  * 
  20.  *    Rev 1.1   12 Jun 1991 00:46:28   GLENN
  21.  * Posted a referee suggestion: Around line 118, break out if no 
  22.  * occurrences (note the IF/ENDIF before the NEXT and RETURN at the 
  23.  * bottom of the function).
  24.  * 
  25.  *    Rev 1.0   07 Jun 1991 23:03:28   GLENN
  26.  * Initial revision.
  27.  *
  28.  *
  29.  */
  30.  
  31.  
  32. /*  $DOC$
  33.  *  $FUNCNAME$
  34.  *     FT_AT2()
  35.  *  $CATEGORY$
  36.  *     String
  37.  *  $ONELINER$
  38.  *     Find position of the nth occurrence of a substring
  39.  *  $SYNTAX$
  40.  *     FT_AT2( <cSearch>, <cTarget> [, <nOccurs> [, <lCaseSens> ] ] ) -> nPos
  41.  *  $ARGUMENTS$
  42.  *     <cSearch> is the character substring to search for.
  43.  *
  44.  *     <cTarget> is the character string to search.
  45.  *
  46.  *     <nOccurs> is the occurrence of cSearch to look for,
  47.  *                defaults to 1.
  48.  *
  49.  *     <lCaseSens> is a logical value denoting case sensitivity.
  50.  *                If .F., then search is NOT sensitive to case,
  51.  *                defaults to .T.
  52.  *  $RETURNS$
  53.  *     The position of the nth occurrence of a substring
  54.  *  $DESCRIPTION$
  55.  *     This function will find the nth occurrence of a substring
  56.  *     within a string.
  57.  *  $EXAMPLES$
  58.  *     cSearch := "t"
  59.  *     cTarget := "This is the day that the Lord has made."
  60.  *
  61.  *     FT_AT2( cSearch, cTarget )            // Returns ( 9 )
  62.  *
  63.  *     FT_AT2( cSearch, cTarget, 2 )         // Returns ( 17 )
  64.  *
  65.  *     FT_AT2( cSearch, cTarget, 2, .F. )    // Returns ( 9 )
  66.  *  $SEEALSO$
  67.  *    FT_FINDITH()
  68.  *  $END$
  69.  */
  70.  
  71. #ifdef FT_TEST
  72.  
  73. FUNCTION MAIN()
  74.    LOCAL cSearch,cTarget,var0
  75.    CLS
  76.    ? "TEST TO DEMONSTRATE EXAMPLES OF FT_AT2"
  77.    ?
  78.    cSearch := 't'
  79.    ? "Find occurrences of 't' in: "
  80.    cTarget := "This is the day that the Lord has made."
  81.    ?? cTarget
  82.    ?
  83.    var0 := ft_at2( cSearch, cTarget )
  84.    ? PADR("FT_AT2( cSearch, cTarget ) -> ",40)
  85.    ?? var0
  86.    ?
  87.    var0 := ft_at2( cSearch, cTarget, 2 )
  88.    ? PADR("FT_AT2( cSearch, cTarget, 2 ) -> ",40)
  89.    ??var0
  90.    ?
  91.    var0 := ft_at2( cSearch, cTarget, 2, .F. )
  92.    ? PADR("FT_AT2( cSearch, cTarget, 2, .F. ) -> ",40)
  93.    ??var0
  94.    ?
  95.    RETURN NIL
  96.  
  97. #endif
  98.  
  99.  
  100. FUNCTION FT_AT2( cSearch, cTarget, nOccurs, lCaseSens )
  101.  
  102.    LOCAL nCount, nPos, nPos2 := 0
  103.    LOCAL cSubstr := cTarget
  104.  
  105.    // Set default parameters as necessary.
  106.    IF lCaseSens == NIL
  107.       lCaseSens := .T.
  108.    ENDIF
  109.  
  110.    IF nOccurs == NIL
  111.       nOccurs := 1
  112.    ENDIF
  113.  
  114.    FOR nCount := 1 TO nOccurs
  115.  
  116.       // Store position of next occurrence of cSearch.
  117.       IF lCaseSens
  118.          nPos := AT( cSearch, cSubstr )
  119.  
  120.       ELSE
  121.          nPos := AT( UPPER( cSearch ), UPPER( cSubstr ) )
  122.  
  123.       ENDIF
  124.  
  125.       // Store position of cSearch relative to original string.
  126.       nPos2 += nPos
  127.  
  128.       // Resize cSubstr
  129.       cSubstr := SUBSTR( cSubstr, AT( cSearch, cSubstr ) +1 )
  130.  
  131.       // Breakout if there are no occurences here
  132.        
  133.       IF nPos == 0
  134.            EXIT
  135.       ENDIF
  136.  
  137.  
  138.    NEXT
  139.  
  140.    RETURN ( nPos2 )
  141.  
  142.