wasCSharpSQLite – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * RegexpCmd.java --
3 *
4 * This file contains the Jacl implementation of the built-in Tcl
5 * "regexp" command.
6 *
7 * Copyright (c) 1997-1999 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and
10 * redistribution of this file, and for a DISCLAIMER OF ALL
11 * WARRANTIES.
12 *
13 * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
14 *
15 * RCS @(#) $Id: RegexpCmd.java,v 1.3 2000/02/23 22:07:23 mo Exp $
16 */
17 using System;
18 using Regexp = sunlabs.brazil.util.regexp.Regexp;
19 namespace tcl.lang
20 {
21  
22 /// <summary> This class implements the built-in "regexp" command in Tcl.</summary>
23  
24 class RegexpCmd : Command
25 {
26  
27 private static readonly string[] validOpts = new string[] { "-indices", "-nocase", "--" };
28 private const int OPT_INDICES = 0;
29 private const int OPT_NOCASE = 1;
30 private const int OPT_LAST = 2;
31 internal static void init( Interp interp )
32 // Current interpreter.
33 {
34 interp.createCommand( "regexp", new tcl.lang.RegexpCmd() );
35 interp.createCommand( "regsub", new tcl.lang.RegsubCmd() );
36 }
37 public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv )
38 {
39 bool nocase = false;
40 bool indices = false;
41  
42 try
43 {
44 int i = 1;
45  
46 while ( argv[i].ToString().StartsWith( "-" ) )
47 {
48 int index = TclIndex.get( interp, argv[i], validOpts, "switch", 0 );
49 i++;
50 switch ( index )
51 {
52  
53 case OPT_INDICES:
54 {
55 indices = true;
56 break;
57 }
58  
59 case OPT_NOCASE:
60 {
61 nocase = true;
62 break;
63 }
64  
65 case OPT_LAST:
66 {
67 goto opts_brk;
68 }
69 }
70 }
71  
72 opts_brk:
73 ;
74  
75  
76 TclObject exp = TclString.newInstance( argv[i++].ToString().Replace( "\\d", "[0-9]" ) );
77  
78 string inString = argv[i++].ToString();
79  
80 int matches = argv.Length - i;
81  
82 Regexp r = TclRegexp.compile( interp, exp, nocase );
83  
84 int[] args = new int[matches * 2];
85 bool matched = r.match( inString, args );
86 if ( matched )
87 {
88 for ( int match = 0; i < argv.Length; i++ )
89 {
90 TclObject obj;
91  
92 int start = args[match++];
93 int end = args[match++];
94 if ( indices )
95 {
96 if ( end >= 0 )
97 {
98 end--;
99 }
100 obj = TclList.newInstance();
101 TclList.append( interp, obj, TclInteger.newInstance( start ) );
102 TclList.append( interp, obj, TclInteger.newInstance( end ) );
103 }
104 else
105 {
106 string range = ( start >= 0 ) ? inString.Substring( start, ( end ) - ( start ) ) : "";
107 obj = TclString.newInstance( range );
108 }
109 try
110 {
111  
112 interp.setVar( argv[i].ToString(), obj, 0 );
113 }
114 catch ( TclException e )
115 {
116  
117 throw new TclException( interp, "couldn't set variable \"" + argv[i] + "\"" );
118 }
119 }
120 }
121 interp.setResult( matched );
122 }
123 catch ( System.IndexOutOfRangeException e )
124 {
125 throw new TclNumArgsException( interp, 1, argv, "?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?" );
126 }
127 return TCL.CompletionCode.RETURN;
128 }
129 } // end RegexpCmd
130 }