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 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
use crate::codepointtrie::error::Error;
use crate::codepointtrie::impl_const::*;
use crate::codepointinvlist::CodePointInversionList;
use core::char::CharTryFromError;
use core::convert::Infallible;
use core::convert::TryFrom;
use core::fmt::Display;
use core::iter::FromIterator;
use core::num::TryFromIntError;
use core::ops::RangeInclusive;
use yoke::Yokeable;
use zerofrom::ZeroFrom;
use zerovec::ule::UleError;
use zerovec::ZeroVec;
/// The type of trie represents whether the trie has an optimization that
/// would make it smaller or faster.
///
/// Regarding performance, a trie being a small or fast type affects the number of array lookups
/// needed for code points in the range `[0x1000, 0x10000)`. In this range, `Small` tries use 4 array lookups,
/// while `Fast` tries use 2 array lookups.
/// Code points before the interval (in `[0, 0x1000)`) will always use 2 array lookups.
/// Code points after the interval (in `[0x10000, 0x10FFFF]`) will always use 4 array lookups.
///
/// Regarding size, `Fast` type tries are larger than `Small` type tries because the minimum size of
/// the index array is larger. The minimum size is the "fast max" limit, which is the limit of the range
/// of code points with 2 array lookups.
///
/// See the document [Unicode Properties and Code Point Tries in ICU4X](https://github.com/unicode-org/icu4x/blob/main/documents/design/properties_code_point_trie.md).
///
/// Also see [`UCPTrieType`](https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/ucptrie_8h.html) in ICU4C.
#[derive(Clone, Copy, PartialEq, Debug, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "databake", derive(databake::Bake))]
#[cfg_attr(feature = "databake", databake(path = icu_collections::codepointtrie))]
pub enum TrieType {
/// Represents the "fast" type code point tries for the
/// [`TrieType`] trait. The "fast max" limit is set to `0xffff`.
Fast = 0,
/// Represents the "small" type code point tries for the
/// [`TrieType`] trait. The "fast max" limit is set to `0x0fff`.
Small = 1,
}
// TrieValue trait
// AsULE is AsUnalignedLittleEndian, i.e. "allowed in a zerovec"
/// A trait representing the values stored in the data array of a [`CodePointTrie`].
/// This trait is used as a type parameter in constructing a `CodePointTrie`.
pub trait TrieValue: Copy + Eq + PartialEq + zerovec::ule::AsULE + 'static {
/// Last-resort fallback value to return if we cannot read data from the trie.
///
/// In most cases, the error value is read from the last element of the `data` array,
/// this value is used for empty codepointtrie arrays
/// Error type when converting from a u32 to this `TrieValue`.
type TryFromU32Error: Display;
/// A parsing function that is primarily motivated by deserialization contexts.
/// When the serialization type width is smaller than 32 bits, then it is expected
/// that the call site will widen the value to a `u32` first.
fn try_from_u32(i: u32) -> Result<Self, Self::TryFromU32Error>;
/// A method for converting back to a `u32` that can roundtrip through
/// [`Self::try_from_u32()`]. The default implementation of this trait
/// method panics in debug mode and returns 0 in release mode.
///
/// This method is allowed to have GIGO behavior when fed a value that has
/// no corresponding `u32` (since such values cannot be stored in the trie)
fn to_u32(self) -> u32;
}
macro_rules! impl_primitive_trie_value {
($primitive:ty, $error:ty) => {
impl TrieValue for $primitive {
type TryFromU32Error = $error;
fn try_from_u32(i: u32) -> Result<Self, Self::TryFromU32Error> {
Self::try_from(i)
}
fn to_u32(self) -> u32 {
// bitcast when the same size, zero-extend/sign-extend
// when not the same size
self as u32
}
}
};
}
impl_primitive_trie_value!(u8, TryFromIntError);
impl_primitive_trie_value!(u16, TryFromIntError);
impl_primitive_trie_value!(u32, Infallible);
impl_primitive_trie_value!(i8, TryFromIntError);
impl_primitive_trie_value!(i16, TryFromIntError);
impl_primitive_trie_value!(i32, TryFromIntError);
impl_primitive_trie_value!(char, CharTryFromError);
/// Helper function used by [`get_range`]. Converts occurrences of trie's null
/// value into the provided `null_value`.
///
/// Note: the ICU version of this helper function uses a `ValueFilter` function
/// to apply a transform on a non-null value. But currently, this implementation
/// stops short of that functionality, and instead leaves the non-null trie value
/// untouched. This is equivalent to having a `ValueFilter` function that is the
/// identity function.
fn maybe_filter_value<T: TrieValue>(value: T, trie_null_value: T, null_value: T) -> T {
if value == trie_null_value {
null_value
} else {
value
}
}
/// This struct represents a de-serialized [`CodePointTrie`] that was exported from
/// ICU binary data.
///
/// For more information:
/// - [ICU Site design doc](http://site.icu-project.org/design/struct/utrie)
/// - [ICU User Guide section on Properties lookup](https://unicode-org.github.io/icu/userguide/strings/properties.html#lookup)
// serde impls in crate::serde
#[derive(Debug, Eq, PartialEq, Yokeable, ZeroFrom)]
pub struct CodePointTrie<'trie, T: TrieValue> {
pub(crate) header: CodePointTrieHeader,
pub(crate) index: ZeroVec<'trie, u16>,
pub(crate) data: ZeroVec<'trie, T>,
// serde impl skips this field
#[zerofrom(clone)] // TrieValue is Copy, this allows us to avoid
// a T: ZeroFrom bound
pub(crate) error_value: T,
}
/// This struct contains the fixed-length header fields of a [`CodePointTrie`].
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "databake", derive(databake::Bake))]
#[cfg_attr(feature = "databake", databake(path = icu_collections::codepointtrie))]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Yokeable, ZeroFrom)]
pub struct CodePointTrieHeader {
/// The code point of the start of the last range of the trie. A
/// range is defined as a partition of the code point space such that the
/// value in this trie associated with all code points of the same range is
/// the same.
///
/// For the property value data for many Unicode properties,
/// often times, `high_start` is `U+10000` or lower. In such cases, not
/// reserving space in the `index` array for duplicate values is a large
/// savings. The "highValue" associated with the `high_start` range is
/// stored at the second-to-last position of the `data` array.
/// (See `impl_const::HIGH_VALUE_NEG_DATA_OFFSET`.)
pub high_start: u32,
/// A version of the `high_start` value that is right-shifted 12 spaces,
/// but is rounded up to a multiple `0x1000` for easy testing from UTF-8
/// lead bytes.
pub shifted12_high_start: u16,
/// Offset for the null block in the "index-3" table of the `index` array.
/// Set to an impossibly high value (e.g., `0xffff`) if there is no
/// dedicated index-3 null block.
pub index3_null_offset: u16,
/// Internal data null block offset, not shifted.
/// Set to an impossibly high value (e.g., `0xfffff`) if there is no
/// dedicated data null block.
pub data_null_offset: u32,
/// The value stored in the trie that represents a null value being
/// associated to a code point.
pub null_value: u32,
/// The enum value representing the type of trie, where trie type is as it
/// is defined in ICU (ex: Fast, Small).
pub trie_type: TrieType,
}
impl TryFrom<u8> for TrieType {
type Error = crate::codepointtrie::error::Error;
fn try_from(trie_type_int: u8) -> Result<TrieType, crate::codepointtrie::error::Error> {
match trie_type_int {
0 => Ok(TrieType::Fast),
1 => Ok(TrieType::Small),
_ => Err(crate::codepointtrie::error::Error::FromDeserialized {
reason: "Cannot parse value for trie_type",
}),
}
}
}
impl<'trie, T: TrieValue> CodePointTrie<'trie, T> {
#[doc(hidden)] // databake internal
pub const fn from_parts(
header: CodePointTrieHeader,
index: ZeroVec<'trie, u16>,
data: ZeroVec<'trie, T>,
error_value: T,
) -> Self {
Self {
header,
index,
data,
error_value,
}
}
/// Returns a new [`CodePointTrie`] backed by borrowed data for the `index`
/// array and `data` array, whose data values have width `W`.
pub fn try_new(
header: CodePointTrieHeader,
index: ZeroVec<'trie, u16>,
data: ZeroVec<'trie, T>,
) -> Result<CodePointTrie<'trie, T>, Error> {
// Validation invariants are not needed here when constructing a new
// `CodePointTrie` because:
//
// - Rust includes the size of a slice (or Vec or similar), which allows it
// to prevent lookups at out-of-bounds indices, whereas in C++, it is the
// programmer's responsibility to keep track of length info.
// - For lookups into collections, Rust guarantees that a fallback value will
// be returned in the case of `.get()` encountering a lookup error, via
// the `Option` type.
// - The `ZeroVec` serializer stores the length of the array along with the
// ZeroVec data, meaning that a deserializer would also see that length info.
let error_value = data.last().ok_or(Error::EmptyDataVector)?;
let trie: CodePointTrie<'trie, T> = CodePointTrie {
header,
index,
data,
error_value,
};
Ok(trie)
}
/// Returns the position in the data array containing the trie's stored
/// error value.
#[inline(always)] // `always` based on normalizer benchmarking
fn trie_error_val_index(&self) -> u32 {
self.data.len() as u32 - ERROR_VALUE_NEG_DATA_OFFSET
}
fn internal_small_index(&self, code_point: u32) -> u32 {
let mut index1_pos: u32 = code_point >> SHIFT_1;
if self.header.trie_type == TrieType::Fast {
debug_assert!(
FAST_TYPE_FAST_INDEXING_MAX < code_point && code_point < self.header.high_start
);
index1_pos = index1_pos + BMP_INDEX_LENGTH - OMITTED_BMP_INDEX_1_LENGTH;
} else {
assert!(code_point < self.header.high_start && self.header.high_start > SMALL_LIMIT);
index1_pos += SMALL_INDEX_LENGTH;
}
let index1_val = if let Some(index1_val) = self.index.get(index1_pos as usize) {
index1_val
} else {
return self.trie_error_val_index();
};
let index3_block_idx: u32 = (index1_val as u32) + ((code_point >> SHIFT_2) & INDEX_2_MASK);
let mut index3_block: u32 =
if let Some(index3_block) = self.index.get(index3_block_idx as usize) {
index3_block as u32
} else {
return self.trie_error_val_index();
};
let mut index3_pos: u32 = (code_point >> SHIFT_3) & INDEX_3_MASK;
let mut data_block: u32;
if index3_block & 0x8000 == 0 {
// 16-bit indexes
data_block =
if let Some(data_block) = self.index.get((index3_block + index3_pos) as usize) {
data_block as u32
} else {
return self.trie_error_val_index();
};
} else {
// 18-bit indexes stored in groups of 9 entries per 8 indexes.
index3_block = (index3_block & 0x7fff) + (index3_pos & !7) + (index3_pos >> 3);
index3_pos &= 7;
data_block = if let Some(data_block) = self.index.get(index3_block as usize) {
data_block as u32
} else {
return self.trie_error_val_index();
};
data_block = (data_block << (2 + (2 * index3_pos))) & 0x30000;
index3_block += 1;
data_block =
if let Some(index3_val) = self.index.get((index3_block + index3_pos) as usize) {
data_block | (index3_val as u32)
} else {
return self.trie_error_val_index();
};
}
// Returns data_pos == data_block (offset) +
// portion of code_point bit field for last (4th) lookup
data_block + (code_point & SMALL_DATA_MASK)
}
/// Returns the position in the `data` array for the given code point,
/// where this code point is at or above the fast limit associated for the
/// `trie_type`. We will refer to that limit as "`fastMax`" here.
///
/// A lookup of the value in the code point trie for a code point in the
/// code point space range [`fastMax`, `high_start`) will be a 4-step
/// lookup: 3 lookups in the `index` array and one lookup in the `data`
/// array. Lookups for code points in the range [`high_start`,
/// `CODE_POINT_MAX`] are short-circuited to be a single lookup, see
/// [`CodePointTrieHeader::high_start`].
fn small_index(&self, code_point: u32) -> u32 {
if code_point >= self.header.high_start {
self.data.len() as u32 - HIGH_VALUE_NEG_DATA_OFFSET
} else {
self.internal_small_index(code_point) // helper fn
}
}
/// Returns the position in the `data` array for the given code point,
/// where this code point is below the fast limit associated for the
/// `trie type`. We will refer to that limit as "`fastMax`" here.
///
/// A lookup of the value in the code point trie for a code point in the
/// code point space range [0, `fastMax`) will be a 2-step lookup: 1
/// lookup in the `index` array and one lookup in the `data` array. By
/// design, for trie type `T`, there is an element allocated in the `index`
/// array for each block of code points in [0, `fastMax`), which in
/// turn guarantees that those code points are represented and only need 1
/// lookup.
#[inline(always)] // `always` based on normalizer benchmarking
fn fast_index(&self, code_point: u32) -> u32 {
let index_array_pos: u32 = code_point >> FAST_TYPE_SHIFT;
let index_array_val: u16 =
if let Some(index_array_val) = self.index.get(index_array_pos as usize) {
index_array_val
} else {
return self.trie_error_val_index();
};
let fast_index_val: u32 = index_array_val as u32 + (code_point & FAST_TYPE_DATA_MASK);
fast_index_val
}
/// Returns the value that is associated with `code_point` in this [`CodePointTrie`].
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
/// let trie = planes::get_planes_trie();
///
/// assert_eq!(0, trie.get32(0x41)); // 'A' as u32
/// assert_eq!(0, trie.get32(0x13E0)); // 'Ꮰ' as u32
/// assert_eq!(1, trie.get32(0x10044)); // '𐁄' as u32
/// ```
#[inline(always)] // `always` based on normalizer benchmarking
pub fn get32(&self, code_point: u32) -> T {
// If we cannot read from the data array, then return the sentinel value
// self.error_value() for the instance type for T: TrieValue.
self.get32_ule(code_point)
.map(|t| T::from_unaligned(*t))
.unwrap_or(self.error_value)
}
/// Returns the value that is associated with `char` in this [`CodePointTrie`].
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
/// let trie = planes::get_planes_trie();
///
/// assert_eq!(0, trie.get('A')); // 'A' as u32
/// assert_eq!(0, trie.get('Ꮰ')); // 'Ꮰ' as u32
/// assert_eq!(1, trie.get('𐁄')); // '𐁄' as u32
/// ```
#[inline(always)]
pub fn get(&self, c: char) -> T {
self.get32(u32::from(c))
}
/// Returns a reference to the ULE of the value that is associated with `code_point` in this [`CodePointTrie`].
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
/// let trie = planes::get_planes_trie();
///
/// assert_eq!(Some(&0), trie.get32_ule(0x41)); // 'A' as u32
/// assert_eq!(Some(&0), trie.get32_ule(0x13E0)); // 'Ꮰ' as u32
/// assert_eq!(Some(&1), trie.get32_ule(0x10044)); // '𐁄' as u32
/// ```
#[inline(always)] // `always` based on normalizer benchmarking
pub fn get32_ule(&self, code_point: u32) -> Option<&T::ULE> {
// All code points up to the fast max limit are represented
// individually in the `index` array to hold their `data` array position, and
// thus only need 2 lookups for a [CodePointTrie::get()](`crate::codepointtrie::CodePointTrie::get`).
// Code points above the "fast max" limit require 4 lookups.
let fast_max = match self.header.trie_type {
TrieType::Fast => FAST_TYPE_FAST_INDEXING_MAX,
TrieType::Small => SMALL_TYPE_FAST_INDEXING_MAX,
};
let data_pos: u32 = if code_point <= fast_max {
Self::fast_index(self, code_point)
} else if code_point <= CODE_POINT_MAX {
Self::small_index(self, code_point)
} else {
self.trie_error_val_index()
};
// Returns the trie value (or trie's error value).
self.data.as_ule_slice().get(data_pos as usize)
}
/// Converts the [`CodePointTrie`] into one that returns another type of the same size.
///
/// Borrowed data remains borrowed, and owned data remains owned.
///
/// If the old and new types are not the same size, use
/// [`CodePointTrie::try_alloc_map_value`].
///
/// # Panics
///
/// Panics if `T` and `P` are different sizes.
///
/// More specifically, panics if [`ZeroVec::try_into_converted()`] panics when converting
/// `ZeroVec<T>` into `ZeroVec<P>`, which happens if `T::ULE` and `P::ULE` differ in size.
///
/// # Examples
///
/// ```no_run
/// use icu::collections::codepointtrie::planes;
/// use icu::collections::codepointtrie::CodePointTrie;
///
/// let planes_trie_u8: CodePointTrie<u8> = planes::get_planes_trie();
/// let planes_trie_i8: CodePointTrie<i8> =
/// planes_trie_u8.try_into_converted().expect("infallible");
///
/// assert_eq!(planes_trie_i8.get32(0x30000), 3);
/// ```
pub fn try_into_converted<P>(self) -> Result<CodePointTrie<'trie, P>, UleError>
where
P: TrieValue,
{
let converted_data = self.data.try_into_converted()?;
let error_ule = self.error_value.to_unaligned();
let slice = &[error_ule];
let error_vec = ZeroVec::<T>::new_borrowed(slice);
let error_converted = error_vec.try_into_converted::<P>()?;
#[allow(clippy::expect_used)] // we know this cannot fail
Ok(CodePointTrie {
header: self.header,
index: self.index,
data: converted_data,
error_value: error_converted
.get(0)
.expect("vector known to have one element"),
})
}
/// Maps the [`CodePointTrie`] into one that returns a different type.
///
/// This function returns owned data.
///
/// If the old and new types are the same size, use the more efficient
/// [`CodePointTrie::try_into_converted`].
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
/// use icu::collections::codepointtrie::CodePointTrie;
///
/// let planes_trie_u8: CodePointTrie<u8> = planes::get_planes_trie();
/// let planes_trie_u16: CodePointTrie<u16> = planes_trie_u8
/// .try_alloc_map_value(TryFrom::try_from)
/// .expect("infallible");
///
/// assert_eq!(planes_trie_u16.get32(0x30000), 3);
/// ```
pub fn try_alloc_map_value<P, E>(
&self,
mut f: impl FnMut(T) -> Result<P, E>,
) -> Result<CodePointTrie<'trie, P>, E>
where
P: TrieValue,
{
let error_converted = f(self.error_value)?;
let converted_data = self.data.iter().map(f).collect::<Result<ZeroVec<P>, E>>()?;
Ok(CodePointTrie {
header: self.header,
index: self.index.clone(),
data: converted_data,
error_value: error_converted,
})
}
/// Returns a [`CodePointMapRange`] struct which represents a range of code
/// points associated with the same trie value. The returned range will be
/// the longest stretch of consecutive code points starting at `start` that
/// share this value.
///
/// This method is designed to use the internal details of
/// the structure of [`CodePointTrie`] to be optimally efficient. This will
/// outperform a naive approach that just uses [`CodePointTrie::get()`].
///
/// This method provides lower-level functionality that can be used in the
/// implementation of other methods that are more convenient to the user.
/// To obtain an optimal partition of the code point space for
/// this trie resulting in the fewest number of ranges, see
/// [`CodePointTrie::iter_ranges()`].
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
///
/// let trie = planes::get_planes_trie();
///
/// const CODE_POINT_MAX: u32 = 0x10ffff;
/// let start = 0x1_0000;
/// let exp_end = 0x1_ffff;
///
/// let start_val = trie.get32(start);
/// assert_eq!(trie.get32(exp_end), start_val);
/// assert_ne!(trie.get32(exp_end + 1), start_val);
///
/// use icu::collections::codepointtrie::CodePointMapRange;
///
/// let cpm_range: CodePointMapRange<u8> = trie.get_range(start).unwrap();
///
/// assert_eq!(cpm_range.range.start(), &start);
/// assert_eq!(cpm_range.range.end(), &exp_end);
/// assert_eq!(cpm_range.value, start_val);
///
/// // `start` can be any code point, whether or not it lies on the boundary
/// // of a maximally large range that still contains `start`
///
/// let submaximal_1_start = start + 0x1234;
/// let submaximal_1 = trie.get_range(submaximal_1_start).unwrap();
/// assert_eq!(submaximal_1.range.start(), &0x1_1234);
/// assert_eq!(submaximal_1.range.end(), &0x1_ffff);
/// assert_eq!(submaximal_1.value, start_val);
///
/// let submaximal_2_start = start + 0xffff;
/// let submaximal_2 = trie.get_range(submaximal_2_start).unwrap();
/// assert_eq!(submaximal_2.range.start(), &0x1_ffff);
/// assert_eq!(submaximal_2.range.end(), &0x1_ffff);
/// assert_eq!(submaximal_2.value, start_val);
/// ```
pub fn get_range(&self, start: u32) -> Option<CodePointMapRange<T>> {
// Exit early if the start code point is out of range, or if it is
// in the last range of code points in high_start..=CODE_POINT_MAX
// (start- and end-inclusive) that all share the same trie value.
if CODE_POINT_MAX < start {
return None;
}
if start >= self.header.high_start {
let di: usize = self.data.len() - (HIGH_VALUE_NEG_DATA_OFFSET as usize);
let value: T = self.data.get(di)?;
return Some(CodePointMapRange {
range: start..=CODE_POINT_MAX,
value,
});
}
let null_value: T = T::try_from_u32(self.header.null_value).ok()?;
let mut prev_i3_block: u32 = u32::MAX; // using u32::MAX (instead of -1 as an i32 in ICU)
let mut prev_block: u32 = u32::MAX; // using u32::MAX (instead of -1 as an i32 in ICU)
let mut c: u32 = start;
let mut trie_value: T = self.error_value();
let mut value: T = self.error_value();
let mut have_value: bool = false;
loop {
let i3_block: u32;
let mut i3: u32;
let i3_block_length: u32;
let data_block_length: u32;
// Initialize values before beginning the iteration in the subsequent
// `loop` block. In particular, use the "i3*" local variables
// (representing the `index` array position's offset + increment
// for a 3rd-level trie lookup) to help initialize the data block
// variable `block` in the loop for the `data` array.
//
// When a lookup code point is <= the trie's *_FAST_INDEXING_MAX that
// corresponds to its `trie_type`, the lookup only takes 2 steps
// (once into the `index`, once into the `data` array); otherwise,
// takes 4 steps (3 iterative lookups into the `index`, once more
// into the `data` array). So for convenience's sake, when we have the
// 2-stage lookup, reuse the "i3*" variable names for the first lookup.
if c <= 0xffff
&& (self.header.trie_type == TrieType::Fast || c <= SMALL_TYPE_FAST_INDEXING_MAX)
{
i3_block = 0;
i3 = c >> FAST_TYPE_SHIFT;
i3_block_length = if self.header.trie_type == TrieType::Fast {
BMP_INDEX_LENGTH
} else {
SMALL_INDEX_LENGTH
};
data_block_length = FAST_TYPE_DATA_BLOCK_LENGTH;
} else {
// Use the multi-stage index.
let mut i1: u32 = c >> SHIFT_1;
if self.header.trie_type == TrieType::Fast {
debug_assert!(0xffff < c && c < self.header.high_start);
i1 = i1 + BMP_INDEX_LENGTH - OMITTED_BMP_INDEX_1_LENGTH;
} else {
debug_assert!(
c < self.header.high_start && self.header.high_start > SMALL_LIMIT
);
i1 += SMALL_INDEX_LENGTH;
}
let i2: u16 = self.index.get(i1 as usize)?;
let i3_block_idx: u32 = (i2 as u32) + ((c >> SHIFT_2) & INDEX_2_MASK);
i3_block = if let Some(i3b) = self.index.get(i3_block_idx as usize) {
i3b as u32
} else {
return None;
};
if i3_block == prev_i3_block && (c - start) >= CP_PER_INDEX_2_ENTRY {
// The index-3 block is the same as the previous one, and filled with value.
debug_assert!((c & (CP_PER_INDEX_2_ENTRY - 1)) == 0);
c += CP_PER_INDEX_2_ENTRY;
if c >= self.header.high_start {
break;
} else {
continue;
}
}
prev_i3_block = i3_block;
if i3_block == self.header.index3_null_offset as u32 {
// This is the index-3 null block.
// All of the `data` array blocks pointed to by the values
// in this block of the `index` 3rd-stage subarray will
// contain this trie's null_value. So if we are in the middle
// of a range, end it and return early, otherwise start a new
// range of null values.
if have_value {
if null_value != value {
return Some(CodePointMapRange {
range: start..=(c - 1),
value,
});
}
} else {
trie_value = T::try_from_u32(self.header.null_value).ok()?;
value = null_value;
have_value = true;
}
prev_block = self.header.data_null_offset;
c = (c + CP_PER_INDEX_2_ENTRY) & !(CP_PER_INDEX_2_ENTRY - 1);
if c >= self.header.high_start {
break;
} else {
continue;
}
}
i3 = (c >> SHIFT_3) & INDEX_3_MASK;
i3_block_length = INDEX_3_BLOCK_LENGTH;
data_block_length = SMALL_DATA_BLOCK_LENGTH;
}
// Enumerate data blocks for one index-3 block.
loop {
let mut block: u32;
if (i3_block & 0x8000) == 0 {
block = if let Some(b) = self.index.get((i3_block + i3) as usize) {
b as u32
} else {
return None;
};
} else {
// 18-bit indexes stored in groups of 9 entries per 8 indexes.
let mut group: u32 = (i3_block & 0x7fff) + (i3 & !7) + (i3 >> 3);
let gi: u32 = i3 & 7;
let gi_val: u32 = if let Some(giv) = self.index.get(group as usize) {
giv.into()
} else {
return None;
};
block = (gi_val << (2 + (2 * gi))) & 0x30000;
group += 1;
let ggi_val: u32 = if let Some(ggiv) = self.index.get((group + gi) as usize) {
ggiv as u32
} else {
return None;
};
block |= ggi_val;
}
// If our previous and current return values of the 3rd-stage `index`
// lookup yield the same `data` block offset, and if we already know that
// the entire `data` block / subarray starting at that offset stores
// `value` and nothing else, then we can extend our range by the length
// of a data block and continue.
// Otherwise, we have to iterate over the values stored in the
// new data block to see if they differ from `value`.
if block == prev_block && (c - start) >= data_block_length {
// The block is the same as the previous one, and filled with value.
debug_assert!((c & (data_block_length - 1)) == 0);
c += data_block_length;
} else {
let data_mask: u32 = data_block_length - 1;
prev_block = block;
if block == self.header.data_null_offset {
// This is the data null block.
// If we are in the middle of a range, end it and
// return early, otherwise start a new range of null
// values.
if have_value {
if null_value != value {
return Some(CodePointMapRange {
range: start..=(c - 1),
value,
});
}
} else {
trie_value = T::try_from_u32(self.header.null_value).ok()?;
value = null_value;
have_value = true;
}
c = (c + data_block_length) & !data_mask;
} else {
let mut di: u32 = block + (c & data_mask);
let mut trie_value_2: T = self.data.get(di as usize)?;
if have_value {
if trie_value_2 != trie_value {
if maybe_filter_value(
trie_value_2,
T::try_from_u32(self.header.null_value).ok()?,
null_value,
) != value
{
return Some(CodePointMapRange {
range: start..=(c - 1),
value,
});
}
// `trie_value` stores the previous value that was retrieved
// from the trie.
// `value` stores the value associated for the range (return
// value) that we are currently building, which is computed
// as a transformation by applying maybe_filter_value()
// to the trie value.
// The current trie value `trie_value_2` within this data block
// differs here from the previous value in `trie_value`.
// But both map to `value` after applying `maybe_filter_value`.
// It is not clear whether the previous or the current trie value
// (or neither) is more likely to match potential subsequent trie
// values that would extend the range by mapping to `value`.
// On the assumption of locality -- often times consecutive
// characters map to the same trie values -- remembering the new
// one might make it faster to extend this range further
// (by increasing the chance that the next `trie_value_2 !=
// trie_value` test will be false).
trie_value = trie_value_2; // may or may not help
}
} else {
trie_value = trie_value_2;
value = maybe_filter_value(
trie_value_2,
T::try_from_u32(self.header.null_value).ok()?,
null_value,
);
have_value = true;
}
c += 1;
while (c & data_mask) != 0 {
di += 1;
trie_value_2 = self.data.get(di as usize)?;
if trie_value_2 != trie_value {
if maybe_filter_value(
trie_value_2,
T::try_from_u32(self.header.null_value).ok()?,
null_value,
) != value
{
return Some(CodePointMapRange {
range: start..=(c - 1),
value,
});
}
// `trie_value` stores the previous value that was retrieved
// from the trie.
// `value` stores the value associated for the range (return
// value) that we are currently building, which is computed
// as a transformation by applying maybe_filter_value()
// to the trie value.
// The current trie value `trie_value_2` within this data block
// differs here from the previous value in `trie_value`.
// But both map to `value` after applying `maybe_filter_value`.
// It is not clear whether the previous or the current trie value
// (or neither) is more likely to match potential subsequent trie
// values that would extend the range by mapping to `value`.
// On the assumption of locality -- often times consecutive
// characters map to the same trie values -- remembering the new
// one might make it faster to extend this range further
// (by increasing the chance that the next `trie_value_2 !=
// trie_value` test will be false).
trie_value = trie_value_2; // may or may not help
}
c += 1;
}
}
}
i3 += 1;
if i3 >= i3_block_length {
break;
}
}
if c >= self.header.high_start {
break;
}
}
debug_assert!(have_value);
// Now that c >= high_start, compare `value` to `high_value` to see
// if we can merge our current range with the high_value range
// high_start..=CODE_POINT_MAX (start- and end-inclusive), otherwise
// stop at high_start - 1.
let di: u32 = self.data.len() as u32 - HIGH_VALUE_NEG_DATA_OFFSET;
let high_value: T = self.data.get(di as usize)?;
if maybe_filter_value(
high_value,
T::try_from_u32(self.header.null_value).ok()?,
null_value,
) != value
{
c -= 1;
} else {
c = CODE_POINT_MAX;
}
Some(CodePointMapRange {
range: start..=c,
value,
})
}
/// Yields an [`Iterator`] returning ranges of consecutive code points that
/// share the same value in the [`CodePointTrie`], as given by
/// [`CodePointTrie::get_range()`].
///
/// # Examples
///
/// ```
/// use core::ops::RangeInclusive;
/// use icu::collections::codepointtrie::planes;
/// use icu::collections::codepointtrie::CodePointMapRange;
///
/// let planes_trie = planes::get_planes_trie();
///
/// let mut ranges = planes_trie.iter_ranges();
///
/// for plane in 0..=16 {
/// let exp_start = plane * 0x1_0000;
/// let exp_end = exp_start + 0xffff;
/// assert_eq!(
/// ranges.next(),
/// Some(CodePointMapRange {
/// range: exp_start..=exp_end,
/// value: plane as u8
/// })
/// );
/// }
///
/// // Hitting the end of the iterator returns `None`, as will subsequent
/// // calls to .next().
/// assert_eq!(ranges.next(), None);
/// assert_eq!(ranges.next(), None);
/// ```
pub fn iter_ranges(&self) -> CodePointMapRangeIterator<T> {
let init_range = Some(CodePointMapRange {
range: u32::MAX..=u32::MAX,
value: self.error_value(),
});
CodePointMapRangeIterator::<T> {
cpt: self,
cpm_range: init_range,
}
}
/// Yields an [`Iterator`] returning the ranges of the code points whose values
/// match `value` in the [`CodePointTrie`].
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
///
/// let trie = planes::get_planes_trie();
///
/// let plane_val = 2;
/// let mut sip_range_iter = trie.iter_ranges_for_value(plane_val as u8);
///
/// let start = plane_val * 0x1_0000;
/// let end = start + 0xffff;
///
/// let sip_range = sip_range_iter.next()
/// .expect("Plane 2 (SIP) should exist in planes data");
/// assert_eq!(start..=end, sip_range);
///
/// assert!(sip_range_iter.next().is_none());
pub fn iter_ranges_for_value(
&self,
value: T,
) -> impl Iterator<Item = RangeInclusive<u32>> + '_ {
self.iter_ranges()
.filter(move |cpm_range| cpm_range.value == value)
.map(|cpm_range| cpm_range.range)
}
/// Yields an [`Iterator`] returning the ranges of the code points after passing
/// the value through a mapping function.
///
/// This is preferable to calling `.get_ranges().map()` since it will coalesce
/// adjacent ranges into one.
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
///
/// let trie = planes::get_planes_trie();
///
/// let plane_val = 2;
/// let mut sip_range_iter = trie.iter_ranges_mapped(|value| value != plane_val as u8).filter(|range| range.value);
///
/// let end = plane_val * 0x1_0000 - 1;
///
/// let sip_range = sip_range_iter.next()
/// .expect("Complemented planes data should have at least one entry");
/// assert_eq!(0..=end, sip_range.range);
pub fn iter_ranges_mapped<'a, U: Eq + 'a>(
&'a self,
mut map: impl FnMut(T) -> U + Copy + 'a,
) -> impl Iterator<Item = CodePointMapRange<U>> + 'a {
crate::iterator_utils::RangeListIteratorCoalescer::new(self.iter_ranges().map(
move |range| CodePointMapRange {
range: range.range,
value: map(range.value),
},
))
}
/// Returns a [`CodePointInversionList`] for the code points that have the given
/// [`TrieValue`] in the trie.
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
///
/// let trie = planes::get_planes_trie();
///
/// let plane_val = 2;
/// let sip = trie.get_set_for_value(plane_val as u8);
///
/// let start = plane_val * 0x1_0000;
/// let end = start + 0xffff;
///
/// assert!(!sip.contains32(start - 1));
/// assert!(sip.contains32(start));
/// assert!(sip.contains32(end));
/// assert!(!sip.contains32(end + 1));
/// ```
pub fn get_set_for_value(&self, value: T) -> CodePointInversionList<'static> {
let value_ranges = self.iter_ranges_for_value(value);
CodePointInversionList::from_iter(value_ranges)
}
/// Returns the value used as an error value for this trie
#[inline]
pub fn error_value(&self) -> T {
self.error_value
}
}
#[cfg(feature = "databake")]
impl<T: TrieValue + databake::Bake> databake::Bake for CodePointTrie<'_, T> {
fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream {
let header = self.header.bake(env);
let index = self.index.bake(env);
let data = self.data.bake(env);
let error_value = self.error_value.bake(env);
databake::quote! { icu_collections::codepointtrie::CodePointTrie::from_parts(#header, #index, #data, #error_value) }
}
}
#[cfg(feature = "databake")]
impl<T: TrieValue + databake::Bake> databake::BakeSize for CodePointTrie<'_, T> {
fn borrows_size(&self) -> usize {
self.header.borrows_size() + self.index.borrows_size() + self.data.borrows_size()
}
}
impl<T: TrieValue + Into<u32>> CodePointTrie<'_, T> {
/// Returns the value that is associated with `code_point` for this [`CodePointTrie`]
/// as a `u32`.
///
/// # Examples
///
/// ```
/// use icu::collections::codepointtrie::planes;
/// let trie = planes::get_planes_trie();
///
/// let cp = '𑖎' as u32;
/// assert_eq!(cp, 0x1158E);
///
/// let plane_num: u8 = trie.get32(cp);
/// assert_eq!(trie.get32_u32(cp), plane_num as u32);
/// ```
// Note: This API method maintains consistency with the corresponding
// original ICU APIs.
pub fn get32_u32(&self, code_point: u32) -> u32 {
self.get32(code_point).into()
}
}
impl<T: TrieValue> Clone for CodePointTrie<'_, T>
where
<T as zerovec::ule::AsULE>::ULE: Clone,
{
fn clone(&self) -> Self {
CodePointTrie {
header: self.header,
index: self.index.clone(),
data: self.data.clone(),
error_value: self.error_value,
}
}
}
/// Represents a range of consecutive code points sharing the same value in a
/// code point map.
///
/// The start and end of the interval is represented as a
/// `RangeInclusive<u32>`, and the value is represented as `T`.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct CodePointMapRange<T> {
/// Range of code points from start to end (inclusive).
pub range: RangeInclusive<u32>,
/// Trie value associated with this range.
pub value: T,
}
/// A custom [`Iterator`] type specifically for a code point trie that returns
/// [`CodePointMapRange`]s.
pub struct CodePointMapRangeIterator<'a, T: TrieValue> {
cpt: &'a CodePointTrie<'a, T>,
// Initialize `range` to Some(CodePointMapRange{ start: u32::MAX, end: u32::MAX, value: 0}).
// When `range` is Some(...) and has a start value different from u32::MAX, then we have
// returned at least one code point range due to a call to `next()`.
// When `range` == `None`, it means that we have hit the end of iteration. It would occur
// after a call to `next()` returns a None <=> we attempted to call `get_range()`
// with a start code point that is > CODE_POINT_MAX.
cpm_range: Option<CodePointMapRange<T>>,
}
impl<T: TrieValue> Iterator for CodePointMapRangeIterator<'_, T> {
type Item = CodePointMapRange<T>;
fn next(&mut self) -> Option<Self::Item> {
self.cpm_range = match &self.cpm_range {
Some(cpmr) => {
if *cpmr.range.start() == u32::MAX {
self.cpt.get_range(0)
} else {
self.cpt.get_range(cpmr.range.end() + 1)
}
}
None => None,
};
// Note: Clone is cheap. We can't Copy because RangeInclusive does not impl Copy.
self.cpm_range.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codepointtrie::planes;
use alloc::vec::Vec;
#[test]
#[cfg(feature = "serde")]
fn test_serde_with_postcard_roundtrip() -> Result<(), postcard::Error> {
let trie = crate::codepointtrie::planes::get_planes_trie();
let trie_serialized: Vec<u8> = postcard::to_allocvec(&trie).unwrap();
// Assert an expected (golden data) version of the serialized trie.
const EXP_TRIE_SERIALIZED: &[u8] = &[
128, 128, 64, 128, 2, 2, 0, 0, 1, 160, 18, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136,
2, 144, 2, 144, 2, 144, 2, 176, 2, 176, 2, 176, 2, 176, 2, 208, 2, 208, 2, 208, 2, 208,
2, 240, 2, 240, 2, 240, 2, 240, 2, 16, 3, 16, 3, 16, 3, 16, 3, 48, 3, 48, 3, 48, 3, 48,
3, 80, 3, 80, 3, 80, 3, 80, 3, 112, 3, 112, 3, 112, 3, 112, 3, 144, 3, 144, 3, 144, 3,
144, 3, 176, 3, 176, 3, 176, 3, 176, 3, 208, 3, 208, 3, 208, 3, 208, 3, 240, 3, 240, 3,
240, 3, 240, 3, 16, 4, 16, 4, 16, 4, 16, 4, 48, 4, 48, 4, 48, 4, 48, 4, 80, 4, 80, 4,
80, 4, 80, 4, 112, 4, 112, 4, 112, 4, 112, 4, 0, 0, 16, 0, 32, 0, 48, 0, 64, 0, 80, 0,
96, 0, 112, 0, 0, 0, 16, 0, 32, 0, 48, 0, 0, 0, 16, 0, 32, 0, 48, 0, 0, 0, 16, 0, 32,
0, 48, 0, 0, 0, 16, 0, 32, 0, 48, 0, 0, 0, 16, 0, 32, 0, 48, 0, 0, 0, 16, 0, 32, 0, 48,
0, 0, 0, 16, 0, 32, 0, 48, 0, 0, 0, 16, 0, 32, 0, 48, 0, 128, 0, 128, 0, 128, 0, 128,
0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128,
0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128,
0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144,
0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144,
0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 144,
0, 144, 0, 144, 0, 144, 0, 144, 0, 144, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160,
0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160,
0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160, 0, 160,
0, 160, 0, 160, 0, 160, 0, 160, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176,
0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176,
0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176, 0, 176,
0, 176, 0, 176, 0, 176, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192,
0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192,
0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192, 0, 192,
0, 192, 0, 192, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208,
0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208,
0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208, 0, 208,
0, 208, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224,
0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224,
0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224, 0, 224,
0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240,
0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240,
0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 240, 0, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1,
16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16,
1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 32, 1, 32, 1, 32, 1,
32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32,
1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1, 32, 1,
32, 1, 32, 1, 32, 1, 32, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48,
1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1,
48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 48, 1, 64, 1, 64,
1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1,
64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64,
1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1,
80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80,
1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1,
96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96,
1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1,
96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 96, 1, 128, 0, 136, 0, 136, 0, 136, 0, 136,
0, 136, 0, 136, 0, 136, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0,
2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2,
0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0,
168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0,
168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 168, 0,
168, 0, 168, 0, 168, 0, 168, 0, 168, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0,
200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0,
200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0, 200, 0,
200, 0, 200, 0, 200, 0, 200, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0,
232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0,
232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0, 232, 0,
232, 0, 232, 0, 232, 0, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8,
1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1,
8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40,
1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1,
40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40, 1, 40,
1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1,
72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72,
1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 72, 1, 104, 1, 104, 1, 104, 1, 104, 1,
104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1,
104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1,
104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 104, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1,
136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1,
136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 136, 1,
136, 1, 136, 1, 136, 1, 136, 1, 136, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1,
168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1,
168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1, 168, 1,
168, 1, 168, 1, 168, 1, 168, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1,
200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1,
200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1, 200, 1,
200, 1, 200, 1, 200, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1,
232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1,
232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1, 232, 1,
232, 1, 232, 1, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2,
8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8,
2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40,
2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2,
40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 40, 2, 72,
2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2,
72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72,
2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 72, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2,
104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2,
104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 104, 2,
104, 2, 104, 2, 104, 2, 104, 2, 104, 2, 244, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 0,
];
assert_eq!(trie_serialized, EXP_TRIE_SERIALIZED);
let trie_deserialized = postcard::from_bytes::<CodePointTrie<u8>>(&trie_serialized)?;
assert_eq!(&trie.index, &trie_deserialized.index);
assert_eq!(&trie.data, &trie_deserialized.data);
assert!(!trie_deserialized.index.is_owned());
assert!(!trie_deserialized.data.is_owned());
Ok(())
}
#[test]
fn test_get_range() {
let planes_trie = planes::get_planes_trie();
let first_range: Option<CodePointMapRange<u8>> = planes_trie.get_range(0x0);
assert_eq!(
first_range,
Some(CodePointMapRange {
range: 0x0..=0xffff,
value: 0
})
);
let second_range: Option<CodePointMapRange<u8>> = planes_trie.get_range(0x1_0000);
assert_eq!(
second_range,
Some(CodePointMapRange {
range: 0x10000..=0x1ffff,
value: 1
})
);
let penultimate_range: Option<CodePointMapRange<u8>> = planes_trie.get_range(0xf_0000);
assert_eq!(
penultimate_range,
Some(CodePointMapRange {
range: 0xf_0000..=0xf_ffff,
value: 15
})
);
let last_range: Option<CodePointMapRange<u8>> = planes_trie.get_range(0x10_0000);
assert_eq!(
last_range,
Some(CodePointMapRange {
range: 0x10_0000..=0x10_ffff,
value: 16
})
);
}
#[test]
fn databake() {
databake::test_bake!(
CodePointTrie<'static, u32>,
const,
crate::codepointtrie::CodePointTrie::from_parts(
crate::codepointtrie::CodePointTrieHeader {
high_start: 1u32,
shifted12_high_start: 2u16,
index3_null_offset: 3u16,
data_null_offset: 4u32,
null_value: 5u32,
trie_type: crate::codepointtrie::TrieType::Small,
},
zerovec::ZeroVec::new(),
zerovec::ZeroVec::new(),
0u32,
),
icu_collections,
[zerovec],
);
}
}