source: trunk/Textoidl/nexttok.pro @ 69

Last change on this file since 69 was 47, checked in by pinsard, 18 years ago

upgrade of TEXT2IDL/TEXtoIDL according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/ : files

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1;
2;+
3; NAME:
4;       NEXTTOK
5; PURPOSE:
6;       Find the next occurance of any of a set of characters in a
7;       string and return the character which occurs next.
8; CATEGORY:
9;       text/strings
10; CALLING SEQUENCE:
11;       tok = nexttok( strn, tokens )
12; INPUTS:
13;       strn   -- string to be searched for sub/superscripts    in
14;       tokens -- string containing characters to be found.     in
15; KEYWORD PARAMETERS:
16;       POSITION -- Set to a named variable to get position     out
17;                   of next token, or -1 if none found.
18;       /HELP    -- Print useful message and exit.
19; OUTPUTS:
20;       tok    -- Contains the character among tokens which     out
21;                 occurs next in strn, or null '' if none found.
22; COMMON BLOCKS:
23; SIDE EFFECTS:
24; NOTES:
25; EXAMPLE:
26;       nexttok( 'x^2 + N_j^3', '^_', position=pos ) returns '^' and sets
27;       pos to 1.
28; MODIFICATION HISTORY:
29;       $Id$
30;       $Log: nexttok.pro,v $
31;       Revision 1.4  2004/06/15 17:25:54  mcraig
32;       Fixed bug in regular expression, changed array notation to square brackets
33;
34;       Revision 1.3  1996/06/14 20:00:27  mcraig
35;       Updated Copyright info.
36;
37;       Revision 1.2  1996/05/09 00:22:17  mcraig
38;       Generalized so that the next occurence of any of a set of characters will
39;       be returned.
40;
41;       Revision 1.1  1996/01/31 18:41:06  mcraig
42;       Initial revision
43;
44; RELEASE:
45;       $Name: Rel_2_1_2 $
46;
47; COPYRIGHT:
48;  Copyright (C) 1996 The Regents of the University of California, All
49;  Rights Reserved.  Written by Matthew W. Craig.
50;  See the file COPYRIGHT for restrictions on distrubting this code.
51;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
52;-
53FUNCTION nexttok, strn, tokens, $
54                  POSITION=position, $
55                  HELP=Help
56
57;  Return to caller on error.
58    On_error, 2
59
60;  Help those in need of it.
61    IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN
62        offset = '   '
63        print, offset+'Find the next occurance of any of a set of characters in a'
64        print, offset+'string and return the character which occurs next.'
65; CALLING SEQUENCE:
66        print, offset+'tok = nexttok( strn, tokens )'
67; INPUTS:
68        print, offset+'Inputs:'
69        print, offset+offset+'strn   -- string to be searched for sub/superscripts    in'
70        print, offset+offset+'tokens -- string containing characters to be found.     in'
71; KEYWORD PARAMETERS:
72        print, offset+'Keywords:'
73        print, offset+offset+'POSITION -- Set to a named variable to get position     out'
74        print, offset+offset+'            of next token, or -1 if none found.'
75        print, offset+offset+'/HELP    -- Print useful message and exit.'
76; OUTPUTS:
77        print, offset+'Outputs:'
78        print, offset+offset+'tok   -- Contains the character among tokens which      out'
79        print, offset+offset+"         occurs next in strn, or null '' if none found."
80; EXAMPLE:
81        print, offset+'Example:'
82        print, offset+offset+"nexttok( 'x^2 + N_j^3', '^_', position=pos ) returns '^' and sets"
83        print, offset+offset+'pos to 1.'
84        return, ''
85    ENDIF
86
87    TmpStr = byte(strn)
88    TmpTok = byte(tokens)
89    NumToks = n_elements(TmpTok)
90
91    MatchIdx = 0L
92    Matches = 0L
93    FOR j=0, NumToks-1 DO BEGIN
94        TmpMatch = where(TmpStr EQ TmpTok(j),  TmpCnt)
95        IF (TmpCnt GT 0) THEN BEGIN
96            MatchIdx = [MatchIdx, Replicate(j, TmpCnt)]
97            Matches = [Matches, TmpMatch]
98        ENDIF
99    ENDFOR
100
101    IF n_elements(MatchIdx) EQ 1 THEN BEGIN
102        Position = -1
103        return, ''
104    ENDIF
105
106    MatchIdx = MatchIdx[1:*]
107    Matches = Matches[1:*]
108
109    SortInd = sort(Matches)
110
111    Position = Matches[SortInd[0]]
112
113    Tok = string(TmpTok[MatchIdx[SortInd[0]]])
114   
115    return, Tok
116END
117
118
119
Note: See TracBrowser for help on using the repository browser.