source: trunk/TEXT2IDL/matchdelim.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
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$
38;       Revision 1.1  2002/09/11 10:17:16  opalod
39;       Initial revision
40;
41;       Revision 1.3  1996/06/14 20:00:27  mcraig
42;       Updated Copyright info.
43;
44;       Revision 1.2  1996/05/09 00:22:17  mcraig
45;       Removed restriction that open delim must be first char.  Added argument
46;       to allow for return of position of open delim.
47;
48;       Revision 1.1  1996/01/31 18:41:06  mcraig
49;       Initial revision
50;
51; RELEASE:
52;       $Name$
53;
54; COPYRIGHT:
55;  Copyright (C) 1996 The Regents of the University of California, All
56;  Rights Reserved.  Written by Matthew W. Craig.
57;  See the file COPYRIGHT for restrictions on distrubting this code.
58;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
59;-
60;
61FUNCTION Matchdelim, InString, OpenPos, $
62                     OPEN_DELIM=OpenDelim, $
63                     CLOSE_DELIM=CloseDelim, $
64                     HELP=Help
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.