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