encodeOrderable static method

Uint8List encodeOrderable(
  1. Object value
)

Encode value to orderable bytes for range index.

Implementation

static Uint8List encodeOrderable(Object value) {
  if (value is num) {
    final buf = Uint8List(9);
    buf[0] = 0x01;
    final bd = ByteData.sublistView(buf, 1);
    bd.setFloat64(0, value.toDouble(), Endian.big);
    if (value >= 0) {
      buf[1] |= 0x80;
    } else {
      for (int i = 1; i < 9; i++) { buf[i] ^= 0xFF; }
    }
    return buf;
  }
  final encoded = utf8.encode(value.toString());
  final buf = Uint8List(1 + encoded.length);
  buf[0] = 0x02;
  buf.setRange(1, buf.length, encoded);
  return buf;
}