2021-11-23 00:03:59 +03:00
|
|
|
import CRC32 from 'crc-32'
|
2022-06-30 16:32:56 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
import { TlEntry } from './types'
|
|
|
|
import { writeTlEntryToString } from './stringify'
|
|
|
|
|
2022-08-29 14:33:11 +03:00
|
|
|
/**
|
|
|
|
* Computes the constructor id for a given TL entry string.
|
|
|
|
*
|
|
|
|
* @param line Line containing TL entry definition
|
|
|
|
*/
|
2021-11-23 00:03:59 +03:00
|
|
|
export function computeConstructorIdFromString(line: string): number {
|
|
|
|
return (
|
|
|
|
CRC32.str(
|
|
|
|
// normalize
|
|
|
|
line
|
2022-06-30 16:32:56 +03:00
|
|
|
.replace(
|
|
|
|
/[{};]|[a-zA-Z0-9_]+:flags\.[0-9]+\?true|#[0-9a-f]{1,8}/g,
|
|
|
|
''
|
|
|
|
)
|
2021-11-23 00:03:59 +03:00
|
|
|
.replace(/[<>]/g, ' ')
|
|
|
|
.replace(/ +/g, ' ')
|
|
|
|
.replace(':bytes', ':string')
|
|
|
|
.trim()
|
|
|
|
) >>> 0
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-29 14:33:11 +03:00
|
|
|
/**
|
|
|
|
* Computes the constructor id for a given TL entry.
|
|
|
|
*
|
|
|
|
* @param entry TL entry
|
|
|
|
*/
|
2021-11-23 00:03:59 +03:00
|
|
|
export function computeConstructorIdFromEntry(entry: TlEntry): number {
|
|
|
|
return CRC32.str(writeTlEntryToString(entry, true)) >>> 0
|
|
|
|
}
|