thymeleaf 0.1.0-beta.1

A framework-neutral Thymeleaf-compatible dynamic template engine for Rust
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
use std::any::TypeId;
use std::collections::HashMap;
use std::io::Read;
use std::sync::{OnceLock, RwLock};

use num_bigint::BigInt;
use num_traits::Signed;

use crate::expression::TemplateValue;
use crate::templateresource::ITemplateResource;
use crate::util::{
    BigDecimalValue, Locale, NumberPointType, NumberUtils, NumberValue, Utf16String,
};
use crate::{TemplateInputException, TemplateProcessingException};

use super::MessageResolutionResult;

type Messages = HashMap<Utf16String, Utf16String>;
type OriginMessages = HashMap<(TypeId, Locale), Messages>;
type OriginParents = HashMap<TypeId, TypeId>;

static ORIGIN_MESSAGES: OnceLock<RwLock<OriginMessages>> = OnceLock::new();
static ORIGIN_PARENTS: OnceLock<RwLock<OriginParents>> = OnceLock::new();

/// 标准消息资源定位、合并与格式化工具。
///
/// 对应 Java: `org.thymeleaf.messageresolver.StandardMessageResolutionUtils`。
pub(crate) struct StandardMessageResolutionUtils;

impl StandardMessageResolutionUtils {
    /// 按基础资源、语言、国家和变体由低到高合并模板消息。
    /// 对应 Java: `StandardMessageResolutionUtils#resolveMessagesForTemplate()`。
    pub(crate) fn resolve_messages_for_template(
        template_resource: &dyn ITemplateResource,
        locale: &Locale,
    ) -> MessageResolutionResult<Messages> {
        let Some(resource_base_name) = template_resource
            .get_base_name()
            .filter(|base_name| !base_name.is_empty())
        else {
            return Ok(HashMap::new());
        };

        let mut combined_messages = HashMap::new();
        for message_resource_name in
            Self::compute_message_resource_names_from_base(&resource_base_name, locale)?
        {
            let message_resource = template_resource
                .relative(Some(&message_resource_name))
                .map_err(|error| Box::new(error) as super::MessageResolutionError)?;
            let Ok(reader) = message_resource.reader() else {
                // Java 版本只忽略派生消息文件不存在或打开失败时产生的 IOException。
                continue;
            };
            combined_messages.extend(Self::read_messages_resource(reader)?);
        }
        Ok(combined_messages)
    }

    /// 返回宿主为 Rust 类型注册的 classpath 等价消息。
    /// 对应 Java: `StandardMessageResolutionUtils#resolveMessagesForOrigin()`。
    pub(crate) fn resolve_messages_for_origin(origin: TypeId, locale: &Locale) -> Messages {
        let messages = read_lock(origin_messages());
        let parents = read_lock(origin_parents());
        let mut combined = Messages::new();
        let mut current = Some(origin);
        let mut visited = std::collections::HashSet::new();

        // Java 从具体类向父类查找,并保留最具体类已经提供的值。
        while let Some(origin_type) = current {
            if !visited.insert(origin_type) {
                break;
            }
            if let Some(current_messages) = messages.get(&(origin_type, locale.clone())) {
                for (key, value) in current_messages {
                    combined.entry(key.clone()).or_insert_with(|| value.clone());
                }
            }
            current = parents.get(&origin_type).copied();
        }
        combined
    }

    /// 注册 Rust 类型对应的 origin 消息资源。
    ///
    /// Rust 没有 JVM `ClassLoader#getResourceAsStream`;宿主集成层在加载等价
    /// classpath 资源后通过此入口登记,解析器仍按 origin 与 Locale 缓存。
    /// 对应 Java 语义:`StandardMessageResolutionUtils` 的 `register_origin_messages` 行为(Rust 侧辅助/私有路径)。
    pub(crate) fn register_origin_messages(origin: TypeId, locale: Locale, messages: Messages) {
        write_lock(origin_messages()).insert((origin, locale), messages);
    }

    /// 登记 Rust origin 类型的直接父类型,复现 Java superclass 消息回退。
    /// 对应 Java 语义:`StandardMessageResolutionUtils` 的 `register_origin_parent` 行为(Rust 侧辅助/私有路径)。
    pub(crate) fn register_origin_parent(
        origin: TypeId,
        parent: TypeId,
    ) -> MessageResolutionResult<()> {
        let mut parents = write_lock(origin_parents());
        if let Some(existing) = parents.get(&origin) {
            if *existing == parent {
                return Ok(());
            }
            return Err(Box::new(OriginRegistrationError::ConflictingParent));
        }
        let mut current = Some(parent);
        let mut visited = std::collections::HashSet::new();
        while let Some(candidate) = current {
            if candidate == origin {
                return Err(Box::new(OriginRegistrationError::Cycle));
            }
            if !visited.insert(candidate) {
                return Err(Box::new(OriginRegistrationError::Cycle));
            }
            current = parents.get(&candidate).copied();
        }
        parents.insert(origin, parent);
        Ok(())
    }

    /// 使用 Java `MessageFormat` 的索引占位符和引号规则格式化消息。
    /// 对应 Java: `StandardMessageResolutionUtils#formatMessage()`。
    pub(crate) fn format_message(
        locale: &Locale,
        message: &Utf16String,
        message_parameters: Option<&[Option<std::sync::Arc<TemplateValue>>]>,
    ) -> MessageResolutionResult<Utf16String> {
        let units = message.as_utf16();
        if !units.contains(&u16::from(b'}')) && !units.contains(&u16::from(b'\'')) {
            return Ok(message.clone());
        }

        let parameters = message_parameters.unwrap_or(&[]);
        let mut result = Vec::with_capacity(units.len());
        let mut index = 0;
        let mut quoted = false;
        while index < units.len() {
            let unit = units[index];
            if unit == u16::from(b'\'') {
                if units.get(index + 1) == Some(&u16::from(b'\'')) {
                    result.push(u16::from(b'\''));
                    index += 2;
                    continue;
                }
                quoted = !quoted;
                index += 1;
                continue;
            }
            if unit == u16::from(b'{') && !quoted {
                let Some(end) = find_format_element_end(units, index + 1) else {
                    return Err(Box::new(MessageFormatError::UnmatchedBraces));
                };
                let element = Utf16String::from_utf16(units[index + 1..end].to_vec());
                let formatted = format_message_element(&element, parameters, locale)?;
                result.extend_from_slice(formatted.as_utf16());
                index = end + 1;
                continue;
            }
            // `MessageFormat` 把未与格式元素配对的右花括号作为普通文本保留。
            result.push(unit);
            index += 1;
        }
        // Java MessageFormat 接受未闭合引号,并将其视为延续到模式末尾。
        Ok(Utf16String::from_utf16(result))
    }

    fn compute_message_resource_names_from_base(
        resource_base_name: &str,
        locale: &Locale,
    ) -> Result<Vec<String>, TemplateProcessingException> {
        let language = locale.get_language().to_string_lossy();
        if is_java_empty_or_whitespace(&language) {
            return Err(TemplateProcessingException::new(Some(format!(
                "Locale \"{locale}\" cannot be used as it does not specify a language."
            ))));
        }

        let country = locale.get_country().to_string_lossy();
        let variant = locale.get_variant().to_string_lossy();
        let mut resource_names = Vec::with_capacity(4);
        resource_names.push(format!("{resource_base_name}.properties"));
        resource_names.push(format!("{resource_base_name}_{language}.properties"));
        if !is_java_empty_or_whitespace(&country) {
            resource_names.push(format!(
                "{resource_base_name}_{language}_{country}.properties"
            ));
        }
        if !is_java_empty_or_whitespace(&variant) {
            resource_names.push(format!(
                "{resource_base_name}_{language}_{country}-{variant}.properties"
            ));
        }
        Ok(resource_names)
    }

    fn read_messages_resource(
        mut properties_reader: Box<dyn Read>,
    ) -> Result<Messages, TemplateInputException> {
        let mut bytes = Vec::new();
        properties_reader.read_to_end(&mut bytes).map_err(|error| {
            TemplateInputException::with_cause(
                Some("Exception loading messages file".to_owned()),
                error,
            )
        })?;
        let mut messages = Messages::new();
        java_properties::PropertiesIter::new_with_encoding(bytes.as_slice(), encoding_rs::UTF_8)
            .read_into(|key, value| {
                messages.insert(
                    Utf16String::from_rust_str(&key),
                    Utf16String::from_rust_str(&value),
                );
            })
            .map_err(|error| {
                TemplateInputException::with_cause(
                    Some("Exception loading messages file".to_owned()),
                    error,
                )
            })?;
        Ok(messages)
    }
}

#[derive(Debug, thiserror::Error)]
enum MessageFormatError {
    #[error("can't parse argument number: {0}")]
    ArgumentNumber(String),
    #[error("unknown format type: {0}")]
    UnknownFormatType(String),
    #[error("Unmatched braces in the pattern.")]
    UnmatchedBraces,
    #[error("Cannot format given Object as a Number")]
    NotANumber,
    #[error("Choice Pattern incorrect: {0}")]
    InvalidChoice(String),
    #[error("{0}")]
    ChoiceArrayIndex(String),
    #[error("{0}")]
    NumberFormatting(String),
}

#[derive(Debug, thiserror::Error)]
enum OriginRegistrationError {
    #[error("Origin parent metadata would create a cycle")]
    Cycle,
    #[error("Origin already has a different registered parent")]
    ConflictingParent,
}

fn format_message_element(
    element: &Utf16String,
    parameters: &[Option<std::sync::Arc<TemplateValue>>],
    locale: &Locale,
) -> MessageResolutionResult<Utf16String> {
    let element_text = element.to_string_lossy();
    let mut sections = element_text.splitn(3, ',');
    let argument_text = sections.next().unwrap_or("").trim();
    let argument_index = argument_text.parse::<usize>().map_err(|_| {
        Box::new(MessageFormatError::ArgumentNumber(argument_text.to_owned()))
            as super::MessageResolutionError
    })?;
    let Some(parameter) = parameters.get(argument_index) else {
        let mut missing = Vec::with_capacity(element.len() + 2);
        missing.push(u16::from(b'{'));
        missing.extend_from_slice(element.as_utf16());
        missing.push(u16::from(b'}'));
        return Ok(Utf16String::from_utf16(missing));
    };
    let value = parameter.as_deref();
    let Some(format_type) = sections.next().map(str::trim) else {
        return format_default_parameter(value, locale);
    };
    let format_style = sections.next().map(str::trim).unwrap_or("");

    match format_type {
        "" => format_default_parameter(value, locale),
        "number" => format_number_parameter(value, locale, format_style),
        "choice" => format_choice_parameter(value, locale, format_style, parameters),
        "date" | "time" => format_temporal_parameter(value, locale, format_type, format_style),
        unknown => Err(Box::new(MessageFormatError::UnknownFormatType(
            unknown.to_owned(),
        ))),
    }
}

fn format_default_parameter(
    value: Option<&TemplateValue>,
    locale: &Locale,
) -> MessageResolutionResult<Utf16String> {
    match value {
        Some(TemplateValue::Number(number)) => format_number(number, locale, NumberStyle::Default),
        Some(TemplateValue::Object(object)) if object.as_any().is::<crate::util::DateValue>() => {
            format_temporal_parameter(value, locale, "", "")
        }
        Some(value) => Ok(value
            .to_utf16_string()
            .unwrap_or_else(|| Utf16String::from_rust_str("null"))),
        None => Ok(Utf16String::from_rust_str("null")),
    }
}

#[derive(Clone, Copy)]
enum NumberStyle {
    Default,
    Integer,
    Currency,
    Percent,
}

fn format_number_parameter(
    value: Option<&TemplateValue>,
    locale: &Locale,
    style: &str,
) -> MessageResolutionResult<Utf16String> {
    let Some(TemplateValue::Number(number)) = value else {
        return Err(Box::new(MessageFormatError::NotANumber));
    };
    let style = match style {
        "" => NumberStyle::Default,
        "integer" => NumberStyle::Integer,
        "currency" => NumberStyle::Currency,
        "percent" => NumberStyle::Percent,
        pattern => return format_decimal_pattern(number, locale, pattern),
    };
    format_number(number, locale, style)
}

fn format_decimal_pattern(
    number: &NumberValue,
    locale: &Locale,
    pattern: &str,
) -> MessageResolutionResult<Utf16String> {
    let subpatterns = split_unquoted(pattern, ';');
    if subpatterns.len() > 2 {
        return malformed_decimal_pattern(pattern);
    }
    let positive = parse_decimal_subpattern(subpatterns[0], pattern)?;
    let negative = subpatterns
        .get(1)
        .map(|subpattern| parse_decimal_subpattern(subpattern, pattern))
        .transpose()?;
    let is_negative = number_is_negative(number);
    let selected = if is_negative {
        negative.as_ref().unwrap_or(&positive)
    } else {
        &positive
    };
    let prefix = if is_negative && negative.is_none() {
        format!("-{}", decimal_affix(selected.prefix, locale))
    } else {
        decimal_affix(selected.prefix, locale)
    };
    let suffix = decimal_affix(selected.suffix, locale);

    let absolute = absolute_number(number);
    let scaled = scale_number(&absolute, selected.multiplier);
    let formatted = if selected.scientific {
        format_scientific_number(&scaled, selected)?
    } else {
        format_fixed_decimal_number(&scaled, locale, selected)?
    };
    Ok(Utf16String::from_rust_str(&format!(
        "{prefix}{formatted}{suffix}"
    )))
}

#[derive(Debug)]
struct DecimalSubpattern<'a> {
    prefix: &'a str,
    suffix: &'a str,
    min_integer_digits: usize,
    min_fraction_digits: usize,
    max_fraction_digits: usize,
    grouping: bool,
    multiplier: i32,
    scientific: bool,
    min_exponent_digits: usize,
}

fn parse_decimal_subpattern<'a>(
    subpattern: &'a str,
    full_pattern: &str,
) -> MessageResolutionResult<DecimalSubpattern<'a>> {
    let (first_digit, last_digit) =
        numeric_pattern_bounds(subpattern).ok_or_else(|| decimal_pattern_error(full_pattern))?;
    let prefix = &subpattern[..first_digit];
    let numeric_pattern = &subpattern[first_digit..last_digit];
    let suffix = &subpattern[last_digit..];
    let (mantissa_pattern, exponent_pattern) = numeric_pattern
        .split_once('E')
        .map_or((numeric_pattern, None), |(mantissa, exponent)| {
            (mantissa, Some(exponent))
        });
    if numeric_pattern.matches('E').count() > 1 {
        return malformed_decimal_pattern(full_pattern);
    }
    let (integer_pattern, fraction_pattern) = mantissa_pattern
        .split_once('.')
        .unwrap_or((mantissa_pattern, ""));
    if mantissa_pattern.matches('.').count() > 1
        || !integer_pattern
            .chars()
            .all(|character| matches!(character, '#' | '0' | ','))
        || !fraction_pattern
            .chars()
            .all(|character| matches!(character, '#' | '0'))
        || exponent_pattern.is_some_and(|exponent| {
            exponent.is_empty() || !exponent.chars().all(|character| character == '0')
        })
    {
        return malformed_decimal_pattern(full_pattern);
    }
    let min_integer_digits = integer_pattern
        .chars()
        .filter(|character| *character == '0')
        .count();
    let min_fraction_digits = fraction_pattern
        .chars()
        .filter(|character| *character == '0')
        .count();
    let max_fraction_digits = fraction_pattern
        .chars()
        .filter(|character| matches!(character, '0' | '#'))
        .count();
    let multiplier = if contains_unquoted(prefix, '%') || contains_unquoted(suffix, '%') {
        100
    } else if contains_unquoted(prefix, '') || contains_unquoted(suffix, '') {
        1000
    } else {
        1
    };
    Ok(DecimalSubpattern {
        prefix,
        suffix,
        min_integer_digits,
        min_fraction_digits,
        max_fraction_digits,
        grouping: integer_pattern.contains(','),
        multiplier,
        scientific: exponent_pattern.is_some(),
        min_exponent_digits: exponent_pattern.map_or(0, str::len),
    })
}

fn numeric_pattern_bounds(pattern: &str) -> Option<(usize, usize)> {
    let mut quoted = false;
    let mut first = None;
    let mut last = None;
    let characters = pattern.char_indices().collect::<Vec<_>>();
    let mut index = 0;
    while index < characters.len() {
        let (byte_index, character) = characters[index];
        if character == '\''
            && characters
                .get(index + 1)
                .is_some_and(|(_, next)| *next == '\'')
        {
            index += 2;
            continue;
        }
        if character == '\'' {
            quoted = !quoted;
        } else if !quoted && matches!(character, '#' | '0') {
            first.get_or_insert(byte_index);
            last = Some(byte_index + character.len_utf8());
        }
        index += 1;
    }
    first.zip(last)
}

fn format_fixed_decimal_number(
    number: &NumberValue,
    locale: &Locale,
    pattern: &DecimalSubpattern<'_>,
) -> MessageResolutionResult<String> {
    let mut formatted = match number {
        NumberValue::Double(value) if value.is_nan() => "NaN".to_owned(),
        NumberValue::Float(value) if value.is_nan() => "NaN".to_owned(),
        NumberValue::Double(value) if value.is_infinite() => "".to_owned(),
        NumberValue::Float(value) if value.is_infinite() => "".to_owned(),
        _ => NumberUtils::format(
            Some(number),
            Some(pattern.min_integer_digits.max(1) as i32),
            Some(if pattern.grouping {
                NumberPointType::Default
            } else {
                NumberPointType::None
            }),
            Some(pattern.max_fraction_digits as i32),
            Some(NumberPointType::Default),
            Some(locale),
        )
        .map_err(|error| {
            Box::new(MessageFormatError::NumberFormatting(error.to_string()))
                as super::MessageResolutionError
        })?
        .expect("non-null number")
        .to_string_lossy(),
    };
    trim_optional_fraction(
        &mut formatted,
        pattern.min_fraction_digits,
        pattern.max_fraction_digits,
        locale,
    );
    Ok(formatted)
}

fn format_scientific_number(
    number: &NumberValue,
    pattern: &DecimalSubpattern<'_>,
) -> MessageResolutionResult<String> {
    let value = number_as_f64(number);
    if value.is_nan() {
        return Ok("NaN".to_owned());
    }
    if value.is_infinite() {
        return Ok("".to_owned());
    }
    let precision = pattern.max_fraction_digits;
    let raw = format!("{value:.precision$E}");
    let (mut mantissa, exponent) = raw
        .split_once('E')
        .ok_or_else(|| decimal_pattern_error("scientific"))?;
    while mantissa.contains('.')
        && mantissa.ends_with('0')
        && mantissa
            .split_once('.')
            .map_or(0, |(_, fraction)| fraction.len())
            > pattern.min_fraction_digits
    {
        mantissa = &mantissa[..mantissa.len() - 1];
    }
    if mantissa.ends_with('.') {
        mantissa = &mantissa[..mantissa.len() - 1];
    }
    let exponent_value = exponent.parse::<i32>().map_err(|_| {
        Box::new(MessageFormatError::NumberFormatting(raw.clone())) as super::MessageResolutionError
    })?;
    let exponent_sign = if exponent_value < 0 { "-" } else { "" };
    let exponent_digits = exponent_value.unsigned_abs().to_string();
    Ok(format!(
        "{mantissa}E{exponent_sign}{:0>width$}",
        exponent_digits,
        width = pattern.min_exponent_digits.max(1)
    ))
}

fn trim_optional_fraction(
    formatted: &mut String,
    min_fraction_digits: usize,
    max_fraction_digits: usize,
    locale: &Locale,
) {
    if max_fraction_digits <= min_fraction_digits {
        return;
    }
    let decimal_separator = if locale_uses_decimal_comma(locale) {
        ','
    } else {
        '.'
    };
    if let Some(decimal_index) = formatted.rfind(decimal_separator) {
        while formatted.ends_with('0')
            && formatted.len() - decimal_index - decimal_separator.len_utf8() > min_fraction_digits
        {
            formatted.pop();
        }
        if min_fraction_digits == 0 && formatted.ends_with(decimal_separator) {
            formatted.pop();
        }
    }
}

fn decimal_affix(affix: &str, locale: &Locale) -> String {
    let currency = NumberUtils::format_currency(Some(&NumberValue::Integer(0)), Some(locale))
        .ok()
        .flatten()
        .map(|formatted| {
            formatted
                .to_string_lossy()
                .trim_matches(|character: char| {
                    character.is_ascii_digit()
                        || matches!(character, '.' | ',' | '-' | '\u{a0}' | ' ')
                })
                .to_owned()
        })
        .unwrap_or_else(|| "¤".to_owned());
    let mut result = String::new();
    let characters = affix.chars().collect::<Vec<_>>();
    let mut index = 0;
    let mut quoted = false;
    while index < characters.len() {
        if characters[index] == '\'' && characters.get(index + 1) == Some(&'\'') {
            result.push('\'');
            index += 2;
            continue;
        }
        if characters[index] == '\'' {
            quoted = !quoted;
            index += 1;
            continue;
        }
        if !quoted && characters[index] == '¤' {
            result.push_str(&currency);
        } else {
            result.push(characters[index]);
        }
        index += 1;
    }
    result
}

fn split_unquoted(value: &str, delimiter: char) -> Vec<&str> {
    let mut result = Vec::new();
    let mut start = 0;
    let mut quoted = false;
    let characters = value.char_indices().collect::<Vec<_>>();
    let mut index = 0;
    while index < characters.len() {
        let (byte_index, character) = characters[index];
        if character == '\''
            && characters
                .get(index + 1)
                .is_some_and(|(_, next)| *next == '\'')
        {
            index += 2;
            continue;
        }
        if character == '\'' {
            quoted = !quoted;
        } else if !quoted && character == delimiter {
            result.push(&value[start..byte_index]);
            start = byte_index + character.len_utf8();
        }
        index += 1;
    }
    result.push(&value[start..]);
    result
}

fn contains_unquoted(value: &str, target: char) -> bool {
    let mut quoted = false;
    let characters = value.chars().collect::<Vec<_>>();
    let mut index = 0;
    while index < characters.len() {
        if characters[index] == '\'' && characters.get(index + 1) == Some(&'\'') {
            index += 2;
            continue;
        }
        if characters[index] == '\'' {
            quoted = !quoted;
        } else if !quoted && characters[index] == target {
            return true;
        }
        index += 1;
    }
    false
}

fn number_is_negative(number: &NumberValue) -> bool {
    match number {
        NumberValue::BigDecimal(value) => value.unscaled_value().is_negative(),
        NumberValue::BigInteger(value) => value.is_negative(),
        NumberValue::Byte(value) => *value < 0,
        NumberValue::Short(value) => *value < 0,
        NumberValue::Integer(value) => *value < 0,
        NumberValue::Long(value) => *value < 0,
        NumberValue::Float(value) => value.is_sign_negative(),
        NumberValue::Double(value) => value.is_sign_negative(),
        NumberValue::Other { double_value, .. } => double_value.is_sign_negative(),
    }
}

fn absolute_number(number: &NumberValue) -> NumberValue {
    match number {
        NumberValue::BigDecimal(value) => NumberValue::BigDecimal(BigDecimalValue::from_unscaled(
            value.unscaled_value().abs(),
            value.scale(),
        )),
        NumberValue::BigInteger(value) => NumberValue::BigInteger(value.abs()),
        NumberValue::Byte(value) => NumberValue::Integer(i32::from(*value).abs()),
        NumberValue::Short(value) => NumberValue::Integer(i32::from(*value).abs()),
        NumberValue::Integer(value) => NumberValue::Long(i64::from(*value).abs()),
        NumberValue::Long(value) => {
            if *value == i64::MIN {
                NumberValue::BigInteger(BigInt::from(*value).abs())
            } else {
                NumberValue::Long(value.abs())
            }
        }
        NumberValue::Float(value) => NumberValue::Float(value.abs()),
        NumberValue::Double(value) => NumberValue::Double(value.abs()),
        NumberValue::Other {
            class_name,
            double_value,
        } => NumberValue::Other {
            class_name: class_name.clone(),
            double_value: double_value.abs(),
        },
    }
}

fn scale_number(number: &NumberValue, multiplier: i32) -> NumberValue {
    if multiplier == 1 {
        return number.clone();
    }
    match number_as_decimal(number) {
        Some(value) => NumberValue::BigDecimal(BigDecimalValue::from_unscaled(
            value.unscaled_value() * BigInt::from(multiplier),
            value.scale(),
        )),
        None => NumberValue::Double(number_as_f64(number) * f64::from(multiplier)),
    }
}

fn number_as_decimal(number: &NumberValue) -> Option<BigDecimalValue> {
    match number {
        NumberValue::BigDecimal(value) => Some(value.clone()),
        NumberValue::BigInteger(value) => Some(BigDecimalValue::from_unscaled(value.clone(), 0)),
        NumberValue::Byte(value) => Some(BigDecimalValue::from_unscaled(BigInt::from(*value), 0)),
        NumberValue::Short(value) => Some(BigDecimalValue::from_unscaled(BigInt::from(*value), 0)),
        NumberValue::Integer(value) => {
            Some(BigDecimalValue::from_unscaled(BigInt::from(*value), 0))
        }
        NumberValue::Long(value) => Some(BigDecimalValue::from_unscaled(BigInt::from(*value), 0)),
        NumberValue::Float(value) if value.is_finite() => {
            BigDecimalValue::parse(&value.to_string()).ok()
        }
        NumberValue::Double(value) if value.is_finite() => {
            BigDecimalValue::parse(&value.to_string()).ok()
        }
        NumberValue::Other { double_value, .. } if double_value.is_finite() => {
            BigDecimalValue::parse(&double_value.to_string()).ok()
        }
        NumberValue::Float(_) | NumberValue::Double(_) | NumberValue::Other { .. } => None,
    }
}

fn decimal_pattern_error(pattern: &str) -> super::MessageResolutionError {
    Box::new(MessageFormatError::NumberFormatting(format!(
        "Malformed pattern \"{pattern}\""
    )))
}

fn malformed_decimal_pattern<T>(pattern: &str) -> MessageResolutionResult<T> {
    Err(decimal_pattern_error(pattern))
}

fn locale_uses_decimal_comma(locale: &Locale) -> bool {
    matches!(
        locale.get_language().to_string_lossy().as_str(),
        "ar" | "bg"
            | "cs"
            | "da"
            | "de"
            | "el"
            | "es"
            | "fi"
            | "fr"
            | "hu"
            | "id"
            | "it"
            | "nl"
            | "no"
            | "pl"
            | "pt"
            | "ro"
            | "ru"
            | "sk"
            | "sl"
            | "sv"
            | "tr"
            | "uk"
            | "vi"
    )
}

fn format_number(
    number: &NumberValue,
    locale: &Locale,
    style: NumberStyle,
) -> MessageResolutionResult<Utf16String> {
    if let Some(non_finite) = non_finite_number_text(number) {
        let formatted = match style {
            NumberStyle::Currency => {
                let symbol = currency_symbol(locale);
                if currency_symbol_after(locale) {
                    format!("{non_finite}\u{a0}{symbol}")
                } else {
                    format!("{symbol}{non_finite}")
                }
            }
            NumberStyle::Percent => format!("{non_finite}%"),
            NumberStyle::Default | NumberStyle::Integer => non_finite.to_owned(),
        };
        return Ok(Utf16String::from_rust_str(&formatted));
    }
    let formatted = match style {
        NumberStyle::Currency => NumberUtils::format_currency(Some(number), Some(locale)),
        NumberStyle::Percent => {
            NumberUtils::format_percent(Some(number), Some(1), Some(0), Some(locale))
        }
        NumberStyle::Integer => NumberUtils::format(
            Some(number),
            Some(1),
            Some(NumberPointType::Default),
            Some(0),
            Some(NumberPointType::Default),
            Some(locale),
        ),
        NumberStyle::Default => {
            let rounded = number_as_f64(number);
            let fraction_digits = default_fraction_digits(rounded);
            NumberUtils::format(
                Some(number),
                Some(1),
                Some(NumberPointType::Default),
                Some(fraction_digits),
                Some(NumberPointType::Default),
                Some(locale),
            )
        }
    }
    .map_err(|error| {
        Box::new(MessageFormatError::NumberFormatting(error.to_string()))
            as super::MessageResolutionError
    })?;
    Ok(formatted.unwrap_or_else(|| Utf16String::from_rust_str("null")))
}

fn non_finite_number_text(number: &NumberValue) -> Option<&'static str> {
    let value = match number {
        NumberValue::Float(value) => f64::from(*value),
        NumberValue::Double(value)
        | NumberValue::Other {
            double_value: value,
            ..
        } => *value,
        _ => return None,
    };
    if value.is_nan() {
        Some("NaN")
    } else if value == f64::INFINITY {
        Some("")
    } else if value == f64::NEG_INFINITY {
        Some("-∞")
    } else {
        None
    }
}

fn currency_symbol(locale: &Locale) -> &'static str {
    match locale.get_country().to_string_lossy().as_str() {
        "US" => "$",
        "GB" => "£",
        "JP" => "",
        "CN" => "¥",
        "KR" => "",
        "CH" => "CHF",
        "IN" => "",
        "CA" => "CA$",
        "AU" => "A$",
        _ if locale_uses_decimal_comma(locale) => "",
        _ => "¤",
    }
}

fn currency_symbol_after(locale: &Locale) -> bool {
    locale_uses_decimal_comma(locale)
}

fn default_fraction_digits(value: f64) -> i32 {
    if !value.is_finite() {
        return 0;
    }
    let rounded = (value * 1000.0).round_ties_even() / 1000.0;
    let rendered = format!("{rounded:.3}");
    rendered.split_once('.').map_or(0, |(_, fraction)| {
        fraction.trim_end_matches('0').len() as i32
    })
}

fn number_as_f64(number: &NumberValue) -> f64 {
    match number {
        NumberValue::BigDecimal(value) => value.to_string().parse().unwrap_or(f64::NAN),
        NumberValue::BigInteger(value) => value.to_string().parse().unwrap_or(f64::INFINITY),
        NumberValue::Byte(value) => f64::from(*value),
        NumberValue::Short(value) => f64::from(*value),
        NumberValue::Integer(value) => f64::from(*value),
        NumberValue::Long(value) => *value as f64,
        NumberValue::Float(value) => f64::from(*value),
        NumberValue::Double(value) => *value,
        NumberValue::Other { double_value, .. } => *double_value,
    }
}

fn format_choice_parameter(
    value: Option<&TemplateValue>,
    locale: &Locale,
    pattern: &str,
    parameters: &[Option<std::sync::Arc<TemplateValue>>],
) -> MessageResolutionResult<Utf16String> {
    let Some(TemplateValue::Number(number)) = value else {
        return Err(Box::new(MessageFormatError::NotANumber));
    };
    let number = number_as_f64(number);
    let mut selected = None;
    for (alternative_index, alternative) in
        split_choice_alternatives(pattern).into_iter().enumerate()
    {
        let Some((limit, comparator, text)) = parse_choice_alternative(alternative) else {
            return if alternative_index == 0 {
                Err(Box::new(MessageFormatError::ChoiceArrayIndex(
                    "Index 0 out of bounds for length 0".to_owned(),
                )))
            } else {
                Err(Box::new(MessageFormatError::InvalidChoice(
                    pattern.to_owned(),
                )))
            };
        };
        if alternative_index == 0 {
            // `ChoiceFormat` 对低于首个 limit 的值仍返回第一个子格式。
            selected = Some(text);
        }
        let matches = match comparator {
            '#' | '' => number >= limit,
            '<' => number > limit,
            _ => false,
        };
        if matches {
            selected = Some(text);
        }
    }
    let selected = selected.unwrap_or("");
    StandardMessageResolutionUtils::format_message(
        locale,
        &Utf16String::from_rust_str(selected),
        Some(parameters),
    )
}

fn split_choice_alternatives(pattern: &str) -> Vec<&str> {
    let mut alternatives = Vec::new();
    let mut start = 0;
    let mut quoted = false;
    for (index, character) in pattern.char_indices() {
        if character == '\'' {
            quoted = !quoted;
        } else if character == '|' && !quoted {
            alternatives.push(&pattern[start..index]);
            start = index + character.len_utf8();
        }
    }
    alternatives.push(&pattern[start..]);
    alternatives
}

fn parse_choice_alternative(alternative: &str) -> Option<(f64, char, &str)> {
    let (index, comparator) = alternative
        .char_indices()
        .find(|(_, character)| matches!(character, '#' | '<' | ''))?;
    let limit_text = alternative[..index].trim();
    let limit = match limit_text {
        "" | "+∞" => f64::INFINITY,
        "-∞" => f64::NEG_INFINITY,
        _ => limit_text.parse().ok()?,
    };
    Some((
        limit,
        comparator,
        &alternative[index + comparator.len_utf8()..],
    ))
}

fn format_temporal_parameter(
    value: Option<&TemplateValue>,
    locale: &Locale,
    format_type: &str,
    style: &str,
) -> MessageResolutionResult<Utf16String> {
    let Some(TemplateValue::Object(object)) = value else {
        return Err(Box::new(MessageFormatError::NumberFormatting(
            "Cannot format given Object as a Date".to_owned(),
        )));
    };
    let Some(date) = object.as_any().downcast_ref::<crate::util::DateValue>() else {
        return Err(Box::new(MessageFormatError::NumberFormatting(
            "Cannot format given Object as a Date".to_owned(),
        )));
    };
    let pattern = temporal_pattern(locale, format_type, style)?;
    crate::util::DateUtils::format(
        Some(date),
        Some(&Utf16String::from_rust_str(&pattern)),
        Some(locale),
    )
    .map_err(|error| Box::new(error) as super::MessageResolutionError)
    .map(|formatted| formatted.unwrap_or_else(|| Utf16String::from_rust_str("null")))
}

fn temporal_pattern(
    locale: &Locale,
    format_type: &str,
    style: &str,
) -> MessageResolutionResult<String> {
    let language = locale.get_language().to_string_lossy();
    let date = match (language.as_str(), style) {
        ("en", "short") => "M/d/yy",
        ("en", "" | "medium") => "MMM d, yyyy",
        ("en", "long") => "MMMM d, yyyy",
        ("en", "full") => "EEEE, MMMM d, yyyy",
        ("de", "short") => "dd.MM.yy",
        ("de", "" | "medium") => "dd.MM.yyyy",
        ("de", "long") => "d. MMMM yyyy",
        ("de", "full") => "EEEE, d. MMMM yyyy",
        ("fr", "short") => "dd/MM/y",
        ("fr", "" | "medium") => "d MMM yyyy",
        ("fr", "long") => "d MMMM yyyy",
        ("fr", "full") => "EEEE d MMMM yyyy",
        ("ja", "short" | "" | "medium") => "yyyy/MM/dd",
        ("ja", "long") => "yyyy年M月d日",
        ("ja", "full") => "yyyy年M月d日EEEE",
        (_, "short") => "dd/MM/yy",
        (_, "" | "medium") => "dd MMM yyyy",
        (_, "long" | "full") => "d MMMM yyyy",
        _ => style,
    };
    let time = match (language.as_str(), style) {
        ("en", "short") => "h:mm\u{202f}a",
        ("en", "" | "medium") => "h:mm:ss\u{202f}a",
        ("en", "long") => "h:mm:ss\u{202f}a z",
        ("en", "full") => "h:mm:ss\u{202f}a zzzz",
        (_, "short") => "HH:mm",
        (_, "" | "medium") => "HH:mm:ss",
        (_, "long") => "HH:mm:ss z",
        (_, "full") => "HH:mm:ss zzzz",
        _ => style,
    };
    Ok(match format_type {
        "time" => time,
        "date" => date,
        "" if language == "en" => "M/d/yy, h:mm\u{202f}a",
        "" => "dd/MM/yy, HH:mm",
        _ => date,
    }
    .to_owned())
}

fn is_java_empty_or_whitespace(value: &str) -> bool {
    value.chars().all(|character| {
        character == ' '
            || (character.is_whitespace()
                && !matches!(character, '\u{00a0}' | '\u{2007}' | '\u{202f}'))
    })
}

fn find_format_element_end(units: &[u16], start: usize) -> Option<usize> {
    let mut depth = 0usize;
    let mut quoted = false;
    let mut index = start;
    while index < units.len() {
        let unit = units[index];
        if unit == u16::from(b'\'') && units.get(index + 1) == Some(&u16::from(b'\'')) {
            index += 2;
            continue;
        }
        if unit == u16::from(b'\'') {
            quoted = !quoted;
        } else if !quoted && unit == u16::from(b'{') {
            depth += 1;
        } else if !quoted && unit == u16::from(b'}') {
            if depth == 0 {
                return Some(index);
            }
            depth -= 1;
        }
        index += 1;
    }
    None
}

fn origin_messages() -> &'static RwLock<OriginMessages> {
    ORIGIN_MESSAGES.get_or_init(|| RwLock::new(HashMap::new()))
}

fn origin_parents() -> &'static RwLock<OriginParents> {
    ORIGIN_PARENTS.get_or_init(|| RwLock::new(HashMap::new()))
}

fn read_lock<T>(lock: &RwLock<T>) -> std::sync::RwLockReadGuard<'_, T> {
    lock.read()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

fn write_lock<T>(lock: &RwLock<T>) -> std::sync::RwLockWriteGuard<'_, T> {
    lock.write()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

#[cfg(test)]
mod tests {
    use std::any::TypeId;
    use std::collections::HashMap;

    use crate::util::{Locale, Utf16String};

    use super::StandardMessageResolutionUtils;

    struct Parent;
    struct Child;
    struct OtherParent;
    struct CycleA;
    struct CycleB;

    #[test]
    fn origin_messages_use_specific_class_before_registered_parent() {
        let locale = Locale::new(
            Utf16String::from_rust_str("en-US"),
            Utf16String::from_rust_str("US"),
        );
        let parent = TypeId::of::<Parent>();
        let child = TypeId::of::<Child>();
        StandardMessageResolutionUtils::register_origin_messages(
            parent,
            locale.clone(),
            HashMap::from([
                (
                    Utf16String::from_rust_str("parent"),
                    Utf16String::from_rust_str("parent-value"),
                ),
                (
                    Utf16String::from_rust_str("same"),
                    Utf16String::from_rust_str("parent-value"),
                ),
            ]),
        );
        StandardMessageResolutionUtils::register_origin_messages(
            child,
            locale.clone(),
            HashMap::from([(
                Utf16String::from_rust_str("same"),
                Utf16String::from_rust_str("child-value"),
            )]),
        );
        StandardMessageResolutionUtils::register_origin_parent(child, parent)
            .expect("valid parent");

        let resolved = StandardMessageResolutionUtils::resolve_messages_for_origin(child, &locale);
        assert_eq!(
            resolved.get(&Utf16String::from_rust_str("parent")),
            Some(&Utf16String::from_rust_str("parent-value"))
        );
        assert_eq!(
            resolved.get(&Utf16String::from_rust_str("same")),
            Some(&Utf16String::from_rust_str("child-value"))
        );
    }

    #[test]
    fn origin_parent_registration_is_idempotent_and_rejects_impossible_hierarchies() {
        let child = TypeId::of::<Child>();
        let parent = TypeId::of::<Parent>();
        StandardMessageResolutionUtils::register_origin_parent(child, parent)
            .expect("same registration is idempotent");
        let conflicting = StandardMessageResolutionUtils::register_origin_parent(
            child,
            TypeId::of::<OtherParent>(),
        )
        .expect_err("Java class cannot have two direct superclasses");
        assert_eq!(
            conflicting.to_string(),
            "Origin already has a different registered parent"
        );

        let cycle_a = TypeId::of::<CycleA>();
        let cycle_b = TypeId::of::<CycleB>();
        StandardMessageResolutionUtils::register_origin_parent(cycle_a, cycle_b)
            .expect("first edge");
        let cycle = StandardMessageResolutionUtils::register_origin_parent(cycle_b, cycle_a)
            .expect_err("Java class hierarchy cannot contain a cycle");
        assert_eq!(
            cycle.to_string(),
            "Origin parent metadata would create a cycle"
        );
    }
}