1 | ;+ |
---|
2 | ; @file_comments |
---|
3 | ; Count number of occurrences of a substring in a string. |
---|
4 | ; |
---|
5 | ; @categories |
---|
6 | ; Text, String |
---|
7 | ; |
---|
8 | ; @param STRN {in}{required}{type=string} |
---|
9 | ; The string in which to count occurences. |
---|
10 | ; |
---|
11 | ; @param SUBSTRN {in}{required}{type=string} |
---|
12 | ; The substring to count occurrences of. |
---|
13 | ; |
---|
14 | ; @param STARTPOS {in}{required} |
---|
15 | ; The position at which to begin the search. |
---|
16 | ; If not supplied, start at beginning of string. |
---|
17 | ; |
---|
18 | ; @keyword HELP |
---|
19 | ; Print useful message and return. |
---|
20 | ; |
---|
21 | ; @returns |
---|
22 | ; Number of occurances of substring in string. |
---|
23 | ; |
---|
24 | ; @restrictions |
---|
25 | ; Overlapping occurances are not counted separately. For |
---|
26 | ; example, counting occurances of 'bb' in 'blah bbb' returns one |
---|
27 | ; occurance. |
---|
28 | ; |
---|
29 | ; @history |
---|
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 | ; Copyright (C) 1996 The Regents of the University of California, All |
---|
42 | ; Rights Reserved. Written by Matthew W. Craig. |
---|
43 | ; See the file COPYRIGHT for restrictions on distrubting this code. |
---|
44 | ; This code comes with absolutely NO warranty; see DISCLAIMER for details. |
---|
45 | ; |
---|
46 | ; @version |
---|
47 | ; $Id$ |
---|
48 | ;- |
---|
49 | FUNCTION strcnt, strn, substrn, startpos, $ |
---|
50 | HELP=Help |
---|
51 | ; |
---|
52 | compile_opt idl2, strictarrsubs |
---|
53 | ; |
---|
54 | |
---|
55 | ; Return to caller if error. |
---|
56 | On_error, 2 |
---|
57 | |
---|
58 | ; Help user, if needed. |
---|
59 | IF (n_params() LT 2) OR keyword_set(Help) THEN BEGIN |
---|
60 | offset = ' ' |
---|
61 | print, offset+'Count number of occurrences of a substring in a string.' |
---|
62 | print, offset+'num = strcnt(strn, substring, [pos])' |
---|
63 | print, offset+'Inputs:' |
---|
64 | print,offset+offset+'string -- The string in which to count occurences. in' |
---|
65 | print,offset+offset+'substring -- The substring to count occurrences of. in' |
---|
66 | print,offset+offset+'pos -- the position at which to begin the search. [in]' |
---|
67 | print,offset+offset+' If not supplied, start at beginning of' |
---|
68 | print,offset+offset+' string.' |
---|
69 | print, offset+'Keywords:' |
---|
70 | print,offset+offset+'/HELP -- Print useful message and return.' |
---|
71 | print, offset+'Outputs:' |
---|
72 | print,offset+offset+'num -- Number of occurances of substring in string. out' |
---|
73 | return, -1 |
---|
74 | ENDIF |
---|
75 | |
---|
76 | IF n_params() EQ 2 THEN startpos = 0 |
---|
77 | |
---|
78 | ; return if we weren't really given a substring to search for. . . |
---|
79 | IF strlen(substrn) EQ 0 THEN BEGIN |
---|
80 | print, "Error: Can't count occurances of null string." |
---|
81 | return, -1 |
---|
82 | ENDIF |
---|
83 | |
---|
84 | ; . . .or if we were told to start at the end of the string. |
---|
85 | tmpstrn = strmid(strn, startpos, strlen(strn)) |
---|
86 | IF strlen(tmpstrn) EQ 0 THEN return, 0 |
---|
87 | |
---|
88 | ; If looking for occurences of single character, process using BYTE |
---|
89 | ; array. |
---|
90 | IF strlen(substrn) EQ 1 THEN BEGIN |
---|
91 | tmpstrn = byte(TmpStrn) |
---|
92 | count = n_elements(where(TmpStrn EQ (byte(substrn))[0])) |
---|
93 | ENDIF ELSE BEGIN |
---|
94 | count = 0L |
---|
95 | pos = rstrpos(tmpstrn, substrn) |
---|
96 | WHILE pos GE 0 DO BEGIN |
---|
97 | count = count + 1 |
---|
98 | pos = rstrpos(tmpstrn, substrn, pos) |
---|
99 | ENDWHILE |
---|
100 | ENDELSE |
---|
101 | |
---|
102 | return, count |
---|
103 | END |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|