stop.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "strings"
  4. )
  5. func findStop(sequence string, stops []string) (bool, string) {
  6. for _, stop := range stops {
  7. if strings.Contains(sequence, stop) {
  8. return true, stop
  9. }
  10. }
  11. return false, ""
  12. }
  13. func containsStopSuffix(sequence string, stops []string) bool {
  14. for _, stop := range stops {
  15. for i := 1; i <= len(stop); i++ {
  16. if strings.HasSuffix(sequence, stop[:i]) {
  17. return true
  18. }
  19. }
  20. }
  21. return false
  22. }
  23. // truncateStop removes the provided stop string from pieces,
  24. // returning the partial pieces with stop removed, including truncating
  25. // the last piece if required
  26. func truncateStop(pieces []string, stop string) []string {
  27. joined := strings.Join(pieces, "")
  28. index := strings.Index(joined, stop)
  29. if index == -1 {
  30. return pieces
  31. }
  32. joined = joined[:index]
  33. // Split truncated string back into pieces of original lengths
  34. lengths := make([]int, len(pieces))
  35. for i, piece := range pieces {
  36. lengths[i] = len(piece)
  37. }
  38. var result []string
  39. start := 0
  40. for _, length := range lengths {
  41. if start >= len(joined) {
  42. break
  43. }
  44. end := start + length
  45. if end > len(joined) {
  46. end = len(joined)
  47. }
  48. result = append(result, joined[start:end])
  49. start = end
  50. }
  51. return result
  52. }