# All files / src/utils string.js

100% Statements 33/33
100% Branches 4/4
100% Functions 2/2
100% Lines 33/33

    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
    4923x

    52x
    52x
    52x
    52x
    52x
    53x
    53x
    53x
    53x
    52x

    1x

    52x
    52x
    1078x
    1078x
    1078x
    1079x
    1079x
    1079x
    1079x
    1078x

    1x

    1078x
    1078x

    115x
    115x

    115x
    115x
    115x
    2360x

    115x

    115x

     import OutputStream from "./output-stream";

    export function* stringDecoder(view, start) {
      let length = 0;
      let index = 0;
      let shift = 0;
      let addr = start;
      while (true) {
        const byte = view.getUint8(addr, true);
        length |= (byte & 0x7f) << shift;
        addr += 1;
        if ((byte & 0x80) === 0) {
          break;
        }
        shift += 7;
      }

      let result = 0;
      while (index < length) {
        result = 0;
        shift = 0;
        while (true) {
          const byte = view.getUint8(addr, true);
          result |= (byte & 0x7f) << shift;
          addr += 1;
          if ((byte & 0x80) === 0) {
            break;
          }
          shift += 7;
        }
        index += 1;
        yield result;
      }
    }

    export function stringEncoder(value) {
      const resultStream = new OutputStream();
      const characterStream = new OutputStream();

      characterStream.push("varuint32", value.length, value);
      let i = 0;
      for (i = 0; i < value.length; i++) {
        characterStream.push("varuint32", value.codePointAt(i), value[i]);
      }
      resultStream.write(characterStream);

      return resultStream;
    }