01: /*
02: * LrangeCmd.java
03: *
04: * Copyright (c) 1997 Cornell University.
05: * Copyright (c) 1997 Sun Microsystems, Inc.
06: *
07: * See the file "license.terms" for information on usage and
08: * redistribution of this file, and for a DISCLAIMER OF ALL
09: * WARRANTIES.
10: *
11: * RCS: @(#) $Id: LrangeCmd.java,v 1.2 2000/03/17 23:31:30 mo Exp $
12: *
13: */
14:
15: package tcl.lang;
16:
17: /**
18: * This class implements the built-in "lrange" command in Tcl.
19: */
20:
21: class LrangeCmd implements Command {
22: /**
23: * See Tcl user documentation for details.
24: * @exception TclException If incorrect number of arguments.
25: */
26:
27: public void cmdProc(Interp interp, TclObject argv[])
28: throws TclException {
29: if (argv.length != 4) {
30: throw new TclNumArgsException(interp, 1, argv,
31: "list first last");
32: }
33:
34: int size = TclList.getLength(interp, argv[1]);
35: int first;
36: int last;
37:
38: first = Util.getIntForIndex(interp, argv[2], size - 1);
39: last = Util.getIntForIndex(interp, argv[3], size - 1);
40:
41: if (last < 0) {
42: interp.resetResult();
43: return;
44: }
45: if (first >= size) {
46: interp.resetResult();
47: return;
48: }
49: if (first <= 0 && last >= size) {
50: interp.setResult(argv[1]);
51: return;
52: }
53:
54: if (first < 0) {
55: first = 0;
56: }
57: if (first >= size) {
58: first = size - 1;
59: }
60: if (last < 0) {
61: last = 0;
62: }
63: if (last >= size) {
64: last = size - 1;
65:
66: }
67: if (first > last) {
68: interp.resetResult();
69: return;
70: }
71:
72: TclObject list = TclList.newInstance();
73:
74: list.preserve();
75: try {
76: for (int i = first; i <= last; i++) {
77: TclList.append(interp, list, TclList.index(interp,
78: argv[1], i));
79: }
80: interp.setResult(list);
81: } finally {
82: list.release();
83: }
84: }
85: }
|