|
1 | | -using System.IO.Compression; |
| 1 | +using System.Buffers; |
| 2 | +using System.IO.Compression; |
2 | 3 |
|
3 | 4 | using GenHTTP.Api.Content; |
4 | 5 | using GenHTTP.Api.Content.IO; |
@@ -131,31 +132,39 @@ private bool ShouldCompressBySize(IResponse response) |
131 | 132 |
|
132 | 133 | return MinimumSize is null || contentLength is null || contentLength >= MinimumSize; |
133 | 134 | } |
| 135 | + |
| 136 | + private static readonly SearchValues<char> Delimiters = SearchValues.Create([',', ';']); |
134 | 137 |
|
135 | 138 | private static HashSet<string> ParseSupported(ReadOnlySpan<char> acceptHeader) |
136 | 139 | { |
137 | 140 | var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
138 | 141 |
|
139 | | - var start = 0; |
140 | | - |
141 | | - while (start <= acceptHeader.Length) |
| 142 | + while (!acceptHeader.IsEmpty) |
142 | 143 | { |
143 | | - var comma = acceptHeader[start..].IndexOf(','); |
144 | | - var end = comma >= 0 ? start + comma : acceptHeader.Length; |
145 | | - |
146 | | - var part = acceptHeader.Slice(start, end - start).Trim(); |
| 144 | + var delimIdx = acceptHeader.IndexOfAny(Delimiters); |
147 | 145 |
|
148 | | - if (!part.IsEmpty) |
| 146 | + ReadOnlySpan<char> token; |
| 147 | + if (delimIdx < 0) |
149 | 148 | { |
150 | | - result.Add(part.ToString()); |
| 149 | + token = acceptHeader.Trim(); |
| 150 | + acceptHeader = default; |
151 | 151 | } |
152 | | - |
153 | | - if (comma < 0) |
| 152 | + else if (acceptHeader[delimIdx] == ',') |
| 153 | + { |
| 154 | + token = acceptHeader[..delimIdx].Trim(); |
| 155 | + acceptHeader = acceptHeader[(delimIdx + 1)..]; |
| 156 | + } |
| 157 | + else |
154 | 158 | { |
155 | | - break; |
| 159 | + token = acceptHeader[..delimIdx].TrimEnd(); |
| 160 | + var commaIdx = acceptHeader[delimIdx..].IndexOf(','); |
| 161 | + acceptHeader = commaIdx >= 0 |
| 162 | + ? acceptHeader[(delimIdx + commaIdx + 1)..] |
| 163 | + : default; |
156 | 164 | } |
157 | 165 |
|
158 | | - start = end + 1; |
| 166 | + if (!token.IsEmpty) |
| 167 | + result.Add(token.ToString()); |
159 | 168 | } |
160 | 169 |
|
161 | 170 | return result; |
|
0 commit comments