4 ms·
Glad the pinnacle of programming is still there: left-pad
by dazhbog 3y ago
Glad the pinnacle of programming is still there: left-pad
- vdfs 3y agoThe entire source code: export function leftPad(str, len, ch) { return new Array(len - str.length).fill(ch || ch === 0 ? ch : ' ').join('') + str }
- winrid 3y agothat actually seems really inefficient, it could avoid the array allocation and fill call by just moving the length check...
- lucacasonato 3y agoYou could also just use `String.prototype.padStart()` [1], and you get a nice optimized C++ implementation :D [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- usrusr 3y agoPass, not a Rust implementation /s
- randomdata 3y agoIs it even AI scale if not performed on the GPU?
- itslennysfault 3y agoNo, you're right. The White House says no more C/C++ it's your civic duty.
- diggan 3y agoWhile funny, worth remembering that .padStart didn't exist when left-pad was first published :)
- JoelEinbinder 3y agoWhile it's not quite C++, here is Chromium's implementation: https://source.chromium.org/chromium/chromium/src/+/main:v8/src/builtins/string-pad.tq;drc=f4a00cc248dd2dc8ec8759fb51620d47b5114090;l=15 https://source.chromium.org/chromium/chromium/src/+/main:v8/...
- _aaed 3y agoIt's Torque, which is essentially C++ https://v8.dev/_img/docs/torque/build-process.svg https://v8.dev/_img/docs/torque/build-process.svg
- papa0101 3y agoIt's not even needed anymore: "HN".padStart(10); That's it.
- seniorsassycat 3y agoDoes pad start respect RTL marks? /s
- chaorace 3y agoSeems like the logical way to do it. Though, honestly, unless you specifically need to do lots of different kinds of padding, something like this makes way more sense: const bespokePad = (x) => ('000000' + x).slice(-6)
- lakpan 3y agoI wanna see you write it from memory. Now do it without bugs fixed in 2013 in that package.
- cesarvarela 3y agoI think this time it won't spread as much because most people just ask their LLM for a leftpad function.
- nerdponx 3y agoWhat are the chances that the LLM suggests the NPM package?