| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"iter" |
| 6 |
|
| 7 |
"github.com/gofiber/utils/v2/internal/unsafeconv" |
| 8 |
) |
| 9 |
|
| 10 |
// SplitTrimSeq iterates over the sep-separated elements of s with ASCII |
| 11 |
// whitespace trimmed and empty elements skipped, as HTTP list fields are read |
| 12 |
// (RFC 9110 Section 5.6.1); the elements alias s. |
| 13 |
func SplitTrimSeq[S byteSeq](s S, sep byte) iter.Seq[S] { |
| 14 |
return func(yield func(S) bool) { |
| 15 |
rest := s // traversal state stays local, so the sequence can be ranged over again |
| 16 |
for { |
| 17 |
// CutByte's body, inline: it is too large to inline into the iterator. |
| 18 |
i := bytes.IndexByte(unsafeconv.Bytes(rest), sep) |
| 19 |
elem := rest |
| 20 |
if i >= 0 { |
| 21 |
elem = rest[:i] |
| 22 |
} |
| 23 |
if elem = TrimSpace(elem); len(elem) > 0 && !yield(elem) { |
| 24 |
return |
| 25 |
} |
| 26 |
if i < 0 { |
| 27 |
return |
| 28 |
} |
| 29 |
rest = rest[i+1:] |
| 30 |
} |
| 31 |
} |
| 32 |
} |