[67] | 1 | ; |
---|
| 2 | ;+ |
---|
| 3 | ; NAME: |
---|
| 4 | ; STRCNT |
---|
| 5 | ; PURPOSE: |
---|
| 6 | ; Count number of occurrences of a substring in a string. |
---|
| 7 | ; CATEGORY: |
---|
| 8 | ; text/strings |
---|
| 9 | ; CALLING SEQUENCE: |
---|
| 10 | ; num = strcnt(strn, substring, [pos]) |
---|
| 11 | ; INPUTS: |
---|
| 12 | ; string -- The string in which to count occurences. in |
---|
| 13 | ; substring -- The substring to count occurrences of. in |
---|
| 14 | ; pos -- the position at which to begin the search. [in] |
---|
| 15 | ; If not supplied, start at beginning of |
---|
| 16 | ; string. |
---|
| 17 | ; KEYWORD PARAMETERS: |
---|
| 18 | ; /HELP -- Print useful message and return. |
---|
| 19 | ; OUTPUTS: |
---|
| 20 | ; num -- Number of occurances of substring in string. out |
---|
| 21 | ; COMMON BLOCKS: |
---|
| 22 | ; SIDE EFFECTS: |
---|
| 23 | ; NOTES: |
---|
| 24 | ; Overlapping occurances are not counted separately. For |
---|
| 25 | ; example, counting occurances of 'bb' in 'blah bbb' returns one |
---|
| 26 | ; occurance. |
---|
| 27 | ; EXAMPLE: |
---|
| 28 | ; MODIFICATION HISTORY: |
---|
| 29 | ; $Id: strcnt.pro,v 1.3 1996/06/14 20:00:27 mcraig Exp $ |
---|
| 30 | ; $Log: strcnt.pro,v $ |
---|
| 31 | ; Revision 1.3 1996/06/14 20:00:27 mcraig |
---|
| 32 | ; Updated Copyright info. |
---|
| 33 | ; |
---|
| 34 | ; Revision 1.2 1996/05/09 00:22:17 mcraig |
---|
| 35 | ; Added fast processing using BYTE arrays if we are counting occurences of |
---|
| 36 | ; a single character. Added error handling. |
---|
| 37 | ; |
---|
| 38 | ; Revision 1.1 1996/01/31 18:47:37 mcraig |
---|
| 39 | ; Initial revision |
---|
| 40 | ; |
---|
| 41 | ; RELEASE: |
---|
| 42 | ; $Name: Rel_2_1_2 $ |
---|
| 43 | ; |
---|
| 44 | ; COPYRIGHT: |
---|
| 45 | ; Copyright (C) 1996 The Regents of the University of California, All |
---|
| 46 | ; Rights Reserved. Written by Matthew W. Craig. |
---|
| 47 | ; See the file COPYRIGHT for restrictions on distrubting this code. |
---|
| 48 | ; This code comes with absolutely NO warranty; see DISCLAIMER for details. |
---|
| 49 | ;- |
---|
| 50 | FUNCTION Strcnt, strn, substrn, startpos, $ |
---|
| 51 | HELP=Help |
---|
| 52 | |
---|
| 53 | ; Return to caller if error. |
---|
| 54 | On_error, 2 |
---|
| 55 | |
---|
| 56 | ; Help user, if needed. |
---|
| 57 | IF (n_params() LT 2) OR keyword_set(Help) THEN BEGIN |
---|
| 58 | offset = ' ' |
---|
| 59 | print, offset+'Count number of occurrences of a substring in a string.' |
---|
| 60 | print, offset+'num = strcnt(strn, substring, [pos])' |
---|
| 61 | print, offset+'Inputs:' |
---|
| 62 | print,offset+offset+'string -- The string in which to count occurences. in' |
---|
| 63 | print,offset+offset+'substring -- The substring to count occurrences of. in' |
---|
| 64 | print,offset+offset+'pos -- the position at which to begin the search. [in]' |
---|
| 65 | print,offset+offset+' If not supplied, start at beginning of' |
---|
| 66 | print,offset+offset+' string.' |
---|
| 67 | print, offset+'Keywords:' |
---|
| 68 | print,offset+offset+'/HELP -- Print useful message and return.' |
---|
| 69 | print, offset+'Outputs:' |
---|
| 70 | print,offset+offset+'num -- Number of occurances of substring in string. out' |
---|
| 71 | return, -1 |
---|
| 72 | ENDIF |
---|
| 73 | |
---|
| 74 | IF n_params() EQ 2 THEN startpos = 0 |
---|
| 75 | |
---|
| 76 | ; return if we weren't really given a substring to search for. . . |
---|
| 77 | IF strlen(substrn) EQ 0 THEN BEGIN |
---|
| 78 | print, "Error: Can't count occurances of null string." |
---|
| 79 | return, -1 |
---|
| 80 | ENDIF |
---|
| 81 | |
---|
| 82 | ; . . .or if we were told to start at the end of the string. |
---|
| 83 | tmpstrn = strmid(strn, startpos, strlen(strn)) |
---|
| 84 | IF strlen(tmpstrn) EQ 0 THEN return, 0 |
---|
| 85 | |
---|
| 86 | ; If looking for occurences of single character, process using BYTE |
---|
| 87 | ; array. |
---|
| 88 | IF strlen(substrn) EQ 1 THEN BEGIN |
---|
| 89 | tmpstrn = byte(TmpStrn) |
---|
| 90 | count = n_elements(where(TmpStrn EQ (byte(substrn))(0))) |
---|
| 91 | ENDIF ELSE BEGIN |
---|
| 92 | count = 0L |
---|
| 93 | pos = rstrpos(tmpstrn, substrn) |
---|
| 94 | WHILE pos GE 0 DO BEGIN |
---|
| 95 | count = count + 1 |
---|
| 96 | pos = rstrpos(tmpstrn, substrn, pos) |
---|
| 97 | ENDWHILE |
---|
| 98 | ENDELSE |
---|
| 99 | |
---|
| 100 | return, count |
---|
| 101 | END |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|