vendor/gems/diff-lcs-1.1.2/lib/diff/lcs.rb
# Given a patchset, convert the current version to the new
# version. If +direction+ is not specified (must be
# <tt>:patch</tt> or <tt>:unpatch</tt>), then discovery of the
# direction of the patch will be attempted.
Given a patchset, convert the current version to the new
version. If +direction+ is not specified (must be
:patch or :unpatch), then discovery of the
direction of the patch will be attempted.
Given a patchset convert the current version to the new version . If + direction + is not specified ( must be : patch or : unpatch ) then discovery of the direction of the patch will be attempted .
def patch(src, patchset, direction = nil)
      string = src.kind_of?(String)
      res = src.class.new
      patchset = __normalize_patchset(patchset)
      direction ||= Diff::LCS.__diff_direction(src, patchset)
      direction ||= :patch
      ai = bj = 0
      patchset.each do |change|
        action = PATCH_MAP[direction][change.action]
        case change
        when Diff::LCS::ContextChange
          case direction
          when :patch
            el = change.new_element
            op = change.old_position
            np = change.new_position
          when :unpatch
            el = change.old_element
            op = change.new_position
            np = change.old_position
          end
          case action
          when '-' 
            while ai < op
              res << (string ? src[ai, 1] : src[ai])
              ai += 1
              bj += 1
            end
            ai += 1
          when '+'
            while bj < np
              res << (string ? src[ai, 1] : src[ai])
              ai += 1
              bj += 1
            end
            res << el
            bj += 1
          when '='
            res << el
            ai += 1
            bj += 1
          when '!'
            while ai < op
              res << (string ? src[ai, 1] : src[ai])
              ai += 1
              bj += 1
            end
            bj += 1
            ai += 1
            res << el
          end
        when Diff::LCS::Change
          case action
          when '-'
            while ai < change.position
              res << (string ? src[ai, 1] : src[ai])
              ai += 1
              bj += 1
            end
            ai += 1
          when '+'
            while bj < change.position
              res << (string ? src[ai, 1] : src[ai])
              ai += 1
              bj += 1
            end
            bj += 1
            res << change.element
          end
        end
      end
      while ai < src.size
        res << (string ? src[ai, 1] : src[ai])
        ai += 1
        bj += 1
      end
      res
    end
def patch ( src , patchset , direction = nil ) string = src . kind_of? ( String ) res = src . class . new patchset = __normalize_patchset ( patchset ) direction ||= Diff :: LCS . __diff_direction ( src , patchset ) direction ||= :patch ai = bj = 0 patchset . each do | change | action = PATCH_MAP [ direction ] [ change . action ] case change when Diff :: LCS :: ContextChange case direction when :patch el = change . new_element op = change . old_position np = change . new_position when :unpatch el = change . old_element op = change . new_position np = change . old_position end case action when '-' while ai < op res << ( string ? src [ ai , 1 ] : src [ ai ] ) ai += 1 bj += 1 end ai += 1 when '+' while bj < np res << ( string ? src [ ai , 1 ] : src [ ai ] ) ai += 1 bj += 1 end res << el bj += 1 when '=' res << el ai += 1 bj += 1 when '!' while ai < op res << ( string ? src [ ai , 1 ] : src [ ai ] ) ai += 1 bj += 1 end bj += 1 ai += 1 res << el end when Diff :: LCS :: Change case action when '-' while ai < change . position res << ( string ? src [ ai , 1 ] : src [ ai ] ) ai += 1 bj += 1 end ai += 1 when '+' while bj < change . position res << ( string ? src [ ai , 1 ] : src [ ai ] ) ai += 1 bj += 1 end bj += 1 res << change . element end end end while ai < src . size res << ( string ? src [ ai , 1 ] : src [ ai ] ) ai += 1 bj += 1 end res end
Given a patchset, convert the current version to the new
version.
Given a patchset convert the current version to the new version .
# Start with a new empty type of the source's class # Normalize the patchset. # Both Change and ContextChange support #action # Remove details from the old string # This only appears in sdiff output with the SDiff callback. # Therefore, we only need to worry about dealing with a single # element.
src
patchset
direction