Skip to content

Instantly share code, notes, and snippets.

@jub0bs

jub0bs/go.mod Secret

Last active May 2, 2025 10:53
Show Gist options
  • Select an option

  • Save jub0bs/0aca3c6bd041e838fe4add58a060be35 to your computer and use it in GitHub Desktop.

Select an option

Save jub0bs/0aca3c6bd041e838fe4add58a060be35 to your computer and use it in GitHub Desktop.
Challenge: make this Go function inlinable and free of bounds checks
module gist.github.com/jub0bs/0aca3c6bd041e838fe4add58a060be35
go 1.24.2
package headers
// TrimOWS trims up to n bytes of optional whitespace (OWS)
// from the start of and/or the end of s.
// If no more than n bytes of OWS are found at the start of s
// and no more than n bytes of OWS are found at the end of s,
// it returns the trimmed result and true.
// Otherwise, it returns some unspecified string and false.
func TrimOWS(s string, n int) (string, bool) {
if s == "" {
return s, true
}
s, ok := trimLeftOWS(s, n)
if !ok {
return "", false
}
s, ok = trimRightOWS(s, n)
if !ok {
return "", false
}
return s, true
}
func trimLeftOWS(s string, n int) (string, bool) {
var i int
for len(s) > 0 {
if i > n {
return "", false
}
if !isOWS(s[0]) {
break
}
s = s[1:]
i++
}
return s, true
}
func trimRightOWS(s string, n int) (string, bool) {
var i int
for len(s) > 0 {
if i > n {
return "", false
}
if !isOWS(s[len(s)-1]) {
break
}
s = s[:len(s)-1]
i++
}
return s, true
}
// See https://httpwg.org/specs/rfc9110.html#whitespace.
func isOWS(b byte) bool {
return b == '\t' || b == ' '
}
package headers
import "testing"
const maxOWS = 1 // number of OWS bytes tolerated on either side
var trimOWStests = []struct {
desc string
s string
want string
ok bool
}{
{
desc: "empty",
s: "",
want: "",
ok: true,
}, {
desc: "no OWS",
s: "foo",
want: "foo",
ok: true,
}, {
desc: "internal OWS",
s: "foo \t\tbar",
want: "foo \t\tbar",
ok: true,
}, {
desc: "leading and trailing OWS",
s: "\tfoo ",
want: "foo",
ok: true,
}, {
desc: "a tolerated amount of OWS around non-OWS",
s: " a ",
want: "a",
ok: true,
}, {
desc: "a tolerated amount of OWS and nothing else",
s: "\t ",
want: "",
ok: true,
}, {
desc: "non-OWS whitespace",
s: "\nfoo\t",
want: "\nfoo",
ok: true,
}, {
desc: "too much leading OWS",
s: " \tfoo\t",
ok: false,
}, {
desc: "too much trailing OWS",
s: " foo\t ",
ok: false,
}, {
desc: "too much leading and trailing OWS",
s: " \tfoo\t ",
ok: false,
},
}
func TestTrimOWS(t *testing.T) {
for _, tc := range trimOWStests {
f := func(t *testing.T) {
allocs := testing.AllocsPerRun(10,
func() { str, truth = TrimOWS(tc.s, maxOWS) },
)
if allocs > 0 {
const tmpl = "TrimOWS(%q, %d) allocates %.2f objects"
t.Errorf(tmpl, tc.s, maxOWS, allocs)
}
got, ok := TrimOWS(tc.s, maxOWS)
if !tc.ok && ok {
// In cases where TrimOWS must fail,
// its string result is unspecified.
const tmpl = "TrimOWS(%q, %d): _, %t; want _, %t"
t.Fatalf(tmpl, tc.s, maxOWS, ok, tc.ok)
}
if tc.ok && (!ok || got != tc.want) {
const tmpl = "TrimOWS(%q, %d): %q, %t; want %q, %t"
t.Errorf(tmpl, tc.s, maxOWS, got, ok, tc.want, tc.ok)
}
}
t.Run(tc.desc, f)
}
}
func BenchmarkTrimOWS(b *testing.B) {
for _, tc := range trimOWStests {
f := func(b *testing.B) {
for range b.N {
str, truth = TrimOWS(tc.s, maxOWS)
}
}
b.Run(tc.desc, f)
}
}
var ( // sinks for avoiding dead-code elimination in benchmarks
str string
truth bool
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment