首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电报TL模式语言中“标志”类型的处理

电报TL模式语言中“标志”类型的处理
EN

Stack Overflow用户
提问于 2016-08-11 18:07:27
回答 1查看 1.1K关注 0票数 2

我编写了一个tl解析器,现在可以使用最新的层(53)。但我不知道如何处理“旗子”类型。它们只在tl文档中提到,但在页面底部没有定义(据我所知):链接

例如,当方法返回“消息”类型时,应该如下所示:message#c09be45f flags:# out:flags.1?true mentioned:flags.4?true media_unread:flags.5?true silent:flags.13?true post:flags.14?true id:int from_id:flags.8?int to_id:Peer fwd_from:flags.2?MessageFwdHeader via_bot_id:flags.11?int reply_to_msg_id:flags.3?int date:int message:string media:flags.9?MessageMedia reply_markup:flags.6?ReplyMarkup entities:flags.7?Vector<MessageEntity> views:flags.10?int edit_date:flags.15?int = Message;

如果我正确地理解了每个标志是在某个变量中设置的,对吗?

我的解析器将“message”类型分解如下:

id: -1063525281 params: name: flags type: name: out bit: 1 type: true name: mentioned bit: 4 type: true name: media\_unread bit: 5 type: true name: silent bit: 13 type: true name: post bit: 14 type: true name: from\_id bit: 8 type: int name: fwd\_from bit: 2 type: MessageFwdHeader name: via\_bot\_id bit: 11 type: int name: reply\_to\_msg\_id bit: 3 type: int name: media bit: 9 type: MessageMedia name: reply\_markup bit: 6 type: ReplyMarkup name: entities bit: 7 type: Vector<MessageEntity> name: views bit: 10 type: int name: edit\_date bit: 15 type: int name: id type: int name: to\_id type: Peer name: date type: int name: message type: string predicate: message type: Message

但是如果标志是某个变量中的位,那么是哪个变量呢?

相关: tl是基于一种正式的、标准化的语言规范,还是专门为电报创建的?我之所以这样问是因为如果它是一种正式语言的子集(比如yaml),那么最好使用已知的解析器来进行tl,而不是重新发明轮子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-11 19:16:55

但是如果标志是某个变量中的位,那么是哪个变量呢?

代码语言:javascript
复制
Message#c09be45f flags:# out:flags.1?true mentioned:flags.4?true media_unread:flags.5?true silent:flags.13?true post:flags.14?true id:int from_id:flags.8?int to_id:Peer fwd_from:flags.2?MessageFwdHeader via_bot_id:flags.11?int reply_to_msg_id:flags.3?int date:int message:string media:flags.9?MessageMedia reply_markup:flags.6?ReplyMarkup entities:flags.7?Vector<MessageEntity> views:flags.10?int edit_date:flags.15?int = Message;

示例flags:# out:flags.1?true

解码标志:BinaryAND(标志,2^ix) === 2^ix ->这将帮助您确定是否包含字段

标志=值如果是flags字段,这通常是带有标志的对象的第一个字段

==标志索引,这是一个表示标志位置的数字,例如,这里的out:flags.1?true是标志位置1中的字段,类型为真

对于上面的示例,如果不是out,则true的值为BAND(flags,2^N) == 2^N,则忽略out字段。

代码-消息编码(Elixir)

代码语言:javascript
复制
def encode(%Message{} = x), do: <<95, 228, 155, 192, encode(:Int, x.flags)::binary, enc_f(:True, x.out, x.flags, 2)::binary, enc_f(:True, x.mentioned, x.flags, 16)::binary, enc_f(:True, x.media_unread, x.flags, 32)::binary, enc_f(:True, x.silent, x.flags, 8192)::binary, enc_f(:True, x.post, x.flags, 16384)::binary, encode(:Int, x.id)::binary, enc_f(:Int, x.from_id, x.flags, 256)::binary, encode(x.to_id)::binary, enc_f(x.fwd_from, x.flags, 4)::binary, enc_f(:Int, x.via_bot_id, x.flags, 2048)::binary, enc_f(:Int, x.reply_to_msg_id, x.flags, 8)::binary, encode(:Int, x.date)::binary, encode(:String, x.message)::binary, enc_f(x.media, x.flags, 512)::binary, enc_f(x.reply_markup, x.flags, 64)::binary, enc_vf(x.entities, x.flags, 128)::binary, enc_f(:Int, x.views, x.flags, 1024)::binary, enc_f(:Int, x.edit_date, x.flags, 32768)::binary>>

代码-消息解码(Elixir)

代码语言:javascript
复制
  def decode(<<95, 228, 155, 192, bin::binary>>) do
    {flags, bin} = decode(:Int, bin)
    {out, bin} = decode(:True, bin, flags, 2) # 1
    {mentioned, bin} = decode(:True, bin, flags, 16) # 4
    {media_unread, bin} = decode(:True, bin, flags, 32) # 5
    {silent, bin} = decode(:True, bin, flags, 8192) # 13
    {post, bin} = decode(:True, bin, flags, 16384) # 14
    {id, bin} = decode(:Int, bin)
    {from_id, bin} = decode(:Int, bin, flags, 256) # 8
    {to_id, bin} = decode(bin)
    {fwd_from, bin} = decode(bin, flags, 4) # 2
    {via_bot_id, bin} = decode(:Int, bin, flags, 2048) # 11
    {reply_to_msg_id, bin} = decode(:Int, bin, flags, 8) # 3
    {date, bin} = decode(:Int, bin)
    {message, bin} = decode(:String, bin)
    {media, bin} = decode(bin, flags, 512) # 9
    {reply_markup, bin} = decode(bin, flags, 64) # 6
    {entities, bin} = decode([:MessageEntity], bin, flags, 128) # 7
    {views, bin} = decode(:Int, bin, flags, 1024) # 10
    {edit_date, bin} = decode(:Int, bin, flags, 32768) # 15
    {%Message{flags: flags, out: out, mentioned: mentioned, media_unread: media_unread, silent: silent, post: post, id: id, from_id: from_id, to_id: to_id, fwd_from: fwd_from, via_bot_id: via_bot_id, reply_to_msg_id: reply_to_msg_id, date: date, message: message, media: media, reply_markup: reply_markup, entities: entities, views: views, edit_date: edit_date}, bin}
  end

#5 == 2^5 == 32
#4 == 2^4 == 16

所以基本上是N == 2^N, where N == ix

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38903321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档