Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First binary-string commit

  • Loading branch information...
commit f99223fc88d2e57fd826d116f1016baae27e1a3e 0 parents
GlitchMr authored
6 .travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
+ - 0.9
+
14 LICENSE
@@ -0,0 +1,14 @@
+Copyright (c) 2012, GlitchMr <glitchmr@myopera.com>
+
+Permission to use, copy, modify, and/or distribute this software for
+any purpose with or without fee is hereby granted, provided that the
+above copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
27 README.md
@@ -0,0 +1,27 @@
+[![Build Status](https://secure.travis-ci.org/GlitchMr/binary-string.png?branch=master)](http://travis-ci.org/GlitchMr/binary-string)
+
+Binary strings still have their uses. Don't let huge warning of Node.js
+stop you from using them. There are valid reasons to use binary strings
+instead of low-level `Buffer`s. Who cares about performance? If we
+would care so much, we all would write in assembly :-).
+
+This module will try to use Node.js binary strings if you are using
+version create before their removal (while destroying all warnings
+about them). If your Node.js version doesn't have binary strings, they
+are emulated which makes them even slower (but well, you have them,
+and if you care about performance, write in assembly).
+
+Unlike `Buffer`s, you can do anything you can with normal strings on
+binary strings (internally, they are normal strings). You can use
+regular expressions, you can compare them, you can use string methods
+on them, you can use them as object keys. The only disadvantage is that
+you cannot easily change single characters (this is probably only real
+advantage of `Buffer`s).
+
+```javascript
+var binaryString = require('binary-string'),
+ // Converts buffer to binary string
+ fromBuffer = binaryString.fromBuffer,
+ // Converts binary string to buffer
+ toBuffer = binaryString.toBuffer
+```
35 lib/binary-string.js
@@ -0,0 +1,35 @@
+(function () {
+ // I need to avoid mandatory warning about using deprecated
+ // function, so I overwrite console.error(), so Node.js will
+ // think that everything is fine. After I'll do it, I restore
+ // original console.error()
+ var consoleError = console.error
+ console.error = function () {}
+ try {
+ // This is like "local" in Perl
+ if (new Buffer([255]).toString('binary') !== "\xFF")
+ throw new Exception
+ // Node.js is good enough to not complain
+ exports.toBuffer = function (binary) {
+ return new Buffer(binary, 'binary')
+ }
+ exports.fromBuffer = function (buffer) {
+ return buffer.toString('binary')
+ }
+ }
+ catch (e) {
+ // So we have to emulate functions removed for "performance"
+ // while having even lower performance. Thank you, Node.js.
+ exports.toBuffer = function (binary) {
+ var i, length, array = []
+ for (i = 0, length = binary.length; i < length; ++i) {
+ array.push(binary.charCodeAt(i))
+ }
+ return new Buffer(array)
+ }
+ exports.fromBuffer = function (buffer) {
+ return String.fromCharCode.apply(String, buffer)
+ }
+ }
+ console.error = consoleError
+}())
12 package.json
@@ -0,0 +1,12 @@
+{
+ "author": "GlitchMr <glitchmr@myopera.com> (http://glitchmr.github.com)",
+ "name": "binary-string",
+ "description": "Binary string because binary encoding still has its uses.",
+ "version": "v1.0.0",
+ "keywords": [ "string", "binary", "encoding" ],
+ "homepage": "http://github.com/GlitchMr/binary-string",
+ "bugs": { "url": "http://github.com/GlitchMr/binary-string/issues" },
+ "main": "./lib/binary-string",
+ "repository": { "type": "git", "url": "https://github.com/GlitchMr/binary-string.git" },
+ "scripts": { "test": "node test/test" }
+}
11 test/test.js
@@ -0,0 +1,11 @@
+var assert = require('assert')
+var binaryString = require('../lib/binary-string')
+console.log('toBuffer basic')
+assert.deepEqual(binaryString.toBuffer('ab'), new Buffer([97, 98]))
+console.log('toBuffer high bytes')
+assert.deepEqual(binaryString.toBuffer("\xFE\xFF"), new Buffer([254, 255]))
+console.log('fromBuffer basic')
+assert.equal(binaryString.fromBuffer(new Buffer([97, 98])), 'ab')
+console.log('fromBuffer high bytes')
+assert.equal(binaryString.fromBuffer(new Buffer([254, 255])), "\xFE\xFF")
+console.log('OK!')
Please sign in to comment.
Something went wrong with that request. Please try again.