1 | ; |
---|
2 | ;+ |
---|
3 | ; NAME: |
---|
4 | ; NEXTTOK |
---|
5 | ; PURPOSE: |
---|
6 | ; Find the next occurance of any of a set of characters in a |
---|
7 | ; string and return the character which occurs next. |
---|
8 | ; CATEGORY: |
---|
9 | ; text/strings |
---|
10 | ; CALLING SEQUENCE: |
---|
11 | ; tok = nexttok( strn, tokens ) |
---|
12 | ; INPUTS: |
---|
13 | ; strn -- string to be searched for sub/superscripts in |
---|
14 | ; tokens -- string containing characters to be found. in |
---|
15 | ; KEYWORD PARAMETERS: |
---|
16 | ; POSITION -- Set to a named variable to get position out |
---|
17 | ; of next token, or -1 if none found. |
---|
18 | ; /HELP -- Print useful message and exit. |
---|
19 | ; OUTPUTS: |
---|
20 | ; tok -- Contains the character among tokens which out |
---|
21 | ; occurs next in strn, or null '' if none found. |
---|
22 | ; COMMON BLOCKS: |
---|
23 | ; SIDE EFFECTS: |
---|
24 | ; NOTES: |
---|
25 | ; EXAMPLE: |
---|
26 | ; nexttok( 'x^2 + N_j^3', '^_', position=pos ) returns '^' and sets |
---|
27 | ; pos to 1. |
---|
28 | ; MODIFICATION HISTORY: |
---|
29 | ; $Id$ |
---|
30 | ; $Log: nexttok.pro,v $ |
---|
31 | ; Revision 1.4 2004/06/15 17:25:54 mcraig |
---|
32 | ; Fixed bug in regular expression, changed array notation to square brackets |
---|
33 | ; |
---|
34 | ; Revision 1.3 1996/06/14 20:00:27 mcraig |
---|
35 | ; Updated Copyright info. |
---|
36 | ; |
---|
37 | ; Revision 1.2 1996/05/09 00:22:17 mcraig |
---|
38 | ; Generalized so that the next occurence of any of a set of characters will |
---|
39 | ; be returned. |
---|
40 | ; |
---|
41 | ; Revision 1.1 1996/01/31 18:41:06 mcraig |
---|
42 | ; Initial revision |
---|
43 | ; |
---|
44 | ; RELEASE: |
---|
45 | ; $Name: Rel_2_1_2 $ |
---|
46 | ; |
---|
47 | ; COPYRIGHT: |
---|
48 | ; Copyright (C) 1996 The Regents of the University of California, All |
---|
49 | ; Rights Reserved. Written by Matthew W. Craig. |
---|
50 | ; See the file COPYRIGHT for restrictions on distrubting this code. |
---|
51 | ; This code comes with absolutely NO warranty; see DISCLAIMER for details. |
---|
52 | ;- |
---|
53 | FUNCTION nexttok, strn, tokens, $ |
---|
54 | POSITION=position, $ |
---|
55 | HELP=Help |
---|
56 | ; |
---|
57 | compile_opt idl2, strictarrsubs |
---|
58 | ; |
---|
59 | |
---|
60 | ; Return to caller on error. |
---|
61 | On_error, 2 |
---|
62 | |
---|
63 | ; Help those in need of it. |
---|
64 | IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN |
---|
65 | offset = ' ' |
---|
66 | print, offset+'Find the next occurance of any of a set of characters in a' |
---|
67 | print, offset+'string and return the character which occurs next.' |
---|
68 | ; CALLING SEQUENCE: |
---|
69 | print, offset+'tok = nexttok( strn, tokens )' |
---|
70 | ; INPUTS: |
---|
71 | print, offset+'Inputs:' |
---|
72 | print, offset+offset+'strn -- string to be searched for sub/superscripts in' |
---|
73 | print, offset+offset+'tokens -- string containing characters to be found. in' |
---|
74 | ; KEYWORD PARAMETERS: |
---|
75 | print, offset+'Keywords:' |
---|
76 | print, offset+offset+'POSITION -- Set to a named variable to get position out' |
---|
77 | print, offset+offset+' of next token, or -1 if none found.' |
---|
78 | print, offset+offset+'/HELP -- Print useful message and exit.' |
---|
79 | ; OUTPUTS: |
---|
80 | print, offset+'Outputs:' |
---|
81 | print, offset+offset+'tok -- Contains the character among tokens which out' |
---|
82 | print, offset+offset+" occurs next in strn, or null '' if none found." |
---|
83 | ; EXAMPLE: |
---|
84 | print, offset+'Example:' |
---|
85 | print, offset+offset+"nexttok( 'x^2 + N_j^3', '^_', position=pos ) returns '^' and sets" |
---|
86 | print, offset+offset+'pos to 1.' |
---|
87 | return, '' |
---|
88 | ENDIF |
---|
89 | |
---|
90 | TmpStr = byte(strn) |
---|
91 | TmpTok = byte(tokens) |
---|
92 | NumToks = n_elements(TmpTok) |
---|
93 | |
---|
94 | MatchIdx = 0L |
---|
95 | Matches = 0L |
---|
96 | FOR j=0, NumToks-1 DO BEGIN |
---|
97 | TmpMatch = where(TmpStr EQ TmpTok[j], TmpCnt) |
---|
98 | IF (TmpCnt GT 0) THEN BEGIN |
---|
99 | MatchIdx = [MatchIdx, Replicate(j, TmpCnt)] |
---|
100 | Matches = [Matches, TmpMatch] |
---|
101 | ENDIF |
---|
102 | ENDFOR |
---|
103 | |
---|
104 | IF n_elements(MatchIdx) EQ 1 THEN BEGIN |
---|
105 | Position = -1 |
---|
106 | return, '' |
---|
107 | ENDIF |
---|
108 | |
---|
109 | MatchIdx = MatchIdx[1:*] |
---|
110 | Matches = Matches[1:*] |
---|
111 | |
---|
112 | SortInd = sort(Matches) |
---|
113 | |
---|
114 | Position = Matches[SortInd[0]] |
---|
115 | |
---|
116 | Tok = string(TmpTok[MatchIdx[SortInd[0]]]) |
---|
117 | |
---|
118 | return, Tok |
---|
119 | END |
---|
120 | |
---|
121 | |
---|
122 | |
---|