Skip to content

fix(py): preserve Unicode when truncating README summaries (#622) - #670

Open
Mohit-Gaur75 wants to merge 1 commit into
FindAny:mainfrom
Mohit-Gaur75:fix/py-readme-unicode-summary
Open

Mohit-Gaur75 wants to merge 1 commit into
FindAny:mainfrom
Mohit-Gaur75:fix/py-readme-unicode-summary

Conversation

@Mohit-Gaur75

Copy link
Copy Markdown

Summary

Closes #622 for the Python port (packages/ghfind-py).

In PR #668, clampText in scoring/src/extract.ts was updated to prevent splitting surrogate pairs when truncating README summaries. However, _clamp_text in packages/ghfind-py/ghfind/_extract.py still measured string length by code points (len(clean)) rather than UTF-16 code units, causing:

  1. Divergent summary length limits between Python and TypeScript when READMEs contained supplementary characters / emojis (e.g., 750 emojis produced a 1500 code point string which was ~3000 UTF-16 code units, leading to subsequent truncation issues in TS sanitizers).
  2. Boundary splitting when surrogate pairs straddle the truncation threshold.

Changes

  • Updated _clamp_text in packages/ghfind-py/ghfind/_extract.py:
    • Counts length in UTF-16 code units (sum(2 if ord(c) > 0xFFFF else 1 for c in clean)).
    • Truncates to limit - 1 units, ensuring that multi-unit characters crossing the target boundary are completely omitted before appending the ellipsis (…).
  • Added unit tests in packages/ghfind-py/tests/test_extract.py matching the TypeScript test suite in scoring/test/score.test.ts for all 5 Unicode boundary test cases (an intro, a section, the full summary, intro where emoji fits before ellipsis, intro at length limit).

Verification

  • Ran python3 -m pytest -q across packages/ghfind-py: all 301 tests pass.
  • Verified test_parse_readme_features_preserves_unicode passes and asserts utf16_len <= 1500.

This branch has not been deployed

No deployments
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.

README summary truncation can split UTF-8 characters and UTF-16 surrogate pairs

1 participant