MCPcopy
hub / github.com/gofiber/fiber / acceptsLanguageOfferExtended

Function acceptsLanguageOfferExtended

helpers.go:284–334  ·  view source on GitHub ↗

acceptsLanguageOfferExtended determines if a language tag offer matches a range according to RFC 4647 Extended Filtering (§3.3.2). - Case-insensitive comparisons - '*' matches zero or more subtags (can "slide") - Unspecified subtags are treated like '*' (so trailing/extraneous tag subtags are fine)

(spec, offer string, _ headerParams)

Source from the content-addressed store, hash-verified

282// - Unspecified subtags are treated like '*' (so trailing/extraneous tag subtags are fine)
283// - Matching fails if sliding encounters a singleton (incl. 'x')
284func acceptsLanguageOfferExtended(spec, offer string, _ headerParams) bool {
285 if spec == "*" {
286 return true
287 }
288 if spec == "" || offer == "" {
289 return false
290 }
291
292 // Use stack-allocated arrays to avoid heap allocations for typical language tags
293 var rsBuf, tsBuf [8]string
294 rs := rsBuf[:0]
295 ts := tsBuf[:0]
296
297 // Parse spec subtags without allocation for typical cases
298 for s := range strings.SplitSeq(spec, "-") {
299 rs = append(rs, s)
300 }
301 // Parse offer subtags without allocation for typical cases
302 for s := range strings.SplitSeq(offer, "-") {
303 ts = append(ts, s)
304 }
305
306 // Step 2: first subtag must match (or be '*')
307 if rs[0] != "*" && !utils.EqualFold(rs[0], ts[0]) {
308 return false
309 }
310
311 i, j := 1, 1 // i = range index, j = tag index
312 for i < len(rs) {
313 if rs[i] == "*" { // 3.A: '*' matches zero or more subtags
314 i++
315 continue
316 }
317 if j >= len(ts) { // 3.B: ran out of tag subtags
318 return false
319 }
320 if utils.EqualFold(rs[i], ts[j]) { // 3.C: exact subtag match
321 i++
322 j++
323 continue
324 }
325 // 3.D: singleton barrier (one letter or digit, incl. 'x')
326 if len(ts[j]) == 1 {
327 return false
328 }
329 // 3.E: slide forward in the tag and try again
330 j++
331 }
332 // 4: matched all range subtags
333 return true
334}
335
336// acceptsOfferType This function determines if an offer type matches a given specification.
337// It checks if the specification is equal to */* (i.e., all types are accepted).

Callers 1

Test_Utils_GetOfferFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_Utils_GetOfferFunction · 0.68