word.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package uniseg
  2. import "unicode/utf8"
  3. // FirstWord returns the first word found in the given byte slice according to
  4. // the rules of [Unicode Standard Annex #29, Word Boundaries]. This function can
  5. // be called continuously to extract all words from a byte slice, as illustrated
  6. // 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 word. If the length of the "rest" slice
  14. // is 0, the entire byte slice "b" has been processed. The "word" byte slice is
  15. // the sub-slice of the input slice containing the identified word.
  16. //
  17. // Given an empty byte slice "b", the function returns nil values.
  18. //
  19. // [Unicode Standard Annex #29, Word Boundaries]: http://unicode.org/reports/tr29/#Word_Boundaries
  20. func FirstWord(b []byte, state int) (word, 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, wbAny
  29. }
  30. // If we don't know the state, determine it now.
  31. if state < 0 {
  32. state, _ = transitionWordBreakState(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 = transitionWordBreakState(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, wbAny
  45. }
  46. }
  47. }
  48. // FirstWordInString is like [FirstWord] but its input and outputs are strings.
  49. func FirstWordInString(str string, state int) (word, rest string, newState int) {
  50. // An empty byte slice returns nothing.
  51. if len(str) == 0 {
  52. return
  53. }
  54. // Extract the first rune.
  55. r, length := utf8.DecodeRuneInString(str)
  56. if len(str) <= length { // If we're already past the end, there is nothing else to parse.
  57. return str, "", wbAny
  58. }
  59. // If we don't know the state, determine it now.
  60. if state < 0 {
  61. state, _ = transitionWordBreakState(state, r, nil, str[length:])
  62. }
  63. // Transition until we find a boundary.
  64. var boundary bool
  65. for {
  66. r, l := utf8.DecodeRuneInString(str[length:])
  67. state, boundary = transitionWordBreakState(state, r, nil, str[length+l:])
  68. if boundary {
  69. return str[:length], str[length:], state
  70. }
  71. length += l
  72. if len(str) <= length {
  73. return str, "", wbAny
  74. }
  75. }
  76. }