mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
[WIP] Saved messages
This commit is contained in:
@@ -51,7 +51,7 @@ public func persistentHash32(_ string: String) -> Int32 {
|
||||
|
||||
private let emptyMemory = malloc(1)!
|
||||
|
||||
public class MemoryBuffer: Equatable, CustomStringConvertible {
|
||||
public class MemoryBuffer: Comparable, Hashable, CustomStringConvertible {
|
||||
public internal(set) var memory: UnsafeMutableRawPointer
|
||||
var capacity: Int
|
||||
public internal(set) var length: Int
|
||||
@@ -122,9 +122,25 @@ public class MemoryBuffer: Equatable, CustomStringConvertible {
|
||||
f(Data(bytesNoCopy: self.memory, count: self.length, deallocator: .none))
|
||||
}
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
if self.length == 0 {
|
||||
hasher.combine(0 as Int)
|
||||
} else {
|
||||
hasher.combine(bytes: UnsafeRawBufferPointer(start: self.memory, count: self.length))
|
||||
}
|
||||
}
|
||||
|
||||
public func withRawBufferPointer(_ f: (UnsafeRawBufferPointer) -> Void) {
|
||||
f(UnsafeRawBufferPointer(start: self.memory, count: self.length))
|
||||
}
|
||||
|
||||
public static func ==(lhs: MemoryBuffer, rhs: MemoryBuffer) -> Bool {
|
||||
return lhs.length == rhs.length && memcmp(lhs.memory, rhs.memory, lhs.length) == 0
|
||||
}
|
||||
|
||||
public static func <(lhs: MemoryBuffer, rhs: MemoryBuffer) -> Bool {
|
||||
return mdb_cmp_memn(lhs.memory, lhs.length, rhs.memory, rhs.length) < 0
|
||||
}
|
||||
}
|
||||
|
||||
public final class WriteBuffer: MemoryBuffer {
|
||||
@@ -134,6 +150,10 @@ public final class WriteBuffer: MemoryBuffer {
|
||||
super.init(memory: malloc(32), capacity: 32, length: 0, freeWhenDone: true)
|
||||
}
|
||||
|
||||
public init(capacity: Int) {
|
||||
super.init(memory: malloc(capacity), capacity: capacity, length: 0, freeWhenDone: true)
|
||||
}
|
||||
|
||||
public func makeReadBufferAndReset() -> ReadBuffer {
|
||||
let buffer = ReadBuffer(memory: self.memory, length: self.offset, freeWhenDone: true)
|
||||
self.memory = malloc(32)
|
||||
@@ -222,6 +242,12 @@ public final class ReadBuffer: MemoryBuffer {
|
||||
return result
|
||||
}
|
||||
|
||||
public func readMemoryBuffer(length: Int) -> MemoryBuffer {
|
||||
let result = MemoryBuffer(memory: malloc(length)!, capacity: length, length: length, freeWhenDone: true)
|
||||
self.read(result.memory, offset: 0, length: length)
|
||||
return result
|
||||
}
|
||||
|
||||
public func skip(_ length: Int) {
|
||||
self.offset += length
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user