All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mustachejava.util.RingBuffer Maven / Gradle / Ivy

There is a newer version: 0.9.14
Show newest version
package com.github.mustachejava.util;

/**
* A very simple ring buffer you can use for parsing and keeping a look behind.
*/
public class RingBuffer {
  private final int ringSize;
  private final int ringHash;
  private final char[] ring;
  private int length = 0;

  public RingBuffer(int ringSize) {
    int i = 1;
    while (i < ringSize) {
      i *= 2;
    }
    this.ringSize = i;
    this.ringHash = i - 1;
    ring = new char[i];
  }

  public void append(char c) {
    ring[length++ & ringHash] = c;
  }

  public void clear() {
    length = 0;
  }

  public boolean compare(String s, boolean exact) {
    int len = s.length();
    if (exact && len != length) return false;
    if (len > ringSize) {
      throw new IllegalArgumentException("Ring buffer too small: " + ringSize + " < " + s.length());
    }
    if (length >= len) {
      int j = 0;
      int position = length & ringHash;
      for (int i = position - len; i < position; i++) {
        char c;
        if (i < 0) {
          c = ring[ringSize + i];
        } else {
          c = ring[i];
        }
        if (s.charAt(j++) != c) {
          return false;
        }
      }
    }
    return true;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy