Skip to content

Commit bb83d99

Browse files
author
Markus Westerlind
committed
perf: Avoid checking for NFC if the string is already normalized (-20%)
1 parent 3071ad4 commit bb83d99

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

idna/src/uts46.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,21 @@ fn passes_bidi(label: &str, is_bidi_domain: bool) -> bool {
220220
}
221221

222222
/// http://www.unicode.org/reports/tr46/#Validity_Criteria
223+
fn validate_full(label: &str, is_bidi_domain: bool, flags: Flags, errors: &mut Vec<Error>) {
224+
// V1: Must be in NFC form.
225+
if label.nfc().ne(label.chars()) {
226+
errors.push(Error::ValidityCriteria);
227+
} else {
228+
validate(label, is_bidi_domain, flags, errors);
229+
}
230+
}
231+
223232
fn validate(label: &str, is_bidi_domain: bool, flags: Flags, errors: &mut Vec<Error>) {
224233
let first_char = label.chars().next();
225234
if first_char == None {
226235
// Empty string, pass
227236
}
228237

229-
// V1: Must be in NFC form.
230-
else if label.nfc().ne(label.chars()) {
231-
errors.push(Error::ValidityCriteria);
232-
}
233-
234238
// V2: No U+002D HYPHEN-MINUS in both third and fourth positions.
235239
//
236240
// NOTE: Spec says that the label must not contain a HYPHEN-MINUS character in both the
@@ -322,12 +326,13 @@ fn processing(domain: &str, flags: Flags, errors: &mut Vec<Error>) -> String {
322326
match punycode::decode_to_string(&label[PUNYCODE_PREFIX.len()..]) {
323327
Some(decoded_label) => {
324328
let flags = Flags { transitional_processing: false, ..flags };
325-
validate(&decoded_label, is_bidi_domain, flags, errors);
329+
validate_full(&decoded_label, is_bidi_domain, flags, errors);
326330
validated.push_str(&decoded_label)
327331
}
328332
None => errors.push(Error::PunycodeError)
329333
}
330334
} else {
335+
// `normalized` is already `NFC` so we can skip that check
331336
validate(label, is_bidi_domain, flags, errors);
332337
validated.push_str(label)
333338
}

0 commit comments

Comments
 (0)