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

org.sweble.wikitext.lazy.utils.ParserCharSequence.rats Maven / Gradle / Ivy

There is a newer version: 3.1.9
Show newest version
/**
 * Copyright 2011 The Open Source Research Group,
 *                University of Erlangen-Nürnberg
 *
 * Licensed 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.
 */

/*!
 *
 * ParserCharSequence
 * ------------------
 *
 *   Wraps the parser stream offered by xtc.parser.ParserBase as CharSequence.
 *
 */

module org.sweble.wikitext.lazy.utils.ParserCharSequence;




// -- Header / Body / Footer ---------------------------------------------------

header
{
  import java.util.regex.Pattern;
  import java.util.regex.Matcher;
}

body
{
  public class ParserCharSequence
          implements
              CharSequence
  {
    protected int base;

    protected int length;

    public ParserCharSequence(int base)
    {
      if (base < 0 || base > yyData.length)
        throw new IndexOutOfBoundsException("Parameter base out of bounds");

      this.base = base;
      this.length = yyData.length - base;
    }

    public ParserCharSequence(int start, int end)
    {
      if (start < 0 || start > yyData.length)
        throw new IndexOutOfBoundsException("Parameter start out of bounds");

      if (end < start || end > yyData.length)
        throw new IndexOutOfBoundsException("Parameter end out of bounds");

      this.base = start;
      this.length = end - start;
    }

    public char charAt(int index) throws IndexOutOfBoundsException
    {
      try
      {
        return (char) character(base + index);
      }
      catch (IOException e)
      {
        e.printStackTrace();
        throw new IndexOutOfBoundsException(
            "Failed to retrieve character! " +
            "See stack trace for more information");
      }
    }

    public int length()
    {
      return length;
    }

    public CharSequence subSequence(int start, int end)
    {
      return new ParserCharSequence(base + start, base + end);
    }

    public String toString()
    {
      final int len = length();
      char[] chs = new char[len];
      for (int i = 0; i < len; ++i)
        chs[i] = charAt(i);
      return new String(chs);
    }
  }
}




// -- End of file --------------------------------------------------------------




© 2015 - 2024 Weber Informatics LLC | Privacy Policy