Top Documents Download Gallery Repository
Are you looking for a Software Engineer? Check Here

16diff Module

The diff module provices measures to detect differences between texts. To utilize it, import the diff module using import function.

Below is an example to show differences between files file1.txt and file2.txt:

diff.compose(stream('file1.txt'), stream('file2.txt')).render(sys.stdout)

16.1Module Function

diff.compose
diff.compose(src1, src2):[icase,sync] {block?}
Extracts differences between two sets of line sequence and returns diff.diff@line instance that contains the difference information.

You can specify a value of string, stream, iterator or list for the argument src1 and src2. In the result, the content of src1 is referred to as an "original" one and that of src2 as a "new" one.

Below is an example to compare between two strings:

str1 = '...'
str2 = '...'
result = diff.compose(str1, str2)

Below is an example to compare between two files:

file1 = stream('file1.txt')
file2 = stream('file2.txt')
result = diff.compose(file1, file2)

Below is an example to compare between two iterators:

chars1 = '...'.each()
chars2 = '...'.each()
result = diff.compose(chars1, chars2)

Below is an example to compare between a file and a string:

file = stream('file.txt')
str = '...'
result = diff.compose(file, str)

If block is specified, it would be evaluated with a block parameter |d:diff.diff@line|, where d is the created instance. In this case, the block's result would become the function's returned value.

If attribute :icase is specified, it wouldn't distinguish upper and lower case of characters.

diff.compose@char
diff.compose@char(src1:string, src2:string):[icase] {block?}
Extracts differences between two strings and returns diff.diff@line instance that contains the difference information.

If block is specified, it would be evaluated with a block parameter |d:diff.diff@char|, where d is the created instance. In this case, the block's result would become the function's returned value.

If attribute :icase is specified, it wouldn't distinguish upper and lower case of characters.

16.2diff.diff@line Class

The diff.diff@line instance is created by function diff.compose() and provides information about differences between two texts by lines.

16.2.1Property

Property Type R/W Explanation
distance number R The distance between the texts. Zero means that they are identical each other.
edits iterator R An iterator that returns diff.edit@line instances stored in the result.
nlines@org number R Number of lines in the "original" text.
nlines@new number R Number of lines in the "new" text.

16.2.2Method

diff.diff@line#eachhunk
diff.diff@line#eachhunk(format?:symbol, lines?:number) {block?}
Creates an iterator that returns diff.hunk@line instance stored in the result.

The argument format takes one of the symbols that specifies the hunk format:

The argument lines specifies a number of common lines appended before and after different lines

In default, this returns an iterator as its result value. Specifying the following attributes would customize the returned value:

See the chapter of Mapping Process in Gura Language Manual for the detail.

If a block is specified, it would be evaluated repeatingly with block parameters |value, idx:number| where value is the iterated value and idx the loop index starting from zero. In this case, the last evaluated value of the block would be the result value. If one of the attributes listed above is specified, an iterator or a list of the evaluated value would be returned.

diff.diff@line#render
diff.diff@line#render(out?:stream:w, format?:symbol, lines?:number) {block?}
Renders diff result to the specified stream.

If the argument out is omitted, this method returns a string of the rendered text. Otherwise, it returns nil.

The argument format takes one of the symbols that specifies the rendering format:

The argument lines specifies a number of common lines appended before and after different lines.

16.3diff.hunk@line Class

The diff.hunk@line instance provides information about a hunk.

16.3.1Property

Property Type R/W Explanation
edits iterator R An iterator that returns diff.edit@line instances stored in the hunk.
lineno@org number R Top line number of the "original" text covered by the hunk.
lineno@new number R Top line number of the "new" text covered by the hunk.
nlines@org number R Number of lines in the "original" text covered by the hunk.
nlines@new number R Number of lines in the "new" text covered by the hunk.

16.3.2Method

diff.hunk@line#print
diff.hunk@line#print(out?:stream):void {block?}
Prints the content of the diff.hunk instance to the specified stream.

16.4diff.edit@line Class

The diff.edit@line provides information about an edit operation.

16.4.1Property

Property Type R/W Explanation
diff.edit@line#type symbol R Edit operation:
  • `copy .. Copy the line.
  • `add .. Add the line.
  • `delete .. Delete the line.
mark string R A mark string that appears on the top of each line in Unified format.
lineno@org number R Line number of the "original" text correspond to the edit.
lineno@new number R Lop line number of the "new" text correspond to the edit.
source string R A source text.
unified string R A composed string in Unified format.

16.4.2Method

diff.edit@line#print
diff.edit@line#print(out?:stream):void {block?}
Prints the content of the diff.edit instance to the specified stream.

16.5diff.diff@char Class

The diff.diff@char instance is created by function diff.compose@char() and provides information about differences between two texts by characters.

16.5.1Property

Property Type R/W Explanation
distance number R The distance between the texts. Zero means that they are identical each other.
edits iterator R An iterator that returns diff.edit@char instances stored in the result.
edits@org iterator R An iterator that returns diff.edit@char instances that are applied to the "original" string.
edits@new iterator R An iterator that returns diff.edit@char instances that are applied to the "new" string.

16.6diff.edit@char Class

The diff.edit@char provides information about an edit operation.

16.6.1Property

Property Type R/W Explanation
diff.edit@char#type symbol R Edit operation:
  • `copy .. Copy the line.
  • `add .. Add the line.
  • `delete .. Delete the line.
diff.edit@char#mark string R A mark string that appears on the top of each line in Unified format.
diff.edit@char#source string R A source text.

16.7Thanks

This module uses dtl (Diff Template Library) which is distributed in the following site:

https://code.google.com/p/dtl-cpp/


Copyright (C) 2011-2017 ypsitau