@@ -42,20 +42,20 @@ public struct ProcessResult: CustomStringConvertible {
42
42
43
43
/// The output bytes of the process. Available only if the process was
44
44
/// asked to redirect its output and no stdout output closure was set.
45
- public let output : Result < [ Int8 ] , AnyError >
45
+ public let output : Result < [ UInt8 ] , AnyError >
46
46
47
47
/// The output bytes of the process. Available only if the process was
48
48
/// asked to redirect its output and no stderr output closure was set.
49
- public let stderrOutput : Result < [ Int8 ] , AnyError >
49
+ public let stderrOutput : Result < [ UInt8 ] , AnyError >
50
50
51
51
/// Create an instance using a POSIX process exit status code and output result.
52
52
///
53
53
/// See `waitpid(2)` for information on the exit status code.
54
54
public init (
55
55
arguments: [ String ] ,
56
56
exitStatusCode: Int32 ,
57
- output: Result < [ Int8 ] , AnyError > ,
58
- stderrOutput: Result < [ Int8 ] , AnyError >
57
+ output: Result < [ UInt8 ] , AnyError > ,
58
+ stderrOutput: Result < [ UInt8 ] , AnyError >
59
59
) {
60
60
let exitStatus : ExitStatus
61
61
if WIFSIGNALED ( exitStatusCode) {
@@ -72,8 +72,8 @@ public struct ProcessResult: CustomStringConvertible {
72
72
public init (
73
73
arguments: [ String ] ,
74
74
exitStatus: ExitStatus ,
75
- output: Result < [ Int8 ] , AnyError > ,
76
- stderrOutput: Result < [ Int8 ] , AnyError >
75
+ output: Result < [ UInt8 ] , AnyError > ,
76
+ stderrOutput: Result < [ UInt8 ] , AnyError >
77
77
) {
78
78
self . arguments = arguments
79
79
self . output = output
@@ -82,28 +82,13 @@ public struct ProcessResult: CustomStringConvertible {
82
82
}
83
83
84
84
/// Converts stdout output bytes to string, assuming they're UTF8.
85
- ///
86
- /// - Throws: Error while reading the process output or if output is not a valid UTF8 sequence.
87
85
public func utf8Output( ) throws -> String {
88
- return try utf8Result ( output)
86
+ return String ( decoding : try output. dematerialize ( ) , as : Unicode . UTF8 . self )
89
87
}
90
88
91
89
/// Converts stderr output bytes to string, assuming they're UTF8.
92
- ///
93
- /// - Throws: Error while reading the process output or if output is not a valid UTF8 sequence.
94
90
public func utf8stderrOutput( ) throws -> String {
95
- return try utf8Result ( stderrOutput)
96
- }
97
-
98
- /// Returns UTF8 string from given result or throw.
99
- private func utf8Result( _ result: Result < [ Int8 ] , AnyError > ) throws -> String {
100
- var bytes = try result. dematerialize ( )
101
- // Null terminate it.
102
- bytes. append ( 0 )
103
- if let output = String ( validatingUTF8: bytes) {
104
- return output
105
- }
106
- throw Error . illegalUTF8Sequence
91
+ return String ( decoding: try stderrOutput. dematerialize ( ) , as: Unicode . UTF8. self)
107
92
}
108
93
109
94
public var description : String {
@@ -157,7 +142,7 @@ public final class Process: ObjectIdentifierProtocol {
157
142
public typealias ProcessID = pid_t
158
143
159
144
/// Typealias for stdout/stderr output closure.
160
- public typealias OutputClosure = ( [ Int8 ] ) -> Void
145
+ public typealias OutputClosure = ( [ UInt8 ] ) -> Void
161
146
162
147
/// Global default setting for verbose.
163
148
public static var verbose = false
@@ -198,10 +183,10 @@ public final class Process: ObjectIdentifierProtocol {
198
183
private var _result : ProcessResult ?
199
184
200
185
/// If redirected, stdout result and reference to the thread reading the output.
201
- private var stdout : ( result: Result < [ Int8 ] , AnyError > , thread: Thread ? ) = ( Result ( [ ] ) , nil )
186
+ private var stdout : ( result: Result < [ UInt8 ] , AnyError > , thread: Thread ? ) = ( Result ( [ ] ) , nil )
202
187
203
188
/// If redirected, stderr result and reference to the thread reading the output.
204
- private var stderr : ( result: Result < [ Int8 ] , AnyError > , thread: Thread ? ) = ( Result ( [ ] ) , nil )
189
+ private var stderr : ( result: Result < [ UInt8 ] , AnyError > , thread: Thread ? ) = ( Result ( [ ] ) , nil )
205
190
206
191
/// Queue to protect concurrent reads.
207
192
private let serialQueue = DispatchQueue ( label: " org.swift.swiftpm.process " )
@@ -440,12 +425,12 @@ public final class Process: ObjectIdentifierProtocol {
440
425
/// Reads the given fd and returns its result.
441
426
///
442
427
/// Closes the fd before returning.
443
- private func readOutput( onFD fd: Int32 , outputClosure: OutputClosure ? ) -> Result < [ Int8 ] , AnyError > {
428
+ private func readOutput( onFD fd: Int32 , outputClosure: OutputClosure ? ) -> Result < [ UInt8 ] , AnyError > {
444
429
// Read all of the data from the output pipe.
445
430
let N = 4096
446
- var buf = [ Int8 ] ( repeating: 0 , count: N + 1 )
431
+ var buf = [ UInt8 ] ( repeating: 0 , count: N + 1 )
447
432
448
- var out = [ Int8 ] ( )
433
+ var out = [ UInt8 ] ( )
449
434
var error : Swift . Error ? = nil
450
435
loop: while true {
451
436
let n = read ( fd, & buf, N)
0 commit comments