forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberFormatter.swift
1426 lines (1241 loc) · 51.2 KB
/
NumberFormatter.swift
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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016, 2019 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
//
@_implementationOnly import CoreFoundation
internal import Synchronization
extension NumberFormatter {
public enum Style : UInt, Sendable {
case none = 0
case decimal = 1
case currency = 2
case percent = 3
case scientific = 4
case spellOut = 5
case ordinal = 6
case currencyISOCode = 8 // 7 is not used
case currencyPlural = 9
case currencyAccounting = 10
}
public enum PadPosition : UInt, Sendable {
case beforePrefix
case afterPrefix
case beforeSuffix
case afterSuffix
}
public enum RoundingMode : UInt, Sendable {
case ceiling
case floor
case down
case up
case halfEven
case halfDown
case halfUp
}
}
open class NumberFormatter : Formatter, @unchecked Sendable {
private let _lock: Mutex<State> = .init(.init())
public override init() {
super.init()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
private convenience init(state: State) {
self.init()
_lock.withLock {
$0 = state
}
}
open override func copy(with zone: NSZone? = nil) -> Any {
return _lock.withLock { state in
// Zone is not Sendable, so just ignore it here
let copy = state.copy()
return NumberFormatter(state: copy)
}
}
open class func localizedString(from num: NSNumber, number nstyle: Style) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = nstyle
return numberFormatter.string(for: num)!
}
// This class is not Sendable, but marking it as such was the only way to work around compiler crashes while attempting to use `~Copyable` like `DateFormatter` does.
final class State : @unchecked Sendable {
class Box {
var formatter: CFNumberFormatter?
init() {}
}
private var _formatter = Box()
// MARK: -
func copy(with zone: NSZone? = nil) -> State {
let copied = State()
copied.formattingContext = formattingContext
copied._numberStyle = _numberStyle
copied._locale = _locale
copied._generatesDecimalNumbers = _generatesDecimalNumbers
copied._textAttributesForNegativeValues = _textAttributesForNegativeValues
copied._textAttributesForPositiveValues = _textAttributesForPositiveValues
copied._allowsFloats = _allowsFloats
copied._decimalSeparator = _decimalSeparator
copied._alwaysShowsDecimalSeparator = _alwaysShowsDecimalSeparator
copied._currencyDecimalSeparator = _currencyDecimalSeparator
copied._usesGroupingSeparator = _usesGroupingSeparator
copied._groupingSeparator = _groupingSeparator
copied._zeroSymbol = _zeroSymbol
copied._textAttributesForZero = _textAttributesForZero
copied._nilSymbol = _nilSymbol
copied._textAttributesForNil = _textAttributesForNil
copied._notANumberSymbol = _notANumberSymbol
copied._textAttributesForNotANumber = _textAttributesForNotANumber
copied._positiveInfinitySymbol = _positiveInfinitySymbol
copied._textAttributesForPositiveInfinity = _textAttributesForPositiveInfinity
copied._negativeInfinitySymbol = _negativeInfinitySymbol
copied._textAttributesForNegativeInfinity = _textAttributesForNegativeInfinity
copied._positivePrefix = _positivePrefix
copied._positiveSuffix = _positiveSuffix
copied._negativePrefix = _negativePrefix
copied._negativeSuffix = _negativeSuffix
copied._currencyCode = _currencyCode
copied._currencySymbol = _currencySymbol
copied._internationalCurrencySymbol = _internationalCurrencySymbol
copied._percentSymbol = _percentSymbol
copied._perMillSymbol = _perMillSymbol
copied._minusSign = _minusSign
copied._plusSign = _plusSign
copied._exponentSymbol = _exponentSymbol
copied._groupingSize = _groupingSize
copied._secondaryGroupingSize = _secondaryGroupingSize
copied._multiplier = _multiplier
copied._formatWidth = _formatWidth
copied._paddingCharacter = _paddingCharacter
copied._paddingPosition = _paddingPosition
copied._roundingMode = _roundingMode
copied._roundingIncrement = _roundingIncrement
copied._minimumIntegerDigits = _minimumIntegerDigits
copied._maximumIntegerDigits = _maximumIntegerDigits
copied._minimumFractionDigits = _minimumFractionDigits
copied._maximumFractionDigits = _maximumFractionDigits
copied._minimum = _minimum
copied._maximum = _maximum
copied._currencyGroupingSeparator = _currencyGroupingSeparator
copied._lenient = _lenient
copied._usesSignificantDigits = _usesSignificantDigits
copied._minimumSignificantDigits = _minimumSignificantDigits
copied._maximumSignificantDigits = _maximumSignificantDigits
copied._partialStringValidationEnabled = _partialStringValidationEnabled
copied._hasThousandSeparators = _hasThousandSeparators
copied._thousandSeparator = _thousandSeparator
copied._localizesFormat = _localizesFormat
copied._positiveFormat = _positiveFormat
copied._negativeFormat = _negativeFormat
copied._roundingBehavior = _roundingBehavior
return copied
}
// MARK: -
func _reset() {
_formatter.formatter = nil
}
func formatter() -> CFNumberFormatter {
if let obj = _formatter.formatter {
return obj
} else {
let numberStyle = CFNumberFormatterStyle(rawValue: CFIndex(_numberStyle.rawValue))!
let obj = CFNumberFormatterCreate(kCFAllocatorSystemDefault, locale._cfObject, numberStyle)!
_setFormatterAttributes(obj)
if _positiveFormat != nil || _negativeFormat != nil {
var format = _positiveFormat ?? "#"
if let negative = _negativeFormat {
format.append(";")
format.append(negative)
}
CFNumberFormatterSetFormat(obj, format._cfObject)
}
_formatter.formatter = obj
return obj
}
}
private func _setFormatterAttributes(_ formatter: CFNumberFormatter) {
if numberStyle == .currency {
// Prefer currencySymbol, then currencyCode then locale.currencySymbol
if let symbol = _currencySymbol {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol._cfObject)
} else if let code = _currencyCode, code.count == 3 {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyCode, value: code._cfObject)
} else {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: locale.currencySymbol?._cfObject)
}
}
if numberStyle == .currencyISOCode {
let code = _currencyCode ?? _currencySymbol ?? locale.currencyCode ?? locale.currencySymbol
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyCode, value: code?._cfObject)
}
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterDecimalSeparator, value: _decimalSeparator?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyDecimalSeparator, value: _currencyDecimalSeparator?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterAlwaysShowDecimalSeparator, value: _alwaysShowsDecimalSeparator._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterGroupingSeparator, value: _groupingSeparator?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterUseGroupingSeparator, value: usesGroupingSeparator._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPercentSymbol, value: _percentSymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterZeroSymbol, value: _zeroSymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterNaNSymbol, value: _notANumberSymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterInfinitySymbol, value: _positiveInfinitySymbol._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMinusSign, value: _minusSign?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPlusSign, value: _plusSign?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterExponentSymbol, value: _exponentSymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMinIntegerDigits, value: _minimumIntegerDigits?._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMaxIntegerDigits, value: _maximumIntegerDigits?._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMinFractionDigits, value: _minimumFractionDigits?._bridgeToObjectiveC()._cfObject)
if minimumFractionDigits <= 0 {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMaxFractionDigits, value: maximumFractionDigits._bridgeToObjectiveC()._cfObject)
}
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterGroupingSize, value: groupingSize._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterSecondaryGroupingSize, value: _secondaryGroupingSize._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterRoundingMode, value: _roundingMode.rawValue._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterRoundingIncrement, value: _roundingIncrement?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterFormatWidth, value: _formatWidth?._bridgeToObjectiveC()._cfObject)
if self.formatWidth > 0 {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPaddingCharacter, value: _paddingCharacter?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPaddingPosition, value: _paddingPosition.rawValue._bridgeToObjectiveC()._cfObject)
} else {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPaddingCharacter, value: ""._cfObject)
}
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMultiplier, value: multiplier?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPositivePrefix, value: _positivePrefix?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPositiveSuffix, value: _positiveSuffix?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterNegativePrefix, value: _negativePrefix?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterNegativeSuffix, value: _negativeSuffix?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPerMillSymbol, value: _percentSymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterInternationalCurrencySymbol, value: _internationalCurrencySymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyGroupingSeparator, value: _currencyGroupingSeparator?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterIsLenient, value: _lenient._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterUseSignificantDigits, value: usesSignificantDigits._cfObject)
if usesSignificantDigits {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMinSignificantDigits, value: minimumSignificantDigits._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMaxSignificantDigits, value: maximumSignificantDigits._bridgeToObjectiveC()._cfObject)
}
}
private func _setFormatterAttribute(_ formatter: CFNumberFormatter, attributeName: CFString, value: AnyObject?) {
if let value = value {
CFNumberFormatterSetProperty(formatter, attributeName, value)
}
}
private func _getFormatterAttribute(attributeName: CFString) -> String? {
// This will only be a constant CFString
nonisolated(unsafe) let nonisolatedAttributeName = attributeName
return CFNumberFormatterCopyProperty(formatter(), nonisolatedAttributeName) as? String
}
private func getFormatterComponents() -> (String?, String?) {
guard let format = CFNumberFormatterGetFormat(formatter())?._swiftObject else {
return (nil, nil)
}
let components = format.components(separatedBy: ";")
let positive = _positiveFormat ?? components.first ?? "#"
let negative = _negativeFormat ?? components.last ?? "#"
return (positive, negative)
}
// MARK: - Properties
private var _numberStyle: Style = .none
var numberStyle: Style {
get {
return _numberStyle
}
set {
_reset()
_numberStyle = newValue
}
}
private var _locale: Locale = Locale.current
var locale: Locale! {
get {
return _locale
}
set {
_reset()
_locale = newValue
}
}
private var _generatesDecimalNumbers: Bool = false
var generatesDecimalNumbers: Bool {
get {
return _generatesDecimalNumbers
}
set {
_reset()
_generatesDecimalNumbers = newValue
}
}
private var _textAttributesForNegativeValues: [String : (Any & Sendable)]?
var textAttributesForNegativeValues: [String : (Any & Sendable)]? {
get {
return _textAttributesForNegativeValues
}
set {
_reset()
_textAttributesForNegativeValues = newValue
}
}
private var _textAttributesForPositiveValues: [String : (Any & Sendable)]?
var textAttributesForPositiveValues: [String : (Any & Sendable)]? {
get {
return _textAttributesForPositiveValues
}
set {
_reset()
_textAttributesForPositiveValues = newValue
}
}
private var _allowsFloats: Bool = true
var allowsFloats: Bool {
get {
return _allowsFloats
}
set {
_reset()
_allowsFloats = newValue
}
}
private var _decimalSeparator: String!
var decimalSeparator: String! {
get {
return _decimalSeparator ?? _getFormatterAttribute(attributeName: kCFNumberFormatterDecimalSeparator)
}
set {
_reset()
_decimalSeparator = newValue
}
}
private var _alwaysShowsDecimalSeparator: Bool = false
var alwaysShowsDecimalSeparator: Bool {
get {
return _alwaysShowsDecimalSeparator
}
set {
_reset()
_alwaysShowsDecimalSeparator = newValue
}
}
private var _currencyDecimalSeparator: String!
var currencyDecimalSeparator: String! {
get {
return _currencyDecimalSeparator ?? _getFormatterAttribute(attributeName: kCFNumberFormatterCurrencyDecimalSeparator)
}
set {
_reset()
_currencyDecimalSeparator = newValue
}
}
private var _usesGroupingSeparator: Bool?
var usesGroupingSeparator: Bool {
get {
return _usesGroupingSeparator ?? defaultUsesGroupingSeparator()
}
set {
_reset()
_usesGroupingSeparator = newValue
}
}
private var _groupingSeparator: String!
var groupingSeparator: String! {
get {
return _groupingSeparator ?? _getFormatterAttribute(attributeName: kCFNumberFormatterGroupingSeparator)
}
set {
_reset()
_groupingSeparator = newValue
}
}
private var _zeroSymbol: String?
var zeroSymbol: String? {
get {
return _zeroSymbol
}
set {
_reset()
_zeroSymbol = newValue
}
}
private var _textAttributesForZero: [String : (Any & Sendable)]?
var textAttributesForZero: [String : (Any & Sendable)]? {
get {
return _textAttributesForZero
}
set {
_reset()
_textAttributesForZero = newValue
}
}
private var _nilSymbol: String = ""
var nilSymbol: String {
get {
return _nilSymbol
}
set {
_reset()
_nilSymbol = newValue
}
}
private var _textAttributesForNil: [String : (Any & Sendable)]?
var textAttributesForNil: [String : (Any & Sendable)]? {
get {
return _textAttributesForNil
}
set {
_reset()
_textAttributesForNil = newValue
}
}
private var _notANumberSymbol: String!
var notANumberSymbol: String! {
get {
return _notANumberSymbol ?? _getFormatterAttribute(attributeName: kCFNumberFormatterNaNSymbol)
}
set {
_reset()
_notANumberSymbol = newValue
}
}
private var _textAttributesForNotANumber: [String : (Any & Sendable)]?
var textAttributesForNotANumber: [String : (Any & Sendable)]? {
get {
return _textAttributesForNotANumber
}
set {
_reset()
_textAttributesForNotANumber = newValue
}
}
private var _positiveInfinitySymbol: String = "+∞"
var positiveInfinitySymbol: String {
get {
return _positiveInfinitySymbol
}
set {
_reset()
_positiveInfinitySymbol = newValue
}
}
private var _textAttributesForPositiveInfinity: [String : (Any & Sendable)]?
var textAttributesForPositiveInfinity: [String : (Any & Sendable)]? {
get {
return _textAttributesForPositiveInfinity
}
set {
_reset()
_textAttributesForPositiveInfinity = newValue
}
}
private var _negativeInfinitySymbol: String = "-∞"
var negativeInfinitySymbol: String {
get {
return _negativeInfinitySymbol
}
set {
_reset()
_negativeInfinitySymbol = newValue
}
}
private var _textAttributesForNegativeInfinity: [String : (Any & Sendable)]?
var textAttributesForNegativeInfinity: [String : (Any & Sendable)]? {
get {
return _textAttributesForNegativeInfinity
}
set {
_reset()
_textAttributesForNegativeInfinity = newValue
}
}
private var _positivePrefix: String!
var positivePrefix: String! {
get {
return _positivePrefix ?? _getFormatterAttribute(attributeName: kCFNumberFormatterPositivePrefix)
}
set {
_reset()
_positivePrefix = newValue
}
}
private var _positiveSuffix: String!
var positiveSuffix: String! {
get {
return _positiveSuffix ?? _getFormatterAttribute(attributeName: kCFNumberFormatterPositiveSuffix)
}
set {
_reset()
_positiveSuffix = newValue
}
}
private var _negativePrefix: String!
var negativePrefix: String! {
get {
return _negativePrefix ?? _getFormatterAttribute(attributeName: kCFNumberFormatterNegativePrefix)
}
set {
_reset()
_negativePrefix = newValue
}
}
private var _negativeSuffix: String!
var negativeSuffix: String! {
get {
return _negativeSuffix ?? _getFormatterAttribute(attributeName: kCFNumberFormatterNegativeSuffix)
}
set {
_reset()
_negativeSuffix = newValue
}
}
private var _currencyCode: String!
var currencyCode: String! {
get {
return _currencyCode ?? _getFormatterAttribute(attributeName: kCFNumberFormatterCurrencyCode)
}
set {
_reset()
_currencyCode = newValue
}
}
private var _currencySymbol: String!
var currencySymbol: String! {
get {
return _currencySymbol ?? _getFormatterAttribute(attributeName: kCFNumberFormatterCurrencySymbol)
}
set {
_reset()
_currencySymbol = newValue
}
}
private var _internationalCurrencySymbol: String!
var internationalCurrencySymbol: String! {
get {
return _internationalCurrencySymbol ?? _getFormatterAttribute(attributeName: kCFNumberFormatterInternationalCurrencySymbol)
}
set {
_reset()
_internationalCurrencySymbol = newValue
}
}
private var _percentSymbol: String!
var percentSymbol: String! {
get {
return _percentSymbol ?? _getFormatterAttribute(attributeName: kCFNumberFormatterPercentSymbol) ?? "%"
}
set {
_reset()
_percentSymbol = newValue
}
}
private var _perMillSymbol: String!
var perMillSymbol: String! {
get {
return _perMillSymbol ?? _getFormatterAttribute(attributeName: kCFNumberFormatterPerMillSymbol)
}
set {
_reset()
_perMillSymbol = newValue
}
}
private var _minusSign: String!
var minusSign: String! {
get {
return _minusSign ?? _getFormatterAttribute(attributeName: kCFNumberFormatterMinusSign)
}
set {
_reset()
_minusSign = newValue
}
}
private var _plusSign: String!
var plusSign: String! {
get {
return _plusSign ?? _getFormatterAttribute(attributeName: kCFNumberFormatterPlusSign)
}
set {
_reset()
_plusSign = newValue
}
}
private var _exponentSymbol: String!
var exponentSymbol: String! {
get {
return _exponentSymbol ?? _getFormatterAttribute(attributeName: kCFNumberFormatterExponentSymbol)
}
set {
_reset()
_exponentSymbol = newValue
}
}
private var _groupingSize: Int?
var groupingSize: Int {
get {
return _groupingSize ?? defaultGroupingSize()
}
set {
_reset()
_groupingSize = newValue
}
}
private var _secondaryGroupingSize: Int = 0
var secondaryGroupingSize: Int {
get {
return _secondaryGroupingSize
}
set {
_reset()
_secondaryGroupingSize = newValue
}
}
private var _multiplier: NSNumber?
var multiplier: NSNumber? {
get {
return _multiplier ?? defaultMultiplier()
}
set {
_reset()
_multiplier = newValue
}
}
private var _formatWidth: Int?
var formatWidth: Int {
get {
return _formatWidth ?? defaultFormatWidth()
}
set {
_reset()
_formatWidth = newValue
}
}
private var _paddingCharacter: String! = " "
var paddingCharacter: String! {
get {
return _paddingCharacter ?? _getFormatterAttribute(attributeName: kCFNumberFormatterPaddingCharacter)
}
set {
_reset()
_paddingCharacter = newValue
}
}
private var _paddingPosition: PadPosition = .beforePrefix
var paddingPosition: PadPosition {
get {
return _paddingPosition
}
set {
_reset()
_paddingPosition = newValue
}
}
private var _roundingMode: RoundingMode = .halfEven
var roundingMode: RoundingMode {
get {
return _roundingMode
}
set {
_reset()
_roundingMode = newValue
}
}
private var _roundingIncrement: NSNumber! = 0
var roundingIncrement: NSNumber! {
get {
return _roundingIncrement
}
set {
_reset()
_roundingIncrement = newValue
}
}
// Use an optional for _minimumIntegerDigits to track if the value is
// set BEFORE the .numberStyle is changed. This allows preserving a setting
// of 0.
private var _minimumIntegerDigits: Int?
var minimumIntegerDigits: Int {
get {
return _minimumIntegerDigits ?? defaultMinimumIntegerDigits()
}
set {
_reset()
_minimumIntegerDigits = newValue
}
}
private var _maximumIntegerDigits: Int?
var maximumIntegerDigits: Int {
get {
return _maximumIntegerDigits ?? defaultMaximumIntegerDigits()
}
set {
_reset()
_maximumIntegerDigits = newValue
}
}
private var _minimumFractionDigits: Int?
var minimumFractionDigits: Int {
get {
return _minimumFractionDigits ?? defaultMinimumFractionDigits()
}
set {
_reset()
_minimumFractionDigits = newValue
}
}
private var _maximumFractionDigits: Int?
var maximumFractionDigits: Int {
get {
return _maximumFractionDigits ?? defaultMaximumFractionDigits()
}
set {
_reset()
_maximumFractionDigits = newValue
}
}
private var _minimum: NSNumber?
var minimum: NSNumber? {
get {
return _minimum
}
set {
_reset()
_minimum = newValue
}
}
private var _maximum: NSNumber?
var maximum: NSNumber? {
get {
return _maximum
}
set {
_reset()
_maximum = newValue
}
}
private var _currencyGroupingSeparator: String!
var currencyGroupingSeparator: String! {
get {
return _currencyGroupingSeparator ?? _getFormatterAttribute(attributeName: kCFNumberFormatterCurrencyGroupingSeparator)
}
set {
_reset()
_currencyGroupingSeparator = newValue
}
}
private var _lenient: Bool = false
var isLenient: Bool {
get {
return _lenient
}
set {
_reset()
_lenient = newValue
}
}
private var _usesSignificantDigits: Bool?
var usesSignificantDigits: Bool {
get {
return _usesSignificantDigits ?? false
}
set {
_reset()
_usesSignificantDigits = newValue
}
}
private var _minimumSignificantDigits: Int?
var minimumSignificantDigits: Int {
get {
return _minimumSignificantDigits ?? defaultMinimumSignificantDigits()
}
set {
_reset()
_usesSignificantDigits = true
_minimumSignificantDigits = newValue
if _maximumSignificantDigits == nil && newValue > defaultMinimumSignificantDigits() {
_maximumSignificantDigits = (newValue < 1000) ? 999 : newValue
}
}
}
private var _maximumSignificantDigits: Int?
var maximumSignificantDigits: Int {
get {
return _maximumSignificantDigits ?? defaultMaximumSignificantDigits()
}
set {
_reset()
_usesSignificantDigits = true
_maximumSignificantDigits = newValue
}
}
private var _partialStringValidationEnabled: Bool = false
var isPartialStringValidationEnabled: Bool {
get {
return _partialStringValidationEnabled
}
set {
_reset()
_partialStringValidationEnabled = newValue
}
}
private var _hasThousandSeparators: Bool = false
var hasThousandSeparators: Bool {
get {
return _hasThousandSeparators
}
set {
_reset()
_hasThousandSeparators = newValue
}
}
private var _thousandSeparator: String!
var thousandSeparator: String! {
get {
return _thousandSeparator
}
set {
_reset()
_thousandSeparator = newValue
}
}
private var _localizesFormat: Bool = true
var localizesFormat: Bool {
get {
return _localizesFormat
}
set {
_reset()
_localizesFormat = newValue
}
}
private var _positiveFormat: String!
var positiveFormat: String! {
get {
return getFormatterComponents().0
}
set {
_reset()
_positiveFormat = newValue
}
}
private var _negativeFormat: String!
var negativeFormat: String! {
get {
return getFormatterComponents().1
}
set {
_reset()
_negativeFormat = newValue
}
}
private var _roundingBehavior: NSDecimalNumberHandler = NSDecimalNumberHandler.default
var roundingBehavior: NSDecimalNumberHandler {
get {
return _roundingBehavior
}
set {
_reset()
_roundingBehavior = newValue
}
}
private func getZeroFormat() -> String {
return string(from: 0) ?? "0"
}
var format: String {
get {
let (p, n) = getFormatterComponents()
let z = _zeroSymbol ?? getZeroFormat()
return "\(p ?? "(null)");\(z);\(n ?? "(null)")"
}
set {
// Special case empty string
if newValue == "" {
_positiveFormat = ""
_negativeFormat = "-"
_zeroSymbol = "0"
_reset()
} else {
let components = newValue.components(separatedBy: ";")
let count = components.count
guard count <= 3 else { return }
_reset()
_positiveFormat = components.first ?? ""
if count == 1 {
_negativeFormat = "-\(_positiveFormat ?? "")"
}
else if count == 2 {
_negativeFormat = components[1]
_zeroSymbol = getZeroFormat()
}
else if count == 3 {
_zeroSymbol = components[1]
_negativeFormat = components[2]
}
if _negativeFormat == nil {
_negativeFormat = getFormatterComponents().1
}
if _zeroSymbol == nil {
_zeroSymbol = getZeroFormat()
}
}
}
}
// this is for NSUnitFormatter
var formattingContext: Context = .unknown // default is NSFormattingContextUnknown
func string(for obj: Any) -> String? {
//we need to allow Swift's numeric types here - Int, Double et al.
guard let number = __SwiftValue.store(obj) as? NSNumber else { return nil }
return string(from: number)
}
// Even though NumberFormatter responds to the usual Formatter methods,
// here are some convenience methods which are a little more obvious.
func string(from number: NSNumber) -> String? {
return CFNumberFormatterCreateStringWithNumber(kCFAllocatorSystemDefault, formatter(), number._cfObject)._swiftObject
}
func number(from string: String) -> NSNumber? {
var range = CFRange(location: 0, length: string.length)
let number = withUnsafeMutablePointer(to: &range) { (rangePointer: UnsafeMutablePointer<CFRange>) -> NSNumber? in
let parseOption = allowsFloats ? 0 : CFNumberFormatterOptionFlags.parseIntegersOnly.rawValue
let result = CFNumberFormatterCreateNumberFromString(kCFAllocatorSystemDefault, formatter(), string._cfObject, rangePointer, parseOption)
return result?._nsObject
}
return number
}