-
• #2
That's excellent... (I often wonder where you still find the memory for it...).
-
• #3
I'm sure it'll make the builds for the original Espruino a little more painful, but it's worth it - and not actually that much more code (it's not a 100% full-featured regex, but should be enough for basic stuff)
-
• #4
Are functions as replacement supported? I would love to be able to use my RunlengthEncoding snippets.
let RLE = { enc: t => t.replace(/([^\d])\1*/g, m => m.length > 1 ? m.length + m[0] : m[0]), dec: t => t.replace(/(\d+)([^\d])/g, (m, r, c) => new Array(+r + 1).join(c)) }
-- Edit --
I did flash this version on my espruino wifi and it seems to work!
But i run into LOW MEMORY issues while using RLE.dec with a ~5k chars long string. -
• #5
edit: function replacements are now supported!
I'm afraid you can't use functions for replacement at the moment - and also there aren't backreferences so your encoder wouldn't work. The code would work but would insert the stringified function - hence lack of memory :)
I'll see about adding the functions - that shouldn't be too hard.
Backreferences are possible, but a reasonable amount of work - I wonder how often they're used. While your RLE code is neat, it's pretty easy and possibly faster to implement in normal JS :)
-
• #6
Funny you say that.. i did just rewrite it in normal JS.. but it is not ready yet. Sadly i already minified it because it worked in the browser and with node js. But with espruino it seems to hang up somewhere.
-
• #7
But it works when unminified? If you find out where it behaves differently, please can you let me know?
-
• #8
I did not test it unminified. Let me try that later today and i will let you know if anything changes.
-
• #9
Just added the ability to use functions (only for regex though) - but you still don't get backreferences I'm afraid.
-
• #10
That was the thing I used in the past (replace with context logic).
but you still don't get backreferences I'm afraid.
Is this referring to
p1, p2, ...
function parms in MDN's doc about String replace w/ function doc? -
• #11
Thanks @Gordon i will give it a try. I am in the process of deminifying my RLE implementation.
The lack of backreferences is negligible. I guess its a more advanced feature anyway.In my case i only need the backreferences for encoding.. which is a pre step on the PC. I only need the decoding part on the espruino.
-
• #12
Okay here are the results in respect of RLE.
First the two contenders:
Unminified
module.exports = { enc: string => { let map = (count, char) => (count > 1 ? count : '') + ('¾µÀÁÂÃÄÅÆÇ'[char] || char), counter = 0, last = ''; return [].reduce.call(string, (result, char, index) => { if (last !== char) { result += map(counter, last); counter = 0; last = char; } counter++; if (index >= string.length - 1) { result += map(counter, last); } return result; }, ''); }, dec: string => { let map = char =>'0123456789'['¾µÀÁÂÃÄÅÆÇ'.indexOf(char)] || char, count = ''; return [].reduce.call(string, (result, char) => { if (!isNaN(parseInt(char))) { count += char; return result; } else { result += count.length ? new Array(parseInt(count) + 1).join(map(char)) : map(char); count = ''; } return result; }, ''); } };
Minified
module.exports=((a,b)=>({enc:t=>b.call(t,(p,c,x)=>{return p.l!==c&&([p.r,p.c,p.l]=[p.r+p.f(p.c,p.l),0,c]),p.c++,x>=t.length-1&&(p.r+=p.f(p.c,p.l)),p},{l:'',r:'',c:0,f:(c,s)=>(c>1?c:'')+(a[s]||s)}).r,dec:t=>b.call(t,(p,c)=>{return[p.r,p.j]=[~~c==c?p.r:p.r+(p.j?Array(+p.j+1).join(p.m(c)):p.m(c)),~~c==c?p.j+c:''],p},{r:'',m:i=>'0123456789'[a.indexOf(i)]||i,j:''}).r}))('¾µÀÁÂÃÄÅÆÇ',[].reduce)
I minify by hand but i tested the code in the browser and nodejs and confirmed both versions work as expected. It is not a straight RLE implementation as you can see.. the purpose is to minify base64 strings, so there is some substitution going on.
In regard to espruino the unminfied version is running fine. The minified version however behaves unexpected. The webIDE freezes.. reconnecting to the board does not work, even typing on the left side is not working. Restarting the webIDE has no effect. I have to reflash my espruino wifi to fix this.
-- Edit --
I tested the latest cutting edge build (1v94.130) with thislet dec = t => t.replace(/(\d+)([^\d])/g, (m, r, c) => new Array(+r + 1).join(c)).replace(/[¾µÀÁÂÃÄÅÆÇ]/g, i => i.charCodeAt(0) % 10);
I get the same behaviour as with my minified non regex version.
-
• #13
Do you have any example input/output? You said it needed base64?
Also, there's actually a good compressor called
heatshrink
built into Espruino. It's not accessible from JS at the moment, but it might make a certain amount of sense? -
• #14
If you try the latest build, it should work now:
>let dec = t => t.replace(/(\d+)([^\d])/g, (m, r, c) => new Array(+r + 1).join(c)).replace(/[¾µÀÁÂÃÄÅÆÇ]/g, i => i.charCodeAt(0) % 10); =function (t) { ... } >dec("6a7b1c") ="aaaaaabbbbbbbc"
It turns out it was because
String.replace
wasn't resuming the search from the correct place. -
• #15
It would need base64 as input for the encoding and the result of that is input for the decoding.
The encoding step basicly substitutes the numbers with some characters outside of the base64 alphabet and then does a runlength encoding. I never tried the encoding step in espruino though.To give a little background.. i recently got a eink display (waveshare 4.2 inch) and i wanted to display full size (400x300) images with espruino. The output of the common img2lcd tool was
to big because of all the overhead of hex notation, commas, spaces and line breaks. The image
information itself is just 15k bytes but in this representation its close to 80k bytes. So i base64 encoded the image which gives me 20k bytes which by the way runs just fine. But i saw major potential to further reduce the size with some RLE.Having any compression/decompression options build in would be nice and if the output of the compression could be an accepted string would be nice for assets.
-
• #16
For my test input of 5310 chars it still crashes.
After quite a while i get this error message:Uncaught Error: Unknown escape character 117 in RegEx at line 1 col 184 ...{return a.charCodeAt(0)%10}) ^ in function "dec" called from line 2 col 229 ...)%10})};console.log(dec(img).length); ^ >
Here is the full example code to reproduce:
let img = '720/Ç66/D65/gf64/wB64/ÆAH63/+2Af63/g2A63/ÂÇAD63/PÄwP62/wPjg62/+Q/PD62/nDÆ+PÆ60/Æ+PÃÂ/j60/l2/vzÂP59/Æj/+/PAf59/kv/ÅÇwB59/Æw2/P¾AH59/lv/Æ/Bwf58/+C2/zÂfh59/wJ2/OF/H58/+Uz/ÃzÁ+f58/yfn/m/fÂ59/A/v+Gd/j58/ÃD/fÂÃÁ/P58/kÁ2/G2fÆ58/+3/ÇÅ2/Ã58/z3/k3/n58/N2/Æz2/+f57/ÆD2/h/Ç/Ã58/yf/Æj3/n58/J2/n2/f+P57/Æ2/ÆP/µ/Â58/Ã2/Àf/f/j58/n/+M2/f+P58/P/ÅÃÆL/Âf57/Æ2/OTÄH/h58/Ã/ÇcnÆ/+X58/zžÆPÅ/Ãf58/nzTÂfv/l57/A2Ph/x3/H56/wACPk/jf/Æf56/P/AOT/l2/x56/ÄP/ÂM2/f2/H56/ef/ÆD/Æ/f+f55/Å+f/jÁ/ÃÇ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Af7/ÆACEAhgGEBAEAEAYHAwEAQBCAIQCAwgCACAQwDCAgDAÂGAgDGBgE7Af7/wAIQCDAYQGAgAIDgMDAIDAEM2BAI2DAQAI2BgMIDAcBAYDAcYCAw7A8/AxhAI2GhA2M2AÂyAgMA2ÂYw2YEAgM2G2AgE2DQgHGQEBAHGRg2G8A7/ÆAÂEAgHiEAPg2A+I4A+AcAfAQC2AHwACAQDxCAHx4AHx2APg8A7/w59A7/60AP5/Æ987A'; let dec = t => t.replace(/(\d+)([^\d])/g, (m, r, c) => new Array(+r + 1).join(c)).replace(/[¾µÀÁÂÃÄÅÆÇ]/g, i => i.charCodeAt(0) % 10); console.log(dec(img).length);
-
• #17
Ahh - it's almost certainly a problem with the upload. There are some huge hacks in the Web IDE to try and add support for non-ascii characters in strings.
Since you're only using base64, can you find a few extra characters that have character codes less than 128? http://www.asciitable.com/
-
• #18
I will give that a try.. i did choose the current characters so i can use that mod 10 hack :)
-
• #19
With the new encoding it doesn't crash.. but it returns after quite some while.. about 15min+ with a wrong result. Thats odd :D
To not further spam this forum with huge encoded data i created
a gist https://gist.github.com/PaddeK/4c717e9c2a86a39efede7ac672c96f5b -
• #20
Does the Espruino implementation of RegEx support the | "or" operator?
Simplest test case:
var p = /a|b/; p.test("a");
Here I'm getting back false... [MDBT42Q 1v99]
-
• #21
No, I'm afraid it doesn't at the moment. I'll file an issue for it. You'll either have to use two regexes, or in the case you have there, use
/[ab]/
-
• #22
/a|b/
is now fixed: https://github.com/espruino/Espruino/issues/1503
There are now finally regular expressions in Espruino! If you download a cutting edge build then you should get them.
There's:
/regex/
syntax to define themregex.exec
to match on a supplied stringstring.replace(regex, replacement)
string.split(regex)
It's not got all the RegEx functionality built in, but should cover most of the common use-cases.
Let me know how it goes! The RegEx system might still need some work, so if you find a RegEx that doesn't seem to be working properly, please let me know!