source: trunk/SRC/Textoidl/matchdelim.pro @ 272

Last change on this file since 272 was 232, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1;+
2;
3; @file_comments
4; Match open/close delimiters in a string.
5;
6; @categories
7; Text, String
8;
9; @param INSTRING {in}{required}{type=string}
10; a string containing an open delimiter (e.g. '{') in which you
11; want to find the matching closing delimiter (e.g. '}')
12;
13; @param OPENPOS {in}{optional}
14; Set to a named variable to receive the
15; position of the first opening delimiter.
16;
17; @keyword OPEN_DELIM {default='{'}
18; A single character containing the opening
19; delimiter (e.g. '(').
20;
21; @keyword CLOSE_DELIM {default='}'}
22; A single character containing the closing
23; delimiter (e.g. ')').
24;
25; @returns
26;
27;
28; @uses
29;
30;
31; @restrictions
32; Any pair of (nonidentical) characters can be used as delimiters.
33;
34; @examples
35; matchdelim('{one{two}}three') returns 9, the character just before 'three'.
36;
37; @history
38; $Log: matchdelim.pro,v $
39; Revision 1.3  1996/06/14 20:00:27  mcraig
40; Updated Copyright info.
41;
42; Revision 1.2  1996/05/09 00:22:17  mcraig
43; Removed restriction that open delim must be first char.  Added argument
44; to allow for return of position of open delim.
45;
46; Revision 1.1  1996/01/31 18:41:06  mcraig
47; Initial revision;
48;
49; Copyright (C) 1996 The Regents of the University of California, All
50; Rights Reserved.  Written by Matthew W. Craig.
51; See the file COPYRIGHT for restrictions on distrubting this code.
52; This code comes with absolutely NO warranty; see DISCLAIMER for details.
53;
54; @version
55; $Id$
56;-
57;
58FUNCTION 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
136END
137
138       
139
140
141
Note: See TracBrowser for help on using the repository browser.