4 ms·
Why does escape'ing and un-escape'ing that string result in code?
by lxe 6y ago
Why does escape'ing and un-escape'ing that string result in code?
- deleted 6y ago[deleted]
- tomxor 6y agoThere is a critical bit of regex in between. In short this technique unpacks two ASCII characters from each unicode point of the input string. Any ASCII safe code can be packed with this dweet: https://www.dwitter.net/d/11852 https://www.dwitter.net/d/11852 The escape function [0] converts unicode values > 0xFF into the string format '%uxxxx`, additionally it will produce two sequences if the unicode point is high enough to require two UTF-16 surrogate pairs. e.g escape() ing the first character '𩡯' results in '%uD866%uDC6F'. The regex throws away the 'uD8' parts of each sequence as we aren't exploiting the full capacity of the unicode space, just the lower 255 values of each surrogate pair. which results in '%66%6F'. Finally this is unescaped which converts these into the separate ASCII chars that were originally packed with dweet 11852 (don't ask me how it packs it I haven't looked yet.) In all this means it's possible to fit 194 evaluable ASCII chars into a dweet (taking into consideration overhead of the unpacking code). There are also more esoteric methods of fitting more characters or data using unicode that are not generally evaluable, e.g image data. All of these techniques are using more than 140 bytes, but dwitter has a character limit not byte limit. But nothing is free, you must fit any decoders into the same dweet so there is always a balance. [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- jugg1es 6y agoyou are a text master. we should call you Master Text.
- scintill76 6y agoI was wondering how (escape`str`) is valid JS to invoke the escape function. Now I know it's called "tagged template literal."