Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Sources/_StringProcessing/Engine/InstPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ struct QuantifyPayload: RawRepresentable {
}

var builtinCC: _CharacterClassModel.Representation {
_CharacterClassModel.Representation(rawValue: self.rawValue & 0xFF)!
_uncheckedRawEnum(self.rawValue & 0xFF)
}
var builtinIsInverted: Bool {
(self.rawValue >> 9) & 1 == 1
Expand Down Expand Up @@ -596,8 +596,7 @@ struct CharacterClassPayload: RawRepresentable {
self.rawValue & CharacterClassPayload.scalarBit != 0
}
var cc: _CharacterClassModel.Representation {
_CharacterClassModel.Representation.init(
rawValue: self.rawValue & CharacterClassPayload.ccMask).unsafelyUnwrapped
_uncheckedRawEnum(self.rawValue & CharacterClassPayload.ccMask)
}
}

Expand Down Expand Up @@ -636,8 +635,7 @@ struct AssertionPayload: RawRepresentable {
}

var kind: DSLTree.Atom.Assertion {
return .init(
rawValue: self.rawValue & AssertionPayload.assertionKindMask).unsafelyUnwrapped
_uncheckedRawEnum(self.rawValue & AssertionPayload.assertionKindMask)
}
var anchorsMatchNewlines: Bool { self.rawValue & AssertionPayload.anchorBit != 0 }
var usesSimpleUnicodeBoundaries: Bool {
Expand Down
6 changes: 3 additions & 3 deletions Sources/_StringProcessing/Engine/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ extension Instruction {

var opcode: OpCode {
get {
OpCode(
rawValue: (rawValue & _opcodeMask) &>> 56
).unsafelyUnwrapped
// Every instruction is built from a real case, with the payload inside 56 bits,
// so the opcode byte is always a valid case index.
_uncheckedRawEnum(rawValue &>> 56)
}
set {
assert(newValue != .invalid, "consider hoisting this")
Expand Down
16 changes: 15 additions & 1 deletion Sources/_StringProcessing/Utility/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,18 @@ extension String {
}
}


/// Converts a raw value into a single-byte enum without going through
/// its checked initializer.
///
/// - Precondition: `rawValue` is the raw value of a case of `T`, and `T` is
/// a no-payload enum laid out as a single byte.
@inline(__always)
//@unsafe
internal func _uncheckedRawEnum<T: RawRepresentable>(
_ rawValue: UInt64, as _: T.Type = T.self
) -> T where T.RawValue == UInt64 {
let result = unsafeBitCast(UInt8(truncatingIfNeeded: rawValue), to: T.self)
assert(result.rawValue == rawValue,
"\(T.self) is no longer laid out as its contiguous case index")
return result
}
Loading