misc.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package util
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/rand"
  21. "regexp"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "unicode"
  26. "github.com/88250/gulu"
  27. "github.com/88250/lute/html"
  28. )
  29. func init() {
  30. rand.Seed(time.Now().UTC().UnixNano())
  31. }
  32. func GetDuplicateName(master string) (ret string) {
  33. ret = master + " (1)"
  34. r := regexp.MustCompile("^(.*) \\((\\d+)\\)$")
  35. m := r.FindStringSubmatch(master)
  36. if nil == m || 3 > len(m) {
  37. return
  38. }
  39. num, _ := strconv.Atoi(m[2])
  40. num++
  41. ret = fmt.Sprintf("%s (%d)", m[1], num)
  42. return
  43. }
  44. var (
  45. letter = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
  46. )
  47. func RandString(length int) string {
  48. b := make([]rune, length)
  49. for i := range b {
  50. b[i] = letter[rand.Intn(len(letter))]
  51. }
  52. return string(b)
  53. }
  54. // InsertElem inserts value at index into a.
  55. // 0 <= index <= len(s)
  56. func InsertElem[T any](s []T, index int, value T) []T {
  57. if len(s) == index { // nil or empty slice or after last element
  58. return append(s, value)
  59. }
  60. s = append(s[:index+1], s[index:]...) // index < len(s)
  61. s[index] = value
  62. return s
  63. }
  64. // RemoveElem removes the element at index i from s.
  65. func RemoveElem[T any](s []T, index int) []T {
  66. return append(s[:index], s[index+1:]...)
  67. }
  68. func EscapeHTML(s string) (ret string) {
  69. ret = s
  70. if "" == strings.TrimSpace(ret) {
  71. return
  72. }
  73. ret = html.EscapeString(ret)
  74. return
  75. }
  76. func UnescapeHTML(s string) (ret string) {
  77. ret = s
  78. if "" == strings.TrimSpace(ret) {
  79. return
  80. }
  81. ret = html.UnescapeString(ret)
  82. return
  83. }
  84. func Reverse(s string) string {
  85. runes := []rune(s)
  86. for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
  87. runes[i], runes[j] = runes[j], runes[i]
  88. }
  89. return string(runes)
  90. }
  91. func RemoveRedundantSpace(str string) string {
  92. buf := bytes.Buffer{}
  93. lastIsChinese := false
  94. lastIsSpace := false
  95. for _, r := range str {
  96. if unicode.IsSpace(r) {
  97. if lastIsChinese || lastIsSpace {
  98. continue
  99. }
  100. buf.WriteRune(' ')
  101. lastIsChinese = false
  102. lastIsSpace = true
  103. continue
  104. }
  105. lastIsSpace = false
  106. buf.WriteRune(r)
  107. if unicode.Is(unicode.Han, r) {
  108. lastIsChinese = true
  109. continue
  110. } else {
  111. lastIsChinese = false
  112. }
  113. }
  114. return buf.String()
  115. }
  116. func Convert2Float(s string) (float64, bool) {
  117. s = gulu.Str.RemoveInvisible(s)
  118. s = strings.ReplaceAll(s, " ", "")
  119. s = strings.ReplaceAll(s, ",", "")
  120. buf := bytes.Buffer{}
  121. for _, r := range s {
  122. if unicode.IsDigit(r) || '.' == r || '-' == r {
  123. buf.WriteRune(r)
  124. }
  125. }
  126. s = buf.String()
  127. ret, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
  128. if err != nil {
  129. return 0, false
  130. }
  131. return ret, true
  132. }
  133. func ContainsSubStr(s string, subStrs []string) bool {
  134. for _, v := range subStrs {
  135. if strings.Contains(s, v) {
  136. return true
  137. }
  138. }
  139. return false
  140. }
  141. func ReplaceStr(strs []string, old, new string) (ret []string, changed bool) {
  142. if old == new {
  143. return strs, false
  144. }
  145. for i, v := range strs {
  146. if v == old {
  147. strs[i] = new
  148. changed = true
  149. }
  150. }
  151. ret = strs
  152. return
  153. }