Source File
diff.go
Belonging Package
github.com/sergi/go-diff/diffmatchpatch
package diffmatchpatch
import (
)
copy([:], )
return
}
:= [:]
for := range {
[] = Diff{}
}
return [:]
copy([:], )
return
}
func ( *DiffMatchPatch) (, string, bool) []Diff {
return .DiffMainRunes([]rune(), []rune(), )
}
func ( *DiffMatchPatch) (, []rune, bool) []Diff {
var time.Time
if .DiffTimeout > 0 {
= time.Now().Add(.DiffTimeout)
}
return .diffMainRunes(, , , )
}
func ( *DiffMatchPatch) (, []rune, bool, time.Time) []Diff {
if runesEqual(, ) {
var []Diff
if len() > 0 {
= append(, Diff{DiffEqual, string()})
}
return
:= commonPrefixLength(, )
:= [:]
= [:]
= [:]
= commonSuffixLength(, )
:= [len()-:]
= [:len()-]
= [:len()-]
:= .diffCompute(, , , )
return append(, Diff{DiffInsert, string()})
return append(, Diff{DiffDelete, string()})
}
var , []rune
if len() > len() {
=
=
} else {
=
=
}
if := runesIndex(, ); != -1 {
if len() > len() {
= DiffDelete
return []Diff{
Diff{DiffDelete, string()},
Diff{DiffInsert, string()},
:= [0]
:= [1]
:= [2]
:= [3]
:= .diffMainRunes(, , , )
:=
= append(, Diff{DiffEqual, string()})
= append(, ...)
return
} else if && len() > 100 && len() > 100 {
return .diffLineMode(, , )
}
return .diffBisect(, , )
}
, , := .diffLinesToRunes(, )
:= .diffMainRunes(, , false, )
= .DiffCleanupSemantic()
:= ""
:= ""
for < len() {
switch [].Type {
case DiffInsert:
++
+= [].Text
case DiffDelete:
++
+= [].Text
return .diffBisect([]rune(), []rune(), )
}
:= 0
:= 0
:= 0
:= 0
for := - + ; <= -; += 2 {
:= +
var int
if == - || ( != && [-1] < [+1]) {
= [+1]
} else {
= [-1] + 1
}
:= -
for < && < {
if [] != [] {
break
}
++
++
}
[] =
+= 2
+= 2
} else if {
:= + -
:= - []
return .diffBisectSplit(, , , , )
}
}
}
for := - + ; <= -; += 2 {
:= +
var int
if == - || ( != && [-1] < [+1]) {
= [+1]
} else {
= [-1] + 1
}
var = -
for < && < {
if [--1] != [--1] {
break
}
++
++
}
[] =
+= 2
+= 2
} else if ! {
:= + -
if >= 0 && < && [] != -1 {
:= []
= -
return .diffBisectSplit(, , , , )
}
}
}
}
return []Diff{
Diff{DiffDelete, string()},
Diff{DiffInsert, string()},
}
}
func ( *DiffMatchPatch) (, []rune, , int,
time.Time) []Diff {
:= [:]
:= [:]
:= [:]
:= [:]
:= .diffMainRunes(, , false, )
:= .diffMainRunes(, , false, )
return append(, ...)
}
func ( *DiffMatchPatch) (, string) (string, string, []string) {
, , := .DiffLinesToRunes(, )
return string(), string(),
}
:= []string{""} // e.g. lineArray[4] == 'Hello\n'
:= map[string]int{} // e.g. lineHash['Hello\n'] == 4
:= .diffLinesToRunesMunge(, &, )
:= .diffLinesToRunesMunge(, &, )
return , ,
}
func ( *DiffMatchPatch) (, []rune) ([]rune, []rune, []string) {
return .DiffLinesToRunes(string(), string())
}
return commonPrefixLength([]rune(), []rune())
}
return commonSuffixLength([]rune(), []rune())
}
:= len()
if == 0 || == 0 {
return 0
if > {
= [-:]
} else if < {
= [0:]
}
if == {
return
}
:= .diffHalfMatchI(, , int(float64(len()+3)/4))
:= [ : +len()/4]
for := runesIndexOf(, , 0); != -1; = runesIndexOf(, , +1) {
:= commonPrefixLength([:], [:])
:= commonSuffixLength([:], [:])
if < + {
= [- : ]
= [ : +]
= len() + len()
= [:-]
= [+:]
= [:-]
= [+:]
}
}
if *2 < len() {
return nil
}
return [][]rune{
,
,
,
,
append(, ...),
}
}
func ( *DiffMatchPatch) ( []Diff) []Diff {
:= [len()-1]
= splice(, , 0, Diff{DiffDelete, })
if {
= .DiffCleanupMerge()
}
= 1
for < len() {
if [-1].Type == DiffDelete &&
[].Type == DiffInsert {
:= [-1].Text
:= [].Text
:= .DiffCommonOverlap(, )
:= .DiffCommonOverlap(, )
if >= {
if float64() >= float64(len())/2 ||
float64() >= float64(len())/2 {
:= Diff{DiffEqual, [:]}
= splice(, , 0, )
[-1].Type = DiffInsert
[-1].Text = [0 : len()-]
[+1].Type = DiffDelete
[+1].Text = [:]
++
}
}
++
}
++
}
return
}
var (
nonAlphaNumericRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
whitespaceRegex = regexp.MustCompile(`\s`)
linebreakRegex = regexp.MustCompile(`[\r\n]`)
blanklineEndRegex = regexp.MustCompile(`\n\r?\n$`)
blanklineStartRegex = regexp.MustCompile(`^\r?\n\r?\n`)
)
return 6
}
, := utf8.DecodeLastRuneInString()
, := utf8.DecodeRuneInString()
:= string()
:= string()
:= nonAlphaNumericRegex.MatchString()
:= nonAlphaNumericRegex.MatchString()
:= && whitespaceRegex.MatchString()
:= && whitespaceRegex.MatchString()
:= && linebreakRegex.MatchString()
:= && linebreakRegex.MatchString()
:= && blanklineEndRegex.MatchString()
:= && blanklineEndRegex.MatchString()
return 5
return 4
return 3
return 2
return 1
}
return 0
}
func ( *DiffMatchPatch) ( []Diff) []Diff {
:= 1
:= .DiffCommonSuffix(, )
if > 0 {
:= [len()-:]
= [0 : len()-]
= + [:len()-]
= +
}
:=
:=
:=
:= diffCleanupSemanticScore(, ) +
diffCleanupSemanticScore(, )
for len() != 0 && len() != 0 {
, := utf8.DecodeRuneInString()
if len() < || [:] != [:] {
break
}
+= [:]
= [:] + [:]
= [:]
:= diffCleanupSemanticScore(, ) +
if >= {
=
=
=
=
}
}
func ( *DiffMatchPatch) ( []Diff) []Diff {
type struct {
int
*
}
:= ""
= &{
: ,
: ,
}
=
=
= [].Text
var int
if {
++
}
if {
++
}
if {
++
}
if {
++
}
if len() > 0 &&
(( && && && ) ||
((len() < .DiffEditCost/2) && == 3)) {
:= .
= splice(, , 0, Diff{DiffDelete, })
= .
= ""
if + > 1 {
if == 0 {
= splice(, -,
+,
Diff{DiffInsert, string()})
} else if == 0 {
= splice(, -,
+,
Diff{DiffDelete, string()})
} else {
= splice(, --,
+,
Diff{DiffDelete, string()},
Diff{DiffInsert, string()})
}
= - - + 1
if != 0 {
++
}
if != 0 {
++
}
:= false
if {
= .()
}
return
}
=
break
}
=
=
}
return
return + ( - )
}
func ( *DiffMatchPatch) ( []Diff) string {
var bytes.Buffer
for , := range {
:= strings.Replace(html.EscapeString(.Text), "\n", "¶<br>", -1)
switch .Type {
case DiffInsert:
_, _ = .WriteString("<ins style=\"background:#e6ffe6;\">")
_, _ = .WriteString()
_, _ = .WriteString("</ins>")
case DiffDelete:
_, _ = .WriteString("<del style=\"background:#ffe6e6;\">")
_, _ = .WriteString()
_, _ = .WriteString("</del>")
case DiffEqual:
_, _ = .WriteString("<span>")
_, _ = .WriteString()
_, _ = .WriteString("</span>")
}
}
return .String()
}
func ( *DiffMatchPatch) ( []Diff) string {
var bytes.Buffer
for , := range {
:= .Text
switch .Type {
case DiffInsert:
_, _ = .WriteString("\x1b[32m")
_, _ = .WriteString()
_, _ = .WriteString("\x1b[0m")
case DiffDelete:
_, _ = .WriteString("\x1b[31m")
_, _ = .WriteString()
_, _ = .WriteString("\x1b[0m")
case DiffEqual:
_, _ = .WriteString()
}
}
return .String()
}
var bytes.Buffer
for , := range {
if .Type != DiffInsert {
_, _ = .WriteString(.Text)
}
}
return .String()
}
func ( *DiffMatchPatch) ( []Diff) string {
var bytes.Buffer
for , := range {
if .Type != DiffDelete {
_, _ = .WriteString(.Text)
}
}
return .String()
}
func ( *DiffMatchPatch) ( []Diff) int {
:= 0
:= 0
:= 0
for , := range {
switch .Type {
case DiffInsert:
+= utf8.RuneCountInString(.Text)
case DiffDelete:
+= utf8.RuneCountInString(.Text)
func ( *DiffMatchPatch) ( []Diff) string {
var bytes.Buffer
for , := range {
switch .Type {
case DiffInsert:
_, _ = .WriteString("+")
_, _ = .WriteString(strings.Replace(url.QueryEscape(.Text), "+", " ", -1))
_, _ = .WriteString("\t")
break
case DiffDelete:
_, _ = .WriteString("-")
_, _ = .WriteString(strconv.Itoa(utf8.RuneCountInString(.Text)))
_, _ = .WriteString("\t")
break
case DiffEqual:
_, _ = .WriteString("=")
_, _ = .WriteString(strconv.Itoa(utf8.RuneCountInString(.Text)))
_, _ = .WriteString("\t")
break
}
}
:= .String()
= [0 : utf8.RuneCountInString()-1]
= unescaper.Replace()
}
return
}
continue
}
:= [1:]
switch := [0]; {
= strings.Replace(, "+", "%2b", -1)
, = url.QueryUnescape()
if != nil {
return nil,
}
if !utf8.ValidString() {
return nil, fmt.Errorf("invalid UTF-8 token: %q", )
}
= append(, Diff{DiffInsert, })
case '=', '-':
, := strconv.ParseInt(, 10, 0)
if != nil {
return nil,
} else if < 0 {
return nil, errors.New("Negative number in DiffFromDelta: " + )
}
if > len() {
break
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |