本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4KB

  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. package diffmatchpatch
  9. import (
  10. "strings"
  11. "unicode/utf8"
  12. )
  13. // unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.
  14. // 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.
  15. var unescaper = strings.NewReplacer(
  16. "%21", "!", "%7E", "~", "%27", "'",
  17. "%28", "(", "%29", ")", "%3B", ";",
  18. "%2F", "/", "%3F", "?", "%3A", ":",
  19. "%40", "@", "%26", "&", "%3D", "=",
  20. "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
  21. // indexOf returns the first index of pattern in str, starting at str[i].
  22. func indexOf(str string, pattern string, i int) int {
  23. if i > len(str)-1 {
  24. return -1
  25. }
  26. if i <= 0 {
  27. return strings.Index(str, pattern)
  28. }
  29. ind := strings.Index(str[i:], pattern)
  30. if ind == -1 {
  31. return -1
  32. }
  33. return ind + i
  34. }
  35. // lastIndexOf returns the last index of pattern in str, starting at str[i].
  36. func lastIndexOf(str string, pattern string, i int) int {
  37. if i < 0 {
  38. return -1
  39. }
  40. if i >= len(str) {
  41. return strings.LastIndex(str, pattern)
  42. }
  43. _, size := utf8.DecodeRuneInString(str[i:])
  44. return strings.LastIndex(str[:i+size], pattern)
  45. }
  46. // runesIndexOf returns the index of pattern in target, starting at target[i].
  47. func runesIndexOf(target, pattern []rune, i int) int {
  48. if i > len(target)-1 {
  49. return -1
  50. }
  51. if i <= 0 {
  52. return runesIndex(target, pattern)
  53. }
  54. ind := runesIndex(target[i:], pattern)
  55. if ind == -1 {
  56. return -1
  57. }
  58. return ind + i
  59. }
  60. func runesEqual(r1, r2 []rune) bool {
  61. if len(r1) != len(r2) {
  62. return false
  63. }
  64. for i, c := range r1 {
  65. if c != r2[i] {
  66. return false
  67. }
  68. }
  69. return true
  70. }
  71. // runesIndex is the equivalent of strings.Index for rune slices.
  72. func runesIndex(r1, r2 []rune) int {
  73. last := len(r1) - len(r2)
  74. for i := 0; i <= last; i++ {
  75. if runesEqual(r1[i:i+len(r2)], r2) {
  76. return i
  77. }
  78. }
  79. return -1
  80. }
上海开阖软件有限公司 沪ICP备12045867号-1