-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix TestMassiveRuneDiffConversion by skip invalid unicode code point
- Loading branch information
Showing
4 changed files
with
64 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package diffmatchpatch | ||
|
||
type index uint32 | ||
|
||
const runeSkipStart = 0xd800 | ||
const runeSkipEnd = 0xdfff + 1 | ||
const runeMax = 0x110000 // next invalid code point | ||
|
||
func stringToIndex(text string) []index { | ||
runes := []rune(text) | ||
indexes := make([]index, len(runes)) | ||
for i, r := range runes { | ||
if r < runeSkipEnd { | ||
indexes[i] = index(r) | ||
} else { | ||
indexes[i] = index(r) - (runeSkipEnd - runeSkipStart) | ||
} | ||
} | ||
return indexes | ||
} | ||
|
||
func indexesToString(indexes []index) string { | ||
runes := make([]rune, len(indexes)) | ||
for i, index := range indexes { | ||
if index < runeSkipStart { | ||
runes[i] = rune(index) | ||
} else { | ||
runes[i] = rune(index + (runeSkipEnd - runeSkipStart)) | ||
} | ||
} | ||
return string(runes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package diffmatchpatch | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestIndexConversion(t *testing.T) { | ||
n := runeMax - (runeSkipEnd - runeSkipStart) | ||
indexes := make([]index, n) | ||
for i := 0; i < n; i++ { | ||
indexes[i] = index(i) | ||
} | ||
indexes2 := stringToIndex(indexesToString(indexes)) | ||
assert.EqualValues(t, indexes, indexes2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters