a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(uint8array): add isUtf8LengthInRange function

Mary a60d862f be9873cc

+79
+5
.changeset/lovely-lines-love.md
··· 1 + --- 2 + "@atcute/uint8array": minor 3 + --- 4 + 5 + add isUtf8LengthInRange function
+12
packages/misc/uint8array/lib/index.bun.ts
··· 154 154 return _byteLength(str, 'utf8'); 155 155 }; 156 156 157 + /** 158 + * checks if a string's UTF-8 byte length is within a given range 159 + * @param str string to measure 160 + * @param min minimum byte length (inclusive) 161 + * @param max maximum byte length (inclusive) 162 + * @returns true if byte length is within [min, max] 163 + */ 164 + export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => { 165 + const len = _byteLength(str, 'utf8'); 166 + return len >= min && len <= max; 167 + }; 168 + 157 169 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => { 158 170 return toUint8Array(_hash('sha256', buffer, 'buffer')) as Uint8Array<ArrayBuffer>; 159 171 };
+12
packages/misc/uint8array/lib/index.node.ts
··· 152 152 return _byteLength(str, 'utf8'); 153 153 }; 154 154 155 + /** 156 + * checks if a string's UTF-8 byte length is within a given range 157 + * @param str string to measure 158 + * @param min minimum byte length (inclusive) 159 + * @param max maximum byte length (inclusive) 160 + * @returns true if byte length is within [min, max] 161 + */ 162 + export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => { 163 + const len = _byteLength(str, 'utf8'); 164 + return len >= min && len <= max; 165 + }; 166 + 155 167 export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => { 156 168 return toUint8Array(_hash('sha256', buffer, 'buffer')) as Uint8Array<ArrayBuffer>; 157 169 };
+50
packages/misc/uint8array/lib/index.ts
··· 274 274 }; 275 275 276 276 /** 277 + * checks if a string's UTF-8 byte length is within a given range. 278 + * includes early-exit optimization when exceeding max length. 279 + * @param str string to measure 280 + * @param min minimum byte length (inclusive) 281 + * @param max maximum byte length (inclusive) 282 + * @returns true if byte length is within [min, max] 283 + */ 284 + export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => { 285 + const len = str.length; 286 + 287 + // fast path: if max possible UTF-8 length is below min, fail 288 + if (len * 3 < min) { 289 + return false; 290 + } 291 + 292 + // fast path: if UTF-16 length satisfies min and max possible satisfies max 293 + if (len >= min && len * 3 <= max) { 294 + return true; 295 + } 296 + 297 + let u16pos = 0; 298 + let u8pos = 0; 299 + 300 + while (u16pos < len) { 301 + const code = str.charCodeAt(u16pos); 302 + 303 + if (code < 0x80) { 304 + u16pos += 1; 305 + u8pos += 1; 306 + } else if (code < 0x800) { 307 + u16pos += 1; 308 + u8pos += 2; 309 + } else if (code < 0xd800 || code > 0xdbff) { 310 + u16pos += 1; 311 + u8pos += 3; 312 + } else { 313 + u16pos += 2; 314 + u8pos += 4; 315 + } 316 + 317 + // early exit once we exceed max 318 + if (u8pos > max) { 319 + return false; 320 + } 321 + } 322 + 323 + return u8pos >= min; 324 + }; 325 + 326 + /** 277 327 * get a SHA-256 digest of this buffer 278 328 */ 279 329 export const toSha256 = async (buffer: Uint8Array<ArrayBuffer>): Promise<Uint8Array<ArrayBuffer>> => {