From 22c0ea19d909c8e46c57d0cbf58a643a57a3eb35 Mon Sep 17 00:00:00 2001
From: Chris Boden <cboden@gmail.com>
Date: Fri, 13 Jul 2012 11:38:16 -0400
Subject: [PATCH] [WebSocket] Performance

Added unit tests for Rsv bit checks
Using binary operators to check bits resulting in massive performance gains
---
 Version/FrameInterface.php |  2 +-
 Version/RFC6455/Frame.php  | 18 +++++-------------
 2 files changed, 6 insertions(+), 14 deletions(-)

diff --git a/Version/FrameInterface.php b/Version/FrameInterface.php
index 4198985..083b6d4 100644
--- a/Version/FrameInterface.php
+++ b/Version/FrameInterface.php
@@ -15,7 +15,7 @@ interface FrameInterface extends DataInterface {
     function isFinal();
 
     /**
-     * Was the payload masked?
+     * Is the payload masked?
      * @return bool
      */
     function isMasked();
diff --git a/Version/RFC6455/Frame.php b/Version/RFC6455/Frame.php
index 93cc68f..4bb39d8 100644
--- a/Version/RFC6455/Frame.php
+++ b/Version/RFC6455/Frame.php
@@ -127,9 +127,7 @@ class Frame implements FrameInterface {
             throw new \UnderflowException('Not enough bytes received to determine if this is the final frame in message');
         }
 
-        $fbb = sprintf('%08b', ord(substr($this->data, 0, 1)));
-
-        return (boolean)(int)$fbb[0];
+        return 128 === (ord($this->data[0]) & 128);
     }
 
     /**
@@ -141,9 +139,7 @@ class Frame implements FrameInterface {
             throw new \UnderflowException('Not enough bytes received to determine reserved bit');
         }
 
-        $fbb = sprintf('%08b', ord(substr($this->data, 0, 1)));
-
-        return (boolean)(int)$fbb[1];
+        return 64 === (ord($this->data[0]) & 64);
     }
 
     /**
@@ -155,9 +151,7 @@ class Frame implements FrameInterface {
             throw new \UnderflowException('Not enough bytes received to determine reserved bit');
         }
 
-        $fbb = sprintf('%08b', ord(substr($this->data, 0, 1)));
-
-        return (boolean)(int)$fbb[2];
+        return 32 === (ord($this->data[0]) & 32);
     }
 
     /**
@@ -169,9 +163,7 @@ class Frame implements FrameInterface {
             throw new \UnderflowException('Not enough bytes received to determine reserved bit');
         }
 
-        $fbb = sprintf('%08b', ord(substr($this->data, 0, 1)));
-
-        return (boolean)(int)$fbb[3];
+        return 16 == (ord($this->data[0]) & 16);
     }
 
     /**
@@ -182,7 +174,7 @@ class Frame implements FrameInterface {
             throw new \UnderflowException("Not enough bytes received ({$this->bytesRecvd}) to determine if mask is set");
         }
 
-        return (boolean)bindec(substr(sprintf('%08b', ord(substr($this->data, 1, 1))), 0, 1));
+        return 128 === (ord($this->data[1]) & 128);
     }
 
     /**