@@ -36,7 +36,7 @@ use crate::provider::CollationReordering;
36
36
use crate :: provider:: CollationReorderingV1 ;
37
37
use crate :: provider:: CollationRootV1 ;
38
38
use crate :: provider:: CollationSpecialPrimaries ;
39
- use crate :: provider:: CollationSpecialPrimariesV1 ;
39
+ use crate :: provider:: CollationSpecialPrimariesV2 ;
40
40
use crate :: provider:: CollationTailoringV1 ;
41
41
use core:: cmp:: Ordering ;
42
42
use core:: convert:: TryFrom ;
@@ -379,7 +379,7 @@ impl LocaleSpecificDataHolder {
379
379
/// Compares strings according to culturally-relevant ordering.
380
380
#[ derive( Debug ) ]
381
381
pub struct Collator {
382
- special_primaries : Option < DataPayload < CollationSpecialPrimariesV1 > > ,
382
+ special_primaries : DataPayload < CollationSpecialPrimariesV2 > ,
383
383
root : DataPayload < CollationRootV1 > ,
384
384
tailoring : Option < DataPayload < CollationTailoringV1 > > ,
385
385
jamo : DataPayload < CollationJamoV1 > ,
@@ -395,7 +395,7 @@ impl Collator {
395
395
/// Constructs a borrowed version of this type for more efficient querying.
396
396
pub fn as_borrowed ( & self ) -> CollatorBorrowed {
397
397
CollatorBorrowed {
398
- special_primaries : self . special_primaries . as_ref ( ) . map ( |s| s . get ( ) ) ,
398
+ special_primaries : self . special_primaries . get ( ) ,
399
399
root : self . root . get ( ) ,
400
400
tailoring : self . tailoring . as_ref ( ) . map ( |s| s. get ( ) ) ,
401
401
jamo : self . jamo . get ( ) ,
@@ -434,7 +434,7 @@ impl Collator {
434
434
options : CollatorOptions ,
435
435
) -> Result < Self , DataError >
436
436
where
437
- D : DataProvider < CollationSpecialPrimariesV1 >
437
+ D : DataProvider < CollationSpecialPrimariesV2 >
438
438
+ DataProvider < CollationRootV1 >
439
439
+ DataProvider < CollationTailoringV1 >
440
440
+ DataProvider < CollationDiacriticsV1 >
@@ -451,7 +451,7 @@ impl Collator {
451
451
provider. load ( Default :: default ( ) ) ?. payload ,
452
452
provider. load ( Default :: default ( ) ) ?. payload ,
453
453
provider. load ( Default :: default ( ) ) ?. payload ,
454
- || provider. load ( Default :: default ( ) ) . map ( |r| r . payload ) ,
454
+ provider. load ( Default :: default ( ) ) ? . payload ,
455
455
prefs,
456
456
options,
457
457
)
@@ -464,7 +464,7 @@ impl Collator {
464
464
decompositions : DataPayload < NormalizerNfdDataV1 > ,
465
465
tables : DataPayload < NormalizerNfdTablesV1 > ,
466
466
jamo : DataPayload < CollationJamoV1 > ,
467
- special_primaries : impl FnOnce ( ) -> Result < DataPayload < CollationSpecialPrimariesV1 > , DataError > ,
467
+ special_primaries : DataPayload < CollationSpecialPrimariesV2 > ,
468
468
prefs : CollatorPreferences ,
469
469
options : CollatorOptions ,
470
470
) -> Result < Self , DataError >
@@ -484,22 +484,14 @@ impl Collator {
484
484
return Err ( DataError :: custom ( "invalid" ) . with_marker ( CollationJamoV1 :: INFO ) ) ;
485
485
}
486
486
487
- let special_primaries = if locale_dependent. merged_options . alternate_handling ( )
488
- == AlternateHandling :: Shifted
489
- || locale_dependent. merged_options . numeric ( )
490
- {
491
- let special_primaries = special_primaries ( ) ?;
492
- // `variant_count` isn't stable yet:
493
- // https://github.com/rust-lang/rust/issues/73662
494
- if special_primaries. get ( ) . last_primaries . len ( ) <= ( MaxVariable :: Currency as usize ) {
495
- return Err (
496
- DataError :: custom ( "invalid" ) . with_marker ( CollationSpecialPrimariesV1 :: INFO )
497
- ) ;
498
- }
499
- Some ( special_primaries)
500
- } else {
501
- None
502
- } ;
487
+ // `variant_count` isn't stable yet:
488
+ // https://github.com/rust-lang/rust/issues/73662
489
+ if special_primaries. get ( ) . last_primaries . len ( ) <= ( MaxVariable :: Currency as usize ) {
490
+ return Err ( DataError :: custom ( "invalid" ) . with_marker ( CollationSpecialPrimariesV2 :: INFO ) ) ;
491
+ }
492
+ if special_primaries. get ( ) . compressible_bytes . len ( ) != 32 {
493
+ return Err ( DataError :: custom ( "invalid" ) . with_marker ( CollationSpecialPrimariesV2 :: INFO ) ) ;
494
+ }
503
495
504
496
Ok ( Collator {
505
497
special_primaries,
@@ -543,7 +535,7 @@ macro_rules! compare {
543
535
/// borrowed version.
544
536
#[ derive( Debug ) ]
545
537
pub struct CollatorBorrowed < ' a > {
546
- special_primaries : Option < & ' a CollationSpecialPrimaries < ' a > > ,
538
+ special_primaries : & ' a CollationSpecialPrimaries < ' a > ,
547
539
root : & ' a CollationData < ' a > ,
548
540
tailoring : Option < & ' a CollationData < ' a > > ,
549
541
jamo : & ' a CollationJamo < ' a > ,
@@ -579,23 +571,15 @@ impl CollatorBorrowed<'static> {
579
571
return Err ( DataError :: custom ( "invalid" ) . with_marker ( CollationJamoV1 :: INFO ) ) ;
580
572
}
581
573
582
- let special_primaries = if locale_dependent. merged_options . alternate_handling ( )
583
- == AlternateHandling :: Shifted
584
- || locale_dependent. merged_options . numeric ( )
585
- {
586
- let special_primaries =
587
- crate :: provider:: Baked :: SINGLETON_COLLATION_SPECIAL_PRIMARIES_V1 ;
588
- // `variant_count` isn't stable yet:
589
- // https://github.com/rust-lang/rust/issues/73662
590
- if special_primaries. last_primaries . len ( ) <= ( MaxVariable :: Currency as usize ) {
591
- return Err (
592
- DataError :: custom ( "invalid" ) . with_marker ( CollationSpecialPrimariesV1 :: INFO )
593
- ) ;
594
- }
595
- Some ( special_primaries)
596
- } else {
597
- None
598
- } ;
574
+ let special_primaries = crate :: provider:: Baked :: SINGLETON_COLLATION_SPECIAL_PRIMARIES_V2 ;
575
+ // `variant_count` isn't stable yet:
576
+ // https://github.com/rust-lang/rust/issues/73662
577
+ if special_primaries. last_primaries . len ( ) <= ( MaxVariable :: Currency as usize ) {
578
+ return Err ( DataError :: custom ( "invalid" ) . with_marker ( CollationSpecialPrimariesV2 :: INFO ) ) ;
579
+ }
580
+ if special_primaries. compressible_bytes . len ( ) != 32 {
581
+ return Err ( DataError :: custom ( "invalid" ) . with_marker ( CollationSpecialPrimariesV2 :: INFO ) ) ;
582
+ }
599
583
600
584
// Attribute belongs closer to `unwrap`, but
601
585
// https://github.com/rust-lang/rust/issues/15701
@@ -623,12 +607,7 @@ impl CollatorBorrowed<'static> {
623
607
/// compile-time optimizations that are possible with [`CollatorBorrowed`].
624
608
pub const fn static_to_owned ( self ) -> Collator {
625
609
Collator {
626
- special_primaries : if let Some ( s) = self . special_primaries {
627
- // `map` not available in const context
628
- Some ( DataPayload :: from_static_ref ( s) )
629
- } else {
630
- None
631
- } ,
610
+ special_primaries : DataPayload :: from_static_ref ( self . special_primaries ) ,
632
611
root : DataPayload :: from_static_ref ( self . root ) ,
633
612
tailoring : if let Some ( s) = self . tailoring {
634
613
// `map` not available in const context
@@ -751,10 +730,6 @@ impl CollatorBorrowed<'_> {
751
730
} else {
752
731
// +1 so that we can use "<" and primary ignorables test out early.
753
732
self . special_primaries
754
- . as_ref ( )
755
- // `unwrap()` is OK, because we've ensured in the constructor that value
756
- // is `Some` if we have alternate handling.
757
- . unwrap ( )
758
733
. last_primary_for_group ( self . options . max_variable ( ) )
759
734
+ 1
760
735
} ;
@@ -763,13 +738,7 @@ impl CollatorBorrowed<'_> {
763
738
// https://github.com/rust-lang/rust/issues/15701
764
739
#[ allow( clippy:: unwrap_used) ]
765
740
let numeric_primary = if self . options . numeric ( ) {
766
- Some (
767
- self . special_primaries
768
- . as_ref ( )
769
- // `unwrap` is OK, because we've ensured `Some` in the constructor
770
- . unwrap ( )
771
- . numeric_primary ,
772
- )
741
+ Some ( self . special_primaries . numeric_primary )
773
742
} else {
774
743
None
775
744
} ;
@@ -1544,3 +1513,25 @@ impl CollatorBorrowed<'_> {
1544
1513
Ordering :: Equal
1545
1514
}
1546
1515
}
1516
+
1517
+ /// Helper for checking if a byte is compressible
1518
+ pub ( crate ) struct CompressibleBytes < ' a > {
1519
+ arr : & ' a [ u8 ; 32 ] ,
1520
+ }
1521
+
1522
+ impl < ' a > CompressibleBytes < ' a > {
1523
+ pub ( crate ) fn new ( arr : & ' a [ u8 ; 32 ] ) -> Self {
1524
+ Self { arr }
1525
+ }
1526
+
1527
+ #[ allow( dead_code) ]
1528
+ pub ( crate ) fn is_compressible ( & self , b : u8 ) -> bool {
1529
+ // Indexing OK by construction and pasting this
1530
+ // into Compiler Explorer shows that the panic
1531
+ // is optimized away.
1532
+ #[ allow( clippy:: indexing_slicing) ]
1533
+ let field = self . arr [ usize:: from ( b >> 3 ) ] ;
1534
+ let mask = 1 << ( b & 0b111 ) ;
1535
+ ( field & mask) != 0
1536
+ }
1537
+ }
0 commit comments