Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Line Diff causes an exception #16

Closed

Description

@urigoren

Running the example usage from here on the javascript version:

  const text1="this\n is\n a\n test\n";
  const text2="this\n wasn't\n a\n test\n";
  // Example code "as is":
  var dmp = new diff_match_patch();
  var a = dmp.diff_linesToChars_(text1, text2);
  var lineText1 = a[0];
  var lineText2 = a[1];
  var lineArray = a[2];
  var diffs = dmp.diff_main(lineText1, lineText2, false);
  dmp.diff_charsToLines_(diffs, lineArray);

Causes this exception:

Uncaught Error: Null input. (diff_main)
at diff_match_patch.diff_main (:2:190)
at :6:19

Activity

  1. NeilFraser commented on May 24, 2018

    @NeilFraser
    Contributor

    Good catch, the wiki is out of date. Use these three lines instead:

    var lineText1 = a.chars1;
    var lineText2 = a.chars2;
    var lineArray = a.lineArray;
    

    Fixing the wiki page now. Thanks!

  2. urigoren commented on May 25, 2018

    @urigoren
    Author

    Thank you for the swift response, if you're editing the docs - you might want to use more explicit variable name than a.
    For example:

    function diff_lineMode(text1, text2) {
      var dmp = new diff_match_patch();
      var lineMode = dmp.diff_linesToChars_(text1, text2);
      var diffs = dmp.diff_main(lineMode.chars1, lineMode.chars2, false);
      dmp.diff_charsToLines_(diffs, lineMode.lineArray);
      return diffs;
    }
    

    Also, why is diff_linesToWords a part of the docs and not a part of the library itself ?

    Thank you so much !

  3. NeilFraser commented on May 25, 2018

    @NeilFraser
    Contributor

    Your suggestion is optimal for JavaScript and Java where an object is returned, but in other languages such as Python the return value is an array. So I think the code on the wiki is a good compromise, that makes it relatively easy for people to translate the example into the language they are using.

    The reason diff_linesToWords is not part of the library is that nobody can agree on the definition of a word. I developed the word count for Google Docs, and learned that it's a fairly arbitrary number. Open Office, MS Word, and Google Docs all report significantly different word counts for the same non-trivial document. For example, is "coca-cola.com" one or two or three words? And then there's the nightmare of what to do about CJK languages. If I were to add a word-splitting algorithm to diff-match-patch, I'd end up dealing with conflicting bug reports for the rest of my life. :)

  4. urigoren commented on May 25, 2018

    @urigoren
    Author

    Got your note a regarding the word-splitting, didn't think about that.

    However, regarding the return type of diff_linesToChars_, I am not a master in lua or dart, but in python, its super simple.

    Just wrap the return value with a namedtuple.
    This will allow your users to access the object as an array or by its attributes - backward compatible, and it works for python2 and python3.

    See example:

    from collections import namedtuple
    LineModeObj = namedtuple("LineModeObj", ("chars1","chars2","lineArray"))
    

    And all you need to do is replace this line with

    return LineModeObj (chars1, chars2, lineArray)
    

    If you grant me collaborator rights, I can open a pull request myself

  5. NeilFraser commented on May 25, 2018

    @NeilFraser
    Contributor

    Unfortunately Python is just one example. C#, C++/QT, and ObjectiveC all use arrays/lists/tuples as return values, and some of the ports not maintained in this repo (such as Go) also use arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions