File tree Expand file tree Collapse file tree 1 file changed +6
-15
lines changed Expand file tree Collapse file tree 1 file changed +6
-15
lines changed Original file line number Diff line number Diff line change @@ -20,25 +20,16 @@ extension std.string {
20
20
/// - Complexity: O(*n*), where *n* is the number of UTF-8 code units in the
21
21
/// Swift string.
22
22
public init ( _ string: String ) {
23
- self . init ( )
24
- let utf8 = string. utf8
25
- self . reserve ( utf8. count)
26
- for char in utf8 {
27
- self . push_back ( value_type ( bitPattern: char) )
23
+ self = string. withCString ( encodedAs: UTF8 . self) { buffer in
24
+ std. string ( buffer, string. utf8. count, . init( ) )
28
25
}
29
26
}
30
27
31
28
public init ( _ string: UnsafePointer < CChar > ? ) {
32
- self . init ( )
33
-
34
- guard let str = string else {
35
- return
36
- }
37
-
38
- let len = UTF8 . _nullCodeUnitOffset ( in: str)
39
- for i in 0 ..< len {
40
- let char = UInt8 ( str [ i] )
41
- self . push_back ( value_type ( bitPattern: char) )
29
+ if let str = string {
30
+ self . init ( str, UTF8 . _nullCodeUnitOffset ( in: str) , . init( ) )
31
+ } else {
32
+ self . init ( )
42
33
}
43
34
}
44
35
}
You can’t perform that action at this time.
0 commit comments