1
1
//
2
- // BinaryDistinctString .swift
2
+ // BinaryDistinct .swift
3
3
// swift-transformers
4
4
//
5
5
// Created by Piotr Kowalczuk on 06.03.25.
@@ -12,44 +12,44 @@ public struct BinaryDistinctString: Equatable, Hashable, Sendable, Comparable, C
12
12
public let value : [ UInt16 ]
13
13
14
14
public var nsString : NSString {
15
- return String ( utf16CodeUnits: self . value, count: self . value. count) as NSString
15
+ String ( utf16CodeUnits: value, count: value. count) as NSString
16
16
}
17
17
18
18
public var string : String {
19
- return String ( self . nsString)
19
+ String ( nsString)
20
20
}
21
21
22
22
public var count : Int {
23
- self . string. count
23
+ string. count
24
24
}
25
25
26
26
/// Satisfies ``CustomStringConvertible`` protocol.
27
27
public var description : String {
28
- return self . string
28
+ string
29
29
}
30
30
31
31
public init ( _ bytes: [ UInt16 ] ) {
32
- self . value = bytes
32
+ value = bytes
33
33
}
34
34
35
35
public init ( _ str: NSString ) {
36
- self . value = Array ( str as String ) . flatMap { $0. utf16 }
36
+ value = Array ( str as String ) . flatMap { $0. utf16 }
37
37
}
38
38
39
39
public init ( _ str: String ) {
40
40
self . init ( str as NSString )
41
41
}
42
42
43
43
public init ( _ character: BinaryDistinctCharacter ) {
44
- self . value = character. bytes
44
+ value = character. bytes
45
45
}
46
46
47
47
public init ( _ characters: [ BinaryDistinctCharacter ] ) {
48
48
var data : [ UInt16 ] = [ ]
49
49
for character in characters {
50
50
data. append ( contentsOf: character. bytes)
51
51
}
52
- self . value = data
52
+ value = data
53
53
}
54
54
55
55
/// Satisfies ``ExpressibleByStringLiteral`` protocol.
@@ -58,59 +58,59 @@ public struct BinaryDistinctString: Equatable, Hashable, Sendable, Comparable, C
58
58
}
59
59
60
60
public static func == ( lhs: BinaryDistinctString , rhs: BinaryDistinctString ) -> Bool {
61
- return lhs. value == rhs. value
61
+ lhs. value == rhs. value
62
62
}
63
63
64
64
public static func < ( lhs: BinaryDistinctString , rhs: BinaryDistinctString ) -> Bool {
65
- return lhs. value. lexicographicallyPrecedes ( rhs. value)
65
+ lhs. value. lexicographicallyPrecedes ( rhs. value)
66
66
}
67
67
68
68
public static func + ( lhs: BinaryDistinctString , rhs: BinaryDistinctString ) -> BinaryDistinctString {
69
- return BinaryDistinctString ( lhs. value + rhs. value)
69
+ BinaryDistinctString ( lhs. value + rhs. value)
70
70
}
71
71
72
72
public func hasPrefix( _ prefix: BinaryDistinctString ) -> Bool {
73
- guard prefix. value. count <= self . value. count else { return false }
74
- return self . value. starts ( with: prefix. value)
73
+ guard prefix. value. count <= value. count else { return false }
74
+ return value. starts ( with: prefix. value)
75
75
}
76
76
77
77
public func hasSuffix( _ suffix: BinaryDistinctString ) -> Bool {
78
- guard suffix. value. count <= self . value. count else { return false }
79
- return self . value. suffix ( suffix. value. count) == suffix. value
78
+ guard suffix. value. count <= value. count else { return false }
79
+ return value. suffix ( suffix. value. count) == suffix. value
80
80
}
81
81
82
82
public func lowercased( ) -> BinaryDistinctString {
83
- . init( self . string. lowercased ( ) )
83
+ . init( string. lowercased ( ) )
84
84
}
85
85
86
86
public func replacingOccurrences( of: Self , with: Self ) -> BinaryDistinctString {
87
- return BinaryDistinctString ( self . string. replacingOccurrences ( of: of. string, with: with. string) )
87
+ BinaryDistinctString ( string. replacingOccurrences ( of: of. string, with: with. string) )
88
88
}
89
89
}
90
90
91
- extension BinaryDistinctString {
92
- public typealias Index = Int // Treat indices as integers
91
+ public extension BinaryDistinctString {
92
+ typealias Index = Int // Treat indices as integers
93
93
94
- public var startIndex : Index { return 0 }
95
- public var endIndex : Index { return self . count }
94
+ var startIndex : Index { 0 }
95
+ var endIndex : Index { count }
96
96
97
- public func index( _ i: Index , offsetBy distance: Int ) -> Index {
97
+ func index( _ i: Index , offsetBy distance: Int ) -> Index {
98
98
let newIndex = i + distance
99
- guard newIndex >= 0 , newIndex <= self . count else {
99
+ guard newIndex >= 0 , newIndex <= count else {
100
100
fatalError ( " Index out of bounds " )
101
101
}
102
102
return newIndex
103
103
}
104
104
105
- public func index( _ i: Index , offsetBy distance: Int , limitedBy limit: Index ) -> Index ? {
105
+ func index( _ i: Index , offsetBy distance: Int , limitedBy limit: Index ) -> Index ? {
106
106
let newIndex = i + distance
107
107
return newIndex <= limit ? newIndex : nil
108
108
}
109
109
}
110
110
111
111
extension BinaryDistinctString : Sequence {
112
112
public func makeIterator( ) -> AnyIterator < BinaryDistinctCharacter > {
113
- var iterator = self . string. makeIterator ( ) // Use native Swift String iterator
113
+ var iterator = string. makeIterator ( ) // Use native Swift String iterator
114
114
115
115
return AnyIterator {
116
116
guard let char = iterator. next ( ) else { return nil }
@@ -119,104 +119,100 @@ extension BinaryDistinctString: Sequence {
119
119
}
120
120
}
121
121
122
- extension BinaryDistinctString {
123
- public subscript( bounds: PartialRangeFrom < Int > ) -> BinaryDistinctString {
124
- get {
125
- let validRange = bounds. lowerBound..< self . value. count // Convert to Range<Int>
126
- return self [ validRange]
127
- }
122
+ public extension BinaryDistinctString {
123
+ subscript( bounds: PartialRangeFrom < Int > ) -> BinaryDistinctString {
124
+ let validRange = bounds. lowerBound..< value. count // Convert to Range<Int>
125
+ return self [ validRange]
128
126
}
129
127
130
128
/// Returns a slice of the `BinaryDistinctString` while ensuring correct rune (grapheme cluster) boundaries.
131
- public subscript( bounds: Range < Int > ) -> BinaryDistinctString {
132
- get {
133
- guard bounds. lowerBound >= 0 , bounds. upperBound <= self . count else {
134
- fatalError ( " Index out of bounds " )
135
- }
129
+ subscript( bounds: Range < Int > ) -> BinaryDistinctString {
130
+ guard bounds. lowerBound >= 0 , bounds. upperBound <= count else {
131
+ fatalError ( " Index out of bounds " )
132
+ }
136
133
137
- let utf8Bytes = self . value
138
- var byteIndices : [ Int ] = [ ]
139
-
140
- // Decode UTF-8 manually to find rune start positions
141
- var currentByteIndex = 0
142
- for (index, scalar) in self . string. unicodeScalars. enumerated ( ) {
143
- if index == bounds. lowerBound {
144
- byteIndices. append ( currentByteIndex)
145
- }
146
- currentByteIndex += scalar. utf8. count
147
- if index == bounds. upperBound - 1 {
148
- byteIndices. append ( currentByteIndex)
149
- break
150
- }
134
+ let utf8Bytes = value
135
+ var byteIndices : [ Int ] = [ ]
136
+
137
+ // Decode UTF-8 manually to find rune start positions
138
+ var currentByteIndex = 0
139
+ for (index, scalar) in string. unicodeScalars. enumerated ( ) {
140
+ if index == bounds. lowerBound {
141
+ byteIndices. append ( currentByteIndex)
142
+ }
143
+ currentByteIndex += scalar. utf8. count
144
+ if index == bounds. upperBound - 1 {
145
+ byteIndices. append ( currentByteIndex)
146
+ break
151
147
}
148
+ }
152
149
153
- // Extract the byte range
154
- let startByteIndex = byteIndices. first ?? 0
155
- let endByteIndex = byteIndices. last ?? utf8Bytes. count
150
+ // Extract the byte range
151
+ let startByteIndex = byteIndices. first ?? 0
152
+ let endByteIndex = byteIndices. last ?? utf8Bytes. count
156
153
157
- let slicedBytes = Array ( utf8Bytes [ startByteIndex..< endByteIndex] )
158
- return BinaryDistinctString ( slicedBytes)
159
- }
154
+ let slicedBytes = Array ( utf8Bytes [ startByteIndex..< endByteIndex] )
155
+ return BinaryDistinctString ( slicedBytes)
160
156
}
161
157
}
162
158
163
- extension Dictionary where Key == BinaryDistinctString {
159
+ public extension Dictionary where Key == BinaryDistinctString {
164
160
/// Merges another `BinaryDistinctDictionary` into this one
165
- public mutating func merge( _ other: [ BinaryDistinctString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
166
- self . merge ( other, uniquingKeysWith: strategy)
161
+ mutating func merge( _ other: [ BinaryDistinctString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
162
+ merge ( other, uniquingKeysWith: strategy)
167
163
}
168
164
169
165
/// Merges a `[String: Value]` dictionary into this one
170
- public mutating func merge( _ other: [ String : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
166
+ mutating func merge( _ other: [ String : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
171
167
let converted = Dictionary ( uniqueKeysWithValues: other. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
172
- self . merge ( converted, uniquingKeysWith: strategy)
168
+ merge ( converted, uniquingKeysWith: strategy)
173
169
}
174
170
175
171
/// Merges a `[NSString: Value]` dictionary into this one
176
- public mutating func merge( _ other: [ NSString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
172
+ mutating func merge( _ other: [ NSString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
177
173
let converted = Dictionary ( uniqueKeysWithValues: other. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
178
- self . merge ( converted, uniquingKeysWith: strategy)
174
+ merge ( converted, uniquingKeysWith: strategy)
179
175
}
180
176
181
- public func merging( _ other: [ String : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
177
+ func merging( _ other: [ String : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
182
178
var newDict = self
183
179
newDict. merge ( other, strategy: strategy)
184
180
return newDict
185
181
}
186
182
187
- public func merging( _ other: [ BinaryDistinctString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
183
+ func merging( _ other: [ BinaryDistinctString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
188
184
var newDict = self
189
185
newDict. merge ( other, strategy: strategy)
190
186
return newDict
191
187
}
192
188
193
- public func merging( _ other: [ NSString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
189
+ func merging( _ other: [ NSString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
194
190
var newDict = self
195
191
newDict. merge ( other, strategy: strategy)
196
192
return newDict
197
193
}
198
194
}
199
195
200
- public protocol StringConvertible : ExpressibleByStringLiteral { }
196
+ public protocol StringConvertible : ExpressibleByStringLiteral { }
201
197
202
- extension BinaryDistinctString : StringConvertible { }
203
- extension String : StringConvertible { }
204
- extension NSString : StringConvertible { }
198
+ extension BinaryDistinctString : StringConvertible { }
199
+ extension String : StringConvertible { }
200
+ extension NSString : StringConvertible { }
205
201
206
202
public struct BinaryDistinctCharacter : Equatable , Hashable , CustomStringConvertible , ExpressibleByStringLiteral {
207
203
let bytes : [ UInt16 ]
208
204
209
205
public init ( _ character: Character ) {
210
- self . bytes = Array ( character. utf16)
206
+ bytes = Array ( character. utf16)
211
207
}
212
208
213
209
public init ( _ string: String ) {
214
- self . bytes = Array ( string. utf16)
210
+ bytes = Array ( string. utf16)
215
211
}
216
212
217
213
public init ( _ nsString: NSString ) {
218
214
let swiftString = nsString as String
219
- self . bytes = Array ( swiftString. utf16)
215
+ bytes = Array ( swiftString. utf16)
220
216
}
221
217
222
218
public init ( bytes: [ UInt16 ] ) {
@@ -229,14 +225,14 @@ public struct BinaryDistinctCharacter: Equatable, Hashable, CustomStringConverti
229
225
}
230
226
231
227
var stringValue : String ? {
232
- String ( utf16CodeUnits: self . bytes, count: self . bytes. count)
228
+ String ( utf16CodeUnits: bytes, count: bytes. count)
233
229
}
234
230
235
231
public var description : String {
236
232
if let str = stringValue {
237
- return " BinaryDistinctCharacter(' \( str) ', bytes: \( bytes. map { String ( format: " 0x%02X " , $0) } ) ) "
233
+ " BinaryDistinctCharacter(' \( str) ', bytes: \( bytes. map { String ( format: " 0x%02X " , $0) } ) ) "
238
234
} else {
239
- return " BinaryDistinctCharacter(invalid UTF-8, bytes: \( bytes. map { String ( format: " 0x%02X " , $0) } ) ) "
235
+ " BinaryDistinctCharacter(invalid UTF-8, bytes: \( bytes. map { String ( format: " 0x%02X " , $0) } ) ) "
240
236
}
241
237
}
242
238
0 commit comments