~martijnbraam/struct

6f1f2717c0c27cd5eabbf611d4b978e3eb16aaa1 — Martijn Braam 1 year, 8 months ago 3e2483f
Support pasting python b'\0' formatted strings
1 files changed, 29 insertions(+), 0 deletions(-)

M index.html
M index.html => index.html +29 -0
@@ 144,6 144,7 @@
            const dataEntry = document.getElementById('data');
            dataEntry.addEventListener('change', onChange);
            dataEntry.addEventListener('keyup', onChange);
            dataEntry.addEventListener('paste', onDataPaste);

            const formatEntry = document.getElementById('struct');
            formatEntry.addEventListener('change', onChange);


@@ 152,6 153,34 @@
            onChange();
        });

        function onDataPaste(event) {
            // The paste event fires before the textarea gets the new value, run this slightly later
            setTimeout(function () {
                const dataEntry = document.getElementById('data');
                let pasted = dataEntry.value.trim();
                if (pasted.startsWith("b'") || pasted.startsWith('b"')) {
                    pasted = pasted.slice(2, -1);
                    let result = '';
                    while (pasted.length > 0) {
                        if (pasted.startsWith('\\x')) {
                            let num = pasted.substring(2, 4);
                            pasted = pasted.substring(4);
                            result += num + ' ';
                        } else if (pasted.startsWith('\\')) {
                            let char = pasted.charCodeAt(1);
                            pasted = pasted.substring(2);
                            result += char.toString(16).padStart(2, '0') + ' ';
                        } else {
                            let char = pasted.charCodeAt(0);
                            pasted = pasted.substring(1);
                            result += char.toString(16).padStart(2, '0') + ' ';
                        }
                    }
                    dataEntry.value = result.trim();
                }
            }, 1);
        }

        function updateData(format) {
            const dataEntry = document.getElementById('data');
            let data = dataEntry.value;