More Int64 improvements

This commit is contained in:
Kylmakalle 2025-01-16 01:48:24 +02:00
parent d212332cd4
commit 737867a380
4 changed files with 197 additions and 96 deletions

View File

@ -37,7 +37,7 @@ NSString *const TGBridgeChatsArrayKey = @"chats";
{
_identifier = [aDecoder decodeInt64ForKey:TGBridgeChatIdentifierKey];
_date = [aDecoder decodeDoubleForKey:TGBridgeChatDateKey];
_fromUid = [aDecoder decodeInt32ForKey:TGBridgeChatFromUidKey];
_fromUid = [aDecoder decodeInt64ForKey:TGBridgeChatFromUidKey];
_text = [aDecoder decodeObjectForKey:TGBridgeChatTextKey];
_outgoing = [aDecoder decodeBoolForKey:TGBridgeChatOutgoingKey];
_unread = [aDecoder decodeBoolForKey:TGBridgeChatUnreadKey];
@ -67,7 +67,7 @@ NSString *const TGBridgeChatsArrayKey = @"chats";
{
[aCoder encodeInt64:self.identifier forKey:TGBridgeChatIdentifierKey];
[aCoder encodeDouble:self.date forKey:TGBridgeChatDateKey];
[aCoder encodeInt32:self.fromUid forKey:TGBridgeChatFromUidKey];
[aCoder encodeInt64:self.fromUid forKey:TGBridgeChatFromUidKey];
[aCoder encodeObject:self.text forKey:TGBridgeChatTextKey];
[aCoder encodeBool:self.outgoing forKey:TGBridgeChatOutgoingKey];
[aCoder encodeBool:self.unread forKey:TGBridgeChatUnreadKey];

View File

@ -1,60 +1,120 @@
#ifndef Telegraph_TGPeerIdAdapter_h
#define Telegraph_TGPeerIdAdapter_h
#define TG_USER_ID_BITS 52
#define TG_MAX_USER_ID ((int64_t)1 << TG_USER_ID_BITS)
// Namespace constants based on Swift implementation
#define TG_NAMESPACE_MASK 0x7
#define TG_NAMESPACE_EMPTY 0x0
#define TG_NAMESPACE_CLOUD 0x1
#define TG_NAMESPACE_GROUP 0x2
#define TG_NAMESPACE_CHANNEL 0x3
#define TG_NAMESPACE_SECRET_CHAT 0x4
#define TG_NAMESPACE_ADMIN_LOG 0x5
#define TG_NAMESPACE_AD 0x6
#define TG_NAMESPACE_MAX 0x7
// Using bit shifts to divide the negative space into quarters
// -2^63 (INT64_MIN) divided into 4 parts using shifts
#define TG_GROUP_RANGE_START ((int64_t)-1)
#define TG_CHANNEL_RANGE_START (((int64_t)1) << 62) // -2^62
#define TG_SECRET_CHAT_RANGE_START (((int64_t)1) << 63) // -2^63 (INT64_MIN)
#define TG_ADMIN_LOG_RANGE_START (-((int64_t)1 << 62) - ((int64_t)1 << 61)) // -2^62 - 2^61
// Helper functions for bit manipulation
static inline uint32_t TGPeerIdGetNamespace(int64_t peerId) {
uint64_t data = (uint64_t)peerId;
return (uint32_t)((data >> 32) & TG_NAMESPACE_MASK);
}
static inline int64_t TGPeerIdGetId(int64_t peerId) {
uint64_t data = (uint64_t)peerId;
uint64_t idHighBits = (data >> (32 + 3)) << 32;
uint64_t idLowBits = data & 0xffffffff;
return (int64_t)(idHighBits | idLowBits);
}
static inline int64_t TGPeerIdMake(uint32_t namespaceId, int64_t id) {
uint64_t data = 0;
uint64_t idBits = (uint64_t)id;
uint64_t idLowBits = idBits & 0xffffffff;
uint64_t idHighBits = (idBits >> 32) & 0xffffffff;
data |= ((uint64_t)(namespaceId & TG_NAMESPACE_MASK)) << 32;
data |= (idHighBits << (32 + 3));
data |= idLowBits;
return (int64_t)data;
}
// Updated peer type checks
static inline bool TGPeerIdIsEmpty(int64_t peerId) {
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_EMPTY;
}
static inline bool TGPeerIdIsUser(int64_t peerId) {
return peerId > 0 && peerId < TG_MAX_USER_ID;
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_CLOUD;
}
static inline bool TGPeerIdIsGroup(int64_t peerId) {
return peerId < 0 && peerId > TG_CHANNEL_RANGE_START;
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_GROUP;
}
static inline bool TGPeerIdIsChannel(int64_t peerId) {
return peerId <= TG_CHANNEL_RANGE_START && peerId > TG_SECRET_CHAT_RANGE_START;
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_CHANNEL;
}
static inline bool TGPeerIdIsSecretChat(int64_t peerId) {
return peerId <= TG_SECRET_CHAT_RANGE_START && peerId > TG_ADMIN_LOG_RANGE_START;
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_SECRET_CHAT;
}
static inline bool TGPeerIdIsAdminLog(int64_t peerId) {
return peerId <= TG_ADMIN_LOG_RANGE_START && peerId > INT64_MIN;
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_ADMIN_LOG;
}
static inline int64_t TGChannelIdFromPeerId(int64_t peerId) {
if (TGPeerIdIsChannel(peerId)) {
return TG_CHANNEL_RANGE_START - peerId;
}
return 0;
static inline bool TGPeerIdIsAd(int64_t peerId) {
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_AD;
}
static inline int64_t TGPeerIdFromChannelId(int64_t channelId) {
return TG_CHANNEL_RANGE_START - channelId;
}
static inline int64_t TGPeerIdFromAdminLogId(int64_t channelId) {
return TG_ADMIN_LOG_RANGE_START - channelId;
// Conversion functions
static inline int64_t TGPeerIdFromUserId(int64_t userId) {
return TGPeerIdMake(TG_NAMESPACE_CLOUD, userId);
}
static inline int64_t TGPeerIdFromGroupId(int64_t groupId) {
return -groupId;
return TGPeerIdMake(TG_NAMESPACE_GROUP, groupId);
}
static inline int64_t TGPeerIdFromChannelId(int64_t channelId) {
return TGPeerIdMake(TG_NAMESPACE_CHANNEL, channelId);
}
static inline int64_t TGPeerIdFromSecretChatId(int64_t secretChatId) {
return TGPeerIdMake(TG_NAMESPACE_SECRET_CHAT, secretChatId);
}
static inline int64_t TGPeerIdFromAdminLogId(int64_t adminLogId) {
return TGPeerIdMake(TG_NAMESPACE_ADMIN_LOG, adminLogId);
}
static inline int64_t TGPeerIdFromAdId(int64_t adId) {
return TGPeerIdMake(TG_NAMESPACE_AD, adId);
}
// Extract IDs
static inline int64_t TGUserIdFromPeerId(int64_t peerId) {
return TGPeerIdIsUser(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGGroupIdFromPeerId(int64_t peerId) {
if (TGPeerIdIsGroup(peerId)) {
return -peerId;
}
return 0;
return TGPeerIdIsGroup(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGChannelIdFromPeerId(int64_t peerId) {
return TGPeerIdIsChannel(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGSecretChatIdFromPeerId(int64_t peerId) {
return TGPeerIdIsSecretChat(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGAdminLogIdFromPeerId(int64_t peerId) {
return TGPeerIdIsAdminLog(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGAdIdFromPeerId(int64_t peerId) {
return TGPeerIdIsAd(peerId) ? TGPeerIdGetId(peerId) : 0;
}
#endif

View File

@ -271,7 +271,7 @@ NSString *const TGBridgeUserChangeFieldsKey = @"fields";
self = [super init];
if (self != nil)
{
_userIdentifier = [aDecoder decodeInt32ForKey:TGBridgeUserChangeIdentifierKey];
_userIdentifier = [aDecoder decodeInt64ForKey:TGBridgeUserChangeIdentifierKey];
_fields = [aDecoder decodeObjectForKey:TGBridgeUserChangeFieldsKey];
}
return self;
@ -279,7 +279,7 @@ NSString *const TGBridgeUserChangeFieldsKey = @"fields";
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt32:self.userIdentifier forKey:TGBridgeUserChangeIdentifierKey];
[aCoder encodeInt64:self.userIdentifier forKey:TGBridgeUserChangeIdentifierKey];
[aCoder encodeObject:self.fields forKey:TGBridgeUserChangeFieldsKey];
}

View File

@ -1,79 +1,120 @@
#ifndef Telegraph_TGPeerIdAdapter_h
#define Telegraph_TGPeerIdAdapter_h
#define TG_USER_ID_BITS 52
#define TG_MAX_USER_ID ((int64_t)1 << TG_USER_ID_BITS)
// Namespace constants based on Swift implementation
#define TG_NAMESPACE_MASK 0x7
#define TG_NAMESPACE_EMPTY 0x0
#define TG_NAMESPACE_CLOUD 0x1
#define TG_NAMESPACE_GROUP 0x2
#define TG_NAMESPACE_CHANNEL 0x3
#define TG_NAMESPACE_SECRET_CHAT 0x4
#define TG_NAMESPACE_ADMIN_LOG 0x5
#define TG_NAMESPACE_AD 0x6
#define TG_NAMESPACE_MAX 0x7
static inline bool TGPeerIdIsGroup(int64_t peerId) {
return peerId < 0 && peerId > INT32_MIN;
// Helper functions for bit manipulation
static inline uint32_t TGPeerIdGetNamespace(int64_t peerId) {
uint64_t data = (uint64_t)peerId;
return (uint32_t)((data >> 32) & TG_NAMESPACE_MASK);
}
static inline int64_t TGPeerIdGetId(int64_t peerId) {
uint64_t data = (uint64_t)peerId;
uint64_t idHighBits = (data >> (32 + 3)) << 32;
uint64_t idLowBits = data & 0xffffffff;
return (int64_t)(idHighBits | idLowBits);
}
static inline int64_t TGPeerIdMake(uint32_t namespaceId, int64_t id) {
uint64_t data = 0;
uint64_t idBits = (uint64_t)id;
uint64_t idLowBits = idBits & 0xffffffff;
uint64_t idHighBits = (idBits >> 32) & 0xffffffff;
data |= ((uint64_t)(namespaceId & TG_NAMESPACE_MASK)) << 32;
data |= (idHighBits << (32 + 3));
data |= idLowBits;
return (int64_t)data;
}
// Updated peer type checks
static inline bool TGPeerIdIsEmpty(int64_t peerId) {
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_EMPTY;
}
static inline bool TGPeerIdIsUser(int64_t peerId) {
return peerId > 0 && peerId < TG_MAX_USER_ID;
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_CLOUD;
}
static inline bool TGPeerIdIsGroup(int64_t peerId) {
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_GROUP;
}
static inline bool TGPeerIdIsChannel(int64_t peerId) {
return peerId <= ((int64_t)INT32_MIN) * 2 && peerId > ((int64_t)INT32_MIN) * 3;
}
static inline bool TGPeerIdIsAdminLog(int64_t peerId) {
return peerId <= ((int64_t)INT32_MIN) * 3 && peerId > ((int64_t)INT32_MIN) * 4;
}
static inline bool TGPeerIdIsAd(int64_t peerId) {
return peerId <= ((int64_t)INT32_MIN) * 4 && peerId > ((int64_t)INT32_MIN) * 5;
}
static inline int32_t TGChannelIdFromPeerId(int64_t peerId) {
if (TGPeerIdIsChannel(peerId)) {
return (int32_t)(((int64_t)INT32_MIN) * 2 - peerId);
} else {
return 0;
}
}
static inline int64_t TGPeerIdFromChannelId(int32_t channelId) {
return ((int64_t)INT32_MIN) * 2 - ((int64_t)channelId);
}
static inline int64_t TGPeerIdFromAdminLogId(int32_t channelId) {
return ((int64_t)INT32_MIN) * 3 - ((int64_t)channelId);
}
static inline int64_t TGPeerIdFromAdId(int32_t channelId) {
return ((int64_t)INT32_MIN) * 4 - ((int64_t)channelId);
}
static inline int64_t TGPeerIdFromGroupId(int32_t groupId) {
return -groupId;
}
static inline int32_t TGGroupIdFromPeerId(int64_t peerId) {
if (TGPeerIdIsGroup(peerId)) {
return (int32_t)-peerId;
} else {
return 0;
}
}
static inline int32_t TGAdminLogIdFromPeerId(int64_t peerId) {
if (TGPeerIdIsAdminLog(peerId)) {
return (int32_t)(((int64_t)INT32_MIN) * 3 - peerId);
} else {
return 0;
}
}
static inline int32_t TGAdIdFromPeerId(int64_t peerId) {
if (TGPeerIdIsAd(peerId)) {
return (int32_t)(((int64_t)INT32_MIN) * 4 - peerId);
} else {
return 0;
}
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_CHANNEL;
}
static inline bool TGPeerIdIsSecretChat(int64_t peerId) {
return peerId <= ((int64_t)INT32_MIN) && peerId > ((int64_t)INT32_MIN) * 2;
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_SECRET_CHAT;
}
static inline bool TGPeerIdIsAdminLog(int64_t peerId) {
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_ADMIN_LOG;
}
static inline bool TGPeerIdIsAd(int64_t peerId) {
return TGPeerIdGetNamespace(peerId) == TG_NAMESPACE_AD;
}
// Conversion functions
static inline int64_t TGPeerIdFromUserId(int64_t userId) {
return TGPeerIdMake(TG_NAMESPACE_CLOUD, userId);
}
static inline int64_t TGPeerIdFromGroupId(int64_t groupId) {
return TGPeerIdMake(TG_NAMESPACE_GROUP, groupId);
}
static inline int64_t TGPeerIdFromChannelId(int64_t channelId) {
return TGPeerIdMake(TG_NAMESPACE_CHANNEL, channelId);
}
static inline int64_t TGPeerIdFromSecretChatId(int64_t secretChatId) {
return TGPeerIdMake(TG_NAMESPACE_SECRET_CHAT, secretChatId);
}
static inline int64_t TGPeerIdFromAdminLogId(int64_t adminLogId) {
return TGPeerIdMake(TG_NAMESPACE_ADMIN_LOG, adminLogId);
}
static inline int64_t TGPeerIdFromAdId(int64_t adId) {
return TGPeerIdMake(TG_NAMESPACE_AD, adId);
}
// Extract IDs
static inline int64_t TGUserIdFromPeerId(int64_t peerId) {
return TGPeerIdIsUser(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGGroupIdFromPeerId(int64_t peerId) {
return TGPeerIdIsGroup(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGChannelIdFromPeerId(int64_t peerId) {
return TGPeerIdIsChannel(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGSecretChatIdFromPeerId(int64_t peerId) {
return TGPeerIdIsSecretChat(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGAdminLogIdFromPeerId(int64_t peerId) {
return TGPeerIdIsAdminLog(peerId) ? TGPeerIdGetId(peerId) : 0;
}
static inline int64_t TGAdIdFromPeerId(int64_t peerId) {
return TGPeerIdIsAd(peerId) ? TGPeerIdGetId(peerId) : 0;
}
#endif