mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 22:55:00 +00:00
Refactoring
This commit is contained in:
@@ -25,7 +25,7 @@ extension _AdaptedPostboxEncoder {
|
||||
self.userInfo = userInfo
|
||||
}
|
||||
|
||||
func makeData(addHeader: Bool) -> (Data, ValueType) {
|
||||
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
|
||||
precondition(addHeader)
|
||||
|
||||
if self.items.isEmpty {
|
||||
@@ -53,7 +53,7 @@ extension _AdaptedPostboxEncoder {
|
||||
buffer.write(&length, offset: 0, length: 4)
|
||||
|
||||
for case .int64(var value) in self.items {
|
||||
buffer.write(&value, offset: 0, length: 4)
|
||||
buffer.write(&value, offset: 0, length: 8)
|
||||
}
|
||||
|
||||
return (buffer.makeData(), .Int64Array)
|
||||
@@ -75,13 +75,17 @@ extension _AdaptedPostboxEncoder {
|
||||
let buffer = WriteBuffer()
|
||||
|
||||
var length: Int32 = Int32(self.items.count)
|
||||
if isDictionary {
|
||||
precondition(length % 2 == 0)
|
||||
length /= 2
|
||||
}
|
||||
buffer.write(&length, offset: 0, length: 4)
|
||||
|
||||
for case .object(let data) in self.items {
|
||||
buffer.write(data)
|
||||
}
|
||||
|
||||
return (buffer.makeData(), .ObjectArray)
|
||||
return (buffer.makeData(), isDictionary ? .ObjectDictionary : .ObjectArray)
|
||||
} else if self.items.allSatisfy({ if case .data = $0 { return true } else { return false } }) {
|
||||
let buffer = WriteBuffer()
|
||||
|
||||
@@ -108,18 +112,28 @@ extension _AdaptedPostboxEncoder.UnkeyedContainer: UnkeyedEncodingContainer {
|
||||
}
|
||||
|
||||
func encode<T>(_ value: T) throws where T : Encodable {
|
||||
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
|
||||
if value is Int32 {
|
||||
try self.encode(value as! Int32)
|
||||
} else if value is Int64 {
|
||||
try self.encode(value as! Int64)
|
||||
} else if value is String {
|
||||
try self.encode(value as! String)
|
||||
} else if value is Data {
|
||||
try self.encode(value as! Data)
|
||||
} else {
|
||||
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
|
||||
|
||||
let innerEncoder = _AdaptedPostboxEncoder(typeHash: typeHash)
|
||||
try! value.encode(to: innerEncoder)
|
||||
let innerEncoder = _AdaptedPostboxEncoder(typeHash: typeHash)
|
||||
try! value.encode(to: innerEncoder)
|
||||
|
||||
let (data, _) = innerEncoder.makeData(addHeader: true)
|
||||
let (data, _) = innerEncoder.makeData(addHeader: true, isDictionary: false)
|
||||
|
||||
let buffer = WriteBuffer()
|
||||
let buffer = WriteBuffer()
|
||||
|
||||
buffer.write(data)
|
||||
buffer.write(data)
|
||||
|
||||
self.items.append(.object(buffer.makeData()))
|
||||
self.items.append(.object(buffer.makeData()))
|
||||
}
|
||||
}
|
||||
|
||||
func encode(_ value: Int32) throws {
|
||||
|
||||
Reference in New Issue
Block a user