Exemplo n.º 1
0
  public static long parseLine(Bytes bytes, Range line, long start, long limit) {
    byte b0 = 0, b1 = 0;
    long ret = -1;

    long i;
    for (i = start; i < limit; i++) {
      b0 = b1;
      b1 = bytes.get(i);

      if (b1 == LF) {
        long len;

        if (b0 == CR) {
          len = i - start - 1;
        } else {
          len = i - start;
        }

        line.set(start, len);
        ret = i + 1;
        break;
      }
    }

    return ret;
  }
Exemplo n.º 2
0
  public static boolean startsWith(Bytes bytes, Range target, byte[] match, boolean caseSensitive) {

    if (target.length < match.length || target.start < 0 || target.last() >= bytes.limit()) {
      return false;
    }

    boolean result = match(bytes, target.start, match, caseSensitive);

    return result;
  }
Exemplo n.º 3
0
  public static boolean split(
      Bytes bytes, Range target, byte sep, Range before, Range after, boolean trimParts) {

    long pos = find(bytes, target.start, target.limit(), sep, true);

    if (pos >= 0) {
      before.setInterval(target.start, pos);
      after.setInterval(pos + 1, target.limit());

      if (trimParts) {
        trim(bytes, before);
        trim(bytes, after);
      }

      return true;
    } else {
      before.assign(target);
      after.reset();

      if (trimParts) {
        trim(bytes, before);
      }

      return false;
    }
  }
Exemplo n.º 4
0
  public static void trim(Bytes bytes, Range target) {

    long start = target.start;
    long len = target.length;
    long finish = start + len - 1;

    if (start < 0 || len == 0) {

      return;
    }

    while (start < finish && bytes.get(start) == ' ') {
      start++;
    }

    while (start < finish && bytes.get(finish) == ' ') {
      finish--;
    }

    target.start = start;
    target.length = finish - start + 1;
  }
Exemplo n.º 5
0
  public static boolean containsAt(
      Bytes bytes, Range target, long offset, byte[] match, boolean caseSensitive) {

    if (offset < 0
        || target.length < offset + match.length
        || target.start < 0
        || target.last() >= bytes.limit()) {
      return false;
    }

    boolean result = match(bytes, target.start + offset, match, caseSensitive);

    return result;
  }
Exemplo n.º 6
0
  /**
   * Scans the buffer until a line separator (CRLF or LF) is found, and matches the 4-byte prefix of
   * the scanned selection against the specified search prefix. Returns the position of the
   * separator, or <code>-1</code> if the limit is reached and separator not found. If the prefix is
   * matched, the negative of the position is returned, to mark the prefix match. Duplicated code
   * for performance reasons.
   */
  public static long scanLnAndMatchPrefix(
      Bytes bytes, Range result, long fromPos, long toPos, long searchPrefix) {

    byte b0, b1, b2, b3;

    long p = fromPos;
    if (p <= toPos) {
      b0 = bytes.get(p);
      if (b0 == LF) {
        result.set(fromPos, 0);
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    p++;
    if (p <= toPos) {
      b1 = bytes.get(p);
      if (b1 == LF) {
        if (b0 == CR) {
          result.set(fromPos, 0);
        } else {
          result.set(fromPos, 1);
        }
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    p++;
    if (p <= toPos) {
      b2 = bytes.get(p);
      if (b2 == LF) {
        if (b1 == CR) {
          result.set(fromPos, 1);
        } else {
          result.set(fromPos, 2);
        }
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    p++;
    if (p <= toPos) {
      b3 = bytes.get(p);
      if (b3 == LF) {
        if (b2 == CR) {
          result.set(fromPos, 2);
        } else {
          result.set(fromPos, 3);
        }
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    long prefix = UTILS.intFrom(b0, b1, b2, b3);

    boolean matchedPrefix = prefix == searchPrefix;

    for (long i = p; i <= toPos; i++) {
      if (bytes.get(i) == LF) {

        if (bytes.get(i - 1) == CR) {
          result.setInterval(fromPos, i - 1);
        } else {
          result.setInterval(fromPos, i);
        }

        long nextPos = i + 1;
        return matchedPrefix ? -nextPos : nextPos;
      }
    }

    result.reset();
    return NOT_FOUND;
  }