1 | ; |
---|
2 | ;+ |
---|
3 | ; NAME: |
---|
4 | ; MATCHDELIM |
---|
5 | ; PURPOSE: |
---|
6 | ; Match open/close delimiters in a string. |
---|
7 | ; CATEGORY: |
---|
8 | ; text/strings |
---|
9 | ; CALLING SEQUENCE: |
---|
10 | ; position = matchdelim( strn, [openpos]) |
---|
11 | ; INPUTS: |
---|
12 | ; strn -- a string containing an open in |
---|
13 | ; delimiter (e.g. '{') in which you |
---|
14 | ; want to find the matching closing |
---|
15 | ; delimiter (e.g. '}') |
---|
16 | ; KEYWORD PARAMETERS: |
---|
17 | ; OPEN_DELIM -- A single character containing the opening in |
---|
18 | ; delimiter (e.g. '('). Default is '{' |
---|
19 | ; CLOSE_DELIM -- A single character containing the closing in |
---|
20 | ; delimiter (e.g. ')'). Default is '}' |
---|
21 | ; OUTPUTS: |
---|
22 | ; position -- returns the position in strn of the out |
---|
23 | ; closing delimiter, -1 if no closing found. |
---|
24 | ; openpos -- Set to a named variable to receive the out |
---|
25 | ; position of the first opening delimiter. |
---|
26 | ; Optional. |
---|
27 | ; COMMON BLOCKS: |
---|
28 | ; SIDE EFFECTS: |
---|
29 | ; NOTES: |
---|
30 | ; - Any pair of (nonidentical) characters can be used as |
---|
31 | ; delimiters. |
---|
32 | ; EXAMPLE: |
---|
33 | ; matchdelim('{one{two}}three') returns 9, the character just |
---|
34 | ; before 'three'. |
---|
35 | ; MODIFICATION HISTORY: |
---|
36 | ; $Id$ |
---|
37 | ; $Log: matchdelim.pro,v $ |
---|
38 | ; Revision 1.3 1996/06/14 20:00:27 mcraig |
---|
39 | ; Updated Copyright info. |
---|
40 | ; |
---|
41 | ; Revision 1.2 1996/05/09 00:22:17 mcraig |
---|
42 | ; Removed restriction that open delim must be first char. Added argument |
---|
43 | ; to allow for return of position of open delim. |
---|
44 | ; |
---|
45 | ; Revision 1.1 1996/01/31 18:41:06 mcraig |
---|
46 | ; Initial revision |
---|
47 | ; |
---|
48 | ; RELEASE: |
---|
49 | ; $Name: Rel_2_1_2 $ |
---|
50 | ; |
---|
51 | ; COPYRIGHT: |
---|
52 | ; Copyright (C) 1996 The Regents of the University of California, All |
---|
53 | ; Rights Reserved. Written by Matthew W. Craig. |
---|
54 | ; See the file COPYRIGHT for restrictions on distrubting this code. |
---|
55 | ; This code comes with absolutely NO warranty; see DISCLAIMER for details. |
---|
56 | ;- |
---|
57 | ; |
---|
58 | FUNCTION Matchdelim, InString, OpenPos, $ |
---|
59 | OPEN_DELIM=OpenDelim, $ |
---|
60 | CLOSE_DELIM=CloseDelim, $ |
---|
61 | HELP=Help |
---|
62 | ; |
---|
63 | compile_opt idl2, strictarrsubs |
---|
64 | ; |
---|
65 | |
---|
66 | ; Return to caller if error. |
---|
67 | On_error, 2 |
---|
68 | |
---|
69 | IF (n_params() LT 1) OR keyword_set(Help) THEN BEGIN |
---|
70 | offset = ' ' |
---|
71 | print, offset+'Match open/close delimiters in a string.' |
---|
72 | print, offset+'position = matchdelim( strn, [openpos])' |
---|
73 | print, offset+'Inputs:' |
---|
74 | print, offset+offset+'strn -- a string containing an open in' |
---|
75 | print, offset+offset+" delimiter (e.g. '{') in which you " |
---|
76 | print, offset+offset+' want to find the matching closing ' |
---|
77 | print, offset+offset+" delimiter (e.g. '}')" |
---|
78 | print, offset+'Keywords:' |
---|
79 | print, offset+offset+'OPEN_DELIM -- A single character containing the opening in' |
---|
80 | print, offset+offset+" delimiter (e.g. '('). Default is '{'" |
---|
81 | print, offset+offset+'CLOSE_DELIM -- A single character containing the closing in' |
---|
82 | print, offset+offset+" delimiter (e.g. ')'). Default is '}'" |
---|
83 | print, offset+'Outputs:' |
---|
84 | print, offset+offset+'position -- returns the position in strn of the out' |
---|
85 | print, offset+offset+' closing delimiter, -1 if no closing found.' |
---|
86 | print, offset+offset+'openpos -- Set to a named variable to receive the out' |
---|
87 | print, offset+offset+' position of the first opening delimiter.' |
---|
88 | print, offset+offset+' Optional.' |
---|
89 | print, offset+'Example:' |
---|
90 | print, offset+offset+"matchdelim('a{one{two}}three') returns 10, the character just" |
---|
91 | print, offset+offset+" before 'three'. " |
---|
92 | print, offset+offset+$ |
---|
93 | "a=matchdelim('aaa[bbb(ccc)]ddd[eee]',f,OP='[',CL=']')" |
---|
94 | print, offset+offset+" returns a=12 (just before ddd), f=3 "+$ |
---|
95 | "(just before bbb)." |
---|
96 | return, -1 |
---|
97 | ENDIF |
---|
98 | |
---|
99 | ; Set default delimiters. |
---|
100 | IF n_elements(OpenDelim) EQ 0 THEN OpenDelim = '{' |
---|
101 | IF n_elements(CloseDelim) EQ 0 THEN CloseDelim = '}' |
---|
102 | |
---|
103 | ; Make sure InString has more than 1 character. |
---|
104 | length = strlen(InString) |
---|
105 | IF (length LE 1) THEN return,-1 |
---|
106 | |
---|
107 | ; Return if no open delimiter |
---|
108 | OpenPos = strpos( InString, OpenDelim ) |
---|
109 | IF (OpenPos EQ -1) THEN BEGIN |
---|
110 | print, 'Error: No opening delimiter' |
---|
111 | return, -1 |
---|
112 | ENDIF |
---|
113 | |
---|
114 | ; Convert strings to array of integers to speed processing. |
---|
115 | OpenDelim = fix((byte(OpenDelim))[0]) |
---|
116 | CloseDelim = fix((byte(CloseDelim))[0]) |
---|
117 | TmpStr = fix(byte(strmid( InString, OpenPos, length))) |
---|
118 | ; Leave the -1* in here. This forces conversion from BYTE to INTEGER, |
---|
119 | ; necessary because there are no negative BYTEs. |
---|
120 | TmpStr = (TmpStr EQ OpenDelim) $ |
---|
121 | -1*(TmpStr EQ CloseDelim) |
---|
122 | length = n_elements(TmpStr) |
---|
123 | |
---|
124 | ; Initialize count of number of delimiters. We've found one, the |
---|
125 | ; first opener. |
---|
126 | BraceCnt = 1 |
---|
127 | i=0 |
---|
128 | WHILE (BraceCnt GT 0) AND (i LT length-1) DO BEGIN |
---|
129 | i = i+1 |
---|
130 | BraceCnt = BraceCnt + TmpStr[i] |
---|
131 | ENDWHILE |
---|
132 | |
---|
133 | i = i + OpenPos |
---|
134 | IF (BraceCnt GT 0) THEN i = -1 |
---|
135 | return, i |
---|
136 | END |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | |
---|