Skip to content

Instantly share code, notes, and snippets.

@bytespider
Forked from 140bytes/LICENSE.txt
Created June 4, 2011 11:46
Show Gist options
  • Select an option

  • Save bytespider/1007834 to your computer and use it in GitHub Desktop.

Select an option

Save bytespider/1007834 to your computer and use it in GitHub Desktop.
140byt.es -- convert string to array of UTF-8 bytes

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression, that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

The 140byt.es site hasn't launched yet, but for now follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(){
/* Rules:
(1) anonymous function // make sure
(2) may be self-executing // to annotate
(3) <=140 bytes // your code
(4) no globals // so everyone
(5) MIT license // can learn
(6) have a good time! // from it!
*/}
function(){/*Rules: (1) anonymous function (2) may be self-executing (3) <=140 bytes (4) no globals (5) MIT license (6) have a good time!*/}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
// [REQUIRED] A name for your library.
// This must match /^[a-z_]\w*$/i
"name": "140bytes",
// [OPTIONAL] A description of your library, phrased as a verb predicate.
// The gist description is used by default.
"description": "Explain the 140byt.es rules.",
// [OPTIONAL] Up to 5 keywords used for indexing.
"keywords": [
"140bytes",
"master",
"rules"
]
}
@Kambfhase
Copy link

Looks good, but ...

2^16 is not 65536. line 40 could be optimized, I guess. Your annotated code misses a ; before the second for loop.

@mathiasbynens
Copy link

I don’t get it. Are you confusing ^ with Math.pow(), e.g. doesn’t 2^21 equal 23 instead of 2097152?

@bytespider
Copy link
Author

You're right, I am. Damn, I'll have to recheck my tests

@mathiasbynens
Copy link

FWIW, the demo doesn’t seem to work for me.

@subzey
Copy link

subzey commented Jun 4, 2011

Something like encodeURIComponent("Привет мир").split('%')) may help you with non-ascii utf8

@bytespider
Copy link
Author

@subzey: I'll try that route, but its possible that may make things more complicated

@bytespider
Copy link
Author

i thought about using >>= to shift of 6 bits at a time leaving only the bits required for the next operation

@mikesherov
Copy link

Hey, got it down to 202 bytes by refactoring the complex b[e] calcs to happen in the for loop... the comments are all mangled and nonsense now, but that shaves off about 10 bytes. I'm sure there are more savings in there with some trickery: https://gist.github.com/1008218

@mikesherov
Copy link

Hey, I see this has been updated! You adding similar refactors to what I just did! looking for more...

@bytespider
Copy link
Author

Yes I took my working code and applied what I could see the major changes from yours were to find out what bits were breaking.

@mikesherov
Copy link

looks like you can drop the parens around (f=3), no?

@bytespider
Copy link
Author

No you'll get and Invalid left hand side assignment error. just realised my demo also fails

@mikesherov
Copy link

also, since you're no longer modifying g, instead of doing "g =128; c < g ...", you can do "c < g=128 .." I believe...

@mikesherov
Copy link

Oh, same problem :-\

@mikesherov
Copy link

Also, it looks like the compressed version is different from the annotated version...
compressed has: "b[h]=(2<<5-f)+(c>>6_i)"
annotated has: "b[h] = (2<<(5-f)) + (c >> 6_i)"

compressed reveals the truth: multiplication and addition/subtraction have higher precedence than bitwise shifts

@bytespider
Copy link
Author

Oh so i've been chasing a none existant bug!? Damn!

@mikesherov
Copy link

another byte gone:
old:
"c<2<<20&&(f=3);"
new
"c<2<<20?f=3:0;"

@mikesherov
Copy link

FOUND THE BUG: g_4 is not 2048 :-.
old:
"c<g_4"
new:
"c<2048"

@mikesherov
Copy link

deleted

@mikesherov
Copy link

@bytespider
Copy link
Author

Can get it to 184bytes if we assume that we'll only have 4byte characters

@mikesherov
Copy link

Could save 4 bytes by doing "f=c<2048?1:..." instead of "c<2048?f=1:...". Also another 2 bytes if you do "i=f=c<2048..." and get rid of ",i=f"

@mikesherov
Copy link

Another byte: "65536" -> "g<<9"

@subzey
Copy link

subzey commented Jun 5, 2011

@bytespider https://gist.github.com/1008764 the encodeURIComponent based version. It's 132 bytes in length (wrapped) and works well.

@jed
Copy link

jed commented Jul 11, 2011

hey @bytespider, could you take the trailing comma out of your package.json keywords?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment