|
14 | 14 | internal import _ForSwiftFoundation
|
15 | 15 | #endif // FOUNDATION_FRAMEWORK
|
16 | 16 |
|
17 |
| -/* |
18 |
| - // Could be silently inexact for float and double. |
19 |
| - extension Scanner { |
20 |
| - |
21 |
| - public func scanDecimal(_ dcm: inout Decimal) -> Bool { |
22 |
| - if let result = scanDecimal() { |
23 |
| - dcm = result |
24 |
| - return true |
25 |
| - } else { |
26 |
| - return false |
27 |
| - } |
28 |
| - } |
29 |
| - |
30 |
| - public func scanDecimal() -> Decimal? { |
31 |
| - |
32 |
| - var result = Decimal.zero |
33 |
| - let string = self._scanString |
34 |
| - let length = string.length |
35 |
| - var buf = _NSStringBuffer(string: string, start: self._scanLocation, end: length) |
36 |
| - var tooBig = false |
37 |
| - let ds = (locale as? Locale ?? Locale.current).decimalSeparator?.first ?? Character(".") |
38 |
| - buf.skip(_skipSet) |
39 |
| - var neg = false |
40 |
| - var ok = false |
41 |
| - |
42 |
| - if buf.currentCharacter == unichar(unicodeScalarLiteral: "-") || buf.currentCharacter == unichar(unicodeScalarLiteral: "+") { |
43 |
| - ok = true |
44 |
| - neg = buf.currentCharacter == unichar(unicodeScalarLiteral: "-") |
45 |
| - buf.advance() |
46 |
| - buf.skip(_skipSet) |
47 |
| - } |
48 |
| - |
49 |
| - // build the mantissa |
50 |
| - while let numeral = decimalValue(buf.currentCharacter) { |
51 |
| - ok = true |
52 |
| - if tooBig || multiplyBy10(&result,andAdd:numeral) != .noError { |
53 |
| - tooBig = true |
54 |
| - if result._exponent == Int32(Int8.max) { |
55 |
| - repeat { |
56 |
| - buf.advance() |
57 |
| - } while decimalValue(buf.currentCharacter) != nil |
58 |
| - return nil |
59 |
| - } |
60 |
| - result._exponent += 1 |
61 |
| - } |
62 |
| - buf.advance() |
63 |
| - } |
64 |
| - |
65 |
| - // get the decimal point |
66 |
| - if let us = UnicodeScalar(buf.currentCharacter), Character(us) == ds { |
67 |
| - ok = true |
68 |
| - buf.advance() |
69 |
| - // continue to build the mantissa |
70 |
| - while let numeral = decimalValue(buf.currentCharacter) { |
71 |
| - if tooBig || multiplyBy10(&result,andAdd:numeral) != .noError { |
72 |
| - tooBig = true |
73 |
| - } else { |
74 |
| - if result._exponent == Int32(Int8.min) { |
75 |
| - repeat { |
76 |
| - buf.advance() |
77 |
| - } while decimalValue(buf.currentCharacter) != nil |
78 |
| - return nil |
79 |
| - } |
80 |
| - result._exponent -= 1 |
81 |
| - } |
82 |
| - buf.advance() |
83 |
| - } |
84 |
| - } |
85 |
| - |
86 |
| - if buf.currentCharacter == unichar(unicodeScalarLiteral: "e") || buf.currentCharacter == unichar(unicodeScalarLiteral: "E") { |
87 |
| - ok = true |
88 |
| - var exponentIsNegative = false |
89 |
| - var exponent: Int32 = 0 |
90 |
| - |
91 |
| - buf.advance() |
92 |
| - if buf.currentCharacter == unichar(unicodeScalarLiteral: "-") { |
93 |
| - exponentIsNegative = true |
94 |
| - buf.advance() |
95 |
| - } else if buf.currentCharacter == unichar(unicodeScalarLiteral: "+") { |
96 |
| - buf.advance() |
97 |
| - } |
98 |
| - |
99 |
| - while let numeral = decimalValue(buf.currentCharacter) { |
100 |
| - exponent = 10 * exponent + Int32(numeral) |
101 |
| - guard exponent <= 2*Int32(Int8.max) else { |
102 |
| - return nil |
103 |
| - } |
104 |
| - |
105 |
| - buf.advance() |
106 |
| - } |
107 |
| - |
108 |
| - if exponentIsNegative { |
109 |
| - exponent = -exponent |
110 |
| - } |
111 |
| - exponent += result._exponent |
112 |
| - guard exponent >= Int32(Int8.min) && exponent <= Int32(Int8.max) else { |
113 |
| - return nil |
114 |
| - } |
115 |
| - result._exponent = exponent |
116 |
| - } |
117 |
| - |
118 |
| - // No valid characters have been seen upto this point so error out. |
119 |
| - guard ok == true else { return nil } |
120 |
| - |
121 |
| - result.isNegative = neg |
122 |
| - |
123 |
| - // if we get to this point, and have NaN, then the input string was probably "-0" |
124 |
| - // or some variation on that, and normalize that to zero. |
125 |
| - if result.isNaN { |
126 |
| - result = Decimal(0) |
127 |
| - } |
128 |
| - |
129 |
| - result.compact() |
130 |
| - self._scanLocation = buf.location |
131 |
| - return result |
132 |
| - } |
133 |
| - |
134 |
| - // Copied from Scanner.swift |
135 |
| - private func decimalValue(_ ch: unichar) -> Int? { |
136 |
| - guard let s = UnicodeScalar(ch), s.isASCII else { return nil } |
137 |
| - return Character(s).wholeNumberValue |
138 |
| - } |
139 |
| - } |
140 |
| - */ |
141 | 17 | @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
|
142 | 18 | extension Decimal : CustomStringConvertible {
|
143 | 19 | public init?(string: __shared String, locale: __shared Locale? = nil) {
|
|
0 commit comments