Advertisement
Guest User

full code

a guest
Mar 22nd, 2019
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 10.39 KB | None | 0 0
  1. package sed
  2.  
  3. class Buffer(s: String) {
  4.  
  5.   import scala.collection.mutable.StringBuilder
  6.   import scala.io.StdIn.readInt
  7.  
  8.   private var buffer: StringBuilder = new StringBuilder(s)
  9.  
  10.   private var cursor: Int = 0  // cursor is in between characters
  11.  
  12.   private var marker: Int = 0  // marker is in between characters
  13.  
  14.   private var paste: String = ""
  15.  
  16.   /*
  17.  
  18.    * A buffer holds a mutable string and maintains a current cursor position
  19.  
  20.    * (cursor) and a position marker (marker). The cursor (and marker) can occupy
  21.  
  22.    * any index between zero and the length of the buffer.  We consider the cursor
  23.  
  24.    * to lie 'between' characters as indicated by the ^ in the diagram below.
  25.  
  26.    * Initially, the cursor is at position zero.
  27.  
  28.    *
  29.  
  30.    *     B U F F E R
  31.  
  32.    *    ^              In this example the cursor is at position zero.
  33.  
  34.    *    0 1 2 3 4 5 6
  35.  
  36.    *    
  37.  
  38.    *  The marker is also used to define a region.  For example, mark the current
  39.  
  40.    *  cursor position; then move the cursor to a new position. E.g.
  41.  
  42.    *  
  43.  
  44.    *     B U F F E R         marker = 0
  45.  
  46.    *    ^_______^            cursor = 4
  47.  
  48.    *    m       c            This highlights the region between 0 and 4
  49.  
  50.    *                         i.e. the letters:  BUFF
  51.  
  52.    */
  53.  
  54.   private def end: Int = buffer.length              // the end of the line
  55.  
  56.   private def lwr: Int = Math.min(marker, cursor)
  57.  
  58.   private def upr: Int = Math.max(marker, cursor)
  59.  
  60.   /*
  61.  
  62.    * Accessor methods to return aspects of the state
  63.  
  64.    */
  65.  
  66.   def getCursor: Int = cursor
  67.  
  68.   def getMarker: Int = marker
  69.  
  70.   def getString: String = buffer.toString
  71.  
  72.   def getPaste: String = paste
  73.  
  74.   /**
  75.    * Repeatedly run a sequence of commands
  76.    */
  77.  
  78.   def rpt (n: Int) ( commands: => Unit ) {
  79.  
  80.     for (i <- 1 to n) { commands }
  81.  
  82.   }
  83.  
  84.   /********************************************************************************
  85.  
  86.    * COURSEWORK STARTS HERE - COMPLETE THE DEFINITIONS OF EACH OF THE OPERATIONS
  87.  
  88.    * WE SUGGEST YOU RUN THE BufferTest SUITE AFTER EVERY CHANGE YOU MAKE TO THESE
  89.  
  90.    * SO YOU CAN SEE PROGRESS AND CHECK THAT YOU'VE NOT BROKEN ANYTHING THAT USED
  91.  
  92.    * TO WORK.
  93.  
  94.    *******************************************************************************/
  95.   /**
  96.  
  97.    * AL: Arrow Left.  Move the cursor one place to the left.  If the cursor is
  98.  
  99.    * already at position zero then it wraps around to the end of the buffer.
  100.  
  101.    * This operation does not change any other state variables.
  102.  
  103.    */
  104.  
  105.  
  106.  
  107.   def al() {
  108.  
  109.    if(cursor == 0) {
  110.     cursor = end
  111.    } else {
  112.       cursor = cursor - 1
  113. }
  114.   }
  115.  
  116.   /**
  117.  
  118.    * AR: Arrow Right.  Move the cursor one place to the right. If the cursor is
  119.  
  120.    * already at the end then it wraps around to position zero.
  121.  
  122.    * This operation does not change any other state variables.
  123.  
  124.    */
  125.  
  126.   def ar() {
  127.     if(cursor == end) {
  128.     cursor = 0
  129.     }else{
  130.       cursor = cursor + 1
  131.     }
  132.   }
  133.  
  134.   /**
  135.  
  136.    * Go to (move) the cursor to a specific position.  This is limited so that if
  137.  
  138.    * the requested position is out of the valid cursor range then the cursor is
  139.  
  140.    * not moved at all.  That is, only valid moves succeed.
  141.  
  142.    * This operation does not change any other state variables.
  143.  
  144.    */
  145.  
  146.  def go(n: Int) {
  147.    if(n <= end && n >= 0) {
  148.      cursor = n
  149.   }
  150.  
  151. }
  152.  
  153.  
  154.   /**
  155.  
  156.    * To Left.  Move the cursor to the left-most (zero) position. This operation
  157.  
  158.    * does not change any other state variables.
  159.  
  160.    */
  161.  
  162.   def tl() {
  163.     cursor = 0
  164.   }
  165.  
  166.  
  167.  
  168.   /**
  169.  
  170.    * To Right. Move the cursor to the right-most (end of buffer) position. This
  171.  
  172.    * operation does not change any other state variables.
  173.  
  174.    */
  175.  
  176.   def tr() {
  177.     cursor = end
  178.   }
  179.  
  180.  
  181.  
  182.   /**
  183.  
  184.    * Define Region.  Save the current cursor position (as marker). Only changes
  185.  
  186.    * the marker. This operation does not change any other state variables.
  187.  
  188.    */
  189.  
  190.   def dr() {
  191.     marker = cursor
  192.   }
  193.  
  194.  
  195.  
  196.   /**
  197.  
  198.    * Define All.  Set the marker to zero, and the cursor to end.  This marks
  199.  
  200.    * the entire buffer. This operation does not change the paste buffer state.
  201.  
  202.    */
  203.  
  204.   def da() {
  205.     cursor = end
  206.     marker = 0
  207.  
  208.   }
  209.  
  210.  
  211.  
  212.   /**
  213.  
  214.    * Enter String. Insert the given string at the current cursor position.
  215.  
  216.    * After insertion the cursor moves to the end of the inserted text.  E.g.
  217.  
  218.    *
  219.  
  220.    *     B U F F E R      
  221.  
  222.    *          ^            cursor = 3
  223.  
  224.    *
  225.  
  226.    * Then perform  es("XYZ")
  227.  
  228.    *
  229.  
  230.    *     B U F X Y Z F E R      
  231.  
  232.    *                ^      cursor = 6
  233.  
  234.    *
  235.  
  236.    * This operation modifies the string buffer and the cursor position. The
  237.  
  238.    * paste buffer and the marker position remain unchanged.
  239.  
  240.    */
  241.  
  242.   def es(s: String) {
  243.    
  244.  
  245.     buffer.insert(cursor, s);
  246.     cursor = cursor + s.length
  247.  
  248.   }
  249.  
  250.   /**
  251.  
  252.    * Copy the contents of the marked region to the paste buffer. This operation
  253.  
  254.    * updates the paste buffer but does not change any other state variables.
  255.  
  256.    */
  257.  
  258.  
  259.   def xc() {
  260.    
  261.     var tmpString = buffer.toString()
  262.    
  263.      paste = tmpString.subSequence(lwr, upr).toString()
  264.   }
  265.  
  266.   /**
  267.  
  268.    * Delete the contents of the marked region and save the cut string in the paste
  269.  
  270.    * buffer. This operation re-sets the cursor and the marker to the start of the
  271.  
  272.    * cut text. For example:
  273.  
  274.    *
  275.  
  276.    *     B U F F E R      marker = 1      
  277.  
  278.    *      ^     ^         cursor = 4
  279.  
  280.    *
  281.  
  282.    * Then perform  xd()
  283.  
  284.    *
  285.  
  286.    *     B E R           marker = 1
  287.  
  288.    *      ^              cursor = 1
  289.  
  290.    *
  291.  
  292.    */
  293.  
  294.   def xd() {
  295.  
  296.   }
  297.  
  298.  
  299.  
  300.   /**
  301.  
  302.    * Insert the contents of the paste buffer at the current cursor position.
  303.  
  304.    * Effectively, this is the same as calling es() with the contents of the
  305.  
  306.    * paste buffer as the string to be inserted. Therefore the cursor aligns
  307.  
  308.    * with the end of the inserted string.
  309.  
  310.    */
  311.  
  312.   def xp() {
  313.  
  314.   }
  315.  
  316.  
  317.  
  318.   /**
  319.  
  320.    * Edit Erase  (forward delete).  Delete the character to the right of the
  321.  
  322.    * cursor. If the cursor is at the end of the buffer then ignore this command
  323.  
  324.    * (there is no character to the right of the cursor). If deletion succeeds
  325.  
  326.    * then do not change the cursor position.  The marker position is not
  327.  
  328.    * changed UNLESS the deletion has caused it to point beyond the end of the
  329.  
  330.    * (updated) buffer. In this case make the marker equal to end.
  331.  
  332.    */
  333.  
  334.   def ee() {
  335.  
  336.   }
  337.  
  338.  
  339.  
  340.   /**
  341.  
  342.    * Edit Delete (backspace/backward delete). Delete the character to the left of
  343.  
  344.    * the cursor. If the cursor is at the start of the buffer (index zero) then
  345.  
  346.    * ignore this command (there is no character to the left of the cursor). If
  347.  
  348.    * deletion succeeds then the cursor is moved one place to the left. The marker
  349.  
  350.    * position is not changed UNLESS the deletion has caused it to point beyond the
  351.  
  352.    * end of the (updated) buffer. In this case make the marker equal to end.
  353.  
  354.    */
  355.  
  356.   def ed() {
  357.  
  358.   }
  359.  
  360.  
  361.  
  362.   /**
  363.  
  364.    * Update the buffer by inverting the case of any alphabetic characters within
  365.  
  366.    * the defined region.  Only the characters between cursor and marker are
  367.  
  368.    * therefore affected.  This operation does not change any other state variables.
  369.  
  370.    */
  371.  
  372.   def cc() {
  373.  
  374.   }
  375.  
  376.  
  377.  
  378.   /**
  379.  
  380.    * Substitute Characters.  Within the defined region substitute any instances of
  381.  
  382.    * character s with character t. This operation does not change any other state
  383.  
  384.    * variables.  For example:
  385.  
  386.    *
  387.  
  388.    *     a a r d v a r k s      marker = 1      
  389.  
  390.    *      ^           ^         cursor = 7
  391.  
  392.    *
  393.  
  394.    * Then perform  sc('a', 'X')
  395.  
  396.    *
  397.  
  398.    *     a X r d v X r k s      marker = 1      
  399.  
  400.    *      ^           ^         cursor = 7
  401.  
  402.    */
  403.  
  404.   def sc(s: Char, t: Char) {
  405.  
  406.   }
  407.  
  408.  
  409.  
  410.   /**
  411.  
  412.    * Delete Duplicate characters.  Within the defined region, for each character,
  413.  
  414.    * if it occurs once then keep it, but if it occurs multiple times then keep
  415.  
  416.    * only the first occurrence.  The characters to the left and right of the
  417.  
  418.    * defined region remain unchanged, but within the defined region the duplicates
  419.  
  420.    * are removed. This operation does not affect the paste buffer. The marker is
  421.  
  422.    * placed finally at the lower end of the defined region and the cursor is placed
  423.  
  424.    * finally at the upper end of the (probably reduced) defined region. For example:
  425.  
  426.    *
  427.  
  428.    *     m i s s i s s i p p i       marker =  1      
  429.  
  430.    *      ^                 ^        cursor = 10
  431.  
  432.    *
  433.  
  434.    * Then perform  sc('a', 'X')
  435.  
  436.    *
  437.  
  438.    *     m i s p i      marker = 1      
  439.  
  440.    *      ^     ^       cursor = 4
  441.  
  442.    */
  443.  
  444.   def dd() {
  445.  
  446.   }
  447.  
  448.  
  449.  
  450.   /**
  451.  
  452.    * Find Forwards.  Starting at the current cursor position, locate the next
  453.  
  454.    * occurrence of the character c. If the cursor reaches the end of the string
  455.  
  456.    * without finding an occurrence of c then return false and return leave the
  457.  
  458.    * cursor position unchanged. If the cursor finds an occurrence of c then leave
  459.  
  460.    * the cursor at that position and return true.
  461.  
  462.    * This operation does not change any variable other than the cursor position.
  463.  
  464.    * Example:
  465.  
  466.    *     m i s s i s s i p p i      
  467.  
  468.    *    ^                          cursor =  0
  469.  
  470.    *
  471.  
  472.    * Then perform  ff('i')
  473.  
  474.    *
  475.  
  476.    *     m i s s i s s i p p i            
  477.  
  478.    *      ^                        cursor =  1: returns true
  479.  
  480.    *  
  481.  
  482.    * Then perform  ff('i')
  483.  
  484.    *
  485.  
  486.    *     m i s s i s s i p p i            
  487.  
  488.    *      ^                        cursor =  1: returns true
  489.  
  490.    *  
  491.  
  492.    * Then perform  ff('p')
  493.  
  494.    *
  495.  
  496.    *     m i s s i s s i p p i            
  497.  
  498.    *                    ^          cursor =  8: returns true
  499.  
  500.    *                    
  501.  
  502.    *  Then perform  ff('s')
  503.  
  504.    *
  505.  
  506.    *     m i s s i s s i p p i            
  507.  
  508.    *    ^                          cursor = 0: returns false
  509.  
  510.    */
  511.  
  512.   def ff(c: Char): Boolean = {
  513.  
  514.     return false;
  515.  
  516.   }
  517.  
  518.  
  519.  
  520.   /**
  521.  
  522.    * Search Forwards.  This generalises the character version of find forwards
  523.  
  524.    * The user provides a predicate to specify the search condition.
  525.  
  526.    * This operation does not change any variable other than the cursor position.
  527.  
  528.    */
  529.  
  530.   def sf(p: Char => Boolean): Boolean = {
  531.  
  532.     return false;
  533.  
  534.   }
  535.  
  536. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement