Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / github.com / sergi / go-diff@v1.1.0 / diffmatchpatch / stringutil.go
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.
4 //
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/
8
9 package diffmatchpatch
10
11 import (
12         "strings"
13         "unicode/utf8"
14 )
15
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", "*")
24
25 // indexOf returns the first index of pattern in str, starting at str[i].
26 func indexOf(str string, pattern string, i int) int {
27         if i > len(str)-1 {
28                 return -1
29         }
30         if i <= 0 {
31                 return strings.Index(str, pattern)
32         }
33         ind := strings.Index(str[i:], pattern)
34         if ind == -1 {
35                 return -1
36         }
37         return ind + i
38 }
39
40 // lastIndexOf returns the last index of pattern in str, starting at str[i].
41 func lastIndexOf(str string, pattern string, i int) int {
42         if i < 0 {
43                 return -1
44         }
45         if i >= len(str) {
46                 return strings.LastIndex(str, pattern)
47         }
48         _, size := utf8.DecodeRuneInString(str[i:])
49         return strings.LastIndex(str[:i+size], pattern)
50 }
51
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 {
55                 return -1
56         }
57         if i <= 0 {
58                 return runesIndex(target, pattern)
59         }
60         ind := runesIndex(target[i:], pattern)
61         if ind == -1 {
62                 return -1
63         }
64         return ind + i
65 }
66
67 func runesEqual(r1, r2 []rune) bool {
68         if len(r1) != len(r2) {
69                 return false
70         }
71         for i, c := range r1 {
72                 if c != r2[i] {
73                         return false
74                 }
75         }
76         return true
77 }
78
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) {
84                         return i
85                 }
86         }
87         return -1
88 }