-
Notifications
You must be signed in to change notification settings - Fork 1
/
ANKFullWidth+Text+Radix.swift
157 lines (134 loc) · 7.72 KB
/
ANKFullWidth+Text+Radix.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
//=----------------------------------------------------------------------------=
// This source file is part of the AwesomeNumbersKit open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
import ANKCoreKit
//*============================================================================*
// MARK: * ANK x Full Width x Text x Radix
//*============================================================================*
extension ANKFullWidth {
//=------------------------------------------------------------------------=
// MARK: Details x Decode
//=------------------------------------------------------------------------=
@inlinable public init?(_ description: some StringProtocol, radix: Int = 10) {
var description = String(description)
let value: Optional<Self> = description.withUTF8 { utf8 in
let (radix) = AnyRadixUIntRoot(radix)
let (sign,body) = ANK.integerComponents(utf8: utf8)
let (digits) = ANK.UnsafeUTF8(rebasing: body)
let (magnitude) = Magnitude(digits: digits, radix: radix)
return magnitude.flatMap({ Self.exactly(sign: sign, magnitude: $0) })
}
if let value { self = value } else { return nil }
}
//=------------------------------------------------------------------------=
// MARK: Details x Encode
//=------------------------------------------------------------------------=
@inlinable public func description(radix: Int = 10, uppercase: Bool = false) -> String {
Swift.withUnsafePointer(to: UInt8(ascii: "-")) { minus in
let radix = AnyRadixUIntRoot(radix)
let alphabet = MaxRadixAlphabetEncoder(uppercase: uppercase)
let prefix = ANK.UnsafeUTF8(start: minus, count: Int(bit: self.isLessThanZero))
let suffix = ANK.UnsafeUTF8(start: nil, count: Int.zero)
return self.magnitude.description(radix: radix, alphabet: alphabet, prefix: prefix, suffix: suffix)
}
}
}
//=----------------------------------------------------------------------------=
// MARK: + Unsigned
//=----------------------------------------------------------------------------=
extension ANKFullWidth where High == High.Magnitude {
//=------------------------------------------------------------------------=
// MARK: Details x Decode x Private
//=------------------------------------------------------------------------=
@inlinable init?(digits: ANK.UnsafeUTF8, radix: AnyRadixUIntRoot) {
switch radix.power.isZero {
case true: self.init(digits: digits, radix: PerfectRadixUIntRoot(unchecked: radix))
case false: self.init(digits: digits, radix: ImperfectRadixUIntRoot(unchecked: radix)) }
}
@inlinable init?(digits: ANK.UnsafeUTF8, radix: PerfectRadixUIntRoot) {
guard !digits.isEmpty else { return nil }
//=--------------------------------------=
var digits = digits.drop(while:{ $0 == 48 })
//=--------------------------------------=
var error = false
let value = Self.uninitialized { value in
for index in value.indices {
if digits.isEmpty {
value[index] = UInt.zero
} else {
let chunk = ANK.UnsafeUTF8(rebasing: ANK.removeSuffix(from: &digits, maxLength: radix.exponent))
guard let word = UInt.truncating(digits: chunk, radix: radix.base) else { return error = true }
value[index] = word
}
}
}
if !error, digits.isEmpty { self = value } else { return nil }
}
@inlinable init?(digits: ANK.UnsafeUTF8, radix: ImperfectRadixUIntRoot) {
guard !digits.isEmpty else { return nil }
//=--------------------------------------=
var digits = digits.drop(while:{ $0 == 48 })
let alignment = digits.count % radix.exponent
//=--------------------------------------=
self.init()
guard let _ = { // this closure makes it 10% faster for some reason
forwards: if !alignment.isZero {
let chunk = ANK.UnsafeUTF8(rebasing: ANK.removePrefix(from: &digits, count: alignment))
guard let word = UInt.truncating(digits: chunk, radix: radix.base) else { return nil }
self.first = word
}
forwards: while !digits.isEmpty {
let chunk = ANK.UnsafeUTF8(rebasing: ANK.removePrefix(from: &digits, count: radix.exponent))
guard let word = UInt.truncating(digits: chunk, radix: radix.base) else { return nil }
guard !self.multiplyReportingOverflow(by: radix.power) else { return nil }
guard !self.addReportingOverflow(word)/*------------*/ else { return nil }
}
}() as Void? else { return nil }
}
//=------------------------------------------------------------------------=
// MARK: Details x Encode x Private
//=------------------------------------------------------------------------=
@inlinable func description(radix: AnyRadixUIntRoot, alphabet: MaxRadixAlphabetEncoder, prefix: ANK.UnsafeUTF8, suffix: ANK.UnsafeUTF8) -> String {
switch radix.power.isZero {
case true: return self.description(radix: PerfectRadixUIntRoot(unchecked: radix), alphabet: alphabet, prefix: prefix, suffix: suffix)
case false: return self.description(radix: ImperfectRadixUIntRoot(unchecked: radix), alphabet: alphabet, prefix: prefix, suffix: suffix) }
}
@inlinable func description(radix: PerfectRadixUIntRoot, alphabet: MaxRadixAlphabetEncoder, prefix: ANK.UnsafeUTF8, suffix: ANK.UnsafeUTF8) -> String {
//=--------------------------------------=
// with one buffer pointer specialization
//=--------------------------------------=
self.withContiguousStorage { buffer in
let index = buffer.lastIndex(where:{ !$0.isZero }) ?? buffer.startIndex
let chunks = ANK.UnsafeWords(rebasing: buffer[...index])
return String.fromUTF8Unchecked(chunks: chunks, radix: radix, alphabet: alphabet, prefix: prefix, suffix: suffix)
}
}
@inlinable func description(radix: ImperfectRadixUIntRoot, alphabet: MaxRadixAlphabetEncoder, prefix: ANK.UnsafeUTF8, suffix: ANK.UnsafeUTF8) -> String {
//=--------------------------------------=
// with one buffer pointer specialization
//=--------------------------------------=
let capacity: Int = radix.divisibilityByPowerUpperBound(self)
return Swift.withUnsafeTemporaryAllocation(of: UInt.self, capacity: capacity) { buffer in
//=----------------------------------=
// de/init: pointee is trivial
//=----------------------------------=
var magnitude: Magnitude = self
var index: Int = buffer.startIndex
//=----------------------------------=
rebasing: repeat {
let (remainder, overflow) = magnitude.formQuotientWithRemainderReportingOverflow(dividingBy: radix.power)
buffer[index] = remainder
buffer.formIndex(after: &index)
assert(!overflow)
} while !magnitude.isZero
//=----------------------------------=
let chunks = ANK.UnsafeWords(rebasing: buffer[..<index])
return String.fromUTF8Unchecked(chunks: chunks, radix: radix, alphabet: alphabet, prefix: prefix, suffix: suffix)
}
}
}