Ejemplo 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;
  }
Ejemplo n.º 2
0
  public static long parseLines(Bytes bytes, Ranges lines, long start, long limit) {
    byte b0 = 0, b1 = 0;
    long ret = -1;

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

      if (b1 == LF) {
        long len;

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

        if (len == 0) {
          ret = i + 1;
          break;
        }

        lines.add(from, len);
        from = i + 1;
      }
    }

    return ret;
  }
Ejemplo n.º 3
0
 public static boolean matchNoCase(Bytes bytes, long start, byte[] match, int offset, int length) {
   for (int i = 0; i < length; i++) {
     byte b = bytes.get(start + i);
     if (b != match[offset + i] && (b < 'A' || CHARS_SWITCH_CASE[b] != match[offset + i])) {
       return false;
     }
   }
   return true;
 }
Ejemplo n.º 4
0
  public static long scan(Bytes bytes, long from, long to, byte value) {
    for (long i = from; i <= to; i++) {
      if (bytes.get(i) == value) {
        return i;
      }
    }

    return -1;
  }
Ejemplo n.º 5
0
  public static byte[] getBytes(Bytes bytes, Range range) {
    byte[] byteArr = new byte[(int) range.length];

    for (int i = 0; i < byteArr.length; i++) {
      byteArr[i] = bytes.get(range.start + i);
    }

    return byteArr;
  }
Ejemplo n.º 6
0
  public static boolean matchSensitive(
      Bytes bytes, long start, byte[] match, int offset, int length) {
    for (int i = 0; i < length; i++) {
      if (bytes.get(start + i) != match[offset + i]) {
        return false;
      }
    }

    return true;
  }
Ejemplo n.º 7
0
  public static long scanNoCase(Bytes bytes, long from, long to, byte value) {
    for (long i = from; i <= to; i++) {
      byte b = bytes.get(i);

      if (b == value || (b >= 'A' && CHARS_SWITCH_CASE[b] == value)) {
        return i;
      }
    }

    return -1;
  }
Ejemplo n.º 8
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;
  }
Ejemplo n.º 9
0
  public static long parseLines(
      Bytes bytes, Ranges lines, LongWrap res, long start, long limit, byte end1, byte end2) {
    byte b0 = 0, b1 = 0, b2 = 0, b3 = 0;
    long ret = -1;
    res.value = NOT_FOUND;

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

      if (b3 == LF) {
        long len;

        if (b2 == CR) {
          len = i - from - 1;
          if (b0 == end1 && b1 == end2 && len > 0) {
            res.value = lines.count;
          }
        } else {
          len = i - from;
          if (b1 == end1 && b2 == end2 && len > 0) {
            res.value = lines.count;
          }
        }

        if (len == 0) {
          ret = i + 1;
          break;
        }

        lines.add(from, len);
        from = i + 1;
      }
    }

    return ret;
  }
Ejemplo n.º 10
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;
  }