@@ -411,6 +411,25 @@ function toggleTagArray(tags: string[], tag: string): string[] {
411411 return tags ;
412412}
413413
414+ // from https://evanhahn.com/getting-the-utf16-bytes-of-javascript-strings/
415+ function * utf16bytes ( str :string ) : Iterable < number > {
416+ for ( let i = 0 ; i < str . length ; i ++ ) {
417+ const charCode = str . charCodeAt ( i ) ;
418+
419+ // Get the most significant byte.
420+ // For example, given 0x1234, yield 0x12.
421+ yield ( charCode & 0xff00 ) >> 8 ;
422+
423+ // Get the least significant byte.
424+ // For example, given 0x1234, yield 0x34.
425+ yield charCode & 0x00ff ;
426+ }
427+ }
428+
429+ function utf8bytes ( str :string ) : Uint8Array {
430+ return new TextEncoder ( ) . encode ( str ) ;
431+ }
432+
414433async function main ( ) {
415434 let data : SearchEntry [ ] ;
416435 var detail = false ;
@@ -587,6 +606,49 @@ async function main() {
587606 visible : detail ,
588607 width : 110 ,
589608 } ,
609+ {
610+ field : "utf8" ,
611+ formatter : ( cell ) => {
612+ const val = cell . getRow ( ) . getData ( ) . example as string ;
613+ if ( ! val ) {
614+ return "" ;
615+ }
616+ const bytes = utf8bytes ( val ) ;
617+ const parts : string [ ] = [ ] ;
618+ for ( const b of bytes ) {
619+ parts . push (
620+ b . toString ( 16 ) . toUpperCase ( ) . padStart ( 2 , "0" )
621+ ) ;
622+ }
623+ return parts . join ( " " ) ;
624+ } ,
625+ headerHozAlign : "center" ,
626+ headerSort : false ,
627+ hozAlign : "center" ,
628+ title : "UTF-8" ,
629+ visible : detail ,
630+ } ,
631+ {
632+ field : "utf16" ,
633+ formatter : ( cell ) => {
634+ const val = cell . getRow ( ) . getData ( ) . example as string ;
635+ if ( ! val ) {
636+ return "" ;
637+ }
638+ const parts : string [ ] = [ ] ;
639+ for ( const b of utf16bytes ( val ) ) {
640+ parts . push (
641+ b . toString ( 16 ) . toUpperCase ( ) . padStart ( 2 , "0" )
642+ ) ;
643+ }
644+ return parts . join ( " " ) ;
645+ } ,
646+ headerHozAlign : "center" ,
647+ headerSort : false ,
648+ hozAlign : "center" ,
649+ title : "UTF-16" ,
650+ visible : detail ,
651+ } ,
590652 {
591653 title : "Name" ,
592654 field : "name" ,
@@ -632,8 +694,12 @@ async function main() {
632694 footerElement : `<span class="w-100 mx-2 my-1">
633695 <img src="/favicon.svg" class="pe-2" style="height:1.2em;" alt="UnicodeSearch logo"/><span class=" d-none d-sm-inline">UnicodeSearch</span>
634696 <span id="rowcount" class="px-3">Rows: ${ data . length . toLocaleString ( ) } </span>
635- <input id="showhidecolumns" type="checkbox" class="ms-2 me-1" ${ detail ? "checked" : "" } title="Toggle detail columns" /> <span class="d-none d-md-inline">Show detail columns</span><span class="d-md-none">Details</span>
636- <input id="imagecheckbox" type="checkbox" class="ms-2 me-1" ${ useImages ? "checked" : "" } title="Font vs Images" /> <span class="d-none d-md-inline">Use images in examples</span><span class="d-md-none">Images</span>
697+ <input id="showhidecolumns" type="checkbox" class="ms-2 me-1" ${
698+ detail ? "checked" : ""
699+ } title="Toggle detail columns" /> <span class="d-none d-md-inline">Show detail columns</span><span class="d-md-none">Details</span>
700+ <input id="imagecheckbox" type="checkbox" class="ms-2 me-1" ${
701+ useImages ? "checked" : ""
702+ } title="Font vs Images" /> <span class="d-none d-md-inline">Use images in examples</span><span class="d-md-none">Images</span>
637703 <a class="d-none d-lg-block float-end" href="https://github.com/FileFormatInfo/unicodesearch">Source</a>
638704 </span>` ,
639705 } ) ;
@@ -665,7 +731,7 @@ async function main() {
665731 table . on ( "tableBuilt" , function ( ) {
666732 document . getElementById ( "showhidecolumns" ) ! . onclick = ( ) => {
667733 detail = ! detail ;
668- toggleColumns ( table , [ "age" , "block" , "category" , "script" ] ) ;
734+ toggleColumns ( table , [ "age" , "block" , "category" , "script" , "utf8" , "utf16" ] ) ;
669735 const qs = new URLSearchParams ( window . location . search ) ;
670736 if ( detail ) {
671737 qs . set ( "detail" , "1" ) ;
0 commit comments