11//
2- // BinaryDistinctString .swift
2+ // BinaryDistinct .swift
33// swift-transformers
44//
55// Created by Piotr Kowalczuk on 06.03.25.
@@ -12,44 +12,44 @@ public struct BinaryDistinctString: Equatable, Hashable, Sendable, Comparable, C
1212 public let value : [ UInt16 ]
1313
1414 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
1616 }
1717
1818 public var string : String {
19- return String ( self . nsString)
19+ String ( nsString)
2020 }
2121
2222 public var count : Int {
23- self . string. count
23+ string. count
2424 }
2525
2626 /// Satisfies ``CustomStringConvertible`` protocol.
2727 public var description : String {
28- return self . string
28+ string
2929 }
3030
3131 public init ( _ bytes: [ UInt16 ] ) {
32- self . value = bytes
32+ value = bytes
3333 }
3434
3535 public init ( _ str: NSString ) {
36- self . value = Array ( str as String ) . flatMap { $0. utf16 }
36+ value = Array ( str as String ) . flatMap { $0. utf16 }
3737 }
3838
3939 public init ( _ str: String ) {
4040 self . init ( str as NSString )
4141 }
4242
4343 public init ( _ character: BinaryDistinctCharacter ) {
44- self . value = character. bytes
44+ value = character. bytes
4545 }
4646
4747 public init ( _ characters: [ BinaryDistinctCharacter ] ) {
4848 var data : [ UInt16 ] = [ ]
4949 for character in characters {
5050 data. append ( contentsOf: character. bytes)
5151 }
52- self . value = data
52+ value = data
5353 }
5454
5555 /// Satisfies ``ExpressibleByStringLiteral`` protocol.
@@ -58,59 +58,59 @@ public struct BinaryDistinctString: Equatable, Hashable, Sendable, Comparable, C
5858 }
5959
6060 public static func == ( lhs: BinaryDistinctString , rhs: BinaryDistinctString ) -> Bool {
61- return lhs. value == rhs. value
61+ lhs. value == rhs. value
6262 }
6363
6464 public static func < ( lhs: BinaryDistinctString , rhs: BinaryDistinctString ) -> Bool {
65- return lhs. value. lexicographicallyPrecedes ( rhs. value)
65+ lhs. value. lexicographicallyPrecedes ( rhs. value)
6666 }
6767
6868 public static func + ( lhs: BinaryDistinctString , rhs: BinaryDistinctString ) -> BinaryDistinctString {
69- return BinaryDistinctString ( lhs. value + rhs. value)
69+ BinaryDistinctString ( lhs. value + rhs. value)
7070 }
7171
7272 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)
7575 }
7676
7777 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
8080 }
8181
8282 public func lowercased( ) -> BinaryDistinctString {
83- . init( self . string. lowercased ( ) )
83+ . init( string. lowercased ( ) )
8484 }
8585
8686 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) )
8888 }
8989}
9090
91- extension BinaryDistinctString {
92- public typealias Index = Int // Treat indices as integers
91+ public extension BinaryDistinctString {
92+ typealias Index = Int // Treat indices as integers
9393
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 }
9696
97- public func index( _ i: Index , offsetBy distance: Int ) -> Index {
97+ func index( _ i: Index , offsetBy distance: Int ) -> Index {
9898 let newIndex = i + distance
99- guard newIndex >= 0 , newIndex <= self . count else {
99+ guard newIndex >= 0 , newIndex <= count else {
100100 fatalError ( " Index out of bounds " )
101101 }
102102 return newIndex
103103 }
104104
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 ? {
106106 let newIndex = i + distance
107107 return newIndex <= limit ? newIndex : nil
108108 }
109109}
110110
111111extension BinaryDistinctString : Sequence {
112112 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
114114
115115 return AnyIterator {
116116 guard let char = iterator. next ( ) else { return nil }
@@ -119,104 +119,100 @@ extension BinaryDistinctString: Sequence {
119119 }
120120}
121121
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]
128126 }
129127
130128 /// 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+ }
136133
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
151147 }
148+ }
152149
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
156153
157- let slicedBytes = Array ( utf8Bytes [ startByteIndex..< endByteIndex] )
158- return BinaryDistinctString ( slicedBytes)
159- }
154+ let slicedBytes = Array ( utf8Bytes [ startByteIndex..< endByteIndex] )
155+ return BinaryDistinctString ( slicedBytes)
160156 }
161157}
162158
163- extension Dictionary where Key == BinaryDistinctString {
159+ public extension Dictionary where Key == BinaryDistinctString {
164160 /// 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)
167163 }
168164
169165 /// 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 } ) {
171167 let converted = Dictionary ( uniqueKeysWithValues: other. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
172- self . merge ( converted, uniquingKeysWith: strategy)
168+ merge ( converted, uniquingKeysWith: strategy)
173169 }
174170
175171 /// 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 } ) {
177173 let converted = Dictionary ( uniqueKeysWithValues: other. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
178- self . merge ( converted, uniquingKeysWith: strategy)
174+ merge ( converted, uniquingKeysWith: strategy)
179175 }
180176
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 {
182178 var newDict = self
183179 newDict. merge ( other, strategy: strategy)
184180 return newDict
185181 }
186182
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 {
188184 var newDict = self
189185 newDict. merge ( other, strategy: strategy)
190186 return newDict
191187 }
192188
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 {
194190 var newDict = self
195191 newDict. merge ( other, strategy: strategy)
196192 return newDict
197193 }
198194}
199195
200- public protocol StringConvertible : ExpressibleByStringLiteral { }
196+ public protocol StringConvertible : ExpressibleByStringLiteral { }
201197
202- extension BinaryDistinctString : StringConvertible { }
203- extension String : StringConvertible { }
204- extension NSString : StringConvertible { }
198+ extension BinaryDistinctString : StringConvertible { }
199+ extension String : StringConvertible { }
200+ extension NSString : StringConvertible { }
205201
206202public struct BinaryDistinctCharacter : Equatable , Hashable , CustomStringConvertible , ExpressibleByStringLiteral {
207203 let bytes : [ UInt16 ]
208204
209205 public init ( _ character: Character ) {
210- self . bytes = Array ( character. utf16)
206+ bytes = Array ( character. utf16)
211207 }
212208
213209 public init ( _ string: String ) {
214- self . bytes = Array ( string. utf16)
210+ bytes = Array ( string. utf16)
215211 }
216212
217213 public init ( _ nsString: NSString ) {
218214 let swiftString = nsString as String
219- self . bytes = Array ( swiftString. utf16)
215+ bytes = Array ( swiftString. utf16)
220216 }
221217
222218 public init ( bytes: [ UInt16 ] ) {
@@ -229,14 +225,14 @@ public struct BinaryDistinctCharacter: Equatable, Hashable, CustomStringConverti
229225 }
230226
231227 var stringValue : String ? {
232- String ( utf16CodeUnits: self . bytes, count: self . bytes. count)
228+ String ( utf16CodeUnits: bytes, count: bytes. count)
233229 }
234230
235231 public var description : String {
236232 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) } ) ) "
238234 } 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) } ) ) "
240236 }
241237 }
242238
0 commit comments