001: /*
002: * RegsubCmd.java
003: *
004: * This contains the Jacl implementation of the built-in Tcl
005: * "regsub" command.
006: *
007: * Copyright (c) 1997-1999 Sun Microsystems, Inc.
008: *
009: * See the file "license.terms" for information on usage and
010: * redistribution of this file, and for a DISCLAIMER OF ALL
011: * WARRANTIES.
012: *
013: * RCS: @(#) $Id: RegsubCmd.java,v 1.4 2000/02/23 22:07:23 mo Exp $
014: */
015:
016: package tcl.lang;
017:
018: import sunlabs.brazil.util.regexp.Regexp;
019: import sunlabs.brazil.util.regexp.Regsub;
020:
021: /**
022: * This class implements the built-in "regsub" command in Tcl.
023: */
024:
025: class RegsubCmd implements Command {
026:
027: static final private String validOpts[] = { "-all", "-nocase", "--" };
028: static final private int OPT_ALL = 0;
029: static final private int OPT_NOCASE = 1;
030: static final private int OPT_LAST = 2;
031:
032: /*
033: *-----------------------------------------------------------------------------
034: *
035: * cmdProc --
036: *
037: * This procedure is invoked to process the "regsub" Tcl command.
038: * See the user documentation for details on what it does.
039: *
040: * Results:
041: * A standard Tcl result.
042: *
043: * Side effects:
044: * See the user documentation.
045: *
046: *-----------------------------------------------------------------------------
047: */
048:
049: public void cmdProc(Interp interp, // Current interpreter.
050: TclObject argv[]) // Arguments to "regsub" command.
051: throws TclException {
052: boolean all = false;
053: boolean nocase = false;
054:
055: try {
056: int i = 1;
057: opts: while (argv[i].toString().startsWith("-")) {
058: int index = TclIndex.get(interp, argv[i], validOpts,
059: "switch", 0);
060: i++;
061: switch (index) {
062: case OPT_ALL: {
063: all = true;
064: break;
065: }
066: case OPT_NOCASE: {
067: nocase = true;
068: break;
069: }
070: case OPT_LAST: {
071: break opts;
072: }
073: }
074: }
075:
076: TclObject exp = argv[i++];
077: String string = argv[i++].toString();
078: String subSpec = argv[i++].toString();
079: String varName = argv[i++].toString();
080: if (i != argv.length) {
081: throw new IndexOutOfBoundsException();
082: }
083:
084: Regexp r = TclRegexp.compile(interp, exp, nocase);
085:
086: int count = 0;
087: String result;
088:
089: if (all == false) {
090: result = r.sub(string, subSpec);
091: if (result == null) {
092: result = string;
093: } else {
094: count++;
095: }
096: } else {
097: StringBuffer sb = new StringBuffer();
098: Regsub s = new Regsub(r, string);
099: while (s.nextMatch()) {
100: count++;
101: sb.append(s.skipped());
102: Regexp.applySubspec(s, subSpec, sb);
103: }
104: sb.append(s.rest());
105: result = sb.toString();
106: }
107:
108: TclObject obj = TclString.newInstance(result);
109: try {
110: interp.setVar(varName, obj, 0);
111: } catch (TclException e) {
112: throw new TclException(interp,
113: "couldn't set variable \"" + varName + "\"");
114: }
115: interp.setResult(count);
116: } catch (IndexOutOfBoundsException e) {
117: throw new TclNumArgsException(interp, 1, argv,
118: "?switches? exp string subSpec varName");
119: }
120: }
121: } // end RegsubCmd
|