wasCSharpSQLite – Blame information for rev 3
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * SplitCmd.java |
||
3 | * |
||
4 | * Copyright (c) 1997 Sun Microsystems, Inc. |
||
5 | * |
||
6 | * See the file "license.terms" for information on usage and |
||
7 | * redistribution of this file, and for a DISCLAIMER OF ALL |
||
8 | * WARRANTIES. |
||
9 | * |
||
10 | * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart |
||
11 | * |
||
12 | * RCS @(#) $Id: SplitCmd.java,v 1.1.1.1 1998/10/14 21:09:19 cvsadmin Exp $ |
||
13 | * |
||
14 | */ |
||
15 | using System; |
||
16 | namespace tcl.lang |
||
17 | { |
||
18 | |||
19 | /// <summary> This class implements the built-in "split" command in Tcl.</summary> |
||
20 | |||
21 | class SplitCmd : Command |
||
22 | { |
||
23 | /// <summary> Default characters for splitting up strings.</summary> |
||
24 | |||
25 | private static char[] defSplitChars = new char[] { ' ', '\n', '\t', '\r' }; |
||
26 | |||
27 | /// <summary> This procedure is invoked to process the "split" Tcl |
||
28 | /// command. See Tcl user documentation for details. |
||
29 | /// |
||
30 | /// </summary> |
||
31 | /// <param name="interp">the current interpreter. |
||
32 | /// </param> |
||
33 | /// <param name="argv">command arguments. |
||
34 | /// </param> |
||
35 | /// <exception cref=""> TclException If incorrect number of arguments. |
||
36 | /// </exception> |
||
37 | |||
38 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv ) |
||
39 | { |
||
40 | char[] splitChars = null; |
||
41 | string inString; |
||
42 | |||
43 | if ( argv.Length == 2 ) |
||
44 | { |
||
45 | splitChars = defSplitChars; |
||
46 | } |
||
47 | else if ( argv.Length == 3 ) |
||
48 | { |
||
49 | |||
50 | splitChars = argv[2].ToString().ToCharArray(); |
||
51 | } |
||
52 | else |
||
53 | { |
||
54 | throw new TclNumArgsException( interp, 1, argv, "string ?splitChars?" ); |
||
55 | } |
||
56 | |||
57 | |||
58 | inString = argv[1].ToString(); |
||
59 | int len = inString.Length; |
||
60 | int num = splitChars.Length; |
||
61 | |||
62 | /* |
||
63 | * Handle the special case of splitting on every character. |
||
64 | */ |
||
65 | |||
66 | if ( num == 0 ) |
||
67 | { |
||
68 | TclObject list = TclList.newInstance(); |
||
69 | |||
70 | list.preserve(); |
||
71 | try |
||
72 | { |
||
73 | for ( int i = 0; i < len; i++ ) |
||
74 | { |
||
75 | TclList.append( interp, list, TclString.newInstance( inString[i] ) ); |
||
76 | } |
||
77 | interp.setResult( list ); |
||
78 | } |
||
79 | finally |
||
80 | { |
||
81 | list.release(); |
||
82 | } |
||
83 | return TCL.CompletionCode.RETURN; |
||
84 | } |
||
85 | |||
86 | /* |
||
87 | * Normal case: split on any of a given set of characters. |
||
88 | * Discard instances of the split characters. |
||
89 | */ |
||
90 | TclObject list2 = TclList.newInstance(); |
||
91 | int elemStart = 0; |
||
92 | |||
93 | list2.preserve(); |
||
94 | try |
||
95 | { |
||
96 | int i, j; |
||
97 | for ( i = 0; i < len; i++ ) |
||
98 | { |
||
99 | char c = inString[i]; |
||
100 | for ( j = 0; j < num; j++ ) |
||
101 | { |
||
102 | if ( c == splitChars[j] ) |
||
103 | { |
||
104 | TclList.append( interp, list2, TclString.newInstance( inString.Substring( elemStart, ( i ) - ( elemStart ) ) ) ); |
||
105 | elemStart = i + 1; |
||
106 | break; |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | if ( i != 0 ) |
||
111 | { |
||
112 | TclList.append( interp, list2, TclString.newInstance( inString.Substring( elemStart ) ) ); |
||
113 | } |
||
114 | interp.setResult( list2 ); |
||
115 | } |
||
116 | finally |
||
117 | { |
||
118 | list2.release(); |
||
119 | } |
||
120 | return TCL.CompletionCode.RETURN; |
||
121 | } |
||
122 | } |
||
123 | } |