source: trunk/SRC/Textoidl/matchdelim.pro

Last change on this file was 493, checked in by pinsard, 10 years ago

fix some typos in comments

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