fix: CFI module — 11 issues from code review
Phase 1 (correctness): - #1: rawValue changed from stored to computed property, eliminating stale cache risk - #2: tokenSamples/textMarker unified to UTF-16 offsets, fixing emoji/CJK positioning - #3: makeOffsetCFI now accepts optional contentPath parameter Phase 2 (robustness): - #4: Resolver uses fixed index access instead of last(where:) for manifest/spine steps - #5: HTML comments and CDATA stripped before regex matching - #6: Text assertion parser handles backslash escapes (\[ \] \) - #7: Token matching uses prefix/suffix with length ratio constraints - #8: makeOffsetRangeCFI validates startOffset <= endOffset Phase 3 (code quality): - #9: Shared internal nilIfEmpty extension in RDEPUBCFIUtilities.swift - #10: RDEPUBCFIMap has markerByPath index dictionary for O(1) lookup - #11: parseRange unified to use try (not try?) for parent parsing Review fixes: - Documented init(rawValue:) parameter is ignored (computed property) - Fixed escape unescaping to handle \\ → \ correctly - Lowered token minLength threshold from 4 to 2 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import Foundation
|
||||
|
||||
public struct RDEPUBCFI: Codable, Equatable, Hashable {
|
||||
public var packagePath: RDEPUBCFIPath
|
||||
public var contentPath: RDEPUBCFIPath
|
||||
public var characterOffset: Int?
|
||||
public var sideBias: RDEPUBCFISideBias?
|
||||
public var textAssertion: RDEPUBCFITextAssertion?
|
||||
|
||||
/// Always computed from components — no stale cached value risk.
|
||||
public var rawValue: String {
|
||||
RDEPUBCFISerializer.serialize(self)
|
||||
}
|
||||
|
||||
/// Initialize from components. The `rawValue` parameter is ignored — rawValue is always
|
||||
/// computed from the components. To parse a CFI string, use `RDEPUBCFIParser.parse(_:)`.
|
||||
public init(
|
||||
rawValue: String = "",
|
||||
packagePath: RDEPUBCFIPath = RDEPUBCFIPath(),
|
||||
contentPath: RDEPUBCFIPath = RDEPUBCFIPath(),
|
||||
characterOffset: Int? = nil,
|
||||
sideBias: RDEPUBCFISideBias? = nil,
|
||||
textAssertion: RDEPUBCFITextAssertion? = nil
|
||||
) {
|
||||
self.packagePath = packagePath
|
||||
self.contentPath = contentPath
|
||||
self.characterOffset = characterOffset
|
||||
self.sideBias = sideBias
|
||||
self.textAssertion = textAssertion
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let rawValue = try container.decode(String.self)
|
||||
let parsed = try RDEPUBCFIParser.parse(rawValue)
|
||||
self.packagePath = parsed.packagePath
|
||||
self.contentPath = parsed.contentPath
|
||||
self.characterOffset = parsed.characterOffset
|
||||
self.sideBias = parsed.sideBias
|
||||
self.textAssertion = parsed.textAssertion
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
public enum RDEPUBCFISideBias: String, Codable, Equatable, Hashable {
|
||||
case before = "b"
|
||||
case after = "a"
|
||||
}
|
||||
Reference in New Issue
Block a user