Java tutorial
//package com.java2s; /* Copyright (c) 2010 Xiaoyun Zhu * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.nio.ByteBuffer; public class Main { public static final int indexOfP(final ByteBuffer bb, final byte b) { return indexOf(bb.array(), bb.position(), bb.remaining(), b); } public static final int indexOfP(final ByteBuffer lineBB, final byte[][] textsLower, final byte[][] textsUpper) { return indexOf(lineBB.array(), lineBB.position(), lineBB.limit(), textsLower, textsUpper); } /** * * @param bb * @param offset * absolute * @param limit * absolute * @param b * @return absolute index */ public static final int indexOf(final byte[] bb, final int offset, final int limit, final byte b) { for (int i = offset; i < limit; i++) { if (bb[i] == b) { return i; } } return -1; } /** * * @param text * @param offset * @param len * relative * @param s * @return absolute index */ public final static int indexOf(final byte[] text, final int offset, final int len, final byte[] s) { return indexOf(text, offset, len, s, 0, s.length); } /** * * @param text * @param offset * @param len1 * relative * @param s * @param offset2 * @param len2 * relative * @return absolute index */ public final static int indexOf(final byte[] text, final int offset, final int len1, final byte[] s, final int offset2, final int len2) { if (len1 >= len2) { final int limit = offset + len1; int idx = 0; byte b; for (int i = offset; i < limit; i++) { b = text[i]; if (b == s[offset2 + idx]) { if (++idx == len2) { return i - len2 + 1; } } else { idx = 0; } } } return -1; } public static final int indexOf(final ByteBuffer bb, final byte b) { return indexOf(bb.array(), 0, bb.limit(), b); } public final static int indexOf(final char[][] pairs, final char c) { int i = 0; for (final char[] pair : pairs) { if (c == pair[0]) { return i; } i++; } return -1; } public static final int indexOf(final byte[] array, final int position, final int limit, final byte[][] textsLower, final byte[][] textsUpper) { byte b; int[] idx = new int[textsLower.length]; for (int i = position; i < limit; i++) { b = array[i]; for (int j = 0; j < idx.length; j++) { final int textIdx = idx[j]; final byte[] textLower = textsLower[j]; final byte[] textUpper = textsUpper[j]; if (b == textLower[textIdx] || b == textUpper[textIdx]) { if (textIdx + 1 < textLower.length) { idx[j] = textIdx + 1; } else { return i - textLower.length + 1; } } else { idx[j] = 0; } } } return -1; } }