Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package util
  5. import "unicode/utf8"
  6. // SplitStringAtByteN splits a string at byte n accounting for rune boundaries. (Combining characters are not accounted for.)
  7. func SplitStringAtByteN(input string, n int) (left, right string) {
  8. if len(input) <= n {
  9. left = input
  10. return
  11. }
  12. if !utf8.ValidString(input) {
  13. left = input[:n-3] + "..."
  14. right = "..." + input[n-3:]
  15. return
  16. }
  17. // in UTF8 "…" is 3 bytes so doesn't really gain us anything...
  18. end := 0
  19. for end <= n-3 {
  20. _, size := utf8.DecodeRuneInString(input[end:])
  21. if end+size > n-3 {
  22. break
  23. }
  24. end += size
  25. }
  26. left = input[:end] + "…"
  27. right = "…" + input[end:]
  28. return
  29. }