File tree Expand file tree Collapse file tree 4 files changed +96
-0
lines changed Expand file tree Collapse file tree 4 files changed +96
-0
lines changed Original file line number Diff line number Diff line change @@ -241,3 +241,23 @@ extension ASCIICharacter {
241241 }
242242
243243}
244+
245+ extension Sequence where Element == ASCIICharacter {
246+
247+ /// Returns a new string by concatenating the elements of the sequence.
248+ public func joined( ) -> ASCIIString {
249+
250+ ASCIIString ( self )
251+
252+ }
253+
254+ /// Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.
255+ public func joined( separator: ASCIIString ) -> ASCIIString {
256+
257+ let joinedStr = map { " \( $0. characterValue) " } . joined ( separator: separator. stringValue)
258+ let joinedData = Data ( map { $0. rawData } . joined ( separator: separator. rawData) )
259+ return ASCIIString ( guaranteedASCII: joinedStr, rawData: joinedData)
260+
261+ }
262+
263+ }
Original file line number Diff line number Diff line change @@ -81,6 +81,7 @@ public struct ASCIIString: Hashable {
8181
8282 }
8383
84+ /// Returns a new `ASCIIString` instance from an `ASCIICharacter`.
8485 @inlinable
8586 public init ( _ character: ASCIICharacter ) {
8687
@@ -174,3 +175,40 @@ extension ASCIIString {
174175 }
175176
176177}
178+
179+ extension Sequence where Element == ASCIIString {
180+
181+ /// Returns a new string by concatenating the elements of the sequence.
182+ public func joined( ) -> ASCIIString {
183+
184+ let joinedStr = map { $0. stringValue } . joined ( )
185+ let joinedData = Data ( map { $0. rawData } . joined ( ) )
186+ return ASCIIString ( guaranteedASCII: joinedStr, rawData: joinedData)
187+
188+ }
189+
190+ /// Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.
191+ public func joined( separator: ASCIIString ) -> ASCIIString {
192+
193+ let joinedStr = map { $0. stringValue } . joined ( separator: separator. stringValue)
194+ let joinedData = Data ( map { $0. rawData } . joined ( separator: separator. rawData) )
195+ return ASCIIString ( guaranteedASCII: joinedStr, rawData: joinedData)
196+
197+ }
198+
199+ }
200+
201+ // MARK: - Internal Utility
202+
203+ extension ASCIIString {
204+
205+ /// Internal use only.
206+ /// Used when string and data are already known to be valid ASCII.
207+ internal init ( guaranteedASCII string: String , rawData: Data ) {
208+
209+ self . stringValue = string
210+ self . rawData = rawData
211+
212+ }
213+
214+ }
Original file line number Diff line number Diff line change @@ -166,6 +166,25 @@ class ASCIICharacterTests: XCTestCase {
166166
167167 }
168168
169+ func testJoined( ) {
170+
171+ XCTAssertEqual (
172+ [ ASCIICharacter ( " A " ) , ASCIICharacter ( " B " ) ] . joined ( ) ,
173+ ASCIIString ( " AB " )
174+ )
175+
176+ XCTAssertEqual (
177+ [ ASCIICharacter ( " A " ) , ASCIICharacter ( " B " ) ] . joined ( separator: " _ " ) ,
178+ ASCIIString ( " A_B " )
179+ )
180+
181+ XCTAssertEqual (
182+ [ ASCIICharacter ( " A " ) , ASCIICharacter ( " B " ) ] . joined ( separator: " 123 " ) ,
183+ ASCIIString ( " A123B " )
184+ )
185+
186+ }
187+
169188}
170189
171190#endif
Original file line number Diff line number Diff line change @@ -119,6 +119,25 @@ class ASCIIStringTests: XCTestCase {
119119
120120 }
121121
122+ func testJoined( ) {
123+
124+ XCTAssertEqual (
125+ [ ASCIIString ( " AB " ) , ASCIIString ( " CD " ) ] . joined ( ) ,
126+ ASCIIString ( " ABCD " )
127+ )
128+
129+ XCTAssertEqual (
130+ [ ASCIIString ( " AB " ) , ASCIIString ( " CD " ) ] . joined ( separator: " _ " ) ,
131+ ASCIIString ( " AB_CD " )
132+ )
133+
134+ XCTAssertEqual (
135+ [ ASCIIString ( " AB " ) , ASCIIString ( " CD " ) ] . joined ( separator: " 123 " ) ,
136+ ASCIIString ( " AB123CD " )
137+ )
138+
139+ }
140+
122141}
123142
124143#endif
You can’t perform that action at this time.
0 commit comments