new encoding

This commit is contained in:
Paul Butler 2025-02-08 09:09:56 -05:00
parent bf1d29e704
commit dac64bd184
3 changed files with 48 additions and 12 deletions

View File

@ -7,7 +7,7 @@ export const EMOJI_LIST = [
"👍", "👍",
"👎", "👎",
"👏", "👏",
"🙌", "😅",
"🤝", "🤝",
"🎉", "🎉",
"🎂", "🎂",

View File

@ -1,12 +1,40 @@
// Variation selectors block https://unicode.org/charts/nameslist/n_FE00.html
// VS1..=VS16
const VARIATION_SELECTOR_START = 0xfe00;
const VARIATION_SELECTOR_END = 0xfe0f;
// Variation selectors supplement https://unicode.org/charts/nameslist/n_E0100.html
// VS17..=VS256
const VARIATION_SELECTOR_SUPPLEMENT_START = 0xe0100;
const VARIATION_SELECTOR_SUPPLEMENT_END = 0xe01ef;
export function toVariationSelector(byte: number): string | null {
if (byte >= 0 && byte < 16) {
return String.fromCodePoint(VARIATION_SELECTOR_START + byte)
} else if (byte >= 16 && byte < 256) {
return String.fromCodePoint(VARIATION_SELECTOR_SUPPLEMENT_START + byte)
} else {
return null
}
}
export function fromVariationSelector(codePoint: number): number | null {
if (codePoint >= VARIATION_SELECTOR_START && codePoint <= VARIATION_SELECTOR_END) {
return codePoint - VARIATION_SELECTOR_START
} else if (codePoint >= VARIATION_SELECTOR_SUPPLEMENT_START && codePoint <= VARIATION_SELECTOR_SUPPLEMENT_END) {
return codePoint - VARIATION_SELECTOR_SUPPLEMENT_START
} else {
return null
}
}
export function encode(emoji: string, text: string): string { export function encode(emoji: string, text: string): string {
// convert the string to utf-8 bytes // convert the string to utf-8 bytes
const bytes = new TextEncoder().encode(text) const bytes = new TextEncoder().encode(text)
let encoded = emoji let encoded = emoji
for (const char of bytes) { for (const byte of bytes) {
const upper = char >> 4 encoded += toVariationSelector(byte)
const lower = char & 0b1111
encoded += String.fromCodePoint(upper + 0xfe00, lower + 0xfe00)
} }
return encoded return encoded
@ -14,16 +42,18 @@ export function encode(emoji: string, text: string): string {
export function decode(text: string): string { export function decode(text: string): string {
let decoded = [] let decoded = []
const chars = Array.from(text)
for (let i = 2; i < text.length; i += 2) {
const upper = text.charCodeAt(i) - 0xfe00
const lower = text.charCodeAt(i + 1) - 0xfe00
if (upper < 0 || upper > 15 || lower < 0 || lower > 15) { for (const char of chars) {
break; const byte = fromVariationSelector(char.codePointAt(0)!)
if (byte === null && decoded.length > 0) {
break
} else if (byte === null) {
continue
} }
decoded.push((upper << 4) | lower) decoded.push(byte)
} }
let decodedArray = new Uint8Array(decoded) let decodedArray = new Uint8Array(decoded)

View File

@ -12,6 +12,12 @@ export default function RootLayout({
}>) { }>) {
return ( return (
<html lang="en"> <html lang="en">
<head>
<link
rel="icon"
href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>😅</text></svg>"
/>
</head>
<body>{children}</body> <body>{children}</body>
</html> </html>
) )