{"cells": [{"cell_type": "markdown", "id": "1a0effd2", "metadata": {}, "source": ["# Magic command to compare files\n", "\n", "Some ways to display differences between files."]}, {"cell_type": "code", "execution_count": 1, "id": "04c56557", "metadata": {}, "outputs": [{"data": {"text/html": ["
run previous cell, wait for 2 seconds
\n", ""], "text/plain": [""]}, "execution_count": 2, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import add_notebook_menu\n", "add_notebook_menu()"]}, {"cell_type": "markdown", "id": "39e694bf", "metadata": {}, "source": ["## Two functions slighly different"]}, {"cell_type": "code", "execution_count": 2, "id": "6921cf4c", "metadata": {}, "outputs": [], "source": ["f1 = '''\n", "def edit_distance_string(s1, s2):\n", " \"\"\"\n", " Computes the edit distance between strings *s1* and *s2*.\n", "\n", " :param s1: first string\n", " :param s2: second string\n", " :return: dist, list of tuples of aligned characters\n", " \"\"\"\n", " n1 = len(s1) + 1\n", " n2 = len(s2) + 1\n", " dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)\n", " pred = numpy.full(dist.shape, 0, dtype=numpy.int32)\n", "\n", " for j in range(1, n2):\n", " dist[0, j] = j\n", " pred[0, j] = 2\n", " for i in range(0, n1):\n", " dist[i, 0] = i\n", " pred[i, 0] = 1\n", " pred[0, 0] = -1\n", "\n", " for j in range(1, n2):\n", " for i in range(1, n1):\n", " c = dist[i, j]\n", "\n", " p = 0\n", " if dist[i - 1, j] + 1 < c:\n", " c = dist[i - 1, j] + 1\n", " p = 1\n", " if dist[i, j - 1] + 1 < c:\n", " c = dist[i, j - 1] + 1\n", " p = 2\n", " d = 0 if s1[i - 1] == s2[j - 1] else 1\n", " if dist[i - 1, j - 1] + d < c:\n", " c = dist[i - 1, j - 1] + d\n", " p = 3\n", " if p == 0:\n", " raise RuntimeError(\n", " \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))\n", "\n", " dist[i, j] = c\n", " pred[i, j] = p\n", "\n", " d = dist[len(s1), len(s2)]\n", " return d\n", "'''"]}, {"cell_type": "code", "execution_count": 3, "id": "33b45b02", "metadata": {}, "outputs": [], "source": ["f2 = '''\n", "def edit_distance_string(s1, s2):\n", " \"\"\"\n", " Computes the edit distance between strings *s1* and *s2*.\n", "\n", " :param s1: first string\n", " :param s2: second string\n", " :return: dist, list of tuples of aligned characters\n", " \"\"\"\n", " n1 = len(s1) + 1\n", " n2 = len(s2) + 1\n", " dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)\n", " pred = numpy.full(dist.shape, 0, dtype=numpy.int32)\n", "\n", " for i in range(0, n1):\n", " dist[i, 0] = i\n", " pred[i, 0] = 1\n", " for j in range(1, n2):\n", " dist[0, j] = j\n", " pred[0, j] = 2\n", " pred[0, 0] = -1\n", "\n", " for i in range(1, n1):\n", " for j in range(1, n2):\n", " c = dist[i, j]\n", "\n", " p = 0\n", " if dist[i - 1, j] + 1 < c:\n", " c = dist[i - 1, j] + 1\n", " p = 1\n", " if dist[i, j - 1] + 1 < c:\n", " c = dist[i, j - 1] + 1\n", " p = 2\n", " d = 0 if s1[i - 1] == s2[j - 1] else 1\n", " if dist[i - 1, j - 1] + d < c:\n", " c = dist[i - 1, j - 1] + d\n", " p = 3\n", " if p == 0:\n", " raise RuntimeError(\n", " \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))\n", "\n", " dist[i, j] = c\n", " pred[i, j] = p\n", "\n", " d = dist[len(s1), len(s2)]\n", " equals = []\n", " i, j = len(s1), len(s2)\n", " p = pred[i, j]\n", " while p != -1:\n", " if p == 3:\n", " equals.append((i - 1, j - 1))\n", " i -= 1\n", " j -= 1\n", " elif p == 2:\n", " j -= 1\n", " elif p == 1:\n", " i -= 1\n", " else:\n", " raise RuntimeError(\n", " \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))\n", " p = pred[i, j]\n", " return d, list(reversed(equals))\n", "'''"]}, {"cell_type": "markdown", "id": "bf9521e3", "metadata": {}, "source": ["## Visual differences: codediff"]}, {"cell_type": "code", "execution_count": 4, "id": "12b4a419", "metadata": {}, "outputs": [], "source": ["%load_ext pyquickhelper"]}, {"cell_type": "code", "execution_count": 5, "id": "d61115ef", "metadata": {}, "outputs": [{"data": {"text/html": ["\n"], "text/plain": [""]}, "metadata": {}, "output_type": "display_data"}], "source": ["%%html\n", ""]}, {"cell_type": "markdown", "id": "a129b5ad", "metadata": {}, "source": ["This is slow due to the edit distance computation. It could be improved by a C++ implementation."]}, {"cell_type": "code", "execution_count": 6, "id": "215c3d21", "metadata": {"scrolled": false}, "outputs": [{"name": "stderr", "output_type": "stream", "text": ["100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 47/47 [00:02<00:00, 23.05it/s]\n"]}, {"data": {"text/html": ["\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
00
11def edit_distance_string(s1, s2):
22 \"\"\"
33 Computes the edit distance between strings *s1* and *s2*.
44
55 :param s1: first string
66 :param s2: second string
77 :return: dist, list of tuples of aligned characters
88 \"\"\"
99 n1 = len(s1) + 1
1010 n2 = len(s2) + 1
1111 dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)
1212 pred = numpy.full(dist.shape, 0, dtype=numpy.int32)
1313
1414 for j in range(1, n2):
for i in range(0, n1):
1515 dist[0, j] = j
dist[i, 0] = i
1616 pred[0, j] = 2
pred[i, 0] = 1
1717 for i in range(0, n1):
for j in range(1, n2):
1818 dist[i, 0] = i
dist[0, j] = j
1919 pred[i, 0] = 1
pred[0, j] = 2
2020 pred[0, 0] = -1
2121
2222 for j in range(1, n2):
for i in range(1, n1):
2323 for i in range(1, n1):
for j in range(1, n2):
2424 c = dist[i, j]
2525
2626 p = 0
2727 if dist[i - 1, j] + 1 < c:
2828 c = dist[i - 1, j] + 1
2929 p = 1
3030 if dist[i, j - 1] + 1 < c:
3131 c = dist[i, j - 1] + 1
3232 p = 2
3333 d = 0 if s1[i - 1] == s2[j - 1] else 1
3434 if dist[i - 1, j - 1] + d < c:
3535 c = dist[i - 1, j - 1] + d
3636 p = 3
3737 if p == 0:
3838 raise RuntimeError(
3939 \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))
4040
4141 dist[i, j] = c
4242 pred[i, j] = p
4343
4444 d = dist[len(s1), len(s2)]
45 return d
45 equals = []
46 i, j = len(s1), len(s2)
47 p = pred[i, j]
48 while p != -1:
49 if p == 3:
50 equals.append((i - 1, j - 1))
51 i -= 1
52 j -= 1
53 elif p == 2:
54 j -= 1
55 elif p == 1:
56 i -= 1
57 else:
58 raise RuntimeError(
59 \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))
60 p = pred[i, j]
61 return d, list(reversed(equals))
4662
"], "text/plain": [""]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["%codediff f1 f2 --verbose 1"]}, {"cell_type": "code", "execution_count": 7, "id": "d99dcabc", "metadata": {"scrolled": false}, "outputs": [{"name": "stderr", "output_type": "stream", "text": ["100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 47/47 [00:02<00:00, 22.99it/s]\n"]}, {"data": {"text/html": ["\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
00
11def edit_distance_string(s1, s2):def edit_distance_string(s1, s2):
22 \"\"\" \"\"\"
33 Computes the edit distance between strings *s1* and *s2*. Computes the edit distance between strings *s1* and *s2*.
44
55 :param s1: first string :param s1: first string
66 :param s2: second string :param s2: second string
77 :return: dist, list of tuples of aligned characters :return: dist, list of tuples of aligned characters
88 \"\"\" \"\"\"
99 n1 = len(s1) + 1 n1 = len(s1) + 1
1010 n2 = len(s2) + 1 n2 = len(s2) + 1
1111 dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64) dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)
1212 pred = numpy.full(dist.shape, 0, dtype=numpy.int32) pred = numpy.full(dist.shape, 0, dtype=numpy.int32)
1313
1414 for j in range(1, n2): for i in range(0, n1):
1515 dist[0, j] = j dist[i, 0] = i
1616 pred[0, j] = 2 pred[i, 0] = 1
1717 for i in range(0, n1): for j in range(1, n2):
1818 dist[i, 0] = i dist[0, j] = j
1919 pred[i, 0] = 1 pred[0, j] = 2
2020 pred[0, 0] = -1 pred[0, 0] = -1
2121
2222 for j in range(1, n2): for i in range(1, n1):
2323 for i in range(1, n1): for j in range(1, n2):
2424 c = dist[i, j] c = dist[i, j]
2525
2626 p = 0 p = 0
2727 if dist[i - 1, j] + 1 < c: if dist[i - 1, j] + 1 < c:
2828 c = dist[i - 1, j] + 1 c = dist[i - 1, j] + 1
2929 p = 1 p = 1
3030 if dist[i, j - 1] + 1 < c: if dist[i, j - 1] + 1 < c:
3131 c = dist[i, j - 1] + 1 c = dist[i, j - 1] + 1
3232 p = 2 p = 2
3333 d = 0 if s1[i - 1] == s2[j - 1] else 1 d = 0 if s1[i - 1] == s2[j - 1] else 1
3434 if dist[i - 1, j - 1] + d < c: if dist[i - 1, j - 1] + d < c:
3535 c = dist[i - 1, j - 1] + d c = dist[i - 1, j - 1] + d
3636 p = 3 p = 3
3737 if p == 0: if p == 0:
3838 raise RuntimeError( raise RuntimeError(
3939 \"Unexpected value for p=%d at position=%r.\" % (p, (i, j))) \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))
4040
4141 dist[i, j] = c dist[i, j] = c
4242 pred[i, j] = p pred[i, j] = p
4343
4444 d = dist[len(s1), len(s2)] d = dist[len(s1), len(s2)]
45 return d
45 equals = []
46 i, j = len(s1), len(s2)
47 p = pred[i, j]
48 while p != -1:
49 if p == 3:
50 equals.append((i - 1, j - 1))
51 i -= 1
52 j -= 1
53 elif p == 2:
54 j -= 1
55 elif p == 1:
56 i -= 1
57 else:
58 raise RuntimeError(
59 \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))
60 p = pred[i, j]
61 return d, list(reversed(equals))
4662
"], "text/plain": [""]}, "execution_count": 8, "metadata": {}, "output_type": "execute_result"}], "source": ["%codediff f1 f2 --verbose 1 --two 1"]}, {"cell_type": "markdown", "id": "529e0082", "metadata": {}, "source": ["## strdiff"]}, {"cell_type": "code", "execution_count": 8, "id": "7da6a6e5", "metadata": {"scrolled": false}, "outputs": [{"data": {"text/html": ["
\n", "

\n", "

def edit_distance_string(s1, s2):

def edit_distance_string(s1, s2):

\n", "

"""

"""

\n", "

Computes the edit distance between strings *s1* and *s2*.

Computes the edit distance between strings *s1* and *s2*.

\n", "

:param s1: first string

:param s1: first string

\n", "

:param s2: second string

:param s2: second string

\n", "

:return: dist, list of tuples of aligned characters

:return: dist, list of tuples of aligned characters

\n", "

"""

"""

\n", "

n1 = len(s1) + 1

n1 = len(s1) + 1

\n", "

n2 = len(s2) + 1

n2 = len(s2) + 1

\n", "

dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)

dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)

\n", "

pred = numpy.full(dist.shape, 0, dtype=numpy.int32)

pred = numpy.full(dist.shape, 0, dtype=numpy.int32)

\n", "

for i in range(0, n1):

\n", "

dist[i, 0] = i

\n", "

pred[i, 0] = 1

\n", "

for j in range(1, n2):

for j in range(1, n2):

\n", "

dist[0, j] = j

dist[0, j] = j

\n", "

pred[0, j] = 2

pred[0, j] = 2

\n", "

for i in range(0, n1):

\n", "

dist[i, 0] = i

\n", "

pred[i, 0] = 1

\n", "

pred[0, 0] = -1

pred[0, 0] = -1

\n", "

for j in range(1, n2):

for i in range(1, n1):

\n", "

for i in range(1, n1):

for j in range(1, n2):

\n", "

c = dist[i, j]

c = dist[i, j]

\n", "

p = 0

p = 0

\n", "

if dist[i - 1, j] + 1 < c:

if dist[i - 1, j] + 1 < c:

\n", "

c = dist[i - 1, j] + 1

c = dist[i - 1, j] + 1

\n", "

p = 1

p = 1

\n", "

if dist[i, j - 1] + 1 < c:

if dist[i, j - 1] + 1 < c:

\n", "

c = dist[i, j - 1] + 1

c = dist[i, j - 1] + 1

\n", "

p = 2

p = 2

\n", "

d = 0 if s1[i - 1] == s2[j - 1] else 1

d = 0 if s1[i - 1] == s2[j - 1] else 1

\n", "

if dist[i - 1, j - 1] + d < c:

if dist[i - 1, j - 1] + d < c:

\n", "

c = dist[i - 1, j - 1] + d

c = dist[i - 1, j - 1] + d

\n", "

p = 3

p = 3

\n", "

if p == 0:

if p == 0:

\n", "

raise RuntimeError(

raise RuntimeError(

\n", "

"Unexpected value for p=%d at position=%r." % (p, (i, j)))

"Unexpected value for p=%d at position=%r." % (p, (i, j)))

\n", "

dist[i, j] = c

dist[i, j] = c

\n", "

pred[i, j] = p

pred[i, j] = p

\n", "

d = dist[len(s1), len(s2)]

d = dist[len(s1), len(s2)]

\n", "

return d

equals = []

\n", "

i, j = len(s1), len(s2)

\n", "

p = pred[i, j]

\n", "

while p != -1:

\n", "

if p == 3:

\n", "

equals.append((i - 1, j - 1))

\n", "

i -= 1

\n", "

j -= 1

\n", "

elif p == 2:

\n", "

j -= 1

\n", "

elif p == 1:

\n", "

i -= 1

\n", "

else:

\n", "

raise RuntimeError(

\n", "

"Unexpected value for p=%d at position=%r." % (p, (i, j)))

\n", "

p = pred[i, j]

\n", "

return d, list(reversed(equals))

\n", "

\n", "
"], "text/plain": [""]}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["%strdiff f1 f2"]}, {"cell_type": "markdown", "id": "0e3c6f48", "metadata": {}, "source": ["## textdiff"]}, {"cell_type": "code", "execution_count": 9, "id": "91936d64", "metadata": {}, "outputs": [{"data": {"text/html": ["
populating...
"]}, "metadata": {}, "output_type": "display_data"}, {"data": {"application/javascript": ["/*\n", "This is part of jsdifflib v1.0. \n", "\n", "Copyright 2007 - 2011 Chas Emerick . All rights reserved.\n", "\n", "Redistribution and use in source and binary forms, with or without modification, are\n", "permitted provided that the following conditions are met:\n", "\n", " 1. Redistributions of source code must retain the above copyright notice, this list of\n", " conditions and the following disclaimer.\n", "\n", " 2. Redistributions in binary form must reproduce the above copyright notice, this list\n", " of conditions and the following disclaimer in the documentation and/or other materials\n", " provided with the distribution.\n", "\n", "THIS SOFTWARE IS PROVIDED BY Chas Emerick ``AS IS'' AND ANY EXPRESS OR IMPLIED\n", "WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n", "FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Chas Emerick OR\n", "CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n", "CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n", "SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n", "ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n", "NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n", "ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n", "\n", "The views and conclusions contained in the software and documentation are those of the\n", "authors and should not be interpreted as representing official policies, either expressed\n", "or implied, of Chas Emerick.\n", "*/\n", "var diffview = {\n", "\t/**\n", "\t * Builds and returns a visual diff view. The single parameter, `params', should contain\n", "\t * the following values:\n", "\t *\n", "\t * - baseTextLines: the array of strings that was used as the base text input to SequenceMatcher\n", "\t * - newTextLines: the array of strings that was used as the new text input to SequenceMatcher\n", "\t * - opcodes: the array of arrays returned by SequenceMatcher.get_opcodes()\n", "\t * - baseTextName: the title to be displayed above the base text listing in the diff view; defaults\n", "\t *\t to \"Base Text\"\n", "\t * - newTextName: the title to be displayed above the new text listing in the diff view; defaults\n", "\t *\t to \"New Text\"\n", "\t * - contextSize: the number of lines of context to show around differences; by default, all lines\n", "\t *\t are shown\n", "\t * - viewType: if 0, a side-by-side diff view is generated (default); if 1, an inline diff view is\n", "\t *\t generated\n", "\t */\n", "\tbuildView: function (params) {\n", "\t\tvar baseTextLines = params.baseTextLines;\n", "\t\tvar newTextLines = params.newTextLines;\n", "\t\tvar opcodes = params.opcodes;\n", "\t\tvar baseTextName = params.baseTextName ? params.baseTextName : \"Base Text\";\n", "\t\tvar newTextName = params.newTextName ? params.newTextName : \"New Text\";\n", "\t\tvar contextSize = params.contextSize;\n", "\t\tvar inline = (params.viewType == 0 || params.viewType == 1) ? params.viewType : 0;\n", "\n", "\t\tif (baseTextLines == null)\n", "\t\t\tthrow \"Cannot build diff view; baseTextLines is not defined.\";\n", "\t\tif (newTextLines == null)\n", "\t\t\tthrow \"Cannot build diff view; newTextLines is not defined.\";\n", "\t\tif (!opcodes)\n", "\t\t\tthrow \"Cannot build diff view; opcodes is not defined.\";\n", "\t\t\n", "\t\tfunction celt (name, clazz) {\n", "\t\t\tvar e = document.createElement(name);\n", "\t\t\te.className = clazz;\n", "\t\t\treturn e;\n", "\t\t}\n", "\t\t\n", "\t\tfunction telt (name, text) {\n", "\t\t\tvar e = document.createElement(name);\n", "\t\t\te.appendChild(document.createTextNode(text));\n", "\t\t\treturn e;\n", "\t\t}\n", "\t\t\n", "\t\tfunction ctelt (name, clazz, text) {\n", "\t\t\tvar e = document.createElement(name);\n", "\t\t\te.className = clazz;\n", "\t\t\te.appendChild(document.createTextNode(text));\n", "\t\t\treturn e;\n", "\t\t}\n", "\t\n", "\t\tvar tdata = document.createElement(\"thead\");\n", "\t\tvar node = document.createElement(\"tr\");\n", "\t\ttdata.appendChild(node);\n", "\t\tif (inline) {\n", "\t\t\tnode.appendChild(document.createElement(\"th\"));\n", "\t\t\tnode.appendChild(document.createElement(\"th\"));\n", "\t\t\tnode.appendChild(ctelt(\"th\", \"texttitle\", baseTextName + \" vs. \" + newTextName));\n", "\t\t} else {\n", "\t\t\tnode.appendChild(document.createElement(\"th\"));\n", "\t\t\tnode.appendChild(ctelt(\"th\", \"texttitle\", baseTextName));\n", "\t\t\tnode.appendChild(document.createElement(\"th\"));\n", "\t\t\tnode.appendChild(ctelt(\"th\", \"texttitle\", newTextName));\n", "\t\t}\n", "\t\ttdata = [tdata];\n", "\t\t\n", "\t\tvar rows = [];\n", "\t\tvar node2;\n", "\t\t\n", "\t\t/**\n", "\t\t * Adds two cells to the given row; if the given row corresponds to a real\n", "\t\t * line number (based on the line index tidx and the endpoint of the \n", "\t\t * range in question tend), then the cells will contain the line number\n", "\t\t * and the line of text from textLines at position tidx (with the class of\n", "\t\t * the second cell set to the name of the change represented), and tidx + 1 will\n", "\t\t * be returned.\t Otherwise, tidx is returned, and two empty cells are added\n", "\t\t * to the given row.\n", "\t\t */\n", "\t\tfunction addCells (row, tidx, tend, textLines, change) {\n", "\t\t\tif (tidx < tend) {\n", "\t\t\t\trow.appendChild(telt(\"th\", (tidx + 1).toString()));\n", "\t\t\t\trow.appendChild(ctelt(\"td\", change, textLines[tidx].replace(/\\t/g, \"\\u00a0\\u00a0\\u00a0\\u00a0\")));\n", "\t\t\t\treturn tidx + 1;\n", "\t\t\t} else {\n", "\t\t\t\trow.appendChild(document.createElement(\"th\"));\n", "\t\t\t\trow.appendChild(celt(\"td\", \"empty\"));\n", "\t\t\t\treturn tidx;\n", "\t\t\t}\n", "\t\t}\n", "\t\t\n", "\t\tfunction addCellsInline (row, tidx, tidx2, textLines, change) {\n", "\t\t\trow.appendChild(telt(\"th\", tidx == null ? \"\" : (tidx + 1).toString()));\n", "\t\t\trow.appendChild(telt(\"th\", tidx2 == null ? \"\" : (tidx2 + 1).toString()));\n", "\t\t\trow.appendChild(ctelt(\"td\", change, textLines[tidx != null ? tidx : tidx2].replace(/\\t/g, \"\\u00a0\\u00a0\\u00a0\\u00a0\")));\n", "\t\t}\n", "\t\t\n", "\t\tfor (var idx = 0; idx < opcodes.length; idx++) {\n", "\t\t\tvar code = opcodes[idx];\n", "\t\t\tvar change = code[0];\n", "\t\t\tvar b = code[1];\n", "\t\t\tvar be = code[2];\n", "\t\t\tvar n = code[3];\n", "\t\t\tvar ne = code[4];\n", "\t\t\tvar rowcnt = Math.max(be - b, ne - n);\n", "\t\t\tvar toprows = [];\n", "\t\t\tvar botrows = [];\n", "\t\t\tfor (var i = 0; i < rowcnt; i++) {\n", "\t\t\t\t// jump ahead if we've alredy provided leading context or if this is the first range\n", "\t\t\t\tif (contextSize && opcodes.length > 1 && ((idx > 0 && i == contextSize) || (idx == 0 && i == 0)) && change==\"equal\") {\n", "\t\t\t\t\tvar jump = rowcnt - ((idx == 0 ? 1 : 2) * contextSize);\n", "\t\t\t\t\tif (jump > 1) {\n", "\t\t\t\t\t\ttoprows.push(node = document.createElement(\"tr\"));\n", "\t\t\t\t\t\t\n", "\t\t\t\t\t\tb += jump;\n", "\t\t\t\t\t\tn += jump;\n", "\t\t\t\t\t\ti += jump - 1;\n", "\t\t\t\t\t\tnode.appendChild(telt(\"th\", \"...\"));\n", "\t\t\t\t\t\tif (!inline) node.appendChild(ctelt(\"td\", \"skip\", \"\"));\n", "\t\t\t\t\t\tnode.appendChild(telt(\"th\", \"...\"));\n", "\t\t\t\t\t\tnode.appendChild(ctelt(\"td\", \"skip\", \"\"));\n", "\t\t\t\t\t\t\n", "\t\t\t\t\t\t// skip last lines if they're all equal\n", "\t\t\t\t\t\tif (idx + 1 == opcodes.length) {\n", "\t\t\t\t\t\t\tbreak;\n", "\t\t\t\t\t\t} else {\n", "\t\t\t\t\t\t\tcontinue;\n", "\t\t\t\t\t\t}\n", "\t\t\t\t\t}\n", "\t\t\t\t}\n", "\t\t\t\t\n", "\t\t\t\ttoprows.push(node = document.createElement(\"tr\"));\n", "\t\t\t\tif (inline) {\n", "\t\t\t\t\tif (change == \"insert\") {\n", "\t\t\t\t\t\taddCellsInline(node, null, n++, newTextLines, change);\n", "\t\t\t\t\t} else if (change == \"replace\") {\n", "\t\t\t\t\t\tbotrows.push(node2 = document.createElement(\"tr\"));\n", "\t\t\t\t\t\tif (b < be) addCellsInline(node, b++, null, baseTextLines, \"delete\");\n", "\t\t\t\t\t\tif (n < ne) addCellsInline(node2, null, n++, newTextLines, \"insert\");\n", "\t\t\t\t\t} else if (change == \"delete\") {\n", "\t\t\t\t\t\taddCellsInline(node, b++, null, baseTextLines, change);\n", "\t\t\t\t\t} else {\n", "\t\t\t\t\t\t// equal\n", "\t\t\t\t\t\taddCellsInline(node, b++, n++, baseTextLines, change);\n", "\t\t\t\t\t}\n", "\t\t\t\t} else {\n", "\t\t\t\t\tb = addCells(node, b, be, baseTextLines, change);\n", "\t\t\t\t\tn = addCells(node, n, ne, newTextLines, change);\n", "\t\t\t\t}\n", "\t\t\t}\n", "\n", "\t\t\tfor (var i = 0; i < toprows.length; i++) rows.push(toprows[i]);\n", "\t\t\tfor (var i = 0; i < botrows.length; i++) rows.push(botrows[i]);\n", "\t\t}\n", "\t\t\n", "\t\trows.push(node = ctelt(\"th\", \"author\", \"diff view generated by \"));\n", "\t\tnode.setAttribute(\"colspan\", inline ? 3 : 4);\n", "\t\tnode.appendChild(node2 = telt(\"a\", \"jsdifflib\"));\n", "\t\tnode2.setAttribute(\"href\", \"http://github.com/cemerick/jsdifflib\");\n", "\t\t\n", "\t\ttdata.push(node = document.createElement(\"tbody\"));\n", "\t\tfor (var idx in rows) rows.hasOwnProperty(idx) && node.appendChild(rows[idx]);\n", "\t\t\n", "\t\tnode = celt(\"table\", \"diff\" + (inline ? \" inlinediff\" : \"\"));\n", "\t\tfor (var idx in tdata) tdata.hasOwnProperty(idx) && node.appendChild(tdata[idx]);\n", "\t\treturn node;\n", "\t}\n", "};\n", "\n", "\n", "/***\n", "This is part of jsdifflib v1.0. \n", "\n", "Copyright (c) 2007, Snowtide Informatics Systems, Inc.\n", "All rights reserved.\n", "\n", "Redistribution and use in source and binary forms, with or without modification,\n", "are permitted provided that the following conditions are met:\n", "\n", "\t* Redistributions of source code must retain the above copyright notice, this\n", "\t\tlist of conditions and the following disclaimer.\n", "\t* Redistributions in binary form must reproduce the above copyright notice,\n", "\t\tthis list of conditions and the following disclaimer in the documentation\n", "\t\tand/or other materials provided with the distribution.\n", "\t* Neither the name of the Snowtide Informatics Systems nor the names of its\n", "\t\tcontributors may be used to endorse or promote products derived from this\n", "\t\tsoftware without specific prior written permission.\n", "\n", "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY\n", "EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n", "OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT\n", "SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\n", "INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED\n", "TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR\n", "BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n", "CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\n", "ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH\n", "DAMAGE.\n", "***/\n", "/* Author: Chas Emerick */\n", "var __whitespace = {\" \":true, \"\\t\":true, \"\\n\":true, \"\\f\":true, \"\\r\":true};\n", "\n", "var difflib = {\n", "\tdefaultJunkFunction: function (c) {\n", "\t\treturn __whitespace.hasOwnProperty(c);\n", "\t},\n", "\t\n", "\tstripLinebreaks: function (str) { return str.replace(/^[\\n\\r]*|[\\n\\r]*$/g, \"\"); },\n", "\t\n", "\tstringAsLines: function (str) {\n", "\t\tvar lfpos = str.indexOf(\"\\n\");\n", "\t\tvar crpos = str.indexOf(\"\\r\");\n", "\t\tvar linebreak = ((lfpos > -1 && crpos > -1) || crpos < 0) ? \"\\n\" : \"\\r\";\n", "\t\t\n", "\t\tvar lines = str.split(linebreak);\n", "\t\tfor (var i = 0; i < lines.length; i++) {\n", "\t\t\tlines[i] = difflib.stripLinebreaks(lines[i]);\n", "\t\t}\n", "\t\t\n", "\t\treturn lines;\n", "\t},\n", "\t\n", "\t// iteration-based reduce implementation\n", "\t__reduce: function (func, list, initial) {\n", "\t\tif (initial != null) {\n", "\t\t\tvar value = initial;\n", "\t\t\tvar idx = 0;\n", "\t\t} else if (list) {\n", "\t\t\tvar value = list[0];\n", "\t\t\tvar idx = 1;\n", "\t\t} else {\n", "\t\t\treturn null;\n", "\t\t}\n", "\t\t\n", "\t\tfor (; idx < list.length; idx++) {\n", "\t\t\tvalue = func(value, list[idx]);\n", "\t\t}\n", "\t\t\n", "\t\treturn value;\n", "\t},\n", "\t\n", "\t// comparison function for sorting lists of numeric tuples\n", "\t__ntuplecomp: function (a, b) {\n", "\t\tvar mlen = Math.max(a.length, b.length);\n", "\t\tfor (var i = 0; i < mlen; i++) {\n", "\t\t\tif (a[i] < b[i]) return -1;\n", "\t\t\tif (a[i] > b[i]) return 1;\n", "\t\t}\n", "\t\t\n", "\t\treturn a.length == b.length ? 0 : (a.length < b.length ? -1 : 1);\n", "\t},\n", "\t\n", "\t__calculate_ratio: function (matches, length) {\n", "\t\treturn length ? 2.0 * matches / length : 1.0;\n", "\t},\n", "\t\n", "\t// returns a function that returns true if a key passed to the returned function\n", "\t// is in the dict (js object) provided to this function; replaces being able to\n", "\t// carry around dict.has_key in python...\n", "\t__isindict: function (dict) {\n", "\t\treturn function (key) { return dict.hasOwnProperty(key); };\n", "\t},\n", "\t\n", "\t// replacement for python's dict.get function -- need easy default values\n", "\t__dictget: function (dict, key, defaultValue) {\n", "\t\treturn dict.hasOwnProperty(key) ? dict[key] : defaultValue;\n", "\t},\t\n", "\t\n", "\tSequenceMatcher: function (a, b, isjunk) {\n", "\t\tthis.set_seqs = function (a, b) {\n", "\t\t\tthis.set_seq1(a);\n", "\t\t\tthis.set_seq2(b);\n", "\t\t}\n", "\t\t\n", "\t\tthis.set_seq1 = function (a) {\n", "\t\t\tif (a == this.a) return;\n", "\t\t\tthis.a = a;\n", "\t\t\tthis.matching_blocks = this.opcodes = null;\n", "\t\t}\n", "\t\t\n", "\t\tthis.set_seq2 = function (b) {\n", "\t\t\tif (b == this.b) return;\n", "\t\t\tthis.b = b;\n", "\t\t\tthis.matching_blocks = this.opcodes = this.fullbcount = null;\n", "\t\t\tthis.__chain_b();\n", "\t\t}\n", "\t\t\n", "\t\tthis.__chain_b = function () {\n", "\t\t\tvar b = this.b;\n", "\t\t\tvar n = b.length;\n", "\t\t\tvar b2j = this.b2j = {};\n", "\t\t\tvar populardict = {};\n", "\t\t\tfor (var i = 0; i < b.length; i++) {\n", "\t\t\t\tvar elt = b[i];\n", "\t\t\t\tif (b2j.hasOwnProperty(elt)) {\n", "\t\t\t\t\tvar indices = b2j[elt];\n", "\t\t\t\t\tif (n >= 200 && indices.length * 100 > n) {\n", "\t\t\t\t\t\tpopulardict[elt] = 1;\n", "\t\t\t\t\t\tdelete b2j[elt];\n", "\t\t\t\t\t} else {\n", "\t\t\t\t\t\tindices.push(i);\n", "\t\t\t\t\t}\n", "\t\t\t\t} else {\n", "\t\t\t\t\tb2j[elt] = [i];\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\n", "\t\t\tfor (var elt in populardict) {\n", "\t\t\t\tif (populardict.hasOwnProperty(elt)) {\n", "\t\t\t\t\tdelete b2j[elt];\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\t\t\n", "\t\t\tvar isjunk = this.isjunk;\n", "\t\t\tvar junkdict = {};\n", "\t\t\tif (isjunk) {\n", "\t\t\t\tfor (var elt in populardict) {\n", "\t\t\t\t\tif (populardict.hasOwnProperty(elt) && isjunk(elt)) {\n", "\t\t\t\t\t\tjunkdict[elt] = 1;\n", "\t\t\t\t\t\tdelete populardict[elt];\n", "\t\t\t\t\t}\n", "\t\t\t\t}\n", "\t\t\t\tfor (var elt in b2j) {\n", "\t\t\t\t\tif (b2j.hasOwnProperty(elt) && isjunk(elt)) {\n", "\t\t\t\t\t\tjunkdict[elt] = 1;\n", "\t\t\t\t\t\tdelete b2j[elt];\n", "\t\t\t\t\t}\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\n", "\t\t\tthis.isbjunk = difflib.__isindict(junkdict);\n", "\t\t\tthis.isbpopular = difflib.__isindict(populardict);\n", "\t\t}\n", "\t\t\n", "\t\tthis.find_longest_match = function (alo, ahi, blo, bhi) {\n", "\t\t\tvar a = this.a;\n", "\t\t\tvar b = this.b;\n", "\t\t\tvar b2j = this.b2j;\n", "\t\t\tvar isbjunk = this.isbjunk;\n", "\t\t\tvar besti = alo;\n", "\t\t\tvar bestj = blo;\n", "\t\t\tvar bestsize = 0;\n", "\t\t\tvar j = null;\n", "\t\t\tvar k;\n", "\t\n", "\t\t\tvar j2len = {};\n", "\t\t\tvar nothing = [];\n", "\t\t\tfor (var i = alo; i < ahi; i++) {\n", "\t\t\t\tvar newj2len = {};\n", "\t\t\t\tvar jdict = difflib.__dictget(b2j, a[i], nothing);\n", "\t\t\t\tfor (var jkey in jdict) {\n", "\t\t\t\t\tif (jdict.hasOwnProperty(jkey)) {\n", "\t\t\t\t\t\tj = jdict[jkey];\n", "\t\t\t\t\t\tif (j < blo) continue;\n", "\t\t\t\t\t\tif (j >= bhi) break;\n", "\t\t\t\t\t\tnewj2len[j] = k = difflib.__dictget(j2len, j - 1, 0) + 1;\n", "\t\t\t\t\t\tif (k > bestsize) {\n", "\t\t\t\t\t\t\tbesti = i - k + 1;\n", "\t\t\t\t\t\t\tbestj = j - k + 1;\n", "\t\t\t\t\t\t\tbestsize = k;\n", "\t\t\t\t\t\t}\n", "\t\t\t\t\t}\n", "\t\t\t\t}\n", "\t\t\t\tj2len = newj2len;\n", "\t\t\t}\n", "\t\n", "\t\t\twhile (besti > alo && bestj > blo && !isbjunk(b[bestj - 1]) && a[besti - 1] == b[bestj - 1]) {\n", "\t\t\t\tbesti--;\n", "\t\t\t\tbestj--;\n", "\t\t\t\tbestsize++;\n", "\t\t\t}\n", "\t\t\t\t\n", "\t\t\twhile (besti + bestsize < ahi && bestj + bestsize < bhi &&\n", "\t\t\t\t\t!isbjunk(b[bestj + bestsize]) &&\n", "\t\t\t\t\ta[besti + bestsize] == b[bestj + bestsize]) {\n", "\t\t\t\tbestsize++;\n", "\t\t\t}\n", "\t\n", "\t\t\twhile (besti > alo && bestj > blo && isbjunk(b[bestj - 1]) && a[besti - 1] == b[bestj - 1]) {\n", "\t\t\t\tbesti--;\n", "\t\t\t\tbestj--;\n", "\t\t\t\tbestsize++;\n", "\t\t\t}\n", "\t\t\t\n", "\t\t\twhile (besti + bestsize < ahi && bestj + bestsize < bhi && isbjunk(b[bestj + bestsize]) &&\n", "\t\t\t\t\ta[besti + bestsize] == b[bestj + bestsize]) {\n", "\t\t\t\tbestsize++;\n", "\t\t\t}\n", "\t\n", "\t\t\treturn [besti, bestj, bestsize];\n", "\t\t}\n", "\t\t\n", "\t\tthis.get_matching_blocks = function () {\n", "\t\t\tif (this.matching_blocks != null) return this.matching_blocks;\n", "\t\t\tvar la = this.a.length;\n", "\t\t\tvar lb = this.b.length;\n", "\t\n", "\t\t\tvar queue = [[0, la, 0, lb]];\n", "\t\t\tvar matching_blocks = [];\n", "\t\t\tvar alo, ahi, blo, bhi, qi, i, j, k, x;\n", "\t\t\twhile (queue.length) {\n", "\t\t\t\tqi = queue.pop();\n", "\t\t\t\talo = qi[0];\n", "\t\t\t\tahi = qi[1];\n", "\t\t\t\tblo = qi[2];\n", "\t\t\t\tbhi = qi[3];\n", "\t\t\t\tx = this.find_longest_match(alo, ahi, blo, bhi);\n", "\t\t\t\ti = x[0];\n", "\t\t\t\tj = x[1];\n", "\t\t\t\tk = x[2];\n", "\t\n", "\t\t\t\tif (k) {\n", "\t\t\t\t\tmatching_blocks.push(x);\n", "\t\t\t\t\tif (alo < i && blo < j)\n", "\t\t\t\t\t\tqueue.push([alo, i, blo, j]);\n", "\t\t\t\t\tif (i+k < ahi && j+k < bhi)\n", "\t\t\t\t\t\tqueue.push([i + k, ahi, j + k, bhi]);\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\t\t\n", "\t\t\tmatching_blocks.sort(difflib.__ntuplecomp);\n", "\t\n", "\t\t\tvar i1 = 0, j1 = 0, k1 = 0, block = 0;\n", "\t\t\tvar i2, j2, k2;\n", "\t\t\tvar non_adjacent = [];\n", "\t\t\tfor (var idx in matching_blocks) {\n", "\t\t\t\tif (matching_blocks.hasOwnProperty(idx)) {\n", "\t\t\t\t\tblock = matching_blocks[idx];\n", "\t\t\t\t\ti2 = block[0];\n", "\t\t\t\t\tj2 = block[1];\n", "\t\t\t\t\tk2 = block[2];\n", "\t\t\t\t\tif (i1 + k1 == i2 && j1 + k1 == j2) {\n", "\t\t\t\t\t\tk1 += k2;\n", "\t\t\t\t\t} else {\n", "\t\t\t\t\t\tif (k1) non_adjacent.push([i1, j1, k1]);\n", "\t\t\t\t\t\ti1 = i2;\n", "\t\t\t\t\t\tj1 = j2;\n", "\t\t\t\t\t\tk1 = k2;\n", "\t\t\t\t\t}\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\t\t\n", "\t\t\tif (k1) non_adjacent.push([i1, j1, k1]);\n", "\t\n", "\t\t\tnon_adjacent.push([la, lb, 0]);\n", "\t\t\tthis.matching_blocks = non_adjacent;\n", "\t\t\treturn this.matching_blocks;\n", "\t\t}\n", "\t\t\n", "\t\tthis.get_opcodes = function () {\n", "\t\t\tif (this.opcodes != null) return this.opcodes;\n", "\t\t\tvar i = 0;\n", "\t\t\tvar j = 0;\n", "\t\t\tvar answer = [];\n", "\t\t\tthis.opcodes = answer;\n", "\t\t\tvar block, ai, bj, size, tag;\n", "\t\t\tvar blocks = this.get_matching_blocks();\n", "\t\t\tfor (var idx in blocks) {\n", "\t\t\t\tif (blocks.hasOwnProperty(idx)) {\n", "\t\t\t\t\tblock = blocks[idx];\n", "\t\t\t\t\tai = block[0];\n", "\t\t\t\t\tbj = block[1];\n", "\t\t\t\t\tsize = block[2];\n", "\t\t\t\t\ttag = '';\n", "\t\t\t\t\tif (i < ai && j < bj) {\n", "\t\t\t\t\t\ttag = 'replace';\n", "\t\t\t\t\t} else if (i < ai) {\n", "\t\t\t\t\t\ttag = 'delete';\n", "\t\t\t\t\t} else if (j < bj) {\n", "\t\t\t\t\t\ttag = 'insert';\n", "\t\t\t\t\t}\n", "\t\t\t\t\tif (tag) answer.push([tag, i, ai, j, bj]);\n", "\t\t\t\t\ti = ai + size;\n", "\t\t\t\t\tj = bj + size;\n", "\t\t\t\t\t\n", "\t\t\t\t\tif (size) answer.push(['equal', ai, i, bj, j]);\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\t\t\n", "\t\t\treturn answer;\n", "\t\t}\n", "\t\t\n", "\t\t// this is a generator function in the python lib, which of course is not supported in javascript\n", "\t\t// the reimplementation builds up the grouped opcodes into a list in their entirety and returns that.\n", "\t\tthis.get_grouped_opcodes = function (n) {\n", "\t\t\tif (!n) n = 3;\n", "\t\t\tvar codes = this.get_opcodes();\n", "\t\t\tif (!codes) codes = [[\"equal\", 0, 1, 0, 1]];\n", "\t\t\tvar code, tag, i1, i2, j1, j2;\n", "\t\t\tif (codes[0][0] == 'equal') {\n", "\t\t\t\tcode = codes[0];\n", "\t\t\t\ttag = code[0];\n", "\t\t\t\ti1 = code[1];\n", "\t\t\t\ti2 = code[2];\n", "\t\t\t\tj1 = code[3];\n", "\t\t\t\tj2 = code[4];\n", "\t\t\t\tcodes[0] = [tag, Math.max(i1, i2 - n), i2, Math.max(j1, j2 - n), j2];\n", "\t\t\t}\n", "\t\t\tif (codes[codes.length - 1][0] == 'equal') {\n", "\t\t\t\tcode = codes[codes.length - 1];\n", "\t\t\t\ttag = code[0];\n", "\t\t\t\ti1 = code[1];\n", "\t\t\t\ti2 = code[2];\n", "\t\t\t\tj1 = code[3];\n", "\t\t\t\tj2 = code[4];\n", "\t\t\t\tcodes[codes.length - 1] = [tag, i1, Math.min(i2, i1 + n), j1, Math.min(j2, j1 + n)];\n", "\t\t\t}\n", "\t\n", "\t\t\tvar nn = n + n;\n", "\t\t\tvar group = [];\n", "\t\t\tvar groups = [];\n", "\t\t\tfor (var idx in codes) {\n", "\t\t\t\tif (codes.hasOwnProperty(idx)) {\n", "\t\t\t\t\tcode = codes[idx];\n", "\t\t\t\t\ttag = code[0];\n", "\t\t\t\t\ti1 = code[1];\n", "\t\t\t\t\ti2 = code[2];\n", "\t\t\t\t\tj1 = code[3];\n", "\t\t\t\t\tj2 = code[4];\n", "\t\t\t\t\tif (tag == 'equal' && i2 - i1 > nn) {\n", "\t\t\t\t\t\tgroup.push([tag, i1, Math.min(i2, i1 + n), j1, Math.min(j2, j1 + n)]);\n", "\t\t\t\t\t\tgroups.push(group);\n", "\t\t\t\t\t\tgroup = [];\n", "\t\t\t\t\t\ti1 = Math.max(i1, i2-n);\n", "\t\t\t\t\t\tj1 = Math.max(j1, j2-n);\n", "\t\t\t\t\t}\n", "\t\t\t\t\t\n", "\t\t\t\t\tgroup.push([tag, i1, i2, j1, j2]);\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\t\t\n", "\t\t\tif (group && !(group.length == 1 && group[0][0] == 'equal')) groups.push(group)\n", "\t\t\t\n", "\t\t\treturn groups;\n", "\t\t}\n", "\t\t\n", "\t\tthis.ratio = function () {\n", "\t\t\tmatches = difflib.__reduce(\n", "\t\t\t\t\t\t\tfunction (sum, triple) { return sum + triple[triple.length - 1]; },\n", "\t\t\t\t\t\t\tthis.get_matching_blocks(), 0);\n", "\t\t\treturn difflib.__calculate_ratio(matches, this.a.length + this.b.length);\n", "\t\t}\n", "\t\t\n", "\t\tthis.quick_ratio = function () {\n", "\t\t\tvar fullbcount, elt;\n", "\t\t\tif (this.fullbcount == null) {\n", "\t\t\t\tthis.fullbcount = fullbcount = {};\n", "\t\t\t\tfor (var i = 0; i < this.b.length; i++) {\n", "\t\t\t\t\telt = this.b[i];\n", "\t\t\t\t\tfullbcount[elt] = difflib.__dictget(fullbcount, elt, 0) + 1;\n", "\t\t\t\t}\n", "\t\t\t}\n", "\t\t\tfullbcount = this.fullbcount;\n", "\t\n", "\t\t\tvar avail = {};\n", "\t\t\tvar availhas = difflib.__isindict(avail);\n", "\t\t\tvar matches = numb = 0;\n", "\t\t\tfor (var i = 0; i < this.a.length; i++) {\n", "\t\t\t\telt = this.a[i];\n", "\t\t\t\tif (availhas(elt)) {\n", "\t\t\t\t\tnumb = avail[elt];\n", "\t\t\t\t} else {\n", "\t\t\t\t\tnumb = difflib.__dictget(fullbcount, elt, 0);\n", "\t\t\t\t}\n", "\t\t\t\tavail[elt] = numb - 1;\n", "\t\t\t\tif (numb > 0) matches++;\n", "\t\t\t}\n", "\t\t\t\n", "\t\t\treturn difflib.__calculate_ratio(matches, this.a.length + this.b.length);\n", "\t\t}\n", "\t\t\n", "\t\tthis.real_quick_ratio = function () {\n", "\t\t\tvar la = this.a.length;\n", "\t\t\tvar lb = this.b.length;\n", "\t\t\treturn _calculate_ratio(Math.min(la, lb), la + lb);\n", "\t\t}\n", "\t\t\n", "\t\tthis.isjunk = isjunk ? isjunk : difflib.defaultJunkFunction;\n", "\t\tthis.a = this.b = null;\n", "\t\tthis.set_seqs(a, b);\n", "\t}\n", "};\n", "\n", "\n", "\n", "function diffUsingJS (viewType, contextSize, baseText, newText) {\n", "\n", " var byId = function (id) { return document.getElementById(id); },\n", " base = difflib.stringAsLines(baseText),\n", " newtxt = difflib.stringAsLines(newText),\n", " sm = new difflib.SequenceMatcher(base, newtxt),\n", " opcodes = sm.get_opcodes(),\n", " diffoutputdiv = byId(\"diffid_2021-08-14_23_16_53_340151\");\n", "\n", " diffoutputdiv.innerHTML = \"\";\n", " contextSize = contextSize || null;\n", "\n", " diffoutputdiv.appendChild(diffview.buildView({\n", " baseTextLines: base,\n", " newTextLines: newtxt,\n", " opcodes: opcodes,\n", " baseTextName: \"Base Text\",\n", " newTextName: \"New Text\",\n", " contextSize: contextSize,\n", " viewType: viewType\n", " }));\n", "}\n", "var tview=0;\n", "var csize='';\n", "var bt = '\\ndef edit_distance_string(s1, s2):\\n \"\"\"\\n Computes the edit distance between strings *s1* and *s2*.\\n\\n :param s1: first string\\n :param s2: second string\\n :return: dist, list of tuples of aligned characters\\n \"\"\"\\n n1 = len(s1) + 1\\n n2 = len(s2) + 1\\n dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)\\n pred = numpy.full(dist.shape, 0, dtype=numpy.int32)\\n\\n for j in range(1, n2):\\n dist[0, j] = j\\n pred[0, j] = 2\\n for i in range(0, n1):\\n dist[i, 0] = i\\n pred[i, 0] = 1\\n pred[0, 0] = -1\\n\\n for j in range(1, n2):\\n for i in range(1, n1):\\n c = dist[i, j]\\n\\n p = 0\\n if dist[i - 1, j] + 1 < c:\\n c = dist[i - 1, j] + 1\\n p = 1\\n if dist[i, j - 1] + 1 < c:\\n c = dist[i, j - 1] + 1\\n p = 2\\n d = 0 if s1[i - 1] == s2[j - 1] else 1\\n if dist[i - 1, j - 1] + d < c:\\n c = dist[i - 1, j - 1] + d\\n p = 3\\n if p == 0:\\n raise RuntimeError(\\n \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))\\n\\n dist[i, j] = c\\n pred[i, j] = p\\n\\n d = dist[len(s1), len(s2)]\\n return d\\n';\n", "var nt = '\\ndef edit_distance_string(s1, s2):\\n \"\"\"\\n Computes the edit distance between strings *s1* and *s2*.\\n\\n :param s1: first string\\n :param s2: second string\\n :return: dist, list of tuples of aligned characters\\n \"\"\"\\n n1 = len(s1) + 1\\n n2 = len(s2) + 1\\n dist = numpy.full((n1, n2), n1 * n2, dtype=numpy.float64)\\n pred = numpy.full(dist.shape, 0, dtype=numpy.int32)\\n\\n for i in range(0, n1):\\n dist[i, 0] = i\\n pred[i, 0] = 1\\n for j in range(1, n2):\\n dist[0, j] = j\\n pred[0, j] = 2\\n pred[0, 0] = -1\\n\\n for i in range(1, n1):\\n for j in range(1, n2):\\n c = dist[i, j]\\n\\n p = 0\\n if dist[i - 1, j] + 1 < c:\\n c = dist[i - 1, j] + 1\\n p = 1\\n if dist[i, j - 1] + 1 < c:\\n c = dist[i, j - 1] + 1\\n p = 2\\n d = 0 if s1[i - 1] == s2[j - 1] else 1\\n if dist[i - 1, j - 1] + d < c:\\n c = dist[i - 1, j - 1] + d\\n p = 3\\n if p == 0:\\n raise RuntimeError(\\n \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))\\n\\n dist[i, j] = c\\n pred[i, j] = p\\n\\n d = dist[len(s1), len(s2)]\\n equals = []\\n i, j = len(s1), len(s2)\\n p = pred[i, j]\\n while p != -1:\\n if p == 3:\\n equals.append((i - 1, j - 1))\\n i -= 1\\n j -= 1\\n elif p == 2:\\n j -= 1\\n elif p == 1:\\n i -= 1\\n else:\\n raise RuntimeError(\\n \"Unexpected value for p=%d at position=%r.\" % (p, (i, j)))\\n p = pred[i, j]\\n return d, list(reversed(equals))\\n';\n", "diffUsingJS(tview, csize, bt, nt) ;\n"], "text/plain": [""]}, "execution_count": 10, "metadata": {}, "output_type": "execute_result"}], "source": ["%textdiff f1 f2"]}, {"cell_type": "code", "execution_count": 10, "id": "b98b46d5", "metadata": {}, "outputs": [], "source": []}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.5"}}, "nbformat": 4, "nbformat_minor": 5}