Skip to content

Commit 5739f5d

Browse files
committed
HybridTextFileDecodingStrategy: Added allowLossy parameter
1 parent 74e0af0 commit 5739f5d

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Sources/TextFile/TextFileDecodingStrategy/Strategies/HybridTextFileDecodingStrategy.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ import struct FoundationEssentials.UUID
2525

2626
/// Hybrid text file decoding strategy.
2727
public struct HybridTextFileDecodingStrategy {
28+
/// If `true`, allow lossy text decoding if decoding can't be detected automatically.
29+
public var allowLossy: Bool
30+
2831
public var convertLineEndings: Bool
2932

30-
public init(convertLineEndings: Bool = true) {
33+
public init(
34+
allowLossy: Bool = true,
35+
convertLineEndings: Bool = true
36+
) {
37+
self.allowLossy = allowLossy
3138
self.convertLineEndings = convertLineEndings
3239
}
3340
}
@@ -101,7 +108,7 @@ extension HybridTextFileDecodingStrategy {
101108
// Step 7: attempt to detect encoding using Standard Lib / Foundation API
102109
var heldError: TextFileDecodeError? = nil
103110
do throws(TextFileDecodeError) {
104-
let decoded = try decodeTextAutomatically(allowLossy: false, in: data, fileURL: fileURL)
111+
let decoded = try decodeTextAutomatically(allowLossy: allowLossy, in: data, fileURL: fileURL)
105112
return decoded
106113
} catch {
107114
heldError = error
@@ -291,10 +298,15 @@ extension HybridTextFileDecodingStrategy {
291298
}
292299
}
293300

294-
// MARK: - Utilities
295-
296301
// MARK: - Static Constructors
297302

303+
extension TextFileDecodingStrategy where Self == HybridTextFileDecodingStrategy {
304+
/// Returns the best text file decoding method for the current platform.
305+
public static func hybrid(allowLossy: Bool = true) -> HybridTextFileDecodingStrategy {
306+
HybridTextFileDecodingStrategy(allowLossy: allowLossy)
307+
}
308+
}
309+
298310
extension TextFileDecodingStrategy where Self == AnyTextFileDecodingStrategy {
299311
fileprivate static func bestNonHybridForCurrentPlatform(allowLossy: Bool) -> AnyTextFileDecodingStrategy {
300312
#if canImport(Darwin)
@@ -304,13 +316,3 @@ extension TextFileDecodingStrategy where Self == AnyTextFileDecodingStrategy {
304316
#endif
305317
}
306318
}
307-
308-
309-
// MARK: - Static Constructors
310-
311-
extension TextFileDecodingStrategy where Self == HybridTextFileDecodingStrategy {
312-
/// Returns the best text file decoding method for the current platform.
313-
public static func hybrid() -> HybridTextFileDecodingStrategy {
314-
HybridTextFileDecodingStrategy()
315-
}
316-
}

0 commit comments

Comments
 (0)