gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 749 B
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{ContentError as e}from"../error/ContentError.js";const t=128,n=63,r=new TextEncoder,o=new TextDecoder("utf-8");export const utf8={bytesFromCodePoint:function(r){if(r<128)return[r];if(r<2048)return[r>>>6|192,r&n|t];if(r<65536)return[r>>>12|224,r>>>6&n|t,r&n|t];if(r<2162687)return[r>>18|240,r>>12&n|t,r>>6&n|t,r&n|t];throw new e("Only 21 bits of space","Unicode",r)},codePointFromBytes:function(e){return e[0]>>7?e[0]>>5==6?2!=e.length?-1:(31&e[0])<<6|e[1]&n:e[0]>>4==14?3!=e.length?-1:(15&e[0])<<12|(e[1]&n)<<6|e[2]&n:4!=e.length?-1:(7&e[0])<<18|(e[1]&n)<<12|(e[2]&n)<<6|e[3]&n:1!=e.length?-1:e[0]},toBytes:function(e){return r.encode(e)},fromBytes:function(e){return o.decode(e)}};