stringutil.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "strconv"
  11. "strings"
  12. "unicode/utf8"
  13. )
  14. // unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.
  15. // 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.
  16. var unescaper = strings.NewReplacer(
  17. "%21", "!", "%7E", "~", "%27", "'",
  18. "%28", "(", "%29", ")", "%3B", ";",
  19. "%2F", "/", "%3F", "?", "%3A", ":",
  20. "%40", "@", "%26", "&", "%3D", "=",
  21. "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
  22. // indexOf returns the first index of pattern in str, starting at str[i].
  23. func indexOf(str string, pattern string, i int) int {
  24. if i > len(str)-1 {
  25. return -1
  26. }
  27. if i <= 0 {
  28. return strings.Index(str, pattern)
  29. }
  30. ind := strings.Index(str[i:], pattern)
  31. if ind == -1 {
  32. return -1
  33. }
  34. return ind + i
  35. }
  36. // lastIndexOf returns the last index of pattern in str, starting at str[i].
  37. func lastIndexOf(str string, pattern string, i int) int {
  38. if i < 0 {
  39. return -1
  40. }
  41. if i >= len(str) {
  42. return strings.LastIndex(str, pattern)
  43. }
  44. _, size := utf8.DecodeRuneInString(str[i:])
  45. return strings.LastIndex(str[:i+size], pattern)
  46. }
  47. // runesIndexOf returns the index of pattern in target, starting at target[i].
  48. func runesIndexOf(target, pattern []rune, i int) int {
  49. if i > len(target)-1 {
  50. return -1
  51. }
  52. if i <= 0 {
  53. return runesIndex(target, pattern)
  54. }
  55. ind := runesIndex(target[i:], pattern)
  56. if ind == -1 {
  57. return -1
  58. }
  59. return ind + i
  60. }
  61. func runesEqual(r1, r2 []rune) bool {
  62. if len(r1) != len(r2) {
  63. return false
  64. }
  65. for i, c := range r1 {
  66. if c != r2[i] {
  67. return false
  68. }
  69. }
  70. return true
  71. }
  72. // runesIndex is the equivalent of strings.Index for rune slices.
  73. func runesIndex(r1, r2 []rune) int {
  74. last := len(r1) - len(r2)
  75. for i := 0; i <= last; i++ {
  76. if runesEqual(r1[i:i+len(r2)], r2) {
  77. return i
  78. }
  79. }
  80. return -1
  81. }
  82. func intArrayToString(ns []uint32) string {
  83. if len(ns) == 0 {
  84. return ""
  85. }
  86. indexSeparator := IndexSeparator[0]
  87. // Appr. 3 chars per num plus the comma.
  88. b := []byte{}
  89. for _, n := range ns {
  90. b = strconv.AppendInt(b, int64(n), 10)
  91. b = append(b, indexSeparator)
  92. }
  93. b = b[:len(b)-1]
  94. return string(b)
  95. }