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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if canImport(FoundationEssentials)
import FoundationEssentials
#endif
internal import _FoundationICU
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
public struct FormatStyleCapitalizationContext : Codable, Hashable, Sendable {
internal enum Option: Int, Codable, Hashable {
case unknown
case standalone
case listItem
case beginningOfSentence
case middleOfSentence
}
internal var option: Option
private init(_ option: Option) {
self.option = option
}
var icuContext: UDisplayContext {
switch self.option {
case .unknown:
return .unknown
case .standalone:
return .standalone
case .listItem:
return .listItem
case .beginningOfSentence:
return .beginningOfSentence
case .middleOfSentence:
return .middleOfSentence
}
}
#if FOUNDATION_FRAMEWORK
var formatterContext: Formatter.Context {
switch option {
case .unknown:
return .unknown
case .standalone:
return .standalone
case .listItem:
return .listItem
case .beginningOfSentence:
return .beginningOfSentence
case .middleOfSentence:
return .middleOfSentence
}
}
#endif // FOUNDATION_FRAMEWORK
public static var unknown : FormatStyleCapitalizationContext {
.init(.unknown)
}
/// For stand-alone usage, such as an isolated name on a calendar page.
public static var standalone : FormatStyleCapitalizationContext {
.init(.standalone)
}
/// For use in a UI list or menu item.
public static var listItem : FormatStyleCapitalizationContext {
.init(.listItem)
}
public static var beginningOfSentence : FormatStyleCapitalizationContext {
.init(.beginningOfSentence)
}
public static var middleOfSentence : FormatStyleCapitalizationContext {
.init(.middleOfSentence)
}
}
@_nonSendable
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
public enum NumberFormatStyleConfiguration {
internal struct Collection : Codable, Hashable, Sendable {
var scale: Double?
var precision: Precision?
var group: Grouping?
var signDisplayStrategy: SignDisplayStrategy?
var decimalSeparatorStrategy: DecimalSeparatorDisplayStrategy?
var rounding: RoundingRule?
var roundingIncrement: RoundingIncrement?
var notation: Notation?
}
public typealias RoundingRule = FloatingPointRoundingRule
typealias Scale = Double
public struct Grouping : Codable, Hashable, CustomStringConvertible, Sendable {
enum Option : Int, Codable, Hashable {
case automatic
case hidden
// unused
// case hiddenBelow10000 // UNUM_GROUPING_MIN2
// case alwaysGreaterThan1000 // UNUM_GROUPING_ON_ALIGNED
// case alwaysGroup3 // UNUM_GROUPING_THOUSANDS
}
var option: Option
public static var automatic: Self { .init(option: .automatic) }
public static var never: Self { .init(option: .hidden) }
public var description: String {
switch option {
case .automatic:
return "automatic"
case .hidden:
return "never"
}
}
}
public struct Precision : Codable, Hashable, Sendable {
enum Option: Hashable {
case significantDigits(min: Int, max: Int?)
case integerAndFractionalLength(minInt: Int?, maxInt: Int?, minFraction: Int?, maxFraction: Int?)
}
var option: Option
// The maximum total length that ICU allows is 999.
// We take one off to reserve one character for the non-zero digit skeleton (the "0" skeleton in the number format)
static let validPartLength = 0..<999
static let validSignificantDigits = 1..<999
// min 3, max 3: 12345 -> 12300
// min 3, max 3: 0.12345 -> 0.123
// min 2, max 4: 3.14159 -> 3.142
// min 2, max 4: 1.23004 -> 1.23
// ^ Trailing zero digits to the right of the decimal separator are suppressed after the minimum number of significant digits have been shown
public static func significantDigits<R: RangeExpression>(_ limits: R) -> Self where R.Bound == Int {
let (lower, upper) = limits.clampedLowerAndUpperBounds(validSignificantDigits)
return Precision(option: .significantDigits(min: lower ?? validSignificantDigits.lowerBound, max: upper))
}
public static func significantDigits(_ digits: Int) -> Self {
return Precision(option: .significantDigits(min: digits, max: digits))
}
// maxInt 2 : 1997 -> 97
// minInt 5: 1997 -> 01997
// maxFrac 2: 0.125 -> 0.12
// minFrac 4: 0.125 -> 0.1250
public static func integerAndFractionLength<R1: RangeExpression, R2: RangeExpression>(integerLimits: R1, fractionLimits: R2) -> Self where R1.Bound == Int, R2.Bound == Int {
let (minInt, maxInt) = integerLimits.clampedLowerAndUpperBounds(validPartLength)
let (minFrac, maxFrac) = fractionLimits.clampedLowerAndUpperBounds(validPartLength)
return Precision(option: .integerAndFractionalLength(minInt: minInt, maxInt: maxInt, minFraction: minFrac, maxFraction: maxFrac))
}
public static func integerAndFractionLength(integer: Int, fraction: Int) -> Self {
return Precision(option: .integerAndFractionalLength(minInt: integer, maxInt: integer, minFraction: fraction, maxFraction: fraction))
}
public static func integerLength<R: RangeExpression>(_ limits: R) -> Self {
let (minInt, maxInt) = limits.clampedLowerAndUpperBounds(validPartLength)
return Precision(option: .integerAndFractionalLength(minInt: minInt, maxInt: maxInt, minFraction: nil, maxFraction: nil))
}
public static func integerLength(_ length: Int) -> Self {
return Precision(option: .integerAndFractionalLength(minInt: length, maxInt: length, minFraction: nil, maxFraction: nil))
}
public static func fractionLength<R: RangeExpression>(_ limits: R) -> Self where R.Bound == Int {
let (minFrac, maxFrac) = limits.clampedLowerAndUpperBounds(validPartLength)
return Precision(option: .integerAndFractionalLength(minInt: nil, maxInt: nil, minFraction: minFrac, maxFraction: maxFrac))
}
public static func fractionLength(_ length: Int) -> Self {
return Precision(option: .integerAndFractionalLength(minInt: nil, maxInt: nil, minFraction: length, maxFraction: length))
}
}
public struct DecimalSeparatorDisplayStrategy : Codable, Hashable, CustomStringConvertible, Sendable {
enum Option : Int, Codable, Hashable {
case automatic
case always
}
var option: Option
// "1.1", "1"
public static var automatic: Self {
.init(option: .automatic)
}
// "1.1", "1."
public static var always : Self { .init(option: .always) }
public var description: String {
switch option {
case .automatic:
return "automatic"
case .always:
return "always"
}
}
}
public struct SignDisplayStrategy : Codable, Hashable, CustomStringConvertible, Sendable {
enum Option : Int, Hashable, Codable {
case always
case hidden
}
var positive: Option
var negative: Option
var zero: Option
// Show the minus sign on negative numbers, and do not show the sign on positive numbers or zero
public static var automatic: Self {
SignDisplayStrategy(positive: .hidden, negative: .always, zero: .hidden)
}
public static var never: Self {
SignDisplayStrategy(positive: .hidden, negative: .hidden, zero: .hidden)
}
// Show the minus sign on negative numbers and the plus sign on positive numbers, and zero if specified
public static func always(includingZero: Bool = true) -> Self {
SignDisplayStrategy(positive: .always, negative: .always, zero: includingZero ? .always : .hidden)
}
public var description: String {
switch positive {
case .always:
switch zero {
case .always:
return "always(includingZero: true)"
case .hidden:
return "always(includingZero: false)"
}
case .hidden:
switch negative {
case .always:
return "automatic"
case .hidden:
return "never"
}
}
}
}
public struct Notation : Codable, Hashable, CustomStringConvertible, Sendable {
enum Option : Int, Codable, Hashable {
case automatic
case scientific
case compactName
}
var option: Option
public static var scientific: Self { .init(option: .scientific) }
public static var automatic: Self { .init(option: .automatic) }
/// Formats the number with localized prefixes or suffixes corresponding to powers of ten. Rounds to integer while showing at least two significant digits by default.
/// For example, "42.3K" for 42300 for the "en_US" locale.
/// - note: We do not support parsing a number string containing localized prefixes or suffixes.
public static var compactName: Self { .init(option: .compactName) }
public var description: String {
switch option {
case .scientific:
return "scientific"
case .automatic:
return "automatic"
case .compactName:
return "compact name"
}
}
}
internal enum RoundingIncrement: Hashable, CustomStringConvertible {
case integer(value: Int)
case floatingPoint(value: Double)
var description: String {
switch self {
case .integer(let value):
return String(value)
case .floatingPoint(let value):
return String(value)
}
}
}
}
@_nonSendable
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
public enum CurrencyFormatStyleConfiguration {
public typealias Grouping = NumberFormatStyleConfiguration.Grouping
public typealias Precision = NumberFormatStyleConfiguration.Precision
public typealias DecimalSeparatorDisplayStrategy = NumberFormatStyleConfiguration.DecimalSeparatorDisplayStrategy
public typealias RoundingRule = NumberFormatStyleConfiguration.RoundingRule
/// The type used to configure notation for currency format styles.
@available(FoundationPreview 0.4, *)
public typealias Notation = NumberFormatStyleConfiguration.Notation
internal typealias RoundingIncrement = NumberFormatStyleConfiguration.RoundingIncrement
internal struct Collection : Codable, Hashable {
var scale: Double?
var precision: Precision?
var group: Grouping?
var signDisplayStrategy: SignDisplayStrategy?
var decimalSeparatorStrategy: DecimalSeparatorDisplayStrategy?
var rounding: RoundingRule?
var roundingIncrement: RoundingIncrement?
var presentation: Presentation
var notation: Notation?
}
public struct SignDisplayStrategy: Codable, Hashable, Sendable {
enum Option : Int, Hashable, Codable {
case always
case hidden
}
var positive: Option
var negative: Option
var zero: Option
var accounting: Bool = false
public static var automatic: Self {
SignDisplayStrategy(positive: .hidden, negative: .always, zero: .hidden)
}
public static var never: Self {
SignDisplayStrategy(positive: .hidden, negative: .hidden, zero: .hidden)
}
// Show the minus sign on negative numbers and the plus sign on positive numbers, and zero if specified
public static func always(showZero: Bool = true) -> Self {
SignDisplayStrategy(positive: .always, negative: .always, zero: showZero ? .always : .hidden)
}
public static var accounting: Self {
SignDisplayStrategy(positive: .hidden, negative: .always, zero: .hidden, accounting: true)
}
public static func accountingAlways(showZero: Bool = false) -> Self {
SignDisplayStrategy(positive: .always, negative: .always, zero: showZero ? .always : .hidden, accounting: true)
}
}
public struct Presentation: Codable, Hashable, Sendable {
enum Option : Int, Codable, Hashable {
case narrow
case standard
case isoCode
case fullName
}
internal var option: Option
public static var narrow: Self { Presentation(option: .narrow) }
public static var standard: Self { Presentation(option: .standard) }
public static var isoCode: Self { Presentation(option: .isoCode) }
public static var fullName: Self { Presentation(option: .fullName) }
}
}
@_nonSendable
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
public enum DescriptiveNumberFormatConfiguration {
public typealias CapitalizationContext = FormatStyleCapitalizationContext
public struct Presentation : Codable, Hashable, Sendable {
internal enum Option : Int, Codable, Hashable {
case spellOut = 1
case ordinal = 2
case cardinal = 3
#if FOUNDATION_FRAMEWORK
fileprivate var numberFormatterStyle : NumberFormatter.Style {
switch self {
case .spellOut:
return .spellOut
case .ordinal:
return .ordinal
case .cardinal:
return .spellOut // cardinal is a special case spellout style
}
}
#endif // FOUNDATION_FRAMEWORK
}
internal var option: Option
public static var spellOut: Self { Presentation(rawValue: 1) }
public static var ordinal: Self { Presentation(rawValue: 2) }
internal static var cardinal: Self { Presentation(rawValue: 3) }
internal init(rawValue: Int) {
option = Option(rawValue: rawValue)!
}
}
internal struct Collection : Codable, Hashable {
var presentation: Presentation
var capitalizationContext: CapitalizationContext?
var icuNumberFormatStyle: UNumberFormatStyle {
switch presentation.option {
case .spellOut:
return .spellout
case .ordinal:
return .ordinal
case .cardinal:
return .spellout // cardinal is a special case spellout stype
}
}
}
}
// MARK: - Codable
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension FloatingPointRoundingRule {
private enum CodingValue: Int, Codable {
case toNearestOrAwayFromZero
case toNearestOrEven
case up
case down
case towardZero
case awayFromZero
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let value = try container.decode(CodingValue.self)
switch value {
case .toNearestOrAwayFromZero:
self = .toNearestOrAwayFromZero
case .toNearestOrEven:
self = .toNearestOrEven
case .up:
self = .up
case .down:
self = .down
case .towardZero:
self = .towardZero
case .awayFromZero:
self = .awayFromZero
}
}
public func encode(to encoder: Encoder) throws {
let codingValue: CodingValue
switch self {
case .toNearestOrAwayFromZero:
codingValue = .toNearestOrAwayFromZero
case .toNearestOrEven:
codingValue = .toNearestOrEven
case .up:
codingValue = .up
case .down:
codingValue = .down
case .towardZero:
codingValue = .towardZero
case .awayFromZero:
codingValue = .awayFromZero
@unknown default:
codingValue = .toNearestOrEven
}
var container = encoder.singleValueContainer()
try container.encode(codingValue)
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension FloatingPointRoundingRule : Codable { }
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.RoundingIncrement: Codable {
private enum CodingKeys: CodingKey {
case integer
case floatingPoint
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let value = try container.decodeIfPresent(Int.self, forKey: .integer) {
self = .integer(value: value)
} else if let value = try container.decodeIfPresent(Double.self, forKey: .floatingPoint) {
self = .floatingPoint(value: value)
} else {
self = .floatingPoint(value: 0.5)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .integer(let value):
try container.encode(value, forKey: .integer)
case .floatingPoint(let value):
try container.encode(value, forKey: .floatingPoint)
}
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.Precision.Option : Codable {
private enum CodingKeys: CodingKey {
case minSignificantDigits
case maxSignificantDigits
case minIntegerLength
case maxIntegerLength
case minFractionalLength
case maxFractionalLength
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let minSignificantDigits = try container.decodeIfPresent(Int.self, forKey: .minSignificantDigits), let maxSignificantDigits = try container.decodeIfPresent(Int.self, forKey: .maxSignificantDigits) {
self = .significantDigits(min: minSignificantDigits, max: maxSignificantDigits)
} else if let minInt = try container.decodeIfPresent(Int.self, forKey: .minIntegerLength), let maxInt = try container.decodeIfPresent(Int.self, forKey: .maxIntegerLength), let minFrac = try container.decodeIfPresent(Int.self, forKey: .minFractionalLength), let maxFrac = try container.decodeIfPresent(Int.self, forKey: .maxFractionalLength) {
self = .integerAndFractionalLength(minInt: minInt, maxInt: maxInt, minFraction: minFrac, maxFraction: maxFrac)
} else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath,
debugDescription: "Invalid Precision"))
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .significantDigits(let min, let max):
try container.encode(min, forKey: .minSignificantDigits)
try container.encode(max, forKey: .maxSignificantDigits)
case .integerAndFractionalLength(let minInt, let maxInt, let minFraction, let maxFraction):
try container.encode(minInt, forKey: .minIntegerLength)
try container.encode(maxInt, forKey: .maxIntegerLength)
try container.encode(minFraction, forKey: .minFractionalLength)
try container.encode(maxFraction, forKey: .maxFractionalLength)
}
}
}
// MARK: - ICU compatibility: NumberFormatStyleConfiguration
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.Collection {
var skeleton: String {
var s = ""
if let scale = scale {
s += scale.skeleton + " "
}
if let precision = precision, let roundingIncrement = roundingIncrement {
s += precision.skeletonWithRoundingIncrement(stem: roundingIncrement.skeleton) + " "
} else if let precision = precision {
s += precision.skeleton + " "
} else if let roundingIncrement = roundingIncrement {
s += roundingIncrement.skeleton + " "
}
if let group = group {
s += group.skeleton + " "
}
if let signDisplayStrategy = signDisplayStrategy {
s += signDisplayStrategy.skeleton + " "
}
if let decimalSeparatorStrategy = decimalSeparatorStrategy {
s += decimalSeparatorStrategy.skeleton + " "
}
if let rounding = rounding {
s += rounding.skeleton + " "
}
if let notation = notation {
s += notation.skeleton + " "
}
return s._trimmingWhitespace()
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.RoundingIncrement {
var skeleton: String {
switch self {
// ICU treats any value <= 0 as invalid. Fallback to the default behavior if that's the case.
case .integer(let value):
if value > 0 {
return "precision-increment/\(Decimal(value))"
} else {
return ""
}
case .floatingPoint(let value):
if value > 0 {
return "precision-increment/\(Decimal(value))"
} else {
return ""
}
}
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.Precision {
private func significantDigitsSkeleton(min: Int, max: Int?) -> String {
var stem: String = ""
stem += String(repeating: "@", count: min)
if let max = max {
stem += String(repeating: "#", count: max - min)
} else {
stem += "+"
}
return stem
}
private func integerAndFractionalLengthSkeleton(minInt: Int?, maxInt: Int?, minFrac: Int?, maxFrac: Int?) -> String {
var stem: String = ""
// Construct skeleton for fractional part
if minFrac != nil || maxFrac != nil {
if maxFrac == 0 {
stem += "precision-integer"
} else {
stem += fractionalStem(min: minFrac ?? 0, max: maxFrac)
}
}
// Construct skeleton for integer part
if minInt != nil || maxInt != nil {
if stem.count > 0 {
stem += " "
}
stem += integerStem(min: minInt ?? 0, max: maxInt)
}
return stem
}
func skeletonWithRoundingIncrement(stem: String) -> String {
guard stem.count > 0 else { return self.skeleton }
var incrementStem = stem
switch self.option {
case .significantDigits(_, _):
// Specifying rounding increment hides the effect of significant digits
break
case .integerAndFractionalLength(let minInt, let maxInt, let minFrac, _):
if let minFrac = minFrac {
if let decimalPoint = incrementStem.lastIndex(of: ".") {
let frac = incrementStem.suffix(from: incrementStem.index(after: decimalPoint))
if minFrac > frac.count {
incrementStem += String(repeating: "0", count: minFrac - frac.count)
}
} else {
incrementStem += "." + String(repeating: "0", count: minFrac)
}
}
if minInt != nil || maxInt != nil {
incrementStem += " " + integerStem(min: minInt ?? 0, max: maxInt)
}
}
return incrementStem
}
var skeleton : String {
switch self.option {
case .significantDigits(let min, let max):
return significantDigitsSkeleton(min: min, max: max)
case .integerAndFractionalLength(let minInt, let maxInt, let minFrac, let maxFrac):
return integerAndFractionalLengthSkeleton(minInt: minInt, maxInt: maxInt, minFrac: minFrac, maxFrac: maxFrac)
}
}
private func integerStem(min: Int, max: Int?) -> String {
var s = "integer-width/"
if max == 0 && min == 0 {
s += "*" // 75459602
return s
}
if let max = max {
guard max >= min else { return "" }
s += String(repeating: "#", count: max - min)
} else {
s += "+"
}
s += String(repeating: "0", count: min)
return s
}
private func fractionalStem(min: Int, max: Int?) -> String {
var s = "."
s += String(repeating: "0", count: min)
if let max = max {
guard max >= min else { return "" }
s += String(repeating: "#", count: max - min)
} else {
s += "+"
}
return s
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.Grouping {
var skeleton : String {
switch self.option {
case .automatic:
// This is the default, so no need to set it
return ""
case .hidden:
return "group-off"
}
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.SignDisplayStrategy {
var skeleton : String {
let stem: String
switch positive {
case .always:
switch zero {
case .always:
stem = "sign-always"
case .hidden:
stem = "sign-except-zero"
}
case .hidden:
switch negative {
case .always:
stem = "sign-auto"
case .hidden:
stem = "sign-never"
}
}
return stem
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.DecimalSeparatorDisplayStrategy {
var skeleton : String {
let stem: String
switch option {
case .always:
stem = "decimal-always"
case .automatic:
stem = "decimal-auto"
}
return stem
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.RoundingRule {
var skeleton : String {
var stem: String
switch self {
case .awayFromZero:
stem = "rounding-mode-up"
case .toNearestOrAwayFromZero:
stem = "rounding-mode-half-up"
case .toNearestOrEven:
stem = "rounding-mode-half-even"
case .up:
stem = "rounding-mode-ceiling"
case .down:
stem = "rounding-mode-floor"
case .towardZero:
stem = "rounding-mode-down"
@unknown default:
stem = ""
}
return stem
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.Notation {
var skeleton : String {
let stem: String
switch self.option {
case .scientific:
stem = "scientific"
case .automatic:
stem = ""
case .compactName:
stem = "compact-short"
}
return stem
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NumberFormatStyleConfiguration.Scale {
var skeleton : String {
return "scale/\(Decimal(self))"
}
}
// MARK: - ICU compatibility: CurrencyFormatStyleConfiguration
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension CurrencyFormatStyleConfiguration.Collection {
var skeleton: String {
var s = presentation.skeleton + " "
if let scale = scale {
s += scale.skeleton + " "
}
if let precision = precision, let roundingIncrement = roundingIncrement {
s += precision.skeletonWithRoundingIncrement(stem: roundingIncrement.skeleton)
} else if let precision = precision {
s += precision.skeleton + " "
} else if let roundingIncrement = roundingIncrement {
s += roundingIncrement.skeleton + " "
}
if let group = group {
s += group.skeleton
}
if let signDisplayStrategy = signDisplayStrategy {
s += signDisplayStrategy.skeleton + " "
}
if let decimalSeparatorStrategy = decimalSeparatorStrategy {
s += decimalSeparatorStrategy.skeleton + " "
}
if let rounding = rounding {
s += rounding.skeleton + " "
}
if let notation = notation {
s += notation.skeleton + " "
}
return s._trimmingWhitespace()
}
var icuNumberFormatStyle: UNumberFormatStyle {
if signDisplayStrategy?.accounting == true {
return .currencyAccounting
}
switch presentation.option {
case .narrow:
return .currencyNarrow
case .standard:
return .currencyStandard
case .isoCode:
return .currencyISO
case .fullName:
return .currencyFullName
}
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension CurrencyFormatStyleConfiguration.SignDisplayStrategy {
var skeleton: String {
var stem: String
if accounting {
switch positive {
case .always:
switch zero {
case .always:
stem = "sign-accounting-always"
case .hidden:
stem = "sign-accounting-except-zero"
}
case .hidden:
stem = "sign-accounting"
}
} else {
switch positive {
case .always:
switch zero {
case .always:
stem = "sign-always"
case .hidden:
stem = "sign-except-zero"
}
case .hidden:
switch negative {
case .always:
stem = "sign-auto"
case .hidden:
stem = "sign-never"
}
}
}
return stem
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension CurrencyFormatStyleConfiguration.Presentation {
var skeleton: String {
var stem: String
switch option {
case .narrow:
stem = "unit-width-narrow"
case .standard:
stem = "unit-width-short"
case .isoCode:
stem = "unit-width-iso-code"
case .fullName:
stem = "unit-width-full-name"
}
return stem
}
}
|