2002/03/21 01:19:32
[org.ibex.core.git] / src / org / mozilla / javascript / BinaryDigitReader.java
diff --git a/src/org/mozilla/javascript/BinaryDigitReader.java b/src/org/mozilla/javascript/BinaryDigitReader.java
new file mode 100644 (file)
index 0000000..a6b4771
--- /dev/null
@@ -0,0 +1,75 @@
+\r
+/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
+ *\r
+ * The contents of this file are subject to the Netscape Public\r
+ * License Version 1.1 (the "License"); you may not use this file\r
+ * except in compliance with the License. You may obtain a copy of\r
+ * the License at http://www.mozilla.org/NPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS\r
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
+ * implied. See the License for the specific language governing\r
+ * rights and limitations under the License.\r
+ *\r
+ * The Original Code is Rhino code, released\r
+ * May 6, 1999.\r
+ *\r
+ * The Initial Developer of the Original Code is Netscape\r
+ * Communications Corporation.  Portions created by Netscape are\r
+ * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
+ * Rights Reserved.\r
+ *\r
+ * Contributor(s): \r
+ * Waldemar Horwat\r
+ *\r
+ * Alternatively, the contents of this file may be used under the\r
+ * terms of the GNU Public License (the "GPL"), in which case the\r
+ * provisions of the GPL are applicable instead of those above.\r
+ * If you wish to allow use of your version of this file only\r
+ * under the terms of the GPL and not to allow others to use your\r
+ * version of this file under the NPL, indicate your decision by\r
+ * deleting the provisions above and replace them with the notice\r
+ * and other provisions required by the GPL.  If you do not delete\r
+ * the provisions above, a recipient may use your version of this\r
+ * file under either the NPL or the GPL.\r
+ */\r
+package org.mozilla.javascript;\r
+\r
+final class BinaryDigitReader {\r
+    int lgBase;                        // Logarithm of base of number\r
+    int digit;                 // Current digit value in radix given by base\r
+    int digitPos;              // Bit position of last bit extracted from digit\r
+    String digits;             // String containing the digits\r
+    int start;                 // Index of the first remaining digit\r
+    int end;                   // Index past the last remaining digit\r
+\r
+    BinaryDigitReader(int base, String digits, int start, int end) {\r
+        lgBase = 0;\r
+        while (base != 1) {\r
+            lgBase++;\r
+            base >>= 1;\r
+        }\r
+        digitPos = 0;\r
+        this.digits = digits;\r
+        this.start = start;\r
+        this.end = end;\r
+    }\r
+\r
+    /* Return the next binary digit from the number or -1 if done */\r
+    int getNextBinaryDigit()\r
+    {\r
+        if (digitPos == 0) {\r
+            if (start == end)\r
+                return -1;\r
+    \r
+            char c = digits.charAt(start++);\r
+            if ('0' <= c && c <= '9')\r
+                digit = c - '0';\r
+            else if ('a' <= c && c <= 'z')\r
+                digit = c - 'a' + 10;\r
+            else digit = c - 'A' + 10;\r
+            digitPos = lgBase;\r
+        }\r
+        return digit >> --digitPos & 1;\r
+    }\r
+}\r