(buf: Uint8Array, chunkEnd: number)
| 144 | } |
| 145 | |
| 146 | function getChunkEnd(buf: Uint8Array, chunkEnd: number) { |
| 147 | // If it's the end, just return. |
| 148 | if (chunkEnd === buf.length) { |
| 149 | return chunkEnd; |
| 150 | } |
| 151 | |
| 152 | let index = chunkEnd - 3; |
| 153 | |
| 154 | if (index >= 0) { |
| 155 | if (isFirstByteOf4ByteChar(buf[index])) { |
| 156 | return chunkEnd + 1; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | index = chunkEnd - 2; |
| 161 | |
| 162 | if (index >= 0) { |
| 163 | if (isFirstByteOf4ByteChar(buf[index])) { |
| 164 | return chunkEnd + 2; |
| 165 | } |
| 166 | |
| 167 | if (isFirstByteOf3ByteChar(buf[index])) { |
| 168 | return chunkEnd + 1; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | index = chunkEnd - 1; |
| 173 | |
| 174 | if (index >= 0) { |
| 175 | if (isFirstByteOf4ByteChar(buf[index])) { |
| 176 | return chunkEnd + 3; |
| 177 | } |
| 178 | |
| 179 | if (isFirstByteOf3ByteChar(buf[index])) { |
| 180 | return chunkEnd + 2; |
| 181 | } |
| 182 | |
| 183 | if (isFirstByteOf2ByteChar(buf[index])) { |
| 184 | return chunkEnd + 1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return chunkEnd; |
| 189 | } |
| 190 | |
| 191 | function isFirstByteOf4ByteChar(byte: number) { |
| 192 | return byte >> 3 === 30; // 11110xxx? |
no test coverage detected