PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/TCL/src/commands/LreplaceCmd.cs

https://bitbucket.org/eumario/csharp-sqlite
C# | 95 lines | 59 code | 11 blank | 25 comment | 8 complexity | 1d02d958c9607e9a5ef51f270a7dd493 MD5 | raw file
  1. /*
  2. * LreplaceCmd.java
  3. *
  4. * Copyright (c) 1997 Cornell University.
  5. * Copyright (c) 1997 Sun Microsystems, Inc.
  6. *
  7. * See the file "license.terms" for information on usage and
  8. * redistribution of this file, and for a DISCLAIMER OF ALL
  9. * WARRANTIES.
  10. *
  11. * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
  12. *
  13. * RCS @(#) $Id: LreplaceCmd.java,v 1.5 2003/01/09 02:15:39 mdejong Exp $
  14. *
  15. */
  16. using System;
  17. namespace tcl.lang
  18. {
  19. /// <summary> This class implements the built-in "lreplace" command in Tcl.</summary>
  20. class LreplaceCmd : Command
  21. {
  22. /// <summary> See Tcl user documentation for details.</summary>
  23. /// <exception cref=""> TclException If incorrect number of arguments.
  24. /// </exception>
  25. public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv )
  26. {
  27. if ( argv.Length < 4 )
  28. {
  29. throw new TclNumArgsException( interp, 1, argv, "list first last ?element element ...?" );
  30. }
  31. int size = TclList.getLength( interp, argv[1] );
  32. int first = Util.getIntForIndex( interp, argv[2], size - 1 );
  33. int last = Util.getIntForIndex( interp, argv[3], size - 1 );
  34. int numToDelete;
  35. if ( first < 0 )
  36. {
  37. first = 0;
  38. }
  39. // Complain if the user asked for a start element that is greater
  40. // than the list length. This won't ever trigger for the "end*"
  41. // case as that will be properly constrained by getIntForIndex
  42. // because we use size-1 (to allow for replacing the last elem).
  43. if ( ( first >= size ) && ( size > 0 ) )
  44. {
  45. throw new TclException( interp, "list doesn't contain element " + argv[2] );
  46. }
  47. if ( last >= size )
  48. {
  49. last = size - 1;
  50. }
  51. if ( first <= last )
  52. {
  53. numToDelete = ( last - first + 1 );
  54. }
  55. else
  56. {
  57. numToDelete = 0;
  58. }
  59. TclObject list = argv[1];
  60. bool isDuplicate = false;
  61. // If the list object is unshared we can modify it directly. Otherwise
  62. // we create a copy to modify: this is "copy on write".
  63. if ( list.Shared )
  64. {
  65. list = list.duplicate();
  66. isDuplicate = true;
  67. }
  68. try
  69. {
  70. TclList.replace( interp, list, first, numToDelete, argv, 4, argv.Length - 1 );
  71. interp.setResult( list );
  72. }
  73. catch ( TclException e )
  74. {
  75. if ( isDuplicate )
  76. {
  77. list.release();
  78. }
  79. throw;
  80. }
  81. return TCL.CompletionCode.RETURN;
  82. }
  83. }
  84. }