Method: Diff::LCS.__diff_direction

Defined in:
lib/gems/diff-lcs-1.1.2/lib/diff/lcs.rb

.__diff_direction(src, patchset, limit = nil) ⇒ Object

Examine the patchset and the source to see in which direction the patch should be applied.

WARNING: By default, this examines the whole patch, so this could take some time. This also works better with Diff::LCS::ContextChange or Diff::LCS::Change as its source, as an array will cause the creation of one of the above.



939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs.rb', line 939

def __diff_direction(src, patchset, limit = nil)
  count = left = left_miss = right = right_miss = 0
  string = src.kind_of?(String)

  patchset.each do |change|
    count += 1

    case change
    when Diff::LCS::Change
        # With a simplistic change, we can't tell the difference between
        # the left and right on '!' actions, so we ignore those. On '='
        # actions, if there's a miss, we miss both left and right.
      element = string ? src[change.position, 1] : src[change.position]

      case change.action
      when '-'
        if element == change.element
          left += 1
        else
          left_miss += 1
        end
      when '+'
        if element == change.element
          right += 1
        else
          right_miss += 1
        end
      when '='
        if element != change.element
          left_miss += 1
          right_miss += 1
        end
      end
    when Diff::LCS::ContextChange
      case change.action
      when '-' # Remove details from the old string
        element = string ? src[change.old_position, 1] : src[change.old_position]
        if element == change.old_element
          left += 1
        else
          left_miss += 1
        end
      when '+'
        element = string ? src[change.new_position, 1] : src[change.new_position]
        if element == change.new_element
          right += 1
        else
          right_miss += 1
        end
      when '='
        le = string ? src[change.old_position, 1] : src[change.old_position]
        re = string ? src[change.new_position, 1] : src[change.new_position]

        left_miss += 1 if le != change.old_element
        right_miss += 1 if re != change.new_element
      when '!'
        element = string ? src[change.old_position, 1] : src[change.old_position]
        if element == change.old_element
          left += 1
        else
          element = string ? src[change.new_position, 1] : src[change.new_position]
          if element == change.new_element
            right += 1
          else
            left_miss += 1
            right_miss += 1
          end
        end
      end
    end

    break if not limit.nil? and count > limit
  end

  no_left = (left == 0) and (left_miss >= 0)
  no_right = (right == 0) and (right_miss >= 0)

  case [no_left, no_right]
  when [false, true]
    return :patch
  when [true, false]
    return :unpatch
  else
    raise "The provided patchset does not appear to apply to the provided value as either source or destination value."
  end
end