Debug Tools
diffmatchpatch Class Reference
Inheritance diagram for diffmatchpatch:
Collaboration diagram for diffmatchpatch:

Public Member Functions

def diff_prettyText (self, diffs)
 
def diff_linesToWords (self, text1, text2, delimiter=re.compile('\n'))
 

Detailed Description

Definition at line 139 of file utilities.py.

Member Function Documentation

◆ diff_linesToWords()

def diff_linesToWords (   self,
  text1,
  text2,
  delimiter = re.compile('\n') 
)
    Split two texts into an array of strings.  Reduce the texts to a string
    of hashes where each Unicode character represents one line.

    95% of this function code is copied from `diff_linesToChars` on:
        https://github.com/google/diff-match-patch/blob/895a9512bbcee0ac5a8ffcee36062c8a79f5dcda/python3/diff_match_patch.py#L381

    Copyright 2018 The diff-match-patch Authors.
    https://github.com/google/diff-match-patch
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0

    Args:
        text1: First string.
        text2: Second string.
        delimiter: a re.compile() expression for the word delimiter type

    Returns:
        Three element tuple, containing the encoded text1, the encoded text2 and
        the array of unique strings.  The zeroth element of the array of unique
        strings is intentionally blank.

Definition at line 216 of file utilities.py.

216  def diff_linesToWords(self, text1, text2, delimiter=re.compile('\n')):
217  """
218  Split two texts into an array of strings. Reduce the texts to a string
219  of hashes where each Unicode character represents one line.
220 
221  95% of this function code is copied from `diff_linesToChars` on:
222  https://github.com/google/diff-match-patch/blob/895a9512bbcee0ac5a8ffcee36062c8a79f5dcda/python3/diff_match_patch.py#L381
223 
224  Copyright 2018 The diff-match-patch Authors.
225  https://github.com/google/diff-match-patch
226  Licensed under the Apache License, Version 2.0 (the "License");
227  you may not use this file except in compliance with the License.
228  You may obtain a copy of the License at
229  http://www.apache.org/licenses/LICENSE-2.0
230 
231  Args:
232  text1: First string.
233  text2: Second string.
234  delimiter: a re.compile() expression for the word delimiter type
235 
236  Returns:
237  Three element tuple, containing the encoded text1, the encoded text2 and
238  the array of unique strings. The zeroth element of the array of unique
239  strings is intentionally blank.
240  """
241  lineArray = [] # e.g. lineArray[4] == "Hello\n"
242  lineHash = {} # e.g. lineHash["Hello\n"] == 4
243 
244  # "\x00" is a valid character, but various debuggers don't like it.
245  # So we'll insert a junk entry to avoid generating a null character.
246  lineArray.append('')
247 
248  def diff_linesToCharsMunge(text):
249  """Split a text into an array of strings. Reduce the texts to a string
250  of hashes where each Unicode character represents one line.
251  Modifies linearray and linehash through being a closure.
252  Args:
253  text: String to encode.
254  Returns:
255  Encoded string.
256  """
257  chars = []
258  # Walk the text, pulling out a substring for each line.
259  # text.split('\n') would would temporarily double our memory footprint.
260  # Modifying text would create many large strings to garbage collect.
261  lineStart = 0
262  lineEnd = -1
263  while lineEnd < len(text) - 1:
264  lineEnd = delimiter.search(text, lineStart)
265 
266  if lineEnd:
267  lineEnd = lineEnd.start()
268 
269  else:
270  lineEnd = len(text) - 1
271 
272  line = text[lineStart:lineEnd + 1]
273 
274  if line in lineHash:
275  chars.append(unichr(lineHash[line]))
276  else:
277  if len(lineArray) == maxLines:
278  # Bail out at maxLines because unichr(maxLines+1) throws.
279  line = text[lineStart:]
280  lineEnd = len(text)
281  lineArray.append(line)
282  lineHash[line] = len(lineArray) - 1
283  chars.append(unichr(len(lineArray) - 1))
284  lineStart = lineEnd + 1
285  return "".join(chars)
286 
287  # Allocate 2/3rds of the space for text1, the rest for text2.
288  maxLines = _g_maximum_lines
289  chars1 = diff_linesToCharsMunge(text1)
290  maxLines = _g_char_limit
291  chars2 = diff_linesToCharsMunge(text2)
292  return (chars1, chars2, lineArray)
293 
294 
295 # An unique identifier for any created object
def diff_linesToWords(self, text1, text2, delimiter=re.compile('\n'))
Definition: utilities.py:216

◆ diff_prettyText()

def diff_prettyText (   self,
  diffs 
)
Convert a diff array into a pretty Text report.
Args:
  diffs: Array of diff tuples.
Returns:
  Text representation.

Definition at line 141 of file utilities.py.

141  def diff_prettyText(self, diffs):
142  """Convert a diff array into a pretty Text report.
143  Args:
144  diffs: Array of diff tuples.
145  Returns:
146  Text representation.
147  """
148  last_text = "\n"
149  last_op_type = self.DIFF_EQUAL
150 
151  results_diff = []
152  cut_next_new_line = [False]
153 
154  # print('\ndiffs:\n%s\n' % diffs)
155  operations = (self.DIFF_INSERT, self.DIFF_DELETE)
156 
157  def parse(sign):
158  # print('new1:', text.encode( 'ascii' ))
159 
160  if text:
161  new = text
162 
163  else:
164  return ''
165 
166  new = textwrap_indent( "%s" % new, sign, lambda line: True )
167 
168  # force the diff change to show up on a new line for highlighting
169  if len(results_diff) > 0:
170  new = '\n' + new
171 
172  if new[-1] == '\n':
173 
174  if op == self.DIFF_INSERT and next_text and new[-1] == '\n' and next_text[0] == '\n':
175  cut_next_new_line[0] = True;
176 
177  # Avoids a double plus sign showing up when the diff has the element (1, '\n')
178  if len(text) > 1: new = new + '%s\n' % sign
179 
180  elif next_op not in operations and next_text and next_text[0] != '\n':
181  new = new + '\n'
182 
183  # print('new2:', new.encode( 'ascii' ))
184  return new
185 
186  for index in range(len(diffs)):
187  op, text = diffs[index]
188  if index < len(diffs) - 1:
189  next_op, next_text = diffs[index+1]
190  else:
191  next_op, next_text = (0, "")
192 
193  if op == self.DIFF_INSERT:
194  results_diff.append( parse( "+ " ) )
195 
196  elif op == self.DIFF_DELETE:
197  results_diff.append( parse( "- " ) )
198 
199  elif op == self.DIFF_EQUAL:
200  # print('new3:', text.encode( 'ascii' ))
201  # if last_op_type != op or last_text and last_text[-1] == '\n': text = textwrap_indent(text, " ")
202  text = textwrap_indent(text, " ")
203 
204  if cut_next_new_line[0]:
205  cut_next_new_line[0] = False
206  text = text[1:]
207 
208  results_diff.append(text)
209  # print('new4:', text.encode( 'ascii' ))
210 
211  last_text = text
212  last_op_type = op
213 
214  return "".join(results_diff)
215 

The documentation for this class was generated from the following file: