Skip to content

Commit

Permalink
BitString -> Bitstring
Browse files Browse the repository at this point in the history
  • Loading branch information
oleganza committed Apr 1, 2023
1 parent 7742e2a commit f094881
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 72 deletions.
6 changes: 3 additions & 3 deletions Source/TonSwift/Address/ExternalAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import Foundation
/// addr_extern$01 len:(## 9) external_address:(bits len) = MsgAddressExt;
/// ```
public struct ExternalAddress {
private(set) var value: BitString
private(set) var value: Bitstring

public init(value: BitString) {
public init(value: Bitstring) {
self.value = value
}

Expand All @@ -16,7 +16,7 @@ public struct ExternalAddress {
}

public static func mock(seed: String) throws -> Self {
let value = BitString(data: Data(seed.utf8).sha256())
let value = Bitstring(data: Data(seed.utf8).sha256())
return ExternalAddress(value: value)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation

public struct BitString: Hashable {
public struct Bitstring: Hashable {

public static let empty = BitString(data: .init(), unchecked: (offset: 0, length: 0))
public static let empty = Bitstring(data: .init(), unchecked: (offset: 0, length: 0))

private let _offset: Int
private let _length: Int
Expand Down Expand Up @@ -111,15 +111,15 @@ public struct BitString: Hashable {
- parameter length: length of the bitstring in bits
- returns substring of bitstring
*/
public func substring(offset: Int, length: Int) throws -> BitString {
public func substring(offset: Int, length: Int) throws -> Bitstring {
// Corner case of empty string
if length == 0 && offset == _length {
return BitString.empty
return Bitstring.empty
}

try checkOffset(offset: offset, length: length)

return BitString(data: _data, unchecked:(offset: _offset + offset, length: length))
return Bitstring(data: _data, unchecked:(offset: _offset + offset, length: length))
}

/// Returns a byte-aligned substring given the `offset` and `length` in bits (same as in `substring` method).
Expand Down Expand Up @@ -148,7 +148,7 @@ public struct BitString: Hashable {
}

/// Drops first `n` bits from the bitstring.
public func dropFirst(_ n: Int) throws -> BitString {
public func dropFirst(_ n: Int) throws -> Bitstring {
return try substring(offset: n, length: self.length - n)
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public struct BitString: Hashable {
}
}

public func padLeft(_ n: Int = 0) -> BitString {
public func padLeft(_ n: Int = 0) -> Bitstring {
let b = Builder(capacity: max(n, self.length))
for _ in self.length..<n {
try! b.store(bit: 0)
Expand Down Expand Up @@ -221,7 +221,7 @@ public struct BitString: Hashable {
}

/// Bitstring implements lexicographic comparison.
extension BitString: Comparable {
extension Bitstring: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
for i in 0..<min(lhs.length, rhs.length) {
let l = lhs.at(unchecked: i)
Expand All @@ -233,15 +233,15 @@ extension BitString: Comparable {
}
}

extension BitString: Equatable {
extension Bitstring: Equatable {

/**
Checks for equality
- parameter lhs: bitstring
- parameter rhs: bitstring
- returns true if the bitstrings are equal, false otherwise
*/
public static func == (lhs: BitString, rhs: BitString) -> Bool {
public static func == (lhs: Bitstring, rhs: Bitstring) -> Bool {
if lhs._length != rhs._length {
return false
}
Expand Down
8 changes: 4 additions & 4 deletions Source/TonSwift/Cells/Boc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func getRefsDescriptor(refs: [Cell], level: UInt32, type: CellType) -> UInt8 {
return UInt8(refs.count) + typeFactor * 8 + UInt8(level) * 32
}

func getBitsDescriptor(bits: BitString) -> UInt8 {
func getBitsDescriptor(bits: Bitstring) -> UInt8 {
let len = bits.length
return UInt8(ceil(Double(len) / 8) + floor(Double(len) / 8))
}

func readCell(reader: Slice, sizeBytes: Int) throws -> (exotic: Bool, bits: BitString, refs: [UInt64]) {
func readCell(reader: Slice, sizeBytes: Int) throws -> (exotic: Bool, bits: Bitstring, refs: [UInt64]) {
let d1 = try reader.loadUint(bits: 8)
let refsCount = d1 % 8
let exotic = d1 & 8 != 0
Expand All @@ -97,7 +97,7 @@ func readCell(reader: Slice, sizeBytes: Int) throws -> (exotic: Bool, bits: BitS
let dataBytesize = Int(ceil(Double(d2) / 2.0))
let paddingAdded = d2 % 2 != 0

var bits = BitString.empty
var bits = Bitstring.empty
if dataBytesize > 0 {
if paddingAdded {
bits = try reader.loadPaddedBits(bits: dataBytesize * 8)
Expand All @@ -122,7 +122,7 @@ func deserializeBoc(src: Data) throws -> [Cell] {
let boc = try Boc(data: src)
let reader = Slice(data: boc.cellData)

var cells: [(bits: BitString, refs: [UInt64], exotic: Bool, result: Cell?)] = []
var cells: [(bits: Bitstring, refs: [UInt64], exotic: Bool, result: Cell?)] = []
for _ in 0..<boc.cells {
let cell = try readCell(reader: reader, sizeBytes: boc.size)
cells.append((cell.bits, cell.refs, cell.exotic, nil))
Expand Down
10 changes: 5 additions & 5 deletions Source/TonSwift/Cells/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Builder {
self.refs = []
}

public convenience init(_ bits: BitString) throws {
public convenience init(_ bits: Bitstring) throws {
self.init()
try self.store(bits: bits)
}
Expand All @@ -45,7 +45,7 @@ public class Builder {

/// Completes cell
public func endCell() throws -> Cell {
let bits = BitString(data: _buffer, unchecked:(offset: 0, length: _length))
let bits = Bitstring(data: _buffer, unchecked:(offset: 0, length: _length))
return try Cell(bits: bits, refs: refs)
}

Expand All @@ -55,8 +55,8 @@ public class Builder {
}

/// Converts builder into BitString
public func bitstring() -> BitString {
return BitString(data: _buffer, unchecked:(offset: 0, length: _length))
public func bitstring() -> Bitstring {
return Bitstring(data: _buffer, unchecked:(offset: 0, length: _length))
}

/// Converts to data if the bitstring contains a whole number of bytes.
Expand Down Expand Up @@ -270,7 +270,7 @@ public class Builder {

/// Writes bits from a bitstring
@discardableResult
public func store(bits: BitString) throws -> Self {
public func store(bits: Bitstring) throws -> Self {
for i in 0..<bits.length {
try store(bit: bits.at(i))
}
Expand Down
30 changes: 15 additions & 15 deletions Source/TonSwift/Cells/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct Cell: Hashable {
private let basic: BasicCell

public var type: CellType { basic.type }
public var bits: BitString { basic.bits }
public var bits: Bitstring { basic.bits }
public var refs: [Cell] { basic.refs }

public var level: UInt32 { mask.level }
Expand All @@ -49,7 +49,7 @@ public struct Cell: Hashable {
/// Initializes a new cell with. Exotic cells are parsed and resolved using their contents.
public init(
exotic: Bool = false,
bits: BitString = BitString.empty,
bits: Bitstring = Bitstring.empty,
refs: [Cell] = []
) throws {

Expand All @@ -72,14 +72,14 @@ public struct Cell: Hashable {
}

public init() {
self.basic = BasicCell(type: .ordinary, bits: BitString.empty, refs: [])
self.basic = BasicCell(type: .ordinary, bits: Bitstring.empty, refs: [])
self.mask = LevelMask()
}

/// Initializes a new cell with plain bytestring. This does not parse Bag-of-Cells (BoC), but uses provided data as a bitstring (byte-aligned).
/// Throws if data contains more than 1023 bits.
public init(data: Data) throws {
try self.init(bits: BitString(data: data))
try self.init(bits: Bitstring(data: data))
}

/**
Expand Down Expand Up @@ -210,11 +210,11 @@ extension Cell: Equatable {
/// This is used for internal computations to produce full-featured `Cell` type with various precomputed data.
fileprivate struct BasicCell: Hashable {
let type: CellType
let bits: BitString
let bits: Bitstring
let refs: [Cell]

/// Parse the exotic cell
static func exotic(bits: BitString, refs: [Cell]) throws -> Self {
static func exotic(bits: Bitstring, refs: [Cell]) throws -> Self {
let reader = Slice(bits: bits)
let typeInt = try reader.preloadUint(bits: 8)

Expand Down Expand Up @@ -290,7 +290,7 @@ fileprivate struct BasicCell: Hashable {
}

// Bits
var currentBits: BitString
var currentBits: Bitstring
if hashI == hashIOffset {
if !(levelI == 0 || type == .prunedBranch) {
throw TonError.custom("Invalid")
Expand All @@ -300,7 +300,7 @@ fileprivate struct BasicCell: Hashable {
if !(levelI != 0 && type != .prunedBranch) {
throw TonError.custom("Invalid: \(levelI), \(type)")
}
currentBits = BitString(data: hashes[Int(hashI - hashIOffset) - 1], unchecked: (offset: 0, length: 256))
currentBits = Bitstring(data: hashes[Int(hashI - hashIOffset) - 1], unchecked: (offset: 0, length: 256))
}

// Depth
Expand Down Expand Up @@ -355,7 +355,7 @@ fileprivate struct BasicCell: Hashable {
}
}

func getRepr(bits: BitString, refs: [Cell], level: UInt32, type: CellType) throws -> Data {
func getRepr(bits: Bitstring, refs: [Cell], level: UInt32, type: CellType) throws -> Data {
// Allocate
let bitsLen = (bits.length + 7) / 8
var repr = Data(count: 2 + bitsLen + (2 + 32) * refs.count)
Expand Down Expand Up @@ -408,7 +408,7 @@ public struct ExoticPruned {
public var pruned: [(depth: UInt32, hash: Data)]
}

func exoticPruned(bits: BitString, refs: [Cell]) throws -> ExoticPruned {
func exoticPruned(bits: Bitstring, refs: [Cell]) throws -> ExoticPruned {
let reader = Slice(bits: bits)

let type = try reader.loadUint(bits: 8)
Expand Down Expand Up @@ -458,7 +458,7 @@ func exoticPruned(bits: BitString, refs: [Cell]) throws -> ExoticPruned {
return ExoticPruned(mask: mask.value, pruned: pruned)
}

func resolvePruned(bits: BitString, refs: [Cell]) throws -> (type: CellType, depths: [UInt32], hashes: [Data], mask: LevelMask) {
func resolvePruned(bits: Bitstring, refs: [Cell]) throws -> (type: CellType, depths: [UInt32], hashes: [Data], mask: LevelMask) {
let pruned = try exoticPruned(bits: bits, refs: refs)
var depths = [UInt32]()
var hashes = [Data]()
Expand All @@ -471,7 +471,7 @@ func resolvePruned(bits: BitString, refs: [Cell]) throws -> (type: CellType, dep
return (CellType.prunedBranch, depths, hashes, mask)
}

func resolveMerkleProof(bits: BitString, refs: [Cell]) throws -> (type: CellType, depths: [UInt32], hashes: [Data], mask: LevelMask) {
func resolveMerkleProof(bits: Bitstring, refs: [Cell]) throws -> (type: CellType, depths: [UInt32], hashes: [Data], mask: LevelMask) {
let _/*merkleProof*/ = try exoticMerkleProof(bits: bits, refs: refs)
let depths = [UInt32]()
let hashes = [Data]()
Expand All @@ -480,7 +480,7 @@ func resolveMerkleProof(bits: BitString, refs: [Cell]) throws -> (type: CellType
return (CellType.merkleProof, depths, hashes, mask)
}

func resolveMerkleUpdate(bits: BitString, refs: [Cell]) throws -> (type: CellType, depths: [UInt32], hashes: [Data], mask: LevelMask) {
func resolveMerkleUpdate(bits: Bitstring, refs: [Cell]) throws -> (type: CellType, depths: [UInt32], hashes: [Data], mask: LevelMask) {
let _/*merkleUpdate*/ = try exoticMerkleUpdate(bits: bits, refs: refs)
let depths = [UInt32]()
let hashes = [Data]()
Expand All @@ -490,7 +490,7 @@ func resolveMerkleUpdate(bits: BitString, refs: [Cell]) throws -> (type: CellTyp
}

@discardableResult
func exoticMerkleUpdate(bits: BitString, refs: [Cell]) throws -> (proofDepth1: UInt32, proofDepth2: UInt32, proofHash1: Data, proofHash2: Data) {
func exoticMerkleUpdate(bits: Bitstring, refs: [Cell]) throws -> (proofDepth1: UInt32, proofDepth2: UInt32, proofHash1: Data, proofHash2: Data) {
let reader = Slice(bits: bits)

// type + hash + hash + depth + depth
Expand Down Expand Up @@ -534,7 +534,7 @@ func exoticMerkleUpdate(bits: BitString, refs: [Cell]) throws -> (proofDepth1: U
}

@discardableResult
func exoticMerkleProof(bits: BitString, refs: [Cell]) throws -> (proofDepth: UInt32, proofHash: Data) {
func exoticMerkleProof(bits: Bitstring, refs: [Cell]) throws -> (proofDepth: UInt32, proofHash: Data) {
let reader = Slice(bits: bits)

// type + hash + depth
Expand Down
26 changes: 13 additions & 13 deletions Source/TonSwift/Cells/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class DictionaryCoder<K: TypeCoder, V: TypeCoder> where K.T: Hashable {
}

// Serialize keys
var paddedMap: [BitString: V.T] = [:]
var paddedMap: [Bitstring: V.T] = [:]
for (k, v) in map {
let b = Builder()
try keyCoder.storeValue(k, to: b)
Expand Down Expand Up @@ -187,21 +187,21 @@ enum Node<T> {
}

class Edge<T> {
let label: BitString
let label: Bitstring
let node: Node<T>

init(label: BitString, node: Node<T>) {
init(label: Bitstring, node: Node<T>) {
self.label = label
self.node = node
}
}

/// Removes `n` bits from all the keys in a map
func removePrefixMap<T>(_ src: [BitString: T], _ length: Int) -> [BitString: T] {
func removePrefixMap<T>(_ src: [Bitstring: T], _ length: Int) -> [Bitstring: T] {
if length == 0 {
return src
}
var res: [BitString: T] = [:]
var res: [Bitstring: T] = [:]
for (k, d) in src {
res[try! k.dropFirst(length)] = d
}
Expand All @@ -210,11 +210,11 @@ func removePrefixMap<T>(_ src: [BitString: T], _ length: Int) -> [BitString: T]

/// Splits the dictionary by the value of the first bit of the keys. 0-prefixed keys go into left map, 1-prefixed keys go into the right one.
/// First bit is removed from the keys.
func forkMap<T>(_ src: [BitString: T]) throws -> (left: [BitString: T], right: [BitString: T]) {
func forkMap<T>(_ src: [Bitstring: T]) throws -> (left: [Bitstring: T], right: [Bitstring: T]) {
try invariant(!src.isEmpty)

var left: [BitString: T] = [:]
var right: [BitString: T] = [:]
var left: [Bitstring: T] = [:]
var right: [Bitstring: T] = [:]
for (k, d) in src {
if k.at(unchecked: 0) == false {
left[try! k.dropFirst(1)] = d
Expand All @@ -228,7 +228,7 @@ func forkMap<T>(_ src: [BitString: T]) throws -> (left: [BitString: T], right: [
return (left, right)
}

func buildNode<T>(_ src: [BitString: T]) throws -> Node<T> {
func buildNode<T>(_ src: [Bitstring: T]) throws -> Node<T> {
try invariant(!src.isEmpty)
if src.count == 1 {
return .leaf(value: src.values.first!)
Expand All @@ -238,7 +238,7 @@ func buildNode<T>(_ src: [BitString: T]) throws -> Node<T> {
}
}

func buildEdge<T>(_ src: [BitString: T]) throws -> Edge<T> {
func buildEdge<T>(_ src: [Bitstring: T]) throws -> Edge<T> {
try invariant(!src.isEmpty)
let label = findCommonPrefix(src: Array(src.keys))
return Edge(label: label, node: try buildNode(removePrefixMap(src, label.length)))
Expand Down Expand Up @@ -287,7 +287,7 @@ func buildEdge<T>(_ src: [BitString: T]) throws -> Edge<T> {
/// cb.store_bits(label, len);
/// }
/// ```
func writeLabel(src: BitString, keyLength: Int, to: Builder) throws {
func writeLabel(src: Bitstring, keyLength: Int, to: Builder) throws {
let k = bitsForInt(keyLength)
let n = src.length

Expand Down Expand Up @@ -335,10 +335,10 @@ func writeEdge<T, V>(src: Edge<T>, keyLength: Int, valueCoder: V, to: Builder) t
try writeNode(src: src.node, keyLength: keyLength - src.label.length, valueCoder: valueCoder, to: to)
}

func findCommonPrefix(src: some Collection<BitString>) -> BitString {
func findCommonPrefix(src: some Collection<Bitstring>) -> Bitstring {
// Corner cases
if src.isEmpty {
return BitString()
return Bitstring()
}
if src.count == 1 {
return src.first!
Expand Down
Loading

0 comments on commit f094881

Please sign in to comment.