MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / getChunkBegin

Function getChunkBegin

src/pkg/utils/istextorbinary.ts:104–144  ·  view source on GitHub ↗
(buf: Uint8Array, chunkBegin: number)

Source from the content-addressed store, hash-verified

102// @todo add documentation for these
103
104function getChunkBegin(buf: Uint8Array, chunkBegin: number) {
105 // If it's the beginning, just return.
106 if (chunkBegin === 0) {
107 return 0;
108 }
109
110 if (!isLaterByteOfUtf8(buf[chunkBegin])) {
111 return chunkBegin;
112 }
113
114 let begin = chunkBegin - 3;
115
116 if (begin >= 0) {
117 if (isFirstByteOf4ByteChar(buf[begin])) {
118 return begin;
119 }
120 }
121
122 begin = chunkBegin - 2;
123
124 if (begin >= 0) {
125 if (isFirstByteOf4ByteChar(buf[begin]) || isFirstByteOf3ByteChar(buf[begin])) {
126 return begin;
127 }
128 }
129
130 begin = chunkBegin - 1;
131
132 if (begin >= 0) {
133 // Is it a 4-byte, 3-byte utf8 character?
134 if (
135 isFirstByteOf4ByteChar(buf[begin]) ||
136 isFirstByteOf3ByteChar(buf[begin]) ||
137 isFirstByteOf2ByteChar(buf[begin])
138 ) {
139 return begin;
140 }
141 }
142
143 return -1;
144}
145
146function getChunkEnd(buf: Uint8Array, chunkEnd: number) {
147 // If it's the end, just return.

Callers 1

getEncodingFunction · 0.85

Calls 4

isLaterByteOfUtf8Function · 0.85
isFirstByteOf4ByteCharFunction · 0.85
isFirstByteOf3ByteCharFunction · 0.85
isFirstByteOf2ByteCharFunction · 0.85

Tested by

no test coverage detected