MCPcopy Create free account
hub / github.com/dunglas/httpsfv / parseDisplayString

Function parseDisplayString

displaystring.go:48–94  ·  view source on GitHub ↗

parseDisplayString parses as defined in https://httpwg.org/specs/rfc9651.html#parse-display.

(s *scanner)

Source from the content-addressed store, hash-verified

46// parseDisplayString parses as defined in
47// https://httpwg.org/specs/rfc9651.html#parse-display.
48func parseDisplayString(s *scanner) (DisplayString, error) {
49 if s.eof() || len(s.data[s.off:]) < 2 || s.data[s.off:2] != `%"` {
50 return "", &UnmarshalError{s.off, ErrInvalidDisplayString}
51 }
52 s.off += 2
53
54 var b strings.Builder
55 for !s.eof() {
56 c := s.data[s.off]
57 s.off++
58
59 switch c {
60 case '%':
61 if len(s.data[s.off:]) < 2 {
62 return "", &UnmarshalError{s.off, ErrInvalidDisplayString}
63 }
64 c0 := unhex(s.data[s.off])
65 if c0 == 0 {
66 return "", &UnmarshalError{s.off, ErrInvalidDisplayString}
67 }
68
69 c1 := unhex(s.data[s.off+1])
70 if c1 == 0 {
71 return "", &UnmarshalError{s.off, ErrInvalidDisplayString}
72 }
73
74 b.WriteByte(c0<<4 | c1)
75 s.off += 2
76 case '"':
77 r := b.String()
78 if !utf8.ValidString(r) {
79 return "", ErrInvalidDisplayString
80 }
81
82 return DisplayString(r), nil
83
84 default:
85 if unicode.Is(notVcharOrSp, rune(c)) {
86 return "", &UnmarshalError{s.off, ErrInvalidDisplayString}
87 }
88
89 b.WriteByte(c)
90 }
91 }
92
93 return "", &UnmarshalError{s.off, ErrInvalidDisplayString}
94}
95
96func unhex(c byte) byte {
97 switch {

Callers 2

TestParseDisplayStringFunction · 0.85
parseBareItemFunction · 0.85

Calls 3

unhexFunction · 0.85
DisplayStringTypeAlias · 0.85
eofMethod · 0.80

Tested by 1

TestParseDisplayStringFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…