Files
AndroidXML/Sources/AndroidXML/TblTableRef.swift

48 lines
1.8 KiB
Swift

/**
* This is a reference to a unique entry (a `TblEntry` structure) in a resource table.
* The value is structured as: `0xpptteeee`, where
* `pp` is the package index,
* `tt` is the type index in that package, and
* `eeee` is the entry index in that type.
* The package and type values start at 1 for the first item, to help catch cases where they have not been supplied.
*/
public struct TblTableRef: CustomStringConvertible {
public let ident: UInt32
public init(_ ident: UInt32) {
self.ident = ident
}
/// Convenience constructor for string-based references.
/// Must conform to format `"@PPTTEEEE"`.
/// Throws an exception if string does not starts with `"@"` or cannot resolve hex-string into `UInt32`.
public init(_ stringRef: String) throws {
guard stringRef.hasPrefix("@") else {
throw AXMLError("A table reference must start with @")
}
guard let idx = UInt32(stringRef[(stringRef.index(after: stringRef.startIndex))..<stringRef.endIndex], radix: 16) else {
throw AXMLError("Invalid table reference format")
}
self.ident = idx
}
/// `ident` as hex string
public var asHex: String { ident.hex(8) }
/// mask: `0xFF000000`
public var package: UInt8 { UInt8((ident & 0xFF000000) >> 24) }
/// mask: `0x00FF0000`
public var type: UInt8 { UInt8((ident & 0x00FF0000) >> 16) }
/// mask: `0x0000FFFF`
public var entry: UInt16 { UInt16(ident & 0x0000FFFF) }
/// `package != 0 || type != 0 || entry != 0`
public func isVlaid() -> Bool { ident != 0 }
/// `package != 0 || type != 0`
public func check() -> Bool { (ident & 0xFFFF0000) != 0 }
/// `package != 0 && type == 0`
public func isInternal() -> Bool { (ident & 0xFFFF0000) != 0 && (ident & 0x00FF0000) == 0 }
public var description: String { "TblTableRef(" + ident.hex(8) + ")" }
}