xutf 1.3.0

Permissive UTF-8/16/32 transcoding, comparison and BOM detection with SIMD ASCII fast paths
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
//! Unicode 17 NFC/NFD normalization with a SIMD quick-check fast path.
//!
//! Quick-check-positive strings return from the in-place API without writes. A
//! 64-lane UTF-8 scan skips ASCII, while compact generated tables drive
//! canonical decomposition, ordering, and composition for non-ASCII text.
//! Normalization uses the string's own allocation and no auxiliary allocation.

use alloc::{string::String, vec::Vec};
use core::{
	fmt, ptr,
	simd::{Simd, cmp::SimdPartialOrd},
};

#[path = "normalize_data.rs"]
mod data;

const CCC_MASK: u32 = 0xff;
const NFD_NO_BIT: u32 = 1 << 8;
const NFC_MAYBE_BIT: u32 = 1 << 9;
const NFC_NO_BIT: u32 = 1 << 10;
const DECOMPOSITION_SHIFT: u32 = 11;
const DECOMPOSITION_MASK: u32 = 0x07ff;
const DECOMPOSITION_ORDER_BIT: u32 = 1 << 22;
const COMPOSITION_SHIFT: u32 = 23;
const ORDER_STACK: usize = 16;
const CODEPOINT_MASK: u64 = (1 << 21) - 1;

const S_BASE: u32 = 0xac00;
const L_BASE: u32 = 0x1100;
const V_BASE: u32 = 0x1161;
const T_BASE: u32 = 0x11a7;
const L_COUNT: u32 = 19;
const V_COUNT: u32 = 21;
const T_COUNT: u32 = 28;
const N_COUNT: u32 = V_COUNT * T_COUNT;
const S_COUNT: u32 = L_COUNT * N_COUNT;

const _: () = assert!(
	data::NORMALIZATION_UNICODE_VERSION.0 == crate::UNICODE_VERSION.0
		&& data::NORMALIZATION_UNICODE_VERSION.1 == crate::UNICODE_VERSION.1
		&& data::NORMALIZATION_UNICODE_VERSION.2 == crate::UNICODE_VERSION.2
);

/// Reports the workspace capacity needed for allocation-free normalization.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NormalizationError {
	required_capacity: usize,
}

impl NormalizationError {
	/// Minimum UTF-8 byte capacity required by the in-place operation.
	#[inline(always)]
	pub const fn required_capacity(self) -> usize {
		self.required_capacity
	}
}

impl fmt::Display for NormalizationError {
	fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(
			formatter,
			"normalization requires capacity for {} UTF-8 bytes",
			self.required_capacity
		)
	}
}

/// Normalizes a string in place without allocating.
///
/// Reserve the capacity reported by [`NormalizationError`] and retry when an
/// expanding decomposition does not fit.
pub trait MakeUnicodeNormalized {
	/// Converts this string to NFC using only its existing allocation.
	fn make_nfc(&mut self) -> Result<(), NormalizationError>;

	/// Converts this string to NFD using only its existing allocation.
	fn make_nfd(&mut self) -> Result<(), NormalizationError>;
}

/// Creates owned NFC or NFD text from a borrowed string.
pub trait ToUnicodeNormalized {
	/// Copies this string once and returns its NFC form.
	fn to_nfc(&self) -> String;

	/// Copies this string once and returns its NFD form.
	fn to_nfd(&self) -> String;
}

/// Converts an owned string to NFC or NFD while reusing its allocation.
pub trait IntoUnicodeNormalized {
	/// Returns this string in NFC, growing its allocation only when required.
	fn into_nfc(self) -> String;

	/// Returns this string in NFD, growing its allocation only when required.
	fn into_nfd(self) -> String;
}

/// Reports whether `text` is definitely in NFC form.
///
/// Quick-check semantics without allocation: `true` means normalizing is a
/// no-op; `false` means normalization *may* change the text (`NFC_QC=Maybe`
/// codepoints and mis-ordered combining marks report `false` without
/// composing). Use before [`ToUnicodeNormalized::to_nfc`] to keep borrowed
/// fast paths.
#[inline]
pub fn is_nfc(text: &str) -> bool {
	scan(text, Form::Nfc).normalized
}

/// [`is_nfc`] over raw codepoints, for callers holding non-UTF-8 text
/// (UTF-16/UTF-32 units) who want the quick-check without transcoding.
///
/// Surrogate and out-of-range values are treated as inert (they normalize
/// to themselves under permissive decoding). Identical verdicts to
/// [`is_nfc`] for any sequence of Unicode scalar values.
pub fn is_nfc_codepoints(codepoints: impl IntoIterator<Item = u32>) -> bool {
	let mut last_ccc = 0u8;
	for cp in codepoints {
		if cp > 0x10ffff {
			last_ccc = 0;
			continue;
		}
		let word = normalization_word(cp);
		let ccc = combining_class(word);
		if (ccc != 0 && last_ccc > ccc) || word & (NFC_NO_BIT | NFC_MAYBE_BIT) != 0 {
			return false;
		}
		last_ccc = ccc;
	}
	true
}

/// Canonical Combining Class (ccc) of a codepoint; 0 for starters,
/// out-of-range input and unassigned codepoints.
#[inline]
pub fn canonical_combining_class(cp: u32) -> u8 {
	if cp > 0x10ffff {
		return 0;
	}
	combining_class(normalization_word(cp))
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Form {
	Nfc,
	Nfd,
}

#[derive(Clone, Copy)]
struct Scan {
	normalized:  bool,
	nfd_len:     usize,
	decompose:   bool,
	reorder:     bool,
	shrinks:     bool,
	stack_order: bool,
}

#[inline(always)]
fn normalization_word(cp: u32) -> u32 {
	debug_assert!(cp <= 0x10ffff);
	let cp = cp as usize;
	let block = data::NORMALIZATION_STAGE1[cp >> data::NORMALIZATION_SHIFT] as usize;
	data::NORMALIZATION_STAGE2
		[(block << data::NORMALIZATION_SHIFT) | (cp & data::NORMALIZATION_MASK)]
}

#[inline(always)]
const fn combining_class(word: u32) -> u8 {
	(word & CCC_MASK) as u8
}

#[inline(always)]
fn decomposition(word: u32) -> Option<&'static [u32]> {
	let index = (word >> DECOMPOSITION_SHIFT) & DECOMPOSITION_MASK;
	if index == 0 {
		return None;
	}
	let record = data::DECOMPOSITION_RECORDS[index as usize] as usize;
	let start = record >> 3;
	let len = record & 7;
	Some(&data::DECOMPOSITION_CHARS[start..start + len])
}

#[inline(always)]
const fn is_hangul_syllable(cp: u32) -> bool {
	cp.wrapping_sub(S_BASE) < S_COUNT
}

#[inline(always)]
fn for_each_decomposed(cp: u32, word: u32, mut emit: impl FnMut(u32)) {
	if is_hangul_syllable(cp) {
		let index = cp - S_BASE;
		emit(L_BASE + index / N_COUNT);
		emit(V_BASE + index % N_COUNT / T_COUNT);
		let trailing = index % T_COUNT;
		if trailing != 0 {
			emit(T_BASE + trailing);
		}
	} else if let Some(mapped) = decomposition(word) {
		for &part in mapped {
			emit(part);
		}
	} else {
		emit(cp);
	}
}

#[inline(always)]
const fn utf8_len(cp: u32) -> usize {
	if cp < 0x80 {
		1
	} else if cp < 0x800 {
		2
	} else if cp < 0x10000 {
		3
	} else {
		4
	}
}

#[inline(always)]
fn decomposed_utf8_len(cp: u32, word: u32) -> usize {
	if !is_hangul_syllable(cp) && decomposition(word).is_none() {
		return utf8_len(cp);
	}
	let mut len = 0;
	for_each_decomposed(cp, word, |part| len += utf8_len(part));
	len
}

#[inline(always)]
fn decode_utf8(input: &[u8], at: usize) -> (u32, usize) {
	let first = input[at];
	if first < 0x80 {
		return (u32::from(first), 1);
	}
	if first < 0xe0 {
		return ((u32::from(first & 0x1f) << 6) | u32::from(input[at + 1] & 0x3f), 2);
	}
	if first < 0xf0 {
		return (
			(u32::from(first & 0x0f) << 12)
				| (u32::from(input[at + 1] & 0x3f) << 6)
				| u32::from(input[at + 2] & 0x3f),
			3,
		);
	}
	(
		(u32::from(first & 7) << 18)
			| (u32::from(input[at + 1] & 0x3f) << 12)
			| (u32::from(input[at + 2] & 0x3f) << 6)
			| u32::from(input[at + 3] & 0x3f),
		4,
	)
}

#[inline(always)]
fn encode_utf8(cp: u32, output: &mut [u8]) -> usize {
	let len = utf8_len(cp);
	match len {
		1 => output[0] = cp as u8,
		2 => {
			output[0] = 0xc0 | (cp >> 6) as u8;
			output[1] = 0x80 | (cp & 0x3f) as u8;
		},
		3 => {
			output[0] = 0xe0 | (cp >> 12) as u8;
			output[1] = 0x80 | ((cp >> 6) & 0x3f) as u8;
			output[2] = 0x80 | (cp & 0x3f) as u8;
		},
		4 => {
			output[0] = 0xf0 | (cp >> 18) as u8;
			output[1] = 0x80 | ((cp >> 12) & 0x3f) as u8;
			output[2] = 0x80 | ((cp >> 6) & 0x3f) as u8;
			output[3] = 0x80 | (cp & 0x3f) as u8;
		},
		_ => unreachable!(),
	}
	len
}

/// Writes one valid scalar without constructing a slice beyond the vector's
/// initialized length.
///
/// # Safety
/// `output` must have room for [`utf8_len`] bytes.
#[inline(always)]
unsafe fn encode_utf8_ptr(cp: u32, output: *mut u8) -> usize {
	let mut encoded = [0; 4];
	let len = encode_utf8(cp, &mut encoded);
	// SAFETY: the caller provides `len` writable bytes and `encoded` contains
	// exactly that many initialized source bytes.
	unsafe { ptr::copy_nonoverlapping(encoded.as_ptr(), output, len) };
	len
}

#[inline(always)]
fn write_decomposition(cp: u32, word: u32, output: &mut [u8]) -> usize {
	let mut written = 0;
	for_each_decomposed(cp, word, |part| {
		written += encode_utf8(part, &mut output[written..]);
	});
	written
}

/// Writes a complete decomposition into spare vector capacity.
///
/// # Safety
/// `output` must have room for [`decomposed_utf8_len`] bytes.
#[inline(always)]
unsafe fn write_decomposition_ptr(cp: u32, word: u32, output: *mut u8) -> usize {
	let mut written = 0;
	for_each_decomposed(cp, word, |part| {
		// SAFETY: the caller reserves the full decomposition and each prior write
		// advances within that region.
		written += unsafe { encode_utf8_ptr(part, output.add(written)) };
	});
	written
}

#[inline(always)]
fn non_ascii_mask(chunk: Simd<u8, 64>) -> u64 {
	chunk.simd_gt(Simd::splat(0x7f)).to_bitmask()
}

#[inline(always)]
fn non_ascii_mask_narrow(chunk: Simd<u8, 16>) -> u64 {
	chunk.simd_gt(Simd::splat(0x7f)).to_bitmask()
}

#[inline]
fn ascii_prefix(input: &[u8]) -> usize {
	if input.is_empty() || input[0] >= 0x80 {
		return 0;
	}
	let mut at = if input.len() >= 16 {
		let mask = non_ascii_mask_narrow(Simd::from_slice(input));
		if mask != 0 {
			return mask.trailing_zeros() as usize;
		}
		16
	} else {
		0
	};
	while input.len() - at >= 256 {
		let first = non_ascii_mask(Simd::from_slice(&input[at..]));
		if first != 0 {
			return at + first.trailing_zeros() as usize;
		}
		let second = non_ascii_mask(Simd::from_slice(&input[at + 64..]));
		if second != 0 {
			return at + 64 + second.trailing_zeros() as usize;
		}
		let third = non_ascii_mask(Simd::from_slice(&input[at + 128..]));
		if third != 0 {
			return at + 128 + third.trailing_zeros() as usize;
		}
		let fourth = non_ascii_mask(Simd::from_slice(&input[at + 192..]));
		if fourth != 0 {
			return at + 192 + fourth.trailing_zeros() as usize;
		}
		at += 256;
	}
	while input.len() - at >= 64 {
		let mask = non_ascii_mask(Simd::from_slice(&input[at..]));
		if mask != 0 {
			return at + mask.trailing_zeros() as usize;
		}
		at += 64;
	}
	while input.len() - at >= 16 {
		let mask = non_ascii_mask_narrow(Simd::from_slice(&input[at..]));
		if mask != 0 {
			return at + mask.trailing_zeros() as usize;
		}
		at += 16;
	}
	while at < input.len() && input[at] < 0x80 {
		at += 1;
	}
	at
}

#[inline(always)]
fn ascii_suffix(input: &[u8]) -> usize {
	if input.is_empty() || input[input.len() - 1] >= 0x80 {
		return 0;
	}
	if input.len() >= 64 {
		let mask = non_ascii_mask(Simd::from_slice(&input[input.len() - 64..]));
		return mask.leading_zeros() as usize;
	}
	input.iter().rev().take_while(|&&byte| byte < 0x80).count()
}

#[inline(always)]
const fn compose_hangul(first: u32, second: u32) -> Option<u32> {
	if first.wrapping_sub(L_BASE) < L_COUNT && second.wrapping_sub(V_BASE) < V_COUNT {
		let leading = first - L_BASE;
		let vowel = second - V_BASE;
		return Some(S_BASE + leading * N_COUNT + vowel * T_COUNT);
	}
	if first.wrapping_sub(S_BASE) < S_COUNT
		&& (first - S_BASE).is_multiple_of(T_COUNT)
		&& second.wrapping_sub(T_BASE + 1) < T_COUNT - 1
	{
		return Some(first + second - T_BASE);
	}
	None
}

#[inline]
fn compose_pair(first: u32, second: u32) -> Option<u32> {
	if let Some(composite) = compose_hangul(first, second) {
		return Some(composite);
	}
	let group = (normalization_word(first) >> COMPOSITION_SHIFT) as usize;
	if group == 0 {
		return None;
	}
	let pairs = &data::COMPOSITION_PAIRS
		[data::COMPOSITION_OFFSETS[group] as usize..data::COMPOSITION_OFFSETS[group + 1] as usize];
	if pairs.len() <= 8 {
		for &packed in pairs {
			let candidate = (packed >> 21) as u32;
			if candidate == second {
				return Some((packed & CODEPOINT_MASK) as u32);
			}
			if candidate > second {
				return None;
			}
		}
		return None;
	}
	pairs
		.binary_search_by_key(&second, |packed| (*packed >> 21) as u32)
		.ok()
		.map(|index| (pairs[index] & CODEPOINT_MASK) as u32)
}

#[inline]
fn decomposition_workspace(bytes: &[u8]) -> (usize, bool) {
	let mut at = 0;
	let mut required = 0;
	let mut shrinks = false;
	while at < bytes.len() {
		let ascii = ascii_prefix(&bytes[at..]);
		if ascii != 0 {
			required += ascii;
			at += ascii;
			if at == bytes.len() {
				break;
			}
		}
		let (cp, width) = decode_utf8(bytes, at);
		let word = normalization_word(cp);
		let decomposed_len = if word & NFD_NO_BIT == 0 {
			width
		} else {
			decomposed_utf8_len(cp, word)
		};
		required += decomposed_len;
		shrinks |= decomposed_len < width;
		at += width;
	}
	(required, shrinks)
}

#[inline]
fn scan(input: &str, form: Form) -> Scan {
	let bytes = input.as_bytes();
	let mut at = 0;
	let mut nfd_len = 0;
	let mut normalized = true;
	let mut decompose = false;
	let mut reorder = false;
	let mut shrinks = false;
	let mut segment_len = 0;
	let mut stack_order = true;
	let mut starter_risky = false;
	let mut last_ccc = 0;

	while at < bytes.len() {
		let ascii = ascii_prefix(&bytes[at..]);
		if ascii != 0 {
			if form == Form::Nfd {
				nfd_len += ascii;
			}
			at += ascii;
			starter_risky = false;
			last_ccc = 0;
			segment_len = 1;
			if at == bytes.len() {
				break;
			}
		}

		let (cp, width) = decode_utf8(bytes, at);
		let word = normalization_word(cp);
		let ccc = combining_class(word);
		if ccc == 0 {
			segment_len = 1;
		} else {
			segment_len += 1;
			stack_order &= segment_len <= ORDER_STACK;
		}
		if form == Form::Nfd {
			let decomposed_len = if word & NFD_NO_BIT == 0 {
				width
			} else {
				decomposed_utf8_len(cp, word)
			};
			nfd_len += decomposed_len;
			shrinks |= decomposed_len < width;
		}
		if ccc != 0 && last_ccc > ccc {
			normalized = false;
			reorder = true;
		}

		match form {
			Form::Nfd => {
				let decomposes = word & NFD_NO_BIT != 0;
				decompose |= decomposes;
				reorder |= decomposes && word & DECOMPOSITION_ORDER_BIT != 0;
				normalized &= !decomposes;
				last_ccc = ccc;
			},
			Form::Nfc => {
				let excluded = word & NFC_NO_BIT != 0;
				let maybe = word & NFC_MAYBE_BIT != 0;
				normalized &= !excluded && !maybe;
				decompose |= excluded;
				reorder |= excluded && word & DECOMPOSITION_ORDER_BIT != 0;
				if maybe && starter_risky {
					decompose = true;
					reorder = true;
				}
				if ccc == 0 {
					starter_risky = decomposition(word).is_some();
				}
				last_ccc = ccc;
			},
		}
		at += width;
	}

	if form == Form::Nfc {
		if decompose {
			(nfd_len, shrinks) = decomposition_workspace(bytes);
		} else {
			nfd_len = bytes.len();
		}
	}

	Scan { normalized, nfd_len, decompose, reorder, shrinks, stack_order }
}

#[inline]
fn shrink_decompositions(bytes: &mut Vec<u8>) {
	let original_len = bytes.len();
	let mut read = 0;
	let mut write = 0;
	while read < original_len {
		let ascii = ascii_prefix(&bytes[read..original_len]);
		if ascii != 0 {
			if read != write {
				bytes.copy_within(read..read + ascii, write);
			}
			read += ascii;
			write += ascii;
			continue;
		}

		let (cp, width) = decode_utf8(bytes, read);
		let word = normalization_word(cp);
		let decomposed_len = decomposed_utf8_len(cp, word);
		if decomposed_len < width {
			let written = write_decomposition(cp, word, &mut bytes[write..]);
			debug_assert_eq!(written, decomposed_len);
			write += written;
		} else {
			if read != write {
				bytes.copy_within(read..read + width, write);
			}
			write += width;
		}
		read += width;
	}
	bytes.truncate(write);
}

#[inline(always)]
const fn previous_char_start(bytes: &[u8]) -> usize {
	let mut start = bytes.len() - 1;
	while bytes[start] & 0xc0 == 0x80 {
		start -= 1;
	}
	start
}

#[inline]
fn expand_decompositions(bytes: &mut Vec<u8>, required: usize) {
	let source_len = bytes.len();
	debug_assert!(required >= source_len);
	debug_assert!(required <= bytes.capacity());
	let output = bytes.as_mut_ptr();
	let mut read = source_len;
	let mut write = required;

	while read != 0 {
		let ascii = ascii_suffix(&bytes[..read]);
		if ascii != 0 {
			read -= ascii;
			write -= ascii;
			// SAFETY: the source is initialized, the destination is within reserved
			// capacity, and `copy` permits the possible overlap.
			unsafe { ptr::copy(output.add(read), output.add(write), ascii) };
			continue;
		}

		let start = previous_char_start(&bytes[..read]);
		let (cp, width) = decode_utf8(bytes, start);
		debug_assert_eq!(start + width, read);
		let word = normalization_word(cp);
		let decomposed_len = decomposed_utf8_len(cp, word);
		write -= decomposed_len;
		if is_hangul_syllable(cp) || decomposition(word).is_some() {
			// SAFETY: `write..write + decomposed_len` lies in reserved capacity and
			// cannot overlap unprocessed input because every remaining mapping is
			// non-shrinking after `shrink_decompositions`.
			let written = unsafe { write_decomposition_ptr(cp, word, output.add(write)) };
			debug_assert_eq!(written, decomposed_len);
		} else {
			// SAFETY: the same directional invariant leaves the source initialized
			// until this overlapping copy completes.
			unsafe { ptr::copy(output.add(start), output.add(write), width) };
		}
		read = start;
	}

	debug_assert_eq!(write, 0);
	// SAFETY: every byte in `0..required` was initialized by the backward pass,
	// and canonical decomposition preserves UTF-8 validity.
	unsafe { bytes.set_len(required) };
}

#[inline]
fn decompose_in_place(bytes: &mut Vec<u8>, required: usize, shrinks: bool) {
	if shrinks {
		shrink_decompositions(bytes);
	}
	expand_decompositions(bytes, required);
}

#[inline]
fn canonical_order(bytes: &mut [u8]) {
	let mut at = 0;
	let mut marks_start = 0;
	let mut previous_ccc = 0;
	while at < bytes.len() {
		let ascii = ascii_prefix(&bytes[at..]);
		if ascii != 0 {
			at += ascii;
			marks_start = at;
			previous_ccc = 0;
			continue;
		}

		let (cp, width) = decode_utf8(bytes, at);
		let ccc = combining_class(normalization_word(cp));
		let next = at + width;
		if ccc == 0 {
			marks_start = next;
			previous_ccc = 0;
		} else if ccc < previous_ccc {
			let mut insert = marks_start;
			while insert < at {
				let (prior, prior_width) = decode_utf8(bytes, insert);
				if combining_class(normalization_word(prior)) > ccc {
					break;
				}
				insert += prior_width;
			}
			bytes[insert..next].rotate_right(width);
		} else {
			previous_ccc = ccc;
		}
		at = next;
	}
}

#[derive(Default)]
struct Composition {
	write:    usize,
	starter:  Option<(usize, usize, u32)>,
	last_ccc: u8,
}

#[inline(always)]
fn emit_ascii(bytes: &mut [u8], source: usize, len: usize, state: &mut Composition) {
	bytes.copy_within(source..source + len, state.write);
	state.write += len;
	state.starter = Some((state.write - 1, 1, u32::from(bytes[state.write - 1])));
	state.last_ccc = 0;
}

#[inline]
fn emit_composed(
	bytes: &mut [u8],
	cp: u32,
	ccc: u8,
	source: Option<(usize, usize)>,
	state: &mut Composition,
) {
	let composite = state.starter.and_then(|(_, _, starter_cp)| {
		(state.last_ccc == 0 || state.last_ccc < ccc)
			.then(|| compose_pair(starter_cp, cp))
			.flatten()
	});
	if let (Some(composite), Some((starter_at, starter_width, _))) = (composite, state.starter) {
		let composite_width = utf8_len(composite);
		let tail_start = starter_at + starter_width;
		if composite_width > starter_width {
			let growth = composite_width - starter_width;
			bytes.copy_within(tail_start..state.write, tail_start + growth);
			state.write += growth;
		} else if composite_width < starter_width {
			let shrink = starter_width - composite_width;
			bytes.copy_within(tail_start..state.write, tail_start - shrink);
			state.write -= shrink;
		}
		let written = encode_utf8(composite, &mut bytes[starter_at..]);
		debug_assert_eq!(written, composite_width);
		state.starter = Some((starter_at, composite_width, composite));
		return;
	}

	let width = if let Some((source, width)) = source {
		bytes.copy_within(source..source + width, state.write);
		width
	} else {
		encode_utf8(cp, &mut bytes[state.write..])
	};
	if ccc == 0 {
		state.starter = Some((state.write, width, cp));
	}
	state.write += width;
	state.last_ccc = ccc;
}

#[inline]
fn compose_in_place(bytes: &mut Vec<u8>) {
	let input_len = bytes.len();
	let mut read = 0;
	let mut state = Composition::default();
	while read < input_len {
		let ascii = ascii_prefix(&bytes[read..input_len]);
		if ascii != 0 {
			emit_ascii(bytes, read, ascii, &mut state);
			read += ascii;
			continue;
		}
		let (cp, width) = decode_utf8(bytes, read);
		let ccc = combining_class(normalization_word(cp));
		emit_composed(bytes, cp, ccc, Some((read, width)), &mut state);
		read += width;
	}
	bytes.truncate(state.write);
}

#[inline]
fn order_and_compose_in_place(bytes: &mut Vec<u8>) {
	let input_len = bytes.len();
	let mut read = 0;
	let mut state = Composition::default();
	let mut codepoints = [0; ORDER_STACK];
	let mut classes = [0; ORDER_STACK];

	while read < input_len {
		let ascii = ascii_prefix(&bytes[read..input_len]);
		if ascii != 0 {
			emit_ascii(bytes, read, ascii, &mut state);
			read += ascii;
			continue;
		}

		let mut count = 0;
		while read < input_len && bytes[read] >= 0x80 {
			let (cp, width) = decode_utf8(bytes, read);
			let ccc = combining_class(normalization_word(cp));
			if count != 0 && ccc == 0 {
				break;
			}
			debug_assert!(count < ORDER_STACK);
			codepoints[count] = cp;
			classes[count] = ccc;
			count += 1;
			read += width;
		}

		let marks = usize::from(classes[0] == 0);
		for current in marks + 1..count {
			let cp = codepoints[current];
			let ccc = classes[current];
			let mut insert = current;
			while insert > marks && classes[insert - 1] > ccc {
				codepoints[insert] = codepoints[insert - 1];
				classes[insert] = classes[insert - 1];
				insert -= 1;
			}
			codepoints[insert] = cp;
			classes[insert] = ccc;
		}
		for index in 0..count {
			emit_composed(bytes, codepoints[index], classes[index], None, &mut state);
		}
	}
	bytes.truncate(state.write);
}

#[inline]
fn normalize_scanned(input: &mut String, form: Form, scan: Scan) {
	debug_assert!(!scan.normalized);
	debug_assert!(input.capacity() >= scan.nfd_len);
	// SAFETY: all transformations below preserve scalar boundaries and encode
	// only valid Unicode scalars before the mutable vector borrow ends.
	let bytes = unsafe { input.as_mut_vec() };
	if form == Form::Nfc && !scan.decompose && scan.reorder && scan.stack_order {
		order_and_compose_in_place(bytes);
		return;
	}
	if scan.decompose {
		decompose_in_place(bytes, scan.nfd_len, scan.shrinks);
		if scan.reorder {
			canonical_order(bytes);
		}
	} else if scan.reorder {
		canonical_order(bytes);
	}
	if form == Form::Nfc {
		compose_in_place(bytes);
	}
}

#[inline]
fn make_normalized(input: &mut String, form: Form) -> Result<(), NormalizationError> {
	let scan = scan(input, form);
	if scan.normalized {
		return Ok(());
	}
	if scan.nfd_len > input.capacity() {
		return Err(NormalizationError { required_capacity: scan.nfd_len });
	}
	normalize_scanned(input, form, scan);
	Ok(())
}

#[inline]
fn to_normalized(input: &str, form: Form) -> String {
	let scan = scan(input, form);
	if scan.normalized {
		return input.to_owned();
	}
	let mut output = String::with_capacity(input.len().max(scan.nfd_len));
	output.push_str(input);
	normalize_scanned(&mut output, form, scan);
	output
}

#[inline]
fn into_normalized(mut input: String, form: Form) -> String {
	let scan = scan(&input, form);
	if scan.normalized {
		return input;
	}
	if scan.nfd_len > input.capacity() {
		input.reserve(scan.nfd_len - input.len());
	}
	normalize_scanned(&mut input, form, scan);
	input
}

impl MakeUnicodeNormalized for String {
	#[inline]
	fn make_nfc(&mut self) -> Result<(), NormalizationError> {
		make_normalized(self, Form::Nfc)
	}

	#[inline]
	fn make_nfd(&mut self) -> Result<(), NormalizationError> {
		make_normalized(self, Form::Nfd)
	}
}

impl ToUnicodeNormalized for str {
	#[inline]
	fn to_nfc(&self) -> String {
		to_normalized(self, Form::Nfc)
	}

	#[inline]
	fn to_nfd(&self) -> String {
		to_normalized(self, Form::Nfd)
	}
}

impl IntoUnicodeNormalized for String {
	#[inline]
	fn into_nfc(self) -> String {
		into_normalized(self, Form::Nfc)
	}

	#[inline]
	fn into_nfd(self) -> String {
		into_normalized(self, Form::Nfd)
	}
}