Skip to content

Commit 57c41f4

Browse files
committed
Merge branch 'iambus-master'
2 parents feac906 + db9db17 commit 57c41f4

File tree

6 files changed

+88
-15
lines changed

6 files changed

+88
-15
lines changed

diffmatchpatch/diff.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,6 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
13201320

13211321
// diffLinesToStrings splits two texts into a list of strings. Each string represents one line.
13221322
func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, string, []string) {
1323-
// '\x00' is a valid character, but various debuggers don't like it. So we'll insert a junk entry to avoid generating a null character.
13241323
lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n'
13251324

13261325
lineHash := make(map[string]int)
@@ -1331,12 +1330,11 @@ func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, stri
13311330
return intArrayToString(strIndexArray1), intArrayToString(strIndexArray2), lineArray
13321331
}
13331332

1334-
// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string.
1335-
func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string, lineHash map[string]int) []uint32 {
1336-
// Walk the text, pulling out a substring for each line. text.split('\n') would would temporarily double our memory footprint. Modifying text would create many large strings to garbage collect.
1333+
// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []index.
1334+
func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string, lineHash map[string]int) []index {
13371335
lineStart := 0
13381336
lineEnd := -1
1339-
strs := []uint32{}
1337+
strs := []index{}
13401338

13411339
for lineEnd < len(text)-1 {
13421340
lineEnd = indexOf(text, "\n", lineStart)
@@ -1350,11 +1348,11 @@ func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]str
13501348
lineValue, ok := lineHash[line]
13511349

13521350
if ok {
1353-
strs = append(strs, uint32(lineValue))
1351+
strs = append(strs, index(lineValue))
13541352
} else {
13551353
*lineArray = append(*lineArray, line)
13561354
lineHash[line] = len(*lineArray) - 1
1357-
strs = append(strs, uint32(len(*lineArray)-1))
1355+
strs = append(strs, index(len(*lineArray)-1))
13581356
}
13591357
}
13601358

diffmatchpatch/diff_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,14 @@ func TestDiffLinesToChars(t *testing.T) {
332332
lineList := []string{
333333
"", // Account for the initial empty element of the lines array.
334334
}
335-
var charList []rune
335+
var charList []index
336336
for x := 1; x < n+1; x++ {
337337
lineList = append(lineList, strconv.Itoa(x)+"\n")
338-
charList = append(charList, rune(x))
338+
charList = append(charList, index(x))
339339
}
340340
lines := strings.Join(lineList, "")
341-
chars := string(charList)
341+
chars := indexesToString(charList)
342+
assert.Equal(t, n, len(charList))
342343

343344
actualChars1, actualChars2, actualLines := dmp.DiffLinesToChars(lines, "")
344345
assert.Equal(t, chars, actualChars1)
@@ -379,12 +380,13 @@ func TestDiffCharsToLines(t *testing.T) {
379380
lineList := []string{
380381
"", // Account for the initial empty element of the lines array.
381382
}
382-
charList := []rune{}
383+
charList := []index{}
383384
for x := 1; x <= n; x++ {
384385
lineList = append(lineList, strconv.Itoa(x)+"\n")
385-
charList = append(charList, rune(x))
386+
charList = append(charList, index(x))
386387
}
387-
chars := string(charList)
388+
assert.Equal(t, n, len(charList))
389+
chars := indexesToString(charList)
388390

389391
actual := dmp.DiffCharsToLines([]Diff{Diff{DiffDelete, chars}}, lineList)
390392
assert.Equal(t, []Diff{Diff{DiffDelete, strings.Join(lineList, "")}}, actual)

diffmatchpatch/index.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package diffmatchpatch
2+
3+
type index uint32
4+
5+
const runeSkipStart = 0xd800
6+
const runeSkipEnd = 0xdfff + 1
7+
const runeMax = 0x110000 // next invalid code point
8+
9+
func stringToIndex(text string) []index {
10+
runes := []rune(text)
11+
indexes := make([]index, len(runes))
12+
for i, r := range runes {
13+
if r < runeSkipEnd {
14+
indexes[i] = index(r)
15+
} else {
16+
indexes[i] = index(r) - (runeSkipEnd - runeSkipStart)
17+
}
18+
}
19+
return indexes
20+
}
21+
22+
func indexesToString(indexes []index) string {
23+
runes := make([]rune, len(indexes))
24+
for i, index := range indexes {
25+
if index < runeSkipStart {
26+
runes[i] = rune(index)
27+
} else {
28+
runes[i] = rune(index + (runeSkipEnd - runeSkipStart))
29+
}
30+
}
31+
return string(runes)
32+
}

diffmatchpatch/index_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package diffmatchpatch
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestIndexConversion(t *testing.T) {
9+
n := runeMax - (runeSkipEnd - runeSkipStart)
10+
indexes := make([]index, n)
11+
for i := 0; i < n; i++ {
12+
indexes[i] = index(i)
13+
}
14+
indexes2 := stringToIndex(indexesToString(indexes))
15+
assert.EqualValues(t, indexes, indexes2)
16+
}

diffmatchpatch/patch_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,28 @@ func TestPatchApply(t *testing.T) {
337337
assert.Equal(t, tc.ExpectedApplies, actualApplies, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
338338
}
339339
}
340+
341+
func TestPatchMakeOutOfRangePanic(t *testing.T) {
342+
text1 := `
343+
1111111111111 000000
344+
------------- ------
345+
xxxxxxxxxxxxx ------
346+
xxxxxxxxxxxxx ------
347+
xxxxxxxxxxxxx xxxxxx
348+
xxxxxxxxxxxxx ......
349+
xxxxxxxxxxxxx 111111
350+
xxxxxxxxxxxxx ??????
351+
xxxxxxxxxxxxx 333333
352+
xxxxxxxxxxxxx 555555
353+
xxxxxxxxxx xxxxx
354+
xxxxxxxxxx xxxxx
355+
xxxxxxxxxx xxxxx
356+
xxxxxxxxxx xxxxx
357+
`
358+
text2 := `
359+
2222222222222 000000
360+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`
361+
dmp := New()
362+
patches := dmp.PatchMake(text1, text2)
363+
assert.Equal(t, 6, len(patches), "TestPatchMakeOutOfRangePanic")
364+
}

diffmatchpatch/stringutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ func runesIndex(r1, r2 []rune) int {
9393
return -1
9494
}
9595

96-
func intArrayToString(ns []uint32) string {
96+
func intArrayToString(ns []index) string {
9797
if len(ns) == 0 {
9898
return ""
9999
}
100100

101101
b := []rune{}
102102
for _, n := range ns {
103-
b = append(b, intToRune(n))
103+
b = append(b, intToRune(uint32(n)))
104104
}
105105
return string(b)
106106
}

0 commit comments

Comments
 (0)