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: matchdelim.pro,v 1.3 1996/06/14 20:00:27 mcraig Exp $ 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; 58FUNCTION Matchdelim, InString, OpenPos, $ 59 OPEN_DELIM=OpenDelim, $ 60 CLOSE_DELIM=CloseDelim, $ 61 HELP=Help 62 63; Return to caller if error. 64 On_error, 2 65 66 IF (n_params() LT 1) OR keyword_set(Help) THEN BEGIN 67 offset = ' ' 68 print, offset+'Match open/close delimiters in a string.' 69 print, offset+'position = matchdelim( strn, [openpos])' 70 print, offset+'Inputs:' 71 print, offset+offset+'strn -- a string containing an open in' 72 print, offset+offset+" delimiter (e.g. '{') in which you " 73 print, offset+offset+' want to find the matching closing ' 74 print, offset+offset+" delimiter (e.g. '}')" 75 print, offset+'Keywords:' 76 print, offset+offset+'OPEN_DELIM -- A single character containing the opening in' 77 print, offset+offset+" delimiter (e.g. '('). Default is '{'" 78 print, offset+offset+'CLOSE_DELIM -- A single character containing the closing in' 79 print, offset+offset+" delimiter (e.g. ')'). Default is '}'" 80 print, offset+'Outputs:' 81 print, offset+offset+'position -- returns the position in strn of the out' 82 print, offset+offset+' closing delimiter, -1 if no closing found.' 83 print, offset+offset+'openpos -- Set to a named variable to receive the out' 84 print, offset+offset+' position of the first opening delimiter.' 85 print, offset+offset+' Optional.' 86 print, offset+'Example:' 87 print, offset+offset+"matchdelim('a{one{two}}three') returns 10, the character just" 88 print, offset+offset+" before 'three'. " 89 print, offset+offset+$ 90 "a=matchdelim('aaa[bbb(ccc)]ddd[eee]',f,OP='[',CL=']')" 91 print, offset+offset+" returns a=12 (just before ddd), f=3 "+$ 92 "(just before bbb)." 93 return, -1 94 ENDIF 95 96; Set default delimiters. 97 IF n_elements(OpenDelim) EQ 0 THEN OpenDelim = '{' 98 IF n_elements(CloseDelim) EQ 0 THEN CloseDelim = '}' 99 100; Make sure InString has more than 1 character. 101 length = strlen(InString) 102 IF (length LE 1) THEN return,-1 103 104; Return if no open delimiter 105 OpenPos = strpos( InString, OpenDelim ) 106 IF (OpenPos EQ -1) THEN BEGIN 107 print, 'Error: No opening delimiter' 108 return, -1 109 ENDIF 110 111; Convert strings to array of integers to speed processing. 112 OpenDelim = fix((byte(OpenDelim))(0)) 113 CloseDelim = fix((byte(CloseDelim))(0)) 114 TmpStr = fix(byte(strmid( InString, OpenPos, length))) 115; Leave the -1* in here. This forces conversion from BYTE to INTEGER, 116; necessary because there are no negative BYTEs. 117 TmpStr = (TmpStr EQ OpenDelim) $ 118 -1*(TmpStr EQ CloseDelim) 119 length = n_elements(TmpStr) 120 121; Initialize count of number of delimiters. We've found one, the 122; first opener. 123 BraceCnt = 1 124 i=0 125 WHILE (BraceCnt GT 0) AND (i LT length-1) DO BEGIN 126 i = i+1 127 BraceCnt = BraceCnt + TmpStr(i) 128 ENDWHILE 129 130 i = i + OpenPos 131 IF (BraceCnt GT 0) THEN i = -1 132 return, i 133END 134 135 136 137 138 139