public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv ) { bool all = false; bool nocase = false; try { int i = 1; while ( argv[i].ToString().StartsWith( "-" ) ) { int index = TclIndex.get( interp, argv[i], validOpts, "switch", 0 ); i++; switch ( index ) { case OPT_ALL: { all = true; break; } case OPT_NOCASE: { nocase = true; break; } case OPT_LAST: { goto opts_brk; } } } opts_brk: ; TclObject exp = argv[i++]; string inString = argv[i++].ToString(); string subSpec = argv[i++].ToString(); string varName = null; if (i != argv.Length) varName = argv[i++].ToString(); if ( i != argv.Length ) { throw new System.IndexOutOfRangeException(); } Regexp r = TclRegexp.compile( interp, exp, nocase ); int count = 0; string result; if ( all == false ) { result = r.sub( inString, subSpec ); if ( (System.Object)result == null ) { result = inString; } else { count++; } } else { StringBuilder sb = new StringBuilder(); Regsub s = new Regsub( r, inString ); while ( s.nextMatch() ) { count++; sb.Append( s.skipped() ); Regexp.applySubspec( s, subSpec, sb ); } sb.Append( s.rest() ); result = sb.ToString(); } TclObject obj = TclString.newInstance( result ); if ( varName == null ) interp.setResult( result ); else { try { interp.setVar( varName, obj, 0 ); } catch ( TclException e ) { throw new TclException( interp, "couldn't set variable \"" + varName + "\"" ); } interp.setResult( count ); } } catch ( System.IndexOutOfRangeException e ) { throw new TclNumArgsException( interp, 1, argv, "?switches? exp string subSpec ?varName?" ); } return TCL.CompletionCode.RETURN; }
public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv) { bool all = false; bool nocase = false; try { int i = 1; while (argv[i].ToString().StartsWith("-")) { int index = TclIndex.get(interp, argv[i], validOpts, "switch", 0); i++; switch (index) { case OPT_ALL: { all = true; break; } case OPT_NOCASE: { nocase = true; break; } case OPT_LAST: { goto opts_brk; } } } opts_brk :; TclObject exp = argv[i++]; string inString = argv[i++].ToString(); string subSpec = argv[i++].ToString(); string varName = argv[i++].ToString(); if (i != argv.Length) { throw new System.IndexOutOfRangeException(); } Regexp r = TclRegexp.compile(interp, exp, nocase); int count = 0; string result; if (all == false) { result = r.sub(inString, subSpec); if ((System.Object)result == null) { result = inString; } else { count++; } } else { System.Text.StringBuilder sb = new System.Text.StringBuilder(); Regsub s = new Regsub(r, inString); while (s.nextMatch()) { count++; sb.Append(s.skipped()); Regexp.applySubspec(s, subSpec, sb); } sb.Append(s.rest()); result = sb.ToString(); } TclObject obj = TclString.newInstance(result); try { interp.setVar(varName, obj, 0); } catch (TclException e) { throw new TclException(interp, "couldn't set variable \"" + varName + "\""); } interp.setResult(count); } catch (System.IndexOutOfRangeException e) { throw new TclNumArgsException(interp, 1, argv, "?switches? exp string subSpec varName"); } return(TCL.CompletionCode.RETURN); }