sentence.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package uniseg
  2. import "unicode/utf8"
  3. // FirstSentence returns the first sentence found in the given byte slice
  4. // according to the rules of [Unicode Standard Annex #29, Sentence Boundaries].
  5. // This function can be called continuously to extract all sentences from a byte
  6. // slice, as illustrated in the example below.
  7. //
  8. // If you don't know the current state, for example when calling the function
  9. // for the first time, you must pass -1. For consecutive calls, pass the state
  10. // and rest slice returned by the previous call.
  11. //
  12. // The "rest" slice is the sub-slice of the original byte slice "b" starting
  13. // after the last byte of the identified sentence. If the length of the "rest"
  14. // slice is 0, the entire byte slice "b" has been processed. The "sentence" byte
  15. // slice is the sub-slice of the input slice containing the identified sentence.
  16. //
  17. // Given an empty byte slice "b", the function returns nil values.
  18. //
  19. // [Unicode Standard Annex #29, Sentence Boundaries]: http://unicode.org/reports/tr29/#Sentence_Boundaries
  20. func FirstSentence(b []byte, state int) (sentence, rest []byte, newState int) {
  21. // An empty byte slice returns nothing.
  22. if len(b) == 0 {
  23. return
  24. }
  25. // Extract the first rune.
  26. r, length := utf8.DecodeRune(b)
  27. if len(b) <= length { // If we're already past the end, there is nothing else to parse.
  28. return b, nil, sbAny
  29. }
  30. // If we don't know the state, determine it now.
  31. if state < 0 {
  32. state, _ = transitionSentenceBreakState(state, r, b[length:], "")
  33. }
  34. // Transition until we find a boundary.
  35. var boundary bool
  36. for {
  37. r, l := utf8.DecodeRune(b[length:])
  38. state, boundary = transitionSentenceBreakState(state, r, b[length+l:], "")
  39. if boundary {
  40. return b[:length], b[length:], state
  41. }
  42. length += l
  43. if len(b) <= length {
  44. return b, nil, sbAny
  45. }
  46. }
  47. }
  48. // FirstSentenceInString is like [FirstSentence] but its input and outputs are
  49. // strings.
  50. func FirstSentenceInString(str string, state int) (sentence, rest string, newState int) {
  51. // An empty byte slice returns nothing.
  52. if len(str) == 0 {
  53. return
  54. }
  55. // Extract the first rune.
  56. r, length := utf8.DecodeRuneInString(str)
  57. if len(str) <= length { // If we're already past the end, there is nothing else to parse.
  58. return str, "", sbAny
  59. }
  60. // If we don't know the state, determine it now.
  61. if state < 0 {
  62. state, _ = transitionSentenceBreakState(state, r, nil, str[length:])
  63. }
  64. // Transition until we find a boundary.
  65. var boundary bool
  66. for {
  67. r, l := utf8.DecodeRuneInString(str[length:])
  68. state, boundary = transitionSentenceBreakState(state, r, nil, str[length+l:])
  69. if boundary {
  70. return str[:length], str[length:], state
  71. }
  72. length += l
  73. if len(str) <= length {
  74. return str, "", sbAny
  75. }
  76. }
  77. }