Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
godebug
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
godebug
Commits
cdcab07d
Commit
cdcab07d
authored
Jan 06, 2013
by
Kyle Lemons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add diff package
parent
a7fab8f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
193 additions
and
0 deletions
+193
-0
diff/diff.go
diff/diff.go
+119
-0
diff/diff_test.go
diff/diff_test.go
+74
-0
No files found.
diff/diff.go
0 → 100644
View file @
cdcab07d
// Package diff implements a linewise diff algorithm.
package
diff
import
(
"bytes"
"fmt"
"strings"
)
// Chunk represents a piece of the diff. A chunk will not have both added and
// deleted lines. Equal lines are always after any added or deleted lines.
// A Chunk may or may not have any lines in it, especially for the first or last
// chunk in a computation.
type
Chunk
struct
{
Added
[]
string
Deleted
[]
string
Equal
[]
string
}
// Diff returns a string containing a line-by-line unified diff of the linewise
// changes required to make A into B. Each line is prefixed with '+', '-', or
// ' ' to indicate if it should be added, removed, or is correct respectively.
func
Diff
(
A
,
B
string
)
string
{
aLines
:=
strings
.
Split
(
A
,
"
\n
"
)
bLines
:=
strings
.
Split
(
B
,
"
\n
"
)
chunks
:=
DiffChunks
(
aLines
,
bLines
)
buf
:=
new
(
bytes
.
Buffer
)
for
_
,
c
:=
range
chunks
{
for
_
,
line
:=
range
c
.
Added
{
fmt
.
Fprintf
(
buf
,
"+%s
\n
"
,
line
)
}
for
_
,
line
:=
range
c
.
Deleted
{
fmt
.
Fprintf
(
buf
,
"-%s
\n
"
,
line
)
}
for
_
,
line
:=
range
c
.
Equal
{
fmt
.
Fprintf
(
buf
,
" %s
\n
"
,
line
)
}
}
return
buf
.
String
()
}
// DiffChunks uses an O(D(N+M)) shortest-edit-script algorithm
// to compute the edits required from A to B and returns the
// edit chunks.
func
DiffChunks
(
A
,
B
[]
string
)
[]
Chunk
{
// algorithm: http://www.xmailserver.org/diff2.pdf
N
,
M
:=
len
(
A
),
len
(
B
)
MAX
:=
N
+
M
V
:=
make
([]
int
,
2
*
MAX
+
1
)
Vs
:=
make
([][]
int
,
0
,
8
)
var
D
int
dLoop
:
for
D
=
0
;
D
<=
MAX
;
D
++
{
for
k
:=
-
D
;
k
<=
D
;
k
+=
2
{
var
x
int
if
k
==
-
D
||
(
k
!=
D
&&
V
[
MAX
+
k
-
1
]
<
V
[
MAX
+
k
+
1
])
{
x
=
V
[
MAX
+
k
+
1
]
}
else
{
x
=
V
[
MAX
+
k
-
1
]
+
1
}
y
:=
x
-
k
for
x
<
N
&&
y
<
M
&&
A
[
x
]
==
B
[
y
]
{
x
++
y
++
}
V
[
MAX
+
k
]
=
x
if
x
>=
N
&&
y
>=
M
{
Vs
=
append
(
Vs
,
append
(
make
([]
int
,
0
,
len
(
V
)),
V
...
))
break
dLoop
}
}
Vs
=
append
(
Vs
,
append
(
make
([]
int
,
0
,
len
(
V
)),
V
...
))
}
if
D
==
0
{
return
nil
}
chunks
:=
make
([]
Chunk
,
D
+
1
)
x
,
y
:=
N
,
M
for
d
:=
D
;
d
>
0
;
d
--
{
V
:=
Vs
[
d
]
k
:=
x
-
y
insert
:=
k
==
-
d
||
(
k
!=
d
&&
V
[
MAX
+
k
-
1
]
<
V
[
MAX
+
k
+
1
])
x1
:=
V
[
MAX
+
k
]
var
x0
,
xM
,
kk
int
if
insert
{
kk
=
k
+
1
x0
=
V
[
MAX
+
kk
]
xM
=
x0
}
else
{
kk
=
k
-
1
x0
=
V
[
MAX
+
kk
]
xM
=
x0
+
1
}
y0
:=
x0
-
kk
var
c
Chunk
if
insert
{
c
.
Added
=
B
[
y0
:
][
:
1
]
}
else
{
c
.
Deleted
=
A
[
x0
:
][
:
1
]
}
if
xM
<
x1
{
c
.
Equal
=
A
[
xM
:
][
:
x1
-
xM
]
}
x
,
y
=
x0
,
y0
chunks
[
d
]
=
c
}
if
x
>
0
{
chunks
[
0
]
.
Equal
=
A
[
:
x
]
}
return
chunks
}
diff/diff_test.go
0 → 100644
View file @
cdcab07d
package
diff
import
(
"reflect"
"testing"
)
func
TestDiff
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
desc
string
A
,
B
[]
string
chunks
[]
Chunk
}{
{
desc
:
"constitution"
,
A
:
[]
string
{
"We the People of the United States, in Order to form a more perfect Union,"
,
"establish Justice, insure domestic Tranquility, provide for the common defence,"
,
"and secure the Blessings of Liberty to ourselves"
,
"and our Posterity, do ordain and establish this Constitution for the United"
,
"States of America."
,
},
B
:
[]
string
{
"We the People of the United States, in Order to form a more perfect Union,"
,
"establish Justice, insure domestic Tranquility, provide for the common defence,"
,
"promote the general Welfare, and secure the Blessings of Liberty to ourselves"
,
"and our Posterity, do ordain and establish this Constitution for the United"
,
"States of America."
,
},
chunks
:
[]
Chunk
{
0
:
{
Equal
:
[]
string
{
"We the People of the United States, in Order to form a more perfect Union,"
,
"establish Justice, insure domestic Tranquility, provide for the common defence,"
,
},
},
1
:
{
Deleted
:
[]
string
{
"and secure the Blessings of Liberty to ourselves"
,
},
},
2
:
{
Added
:
[]
string
{
"promote the general Welfare, and secure the Blessings of Liberty to ourselves"
,
},
Equal
:
[]
string
{
"and our Posterity, do ordain and establish this Constitution for the United"
,
"States of America."
,
},
},
},
},
}
for
_
,
test
:=
range
tests
{
got
:=
DiffChunks
(
test
.
A
,
test
.
B
)
if
got
,
want
:=
len
(
got
),
len
(
test
.
chunks
);
got
!=
want
{
t
.
Errorf
(
"%s: edit distance = %v, want %v"
,
test
.
desc
,
got
-
1
,
want
-
1
)
continue
}
for
i
:=
range
got
{
got
,
want
:=
got
[
i
],
test
.
chunks
[
i
]
if
got
,
want
:=
got
.
Added
,
want
.
Added
;
!
reflect
.
DeepEqual
(
got
,
want
)
{
t
.
Errorf
(
"%s[%d]: Added = %v, want %v"
,
test
.
desc
,
i
,
got
,
want
)
}
if
got
,
want
:=
got
.
Deleted
,
want
.
Deleted
;
!
reflect
.
DeepEqual
(
got
,
want
)
{
t
.
Errorf
(
"%s[%d]: Deleted = %v, want %v"
,
test
.
desc
,
i
,
got
,
want
)
}
if
got
,
want
:=
got
.
Equal
,
want
.
Equal
;
!
reflect
.
DeepEqual
(
got
,
want
)
{
t
.
Errorf
(
"%s[%d]: Equal = %v, want %v"
,
test
.
desc
,
i
,
got
,
want
)
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment