Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2015 Santhosh Kumar Tekuri The JLibs authors license this file to you under the Apache License, version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package jlibs.nbp;

Author(s):
Santhosh Kumar T
 
 public final class Stream{
     final int chars[];
     int begin = 0;
     int end = 0;
 
     public Stream(int capacity){
          = new int[capacity+1];
     }
 
     int capacity(){
         return .-1;
     }
 
     public int length(){
         int len = -;
         return len<0 ? len+. : len;
     }
 
     public int charAt(int index){
         assert index>=0 && index<length();
         return [(+index)%.];
     }
 
     public void clear(){
          =  = . = 0;
     }
 
     String toString(int length){
         StringBuilder buff = new StringBuilder();
         for(int i=0; i<lengthi++){
             int data = charAt(i);
             if(data==-1)
                 buff.append("<EOF>");
             else
                 buff.appendCodePoint(data);
         }
         return buff.toString();
     }
 
     @Override
     public String toString(){
         return toString(length());
     }
 
     public final LookAhead lookAhead = new LookAhead();
     public final class LookAhead{
         int end;
 
         public int length(){
             int len = -;
             return len<0 ? len+. : len;
         }
 
         public boolean isEmpty(){
             return ==;
         }
 
         public int charAt(int index){
             assert index>=0 && index<length();
             return [(+index)%.];
         }
 
         // returns true if this was a fresh data added
         public boolean add(int ch){
             if(this.!=Stream.this.){ // hasNext()
                 assert getNext()==ch : "expected char: "+ch;
                  = (+1)%.;
                 return false;
             }else{
                 assert capacity()>Stream.this.length() : "Stream is Full";
                 [] = ch;
                 this. = Stream.this. = (+1)%.;
                 return true;
             }
         }
 
         public void consumed(){
             assert Stream.this.length()>0 : "nothing found to consume";
            
            if(==// length()==0
                 =  = (+1)%.;
            else
                 = (+1)%.;
        }
        public void reset(){
             = ;
        }
        public int getNext(){
            return this.==Stream.this. ? -2 : [];
        }
        @Override
        public String toString(){
            return Stream.this.toString(length());
        }
    }
New to GrepCode? Check out our FAQ X