匹配卫兵中二进制的最后一个字节

时间:2011-10-11 20:57:19

标签: erlang

是否可以使用模式匹配匹配二进制文件中的最后一个字节。类似的东西:

<<Rest/binary, 45>> = BinaryToMatch 

3 个答案:

答案 0 :(得分:4)

简而言之,没有(至少在R14B01中没有)。您还必须知道二进制文件的大小。

Skip = byte_size(BinaryToMatch) - 1,
<<_:Skip/binary, 45>> = BinaryToMatch.

答案 1 :(得分:2)

或者,您也可以45 = binary:last(Bin)。不幸的是,这个功能不是BIF的保护。

答案 2 :(得分:1)

在erlang 19中(可能以前的版本也支持这个,我不测试它们)你可以使用这个警卫:

 public function someMethod(Request $request){
    //1)  instanciate entity manager 
    $em = $this->getDoctrine()->getManager();

    //2)   Fetch some datas ..
    $datas = $em->getRepository('MyCustomBundle:Entity')->findAll();
    $otherDatas = $em->getRepository('MyCustomBundle:AnotherEntity')->findAll();

   //3) Inject datas into view
   return $this->render('MyCustomBundle:Views:myview.html.twig', array('data'=>$datas,'otherDatas'=>$otherDatas));
   }