# All files / src/utils string.js

100% Statements 42/42
100% Branches 4/4
100% Functions 4/4
100% Lines 41/41

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

    58x
    58x
    58x
    58x
    58x
    59x
    59x
    59x
    59x
    58x

    1x

    58x
    58x
    1119x
    1119x
    1119x
    1120x
    1120x
    1120x
    1120x
    1119x

    1x

    1119x
    1119x

    63x
    63x

    63x
    63x
    63x
    1226x

    63x

    63x

    22x
    58x
    58x
    58x
    58x
    1119x
    1119x

    58x

     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;
    }

    export const getText = view => ptr => {
      let text = '';
      const decoder = stringDecoder(view, ptr);
      let iterator = decoder.next();
      while (!iterator.done) {
        text += String.fromCodePoint(iterator.value);
        iterator = decoder.next();
      }

      return text;
    };