mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
Replaced fatalErrors in HuffmanTree with SymbolNotFound errors.
This commit is contained in:
@@ -381,18 +381,18 @@ public class Deflate: DecompressionAlgorithm {
|
||||
for code in bldCodes {
|
||||
switch code {
|
||||
case .byte(let byte):
|
||||
mainLiterals.code(symbol: byte.toInt(), &bitWriter)
|
||||
try mainLiterals.code(symbol: byte.toInt(), &bitWriter, DeflateError.symbolNotFound)
|
||||
case .lengthDistance(let ld):
|
||||
mainLiterals.code(symbol: ld.lengthSymbol, &bitWriter)
|
||||
try mainLiterals.code(symbol: ld.lengthSymbol, &bitWriter, DeflateError.symbolNotFound)
|
||||
bitWriter.write(number: ld.lengthExtraBits, bitsCount: ld.lengthExtraBitsCount)
|
||||
|
||||
mainDistances.code(symbol: ld.distanceSymbol, &bitWriter)
|
||||
try mainDistances.code(symbol: ld.distanceSymbol, &bitWriter, DeflateError.symbolNotFound)
|
||||
bitWriter.write(number: ld.distanceExtraBits, bitsCount: ld.distanceExtraBitsCount)
|
||||
}
|
||||
}
|
||||
|
||||
// End data symbol.
|
||||
mainLiterals.code(symbol: 256, &bitWriter)
|
||||
try mainLiterals.code(symbol: 256, &bitWriter, DeflateError.symbolNotFound)
|
||||
bitWriter.finish()
|
||||
|
||||
return bitWriter.buffer
|
||||
|
||||
@@ -156,7 +156,7 @@ class HuffmanTree {
|
||||
}
|
||||
}
|
||||
|
||||
func code(symbol: Int, _ bitWriter: inout BitToByteWriter) {
|
||||
func code(symbol: Int, _ bitWriter: inout BitToByteWriter, _ symbolNotFoundError: Error) throws {
|
||||
precondition(self.coding, "HuffmanTree is not initalized for coding!")
|
||||
|
||||
var index = 0
|
||||
@@ -166,7 +166,7 @@ class HuffmanTree {
|
||||
if foundSymbol == symbol {
|
||||
return
|
||||
} else {
|
||||
fatalError("Symbol not found, this error should be replaced with Error.")
|
||||
throw symbolNotFoundError
|
||||
}
|
||||
case .branch:
|
||||
let leftChildIndex = 2 * index + 1
|
||||
@@ -200,7 +200,7 @@ class HuffmanTree {
|
||||
bitWriter.write(bit: 1)
|
||||
continue
|
||||
} else {
|
||||
fatalError("Symbol not found, this error should be replaced with Error.")
|
||||
throw symbolNotFoundError
|
||||
}
|
||||
case .branch(let rightArray):
|
||||
if rightArray.contains(symbol) {
|
||||
@@ -208,7 +208,7 @@ class HuffmanTree {
|
||||
bitWriter.write(bit: 1)
|
||||
continue
|
||||
} else {
|
||||
fatalError("Symbol not found, this error should be replaced with Error.")
|
||||
throw symbolNotFoundError
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user