Skip to content

Commit 6f8de53

Browse files
authored
Prefilter bidirectional Unicode checks by UTF-8 leading bytes (astral-sh#29561)
1 parent c46acee commit 6f8de53

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎crates/ruff_linter/resources/test/fixtures/pylint/bidirectional_unicode.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ def subtract_funds(account: str, amount: int):
2929

3030
# OK
3131
print("Hello World")
32+
33+
# OK
34+
print("“مرحبا”")

‎crates/ruff_linter/src/rules/pylint/rules/bidirectional_unicode.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ impl Violation for BidirectionalUnicode {
6363

6464
/// PLE2502
6565
pub(crate) fn bidirectional_unicode(line: &Line, context: &LintContext) {
66-
if line.contains(BIDI_UNICODE) {
66+
let text = line.as_str();
67+
// All characters in BIDI_UNICODE start with 0xD8 or 0xE2 in UTF-8. These are leading
68+
// bytes, so the first match is a character boundary and the preceding text can be skipped.
69+
if let Some(start) = memchr::memchr2(0xD8, 0xE2, text.as_bytes())
70+
&& text[start..].contains(BIDI_UNICODE)
71+
{
6772
context.report_diagnostic(BidirectionalUnicode, line.full_range());
6873
}
6974
}

0 commit comments

Comments
 (0)