From be071dd2635425a8122c8e3d3f96a971ad5d03b1 Mon Sep 17 00:00:00 2001 From: mohit-Gaur75 Date: Tue, 24 Nov 2026 02:55:35 +0530 Subject: [PATCH] fix(py): preserve Unicode when truncating README summaries (#622) --- packages/ghfind-py/ghfind/_extract.py | 15 ++++++++- packages/ghfind-py/tests/test_extract.py | 40 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/ghfind-py/ghfind/_extract.py b/packages/ghfind-py/ghfind/_extract.py index 294da75c..c877fc65 100644 --- a/packages/ghfind-py/ghfind/_extract.py +++ b/packages/ghfind-py/ghfind/_extract.py @@ -265,7 +265,20 @@ def _text_matches(text: str, words: List[str]) -> bool: def _clamp_text(text: str, limit: int) -> str: clean = re.sub(r"\s+", " ", text).strip() - return (clean[: limit - 1] + "…") if len(clean) > limit else clean + total_units = sum(2 if ord(c) > 0xFFFF else 1 for c in clean) + if total_units <= limit: + return clean + + target = limit - 1 + current_units = 0 + retained_chars = [] + for c in clean: + c_units = 2 if ord(c) > 0xFFFF else 1 + if current_units + c_units > target: + break + retained_chars.append(c) + current_units += c_units + return "".join(retained_chars) + "…" def parse_readme_features(markdown: str) -> Dict[str, Any]: diff --git a/packages/ghfind-py/tests/test_extract.py b/packages/ghfind-py/tests/test_extract.py index 7bde0604..0fa43f05 100644 --- a/packages/ghfind-py/tests/test_extract.py +++ b/packages/ghfind-py/tests/test_extract.py @@ -2,6 +2,8 @@ from datetime import datetime, timezone +import pytest + from ghfind._extract import ( compute_impact_from_contrib_map, compute_impact_quality_signals, @@ -10,6 +12,7 @@ is_doc_like_impact_pr, original_repo_quality_score, parse_github_noreply_login, + parse_readme_features, resolve_first_commit_github_login, ) @@ -196,3 +199,40 @@ def docs(repo): assert compute_impact_quality_signals(broad, 100, "alice")["impact_quality_cap"] is None concentrated = [docs(f"org/project-{0 if i < 10 else i}") for i in range(12)] assert compute_impact_quality_signals(concentrated, 12, "alice")["impact_quality_cap"] is None + + +_INTRO_PREFIX = "ζ–‡" * 348 +_SECTION_PREFIX = "a" * 218 +_TITLE_PREFIX = "a" * 1491 +_PREFIX_WITH_EMOJI = f"{'ζ–‡' * 347}πŸš€" + + +@pytest.mark.parametrize( + "name,markdown,expected", + [ + ("an intro", f"# Project\n\n{_INTRO_PREFIX}πŸš€end", f"Intro: {_INTRO_PREFIX}… Sections:"), + ( + "a section", + f"# Project\n\nOverview.\n\n## Usage\n\n{_SECTION_PREFIX}πŸš€end", + f"Usage: {_SECTION_PREFIX}… Signals: usage", + ), + ("the full summary", f"# {_TITLE_PREFIX}πŸš€end", f"Title: {_TITLE_PREFIX}…"), + ( + "an intro where the emoji fits before the ellipsis", + f"# Project\n\n{_PREFIX_WITH_EMOJI}end", + f"Intro: {_PREFIX_WITH_EMOJI}… Sections:", + ), + ( + "an intro at the length limit", + f"# Project\n\n{_INTRO_PREFIX}πŸš€", + f"Intro: {_INTRO_PREFIX}πŸš€ Sections:", + ), + ], +) +def test_parse_readme_features_preserves_unicode(name, markdown, expected): + features = parse_readme_features(markdown) + summary = features["prompt_summary"] + assert expected in summary + utf16_len = sum(2 if ord(c) > 0xFFFF else 1 for c in summary) + assert utf16_len <= 1500 +