All files / buffers/src/utf8 decodeAscii.ts

46.15% Statements 54/117
50% Branches 31/62
100% Functions 2/2
43.8% Lines 46/105

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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 1685x                                                   5x 2x 2x 14x 14x 12x         5x 17x 2x 1x                   1x 1x 1x       1x                 15x 15x 15x 15x 15x 10x 10x   5x 3x   2x 2x       2x   2x 1x 1x 1x       1x               1x 1x 1x 1x 1x       1x                   1x 1x 1x 1x       1x 1x 1x       1x                                                                              
const fromCharCode = String.fromCharCode;
 
/** This code was borrowed form cbor-x under the MIT license. */
 
// MIT License
 
// Copyright (c) 2020 Kris Zyp
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
 
export const decodeAscii = (src: Uint8Array, position: number, length: number): string | undefined => {
  const bytes = [];
  for (let i = 0; i < length; i++) {
    const byte = src[position++];
    if (byte & 0x80) return;
    bytes.push(byte);
  }
  return fromCharCode.apply(String, bytes);
};
 
export const decodeAsciiMax15 = (src: Uint8Array, position: number, length: number): string | undefined => {
  if (length < 4) {
    if (length < 2) {
      if (length === 0) return '';
      else E{
        const a = src[position++];
        Iif ((a & 0x80) > 1) {
          position -= 1;
          return;
        }
        return fromCharCode(a);
      }
    } else {
      const a = src[position++];
      const b = src[position++];
      Iif ((a & 0x80) > 0 || (b & 0x80) > 0) {
        position -= 2;
        return;
      }
      if (length < 3) return fromCharCode(a, b);
      const c = src[position++];
      Iif ((c & 0x80) > 0) {
        position -= 3;
        return;
      }
      return fromCharCode(a, b, c);
    }
  } else {
    const a = src[position++];
    const b = src[position++];
    const c = src[position++];
    const d = src[position++];
    if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
      position -= 4;
      return;
    }
    if (length < 6) {
      if (length === 4) return fromCharCode(a, b, c, d);
      else {
        const e = src[position++];
        Iif ((e & 0x80) > 0) {
          position -= 5;
          return;
        }
        return fromCharCode(a, b, c, d, e);
      }
    } else if (length < 8) {
      const e = src[position++];
      const f = src[position++];
      Iif ((e & 0x80) > 0 || (f & 0x80) > 0) {
        position -= 6;
        return;
      }
      if (length < 7) return fromCharCode(a, b, c, d, e, f);
      const g = src[position++];
      Iif ((g & 0x80) > 0) {
        position -= 7;
        return;
      }
      return fromCharCode(a, b, c, d, e, f, g);
    } else {
      const e = src[position++];
      const f = src[position++];
      const g = src[position++];
      const h = src[position++];
      Iif ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
        position -= 8;
        return;
      }
      Iif (length < 10) {
        if (length === 8) return fromCharCode(a, b, c, d, e, f, g, h);
        else {
          const i = src[position++];
          Iif ((i & 0x80) > 0) {
            position -= 9;
            return;
          }
          return fromCharCode(a, b, c, d, e, f, g, h, i);
        }
      } else if (length < 12) {
        const i = src[position++];
        const j = src[position++];
        Iif ((i & 0x80) > 0 || (j & 0x80) > 0) {
          position -= 10;
          return;
        }
        Iif (length < 11) return fromCharCode(a, b, c, d, e, f, g, h, i, j);
        const k = src[position++];
        Iif ((k & 0x80) > 0) {
          position -= 11;
          return;
        }
        return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
      } else E{
        const i = src[position++];
        const j = src[position++];
        const k = src[position++];
        const l = src[position++];
        Iif ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
          position -= 12;
          return;
        }
        if (length < 14) {
          if (length === 12) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
          else {
            const m = src[position++];
            Iif ((m & 0x80) > 0) {
              position -= 13;
              return;
            }
            return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
          }
        } else {
          const m = src[position++];
          const n = src[position++];
          Iif ((m & 0x80) > 0 || (n & 0x80) > 0) {
            position -= 14;
            return;
          }
          Iif (length < 15) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
          const o = src[position++];
          Iif ((o & 0x80) > 0) {
            position -= 15;
            return;
          }
          return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
        }
      }
    }
  }
};