All files / json-crdt-patch/util/binary CrdtReader.ts

80.72% Statements 67/83
80% Branches 20/25
100% Functions 5/5
83.58% Lines 56/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150192x     192x   1122599x 1122599x 1046194x 1046194x       23815x 23815x 22059x 22059x 22059x       1726026x 1726026x 764064x 764064x 344702x 344702x 302891x 302891x 302105x 302105x 226x       301879x 301879x 907x         300972x 300972x 17478x           283494x 283494x                   33289x 33289x 21908x 21908x                             1534947x 1534947x 1534947x 1534947x 266627x 266627x 90779x 90779x 14319x 14319x 14205x 114x 114x 28x         86x 86x 32x           54x 54x 32x                   22x 22x                                                                
import {Reader} from '@jsonjoy.com/util/lib/buffers/Reader';
 
/** @todo Rename file name. */
export class CrdtReader extends Reader {
  public id(): [x: number, y: number] {
    const byte = this.u8();
    if (byte <= 0b0_111_1111) return [byte >>> 4, byte & 0b1111];
    this.x--;
    return [this.b1vu56()[1], this.vu57()];
  }
 
  public idSkip(): void {
    const byte = this.u8();
    if (byte <= 0b0_111_1111) return;
    this.x--;
    this.b1vu56();
    this.vu57Skip();
  }
 
  public vu57(): number {
    const o1 = this.u8();
    if (o1 <= 0b01111111) return o1;
    const o2 = this.u8();
    if (o2 <= 0b01111111) return (o2 << 7) | (o1 & 0b01111111);
    const o3 = this.u8();
    if (o3 <= 0b01111111) return (o3 << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b01111111);
    const o4 = this.u8();
    if (o4 <= 0b01111111) return (o4 << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b01111111);
    const o5 = this.u8();
    if (o5 <= 0b01111111)
      return (
        o5 * 0b10000000000000000000000000000 +
        (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b0111_1111))
      );
    const o6 = this.u8();
    if (o6 <= 0b01111111)
      return (
        o6 * 0b100000000000000000000000000000000000 +
        ((o5 & 0b01111111) * 0b10000000000000000000000000000 +
          (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b0111_1111)))
      );
    const o7 = this.u8();
    if (o7 <= 0b01111111)
      return (
        o7 * 0b1000000000000000000000000000000000000000000 +
        ((o6 & 0b01111111) * 0b100000000000000000000000000000000000 +
          ((o5 & 0b01111111) * 0b10000000000000000000000000000 +
            (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b0111_1111))))
      );
    const o8 = this.u8();
    return (
      o8 * 0b10000000000000000000000000000000000000000000000000 +
      ((o7 & 0b01111111) * 0b1000000000000000000000000000000000000000000 +
        ((o6 & 0b01111111) * 0b100000000000000000000000000000000000 +
          ((o5 & 0b01111111) * 0b10000000000000000000000000000 +
            (((o4 & 0b01111111) << 21) | ((o3 & 0b01111111) << 14) | ((o2 & 0b01111111) << 7) | (o1 & 0b0111_1111)))))
    );
  }
 
  public vu57Skip(): void {
    const o1 = this.u8();
    if (o1 <= 0b01111111) return;
    const o2 = this.u8();
    if (o2 <= 0b01111111) return;
    const o3 = this.u8();
    Iif (o3 <= 0b01111111) return;
    const o4 = this.u8();
    Iif (o4 <= 0b01111111) return;
    const o5 = this.u8();
    Iif (o5 <= 0b01111111) return;
    const o6 = this.u8();
    Iif (o6 <= 0b01111111) return;
    const o7 = this.u8();
    Iif (o7 <= 0b01111111) return;
    this.x++;
  }
 
  public b1vu56(): [flag: 0 | 1, value56: number] {
    const byte = this.u8();
    const flag: 0 | 1 = byte & 0b10000000 ? 1 : 0;
    const o1 = 0b0_1_111111 & byte;
    if (o1 <= 0b0_0_111111) return [flag, o1];
    const o2 = this.u8();
    if (o2 <= 0b01111111) return [flag, (o2 << 6) | (o1 & 0b0_0_111111)];
    const o3 = this.u8();
    if (o3 <= 0b01111111) return [flag, (o3 << 13) | ((o2 & 0b01111111) << 6) | (o1 & 0b0_0_111111)];
    const o4 = this.u8();
    if (o4 <= 0b01111111)
      return [flag, (o4 << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 0b0_0_111111)];
    const o5 = this.u8();
    if (o5 <= 0b01111111)
      return [
        flag,
        o5 * 0b1000000000000000000000000000 +
          (((o4 & 0b01111111) << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 0b0_0_111111)),
      ];
    const o6 = this.u8();
    if (o6 <= 0b01111111)
      return [
        flag,
        o6 * 0b10000000000000000000000000000000000 +
          ((o5 & 0b01111111) * 0b1000000000000000000000000000 +
            (((o4 & 0b01111111) << 20) | ((o3 & 0b01111111) << 13) | ((o2 & 0b01111111) << 6) | (o1 & 0b0_0_111111))),
      ];
    const o7 = this.u8();
    if (o7 <= 0b01111111)
      return [
        flag,
        o7 * 0b100000000000000000000000000000000000000000 +
          ((o6 & 0b01111111) * 0b10000000000000000000000000000000000 +
            ((o5 & 0b01111111) * 0b1000000000000000000000000000 +
              (((o4 & 0b01111111) << 20) |
                ((o3 & 0b01111111) << 13) |
                ((o2 & 0b01111111) << 6) |
                (o1 & 0b0_0_111111)))),
      ];
    const o8 = this.u8();
    return [
      flag,
      o8 * 0b1000000000000000000000000000000000000000000000000 +
        ((o7 & 0b01111111) * 0b100000000000000000000000000000000000000000 +
          ((o6 & 0b01111111) * 0b10000000000000000000000000000000000 +
            ((o5 & 0b01111111) * 0b1000000000000000000000000000 +
              (((o4 & 0b01111111) << 20) |
                ((o3 & 0b01111111) << 13) |
                ((o2 & 0b01111111) << 6) |
                (o1 & 0b0_0_111111))))),
    ];
  }
 
  // public b1vu56Skip(): void {
  //   const byte = this.u8();
  //   const o1 = 0b0_1_111111 & byte;
  //   if (o1 <= 0b0_0_111111) return;
  //   const o2 = this.u8();
  //   if (o2 <= 0b01111111) return;
  //   const o3 = this.u8();
  //   if (o3 <= 0b01111111) return;
  //   const o4 = this.u8();
  //   if (o4 <= 0b01111111) return;
  //   const o5 = this.u8();
  //   if (o5 <= 0b01111111) return;
  //   const o6 = this.u8();
  //   if (o6 <= 0b01111111) return;
  //   const o7 = this.u8();
  //   if (o7 <= 0b01111111) return;
  //   this.u8();
  // }
}