import { useContext } from 'react'; import styles from '@/app/page.module.css' import { UtfdumpContext } from '@/context/utfdump'; import { EncodedCodepoint } from 'utfdump_wasm'; export function OutputTable(props: { currentString: string }) { const ctx = useContext(UtfdumpContext); let rows = []; if (ctx.utfdump) { for (const codepointStr of props.currentString) { const codepoint = codepointStr.codePointAt(0); if (codepoint !== undefined) { const charData = ctx.utfdump.codepoint_char_data(codepoint); if (charData !== undefined) { const encodedUtf8 = charData.encoded_utf8(); let encodedUtf8Str; if (encodedUtf8 !== undefined) { encodedUtf8Str = codepointToString(encodedUtf8); encodedUtf8.free(); } const encodedUtf16 = charData.encoded_utf16_le(); let encodedUtf16Str; if (encodedUtf16 !== undefined) { encodedUtf16Str = codepointToString(encodedUtf16); encodedUtf16.free(); } const combiningClassNum = charData.combining_class(); const combiningClassName = ctx.utfdump.combining_class_name(combiningClassNum); let charDisplayStr = codepointStr.replace(/\s+/, ' '); if (combiningClassNum !== 0) { charDisplayStr = '\u25cc' + charDisplayStr; } rows.push(( {charDisplayStr} {charData.name()} U+{codepoint.toString(16).padStart(4, '0')} {encodedUtf8Str} {encodedUtf16Str} {charData.category_full()} {combiningClassName || combiningClassNum} {charData.bidi_full()} {charData.numeric_value() || 'Not numeric'} )); charData.free(); } else { // TODO: push "invalid character" row in this case } } } } return (
{rows}
Name Code UTF-8 UTF-16LE Category Combining Bidirectional Numeric value
); } function codepointToString(codepoint: EncodedCodepoint): string { const bytes = [ codepoint.b0, codepoint.b1, codepoint.b2, codepoint.b3 ].slice(0, codepoint.len); return bytes.map((b) => b.toString(16).padStart(2, '0')).join(' '); }