1 // Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
2 // https://github.com/sergi/go-diff
3 // See the included LICENSE file for license details.
5 // go-diff is a Go implementation of Google's Diff, Match, and Patch library
6 // Original library is Copyright (c) 2006 Google Inc.
7 // http://code.google.com/p/google-diff-match-patch/
16 // unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.
17 // In speed critical applications this could be dropped since the receiving application will certainly decode these fine. Note that this function is case-sensitive. Thus "%3F" would not be unescaped. But this is ok because it is only called with the output of HttpUtility.UrlEncode which returns lowercase hex. Example: "%3f" -> "?", "%24" -> "$", etc.
18 var unescaper = strings.NewReplacer(
19 "%21", "!", "%7E", "~", "%27", "'",
20 "%28", "(", "%29", ")", "%3B", ";",
21 "%2F", "/", "%3F", "?", "%3A", ":",
22 "%40", "@", "%26", "&", "%3D", "=",
23 "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
25 // indexOf returns the first index of pattern in str, starting at str[i].
26 func indexOf(str string, pattern string, i int) int {
31 return strings.Index(str, pattern)
33 ind := strings.Index(str[i:], pattern)
40 // lastIndexOf returns the last index of pattern in str, starting at str[i].
41 func lastIndexOf(str string, pattern string, i int) int {
46 return strings.LastIndex(str, pattern)
48 _, size := utf8.DecodeRuneInString(str[i:])
49 return strings.LastIndex(str[:i+size], pattern)
52 // runesIndexOf returns the index of pattern in target, starting at target[i].
53 func runesIndexOf(target, pattern []rune, i int) int {
54 if i > len(target)-1 {
58 return runesIndex(target, pattern)
60 ind := runesIndex(target[i:], pattern)
67 func runesEqual(r1, r2 []rune) bool {
68 if len(r1) != len(r2) {
71 for i, c := range r1 {
79 // runesIndex is the equivalent of strings.Index for rune slices.
80 func runesIndex(r1, r2 []rune) int {
81 last := len(r1) - len(r2)
82 for i := 0; i <= last; i++ {
83 if runesEqual(r1[i:i+len(r2)], r2) {