8000 fix: add reverse prefix matching for Accept-Language (RFC 4647 §3.4) by kawasima · Pull Request #115 · enkan/enkan · GitHub
[go: up one dir, main page]

Skip to content

fix: add reverse prefix matching for Accept-Language (RFC 4647 §3.4) - #115

Merged
kawasima merged 2 commits into
developfrom
feature/language-prefix-matching
Mar 28, 2026
Merged

kawasima merged 2 commits into
developfrom
feature/language-prefix-matching

Conversation

@kawasima
Copy link
Copy Markdown
Collaborator

Summary

  • Add reverse prefix matching: accept range "en" now matches available tag "en-gb" per RFC 4647 §3.4 Basic Filtering
  • Fix wildcard "*" handling to use the actual q value from the accepts map instead of hardcoded 0.01
  • Existing direction-1 behavior preserved: available "en-gb" still tries "en-gb" then "en" in accepts

Previously, Accept-Language: en with available ["en-gb"] returned null. Now correctly returns "en-gb".

Closes #111

Test plan

  • acceptLanguageRangeMatchesMoreSpecificTag — Accept-Language: en with available ["en-gb"] → "en-gb"
  • acceptLanguageRangeWithQuality — en;q=0.5, fr;q=1.0 with available ["en-gb", "fr-ca"] → "fr-ca"
  • acceptLanguageExactMatchPreferredOverPrefixMatch — exact en-gb;q=0.9 beats prefix en;q=0.5 on en-us
  • acceptLanguageWildcardStillWorks — Accept-Language: * returns a result
  • All 23 AcceptHeaderNegotiator tests pass (including existing language tests)

🤖 Generated with Claude Code

kawasima and others added 2 commits March 28, 2026 20:16
The scoring function only truncated the available tag to find a
matching range in accepts (e.g. available "en-gb" tries "en"). The
reverse — accept range "en" matching available "en-gb" — was missing.

Add a second pass that checks if any accept range is a prefix of the
available tag. Also fix wildcard "*" handling to use the actual q
value from the accepts map instead of a hardcoded 0.01.

Closes #111

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move range + "-" concatenation outside the per-tag scoring lambda into
a pre-computed List<RangePrefix>. Avoids a string allocation per
accept entry per available tag during language negotiation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kawasima
kawasima merged commit b8fd4a4 into develop Mar 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0