Debian Codegraph @

cut.go

raw
1 package utils
2
3 import (
4 "bytes"
5
6 "github.com/gofiber/utils/v2/internal/unsafeconv"
7 )
8
9 // CutByte is strings.Cut/bytes.Cut for a single-byte separator, generic over
10 // strings and byte slices; a miss returns s, the zero S ("" or nil), and
11 // false. The parts alias s.
12 func CutByte[S byteSeq](s S, sep byte) (before, after S, found bool) { //nolint:nonamedreturns // the two same-typed parts are only readable named
13 if i := bytes.IndexByte(unsafeconv.Bytes(s), sep); i >= 0 {
14 return s[:i], s[i+1:], true
15 }
16 return s, after, false
17 }
18
19 // LastCutByte is CutByte around the last occurrence of sep.
20 func LastCutByte[S byteSeq](s S, sep byte) (before, after S, found bool) { //nolint:nonamedreturns // the two same-typed parts are only readable named
21 if i := bytes.LastIndexByte(unsafeconv.Bytes(s), sep); i >= 0 {
22 return s[:i], s[i+1:], true
23 }
24 return s, after, false
25 }