sproutit-narwhal 0.1.106
Sign up to get free protection for your applications and to get access to all the features.
- data/DISTRIBUTION.yml +15 -0
- data/README.md +86 -0
- data/Rakefile +349 -0
- data/VERSION.yml +7 -0
- data/bin/activate +50 -0
- data/bin/activate.bash +50 -0
- data/bin/activate.cmd +3 -0
- data/bin/js +67 -0
- data/bin/json +2 -0
- data/bin/narwhal +67 -0
- data/bin/narwhal.cmd +29 -0
- data/bin/sea +45 -0
- data/bin/sea.cmd +25 -0
- data/bin/tusk +2 -0
- data/bin/tusk.cmd +5 -0
- data/catalog.json +902 -0
- data/docs/available-packages.md +32 -0
- data/docs/browser-api-plan.md +290 -0
- data/docs/browser-api.md +153 -0
- data/docs/download.md +25 -0
- data/docs/engines.md +32 -0
- data/docs/json-tool.md +121 -0
- data/docs/lib/binary.wiki +242 -0
- data/docs/lib/file.wiki +325 -0
- data/docs/lib/os/popen.md +70 -0
- data/docs/modules.md +38 -0
- data/docs/narwhal.md +487 -0
- data/docs/packages-howto.md +32 -0
- data/docs/packages.md +30 -0
- data/docs/posts/2009-07-29-hello-0.1.md +19 -0
- data/docs/quick-start.md +69 -0
- data/docs/sea.md +49 -0
- data/engines/browser/lib/binary.js +2 -0
- data/engines/browser/lib/reactor.js +21 -0
- data/engines/browser/lib/system.js +3 -0
- data/engines/default/lib/array.js +164 -0
- data/engines/default/lib/binary-engine.js +53 -0
- data/engines/default/lib/binary.js +755 -0
- data/engines/default/lib/date.js +8 -0
- data/engines/default/lib/file-engine.js +119 -0
- data/engines/default/lib/function.js +119 -0
- data/engines/default/lib/global.js +11 -0
- data/engines/default/lib/io-engine.js +26 -0
- data/engines/default/lib/json.js +488 -0
- data/engines/default/lib/object.js +69 -0
- data/engines/default/lib/os-engine.js +3 -0
- data/engines/default/lib/reactor.js +12 -0
- data/engines/default/lib/string.js +84 -0
- data/engines/default/lib/system.js +20 -0
- data/engines/default/lib/worker.js +133 -0
- data/engines/jsc/README.md +18 -0
- data/engines/jsc/bootstrap.js +53 -0
- data/engines/jsc/deps/http-parser/LICENSE +77 -0
- data/engines/jsc/deps/http-parser/README.md +145 -0
- data/engines/jsc/deps/http-parser/http_parser.c +6087 -0
- data/engines/jsc/deps/http-parser/http_parser.h +141 -0
- data/engines/jsc/deps/http-parser/http_parser.rl +500 -0
- data/engines/jsc/deps/http-parser/test.c +858 -0
- data/engines/jsc/include/binary-engine.h +11 -0
- data/engines/jsc/include/io-engine.h +23 -0
- data/engines/jsc/include/narwhal.h +427 -0
- data/engines/jsc/lib/file-engine.js +31 -0
- data/engines/jsc/lib/http.js +1 -0
- data/engines/jsc/lib/io-engine.js +202 -0
- data/engines/jsc/lib/os-engine.js +25 -0
- data/engines/jsc/lib/system.js +18 -0
- data/engines/jsc/lib/zip.js +1 -0
- data/engines/jsc/narwhal-jsc.c +273 -0
- data/engines/jsc/narwhal.c +29 -0
- data/engines/jsc/package.json +8 -0
- data/engines/jsc/src/binary-engine.cc +290 -0
- data/engines/jsc/src/file-engine.cc +405 -0
- data/engines/jsc/src/io-engine.cc +423 -0
- data/engines/jsc/src/jack/handler/jill.cc +710 -0
- data/engines/jsc/src/os-engine.cc +210 -0
- data/engines/rhino/bin/narwhal-rhino +68 -0
- data/engines/rhino/bin/narwhal-rhino.cmd +34 -0
- data/engines/rhino/bootstrap.js +119 -0
- data/engines/rhino/jars/jline.jar +0 -0
- data/engines/rhino/jars/jna.jar +0 -0
- data/engines/rhino/jars/js.jar +0 -0
- data/engines/rhino/lib/binary-engine.js +83 -0
- data/engines/rhino/lib/concurrency.js +6 -0
- data/engines/rhino/lib/event-queue.js +18 -0
- data/engines/rhino/lib/file-engine.js +216 -0
- data/engines/rhino/lib/http-client-engine.js +90 -0
- data/engines/rhino/lib/http-engine.js +10 -0
- data/engines/rhino/lib/io-engine.js +347 -0
- data/engines/rhino/lib/md5-engine.js +40 -0
- data/engines/rhino/lib/os-engine.js +150 -0
- data/engines/rhino/lib/packages-engine.js +71 -0
- data/engines/rhino/lib/sandbox-engine.js +70 -0
- data/engines/rhino/lib/system.js +38 -0
- data/engines/rhino/lib/worker-engine.js +23 -0
- data/engines/rhino/lib/zip.js +78 -0
- data/engines/rhino/package.json +4 -0
- data/engines/secure/lib/file.js +6 -0
- data/engines/secure/lib/system.js +6 -0
- data/engines/template/bin/narwhal-engine-name +32 -0
- data/engines/template/bootstrap.js +40 -0
- data/engines/template/lib/file-engine.js +118 -0
- data/engines/template/lib/system.js +17 -0
- data/examples/browser-deployment-jackconfig.js +35 -0
- data/examples/fibonacci-worker.js +35 -0
- data/examples/fibonacci.js +19 -0
- data/examples/hello +2 -0
- data/examples/narwhal +3 -0
- data/examples/not-quite-a-quine.js +1 -0
- data/extconf.rb +44 -0
- data/gem_bin/narwhal +5 -0
- data/gem_bin/sea +4 -0
- data/gem_bin/tusk +4 -0
- data/lib/args.js +849 -0
- data/lib/base16.js +16 -0
- data/lib/base64.js +120 -0
- data/lib/codec/base64.js +8 -0
- data/lib/crc32.js +60 -0
- data/lib/file-bootstrap.js +187 -0
- data/lib/file.js +659 -0
- data/lib/hash.js +28 -0
- data/lib/hashp.js +65 -0
- data/lib/html.js +16 -0
- data/lib/http-client.js +134 -0
- data/lib/http.js +17 -0
- data/lib/io.js +98 -0
- data/lib/jsmin.js +315 -0
- data/lib/jsonpath.js +89 -0
- data/lib/logger.js +55 -0
- data/lib/md4.js +146 -0
- data/lib/md5.js +164 -0
- data/lib/mime.js +166 -0
- data/lib/narwhal.js +102 -0
- data/lib/narwhal/client.js +261 -0
- data/lib/narwhal/compile.js +99 -0
- data/lib/narwhal/env.js +140 -0
- data/lib/narwhal/inline.js +106 -0
- data/lib/narwhal/json.js +324 -0
- data/lib/narwhal/json.md +178 -0
- data/lib/narwhal/repl.js +96 -0
- data/lib/narwhal/server-test.js +6 -0
- data/lib/narwhal/server.js +270 -0
- data/lib/narwhal/tusk.js +170 -0
- data/lib/narwhal/tusk/bin.js +13 -0
- data/lib/narwhal/tusk/bundle.js +0 -0
- data/lib/narwhal/tusk/catalog.js +22 -0
- data/lib/narwhal/tusk/clone.js +66 -0
- data/lib/narwhal/tusk/consolidate.js +25 -0
- data/lib/narwhal/tusk/create-catalog.js +80 -0
- data/lib/narwhal/tusk/engine.js +42 -0
- data/lib/narwhal/tusk/freeze.js +0 -0
- data/lib/narwhal/tusk/init.js +56 -0
- data/lib/narwhal/tusk/install.js +288 -0
- data/lib/narwhal/tusk/list.js +20 -0
- data/lib/narwhal/tusk/orphans.js +0 -0
- data/lib/narwhal/tusk/reheat.js +15 -0
- data/lib/narwhal/tusk/remove.js +15 -0
- data/lib/narwhal/tusk/search.js +145 -0
- data/lib/narwhal/tusk/update.js +21 -0
- data/lib/narwhal/tusk/upgrade.js +0 -0
- data/lib/os.js +33 -0
- data/lib/packages.js +423 -0
- data/lib/printf.js +169 -0
- data/lib/promise.js +352 -0
- data/lib/querystring.js +176 -0
- data/lib/ref-send.js +257 -0
- data/lib/regexp.js +12 -0
- data/lib/sandbox.js +422 -0
- data/lib/sha.js +112 -0
- data/lib/sha256.js +102 -0
- data/lib/struct.js +228 -0
- data/lib/term.js +179 -0
- data/lib/test/assert.js +95 -0
- data/lib/test/equiv.js +188 -0
- data/lib/test/jsdump.js +165 -0
- data/lib/test/runner.js +129 -0
- data/lib/unload.js +13 -0
- data/lib/uri.js +378 -0
- data/lib/url.js +5 -0
- data/lib/utf8.js +64 -0
- data/lib/util.js +985 -0
- data/lib/uuid.js +89 -0
- data/lib/xregexp.js +521 -0
- data/local.json.template +1 -0
- data/narwhal.gemspec +105 -0
- data/narwhal.js +213 -0
- data/package.json +26 -0
- data/packages/readline/engines/default/lib/readline.js +4 -0
- data/packages/readline/engines/rhino/lib/readline.js +6 -0
- data/packages/readline/package.json +5 -0
- data/sources.json +207 -0
- data/tests/all-tests.js +17 -0
- data/tests/args.js +31 -0
- data/tests/args/domain.js +215 -0
- data/tests/args/options.js +36 -0
- data/tests/args/shifting.js +92 -0
- data/tests/args/validation.js +31 -0
- data/tests/base64.js +23 -0
- data/tests/commonjs.js +3 -0
- data/tests/commonjs/all-tests.js +12 -0
- data/tests/commonjs/bytearray-encodings-tests.js +69 -0
- data/tests/commonjs/bytearray-tests.js +465 -0
- data/tests/commonjs/bytestring-encodings-tests.js +89 -0
- data/tests/commonjs/bytestring-tests.js +263 -0
- data/tests/commonjs/es5/all-tests.js +3 -0
- data/tests/commonjs/es5/bind.js +29 -0
- data/tests/commonjs/file-tests.js +315 -0
- data/tests/commonjs/file/dirname.js +31 -0
- data/tests/commonjs/file/extension.js +45 -0
- data/tests/commonjs/file/is-absolute.js +11 -0
- data/tests/commonjs/file/iterator.js +101 -0
- data/tests/commonjs/file/normal.js +27 -0
- data/tests/commonjs/file/path.js +17 -0
- data/tests/commonjs/file/relative.js +42 -0
- data/tests/commonjs/file/resolve.js +44 -0
- data/tests/commonjs/module-tests.js +9 -0
- data/tests/commonjs/modules/absolute/b.js +1 -0
- data/tests/commonjs/modules/absolute/program.js +5 -0
- data/tests/commonjs/modules/absolute/submodule/a.js +3 -0
- data/tests/commonjs/modules/absolute/test.js +9 -0
- data/tests/commonjs/modules/all-tests.js +47 -0
- data/tests/commonjs/modules/config.js +11 -0
- data/tests/commonjs/modules/cyclic/a.js +4 -0
- data/tests/commonjs/modules/cyclic/b.js +4 -0
- data/tests/commonjs/modules/cyclic/program.js +10 -0
- data/tests/commonjs/modules/cyclic/test.js +9 -0
- data/tests/commonjs/modules/determinism/program.js +3 -0
- data/tests/commonjs/modules/determinism/submodule/a.js +8 -0
- data/tests/commonjs/modules/determinism/submodule/b.js +2 -0
- data/tests/commonjs/modules/determinism/test.js +9 -0
- data/tests/commonjs/modules/exactExports/a.js +3 -0
- data/tests/commonjs/modules/exactExports/program.js +4 -0
- data/tests/commonjs/modules/exactExports/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/hasOwnProperty.js +0 -0
- data/tests/commonjs/modules/hasOwnProperty/program.js +3 -0
- data/tests/commonjs/modules/hasOwnProperty/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/toString.js +0 -0
- data/tests/commonjs/modules/method/a.js +12 -0
- data/tests/commonjs/modules/method/program.js +8 -0
- data/tests/commonjs/modules/method/test.js +9 -0
- data/tests/commonjs/modules/missing/program.js +8 -0
- data/tests/commonjs/modules/missing/test.js +9 -0
- data/tests/commonjs/modules/monkeys/a.js +1 -0
- data/tests/commonjs/modules/monkeys/program.js +4 -0
- data/tests/commonjs/modules/monkeys/test.js +9 -0
- data/tests/commonjs/modules/nested/a/b/c/d.js +3 -0
- data/tests/commonjs/modules/nested/program.js +3 -0
- data/tests/commonjs/modules/nested/test.js +9 -0
- data/tests/commonjs/modules/relative/program.js +5 -0
- data/tests/commonjs/modules/relative/submodule/a.js +1 -0
- data/tests/commonjs/modules/relative/submodule/b.js +2 -0
- data/tests/commonjs/modules/relative/test.js +9 -0
- data/tests/commonjs/modules/transitive/a.js +1 -0
- data/tests/commonjs/modules/transitive/b.js +1 -0
- data/tests/commonjs/modules/transitive/c.js +3 -0
- data/tests/commonjs/modules/transitive/program.js +3 -0
- data/tests/commonjs/modules/transitive/test.js +9 -0
- data/tests/file/all-tests.js +61 -0
- data/tests/file/fnmatch.js +102 -0
- data/tests/file/glob.js +466 -0
- data/tests/file/match.js +102 -0
- data/tests/global.js +6 -0
- data/tests/global/array.js +19 -0
- data/tests/hashes.js +94 -0
- data/tests/html.js +13 -0
- data/tests/io/stringio.js +21 -0
- data/tests/os/all-tests.js +4 -0
- data/tests/os/popen.js +41 -0
- data/tests/os/system.js +22 -0
- data/tests/printf.js +123 -0
- data/tests/query-string.js +87 -0
- data/tests/sandbox/byte-io.js +20 -0
- data/tests/sandbox/fileName.js +3 -0
- data/tests/sandbox/foo.js +0 -0
- data/tests/sandbox/reload.js +79 -0
- data/tests/string.js +35 -0
- data/tests/uri.js +41 -0
- data/tests/util/all-tests.js +79 -0
- data/tests/util/array.js +207 -0
- data/tests/util/array/is-arguments.js +29 -0
- data/tests/util/array/is-array-like.js +29 -0
- data/tests/util/case.js +9 -0
- data/tests/util/collection.js +104 -0
- data/tests/util/eq.js +57 -0
- data/tests/util/expand.js +45 -0
- data/tests/util/object.js +125 -0
- data/tests/util/operator.js +25 -0
- data/tests/util/range.js +19 -0
- data/tests/util/repr.js +26 -0
- data/tests/util/string.js +34 -0
- data/tests/util/unique.js +12 -0
- metadata +434 -0
@@ -0,0 +1,755 @@
|
|
1
|
+
/* Binary */
|
2
|
+
|
3
|
+
var engine = require("binary-engine"),
|
4
|
+
B_ALLOC = engine.B_ALLOC,
|
5
|
+
B_LENGTH = engine.B_LENGTH,
|
6
|
+
B_GET = engine.B_GET,
|
7
|
+
B_SET = engine.B_SET,
|
8
|
+
B_FILL = engine.B_FILL,
|
9
|
+
B_COPY = engine.B_COPY,
|
10
|
+
B_DECODE = engine.B_DECODE,
|
11
|
+
B_ENCODE = engine.B_ENCODE,
|
12
|
+
B_DECODE_DEFAULT = engine.B_DECODE_DEFAULT,
|
13
|
+
B_ENCODE_DEFAULT = engine.B_ENCODE_DEFAULT,
|
14
|
+
B_TRANSCODE = engine.B_TRANSCODE;
|
15
|
+
|
16
|
+
var Binary = exports.Binary = function() {
|
17
|
+
// this._bytes
|
18
|
+
// this._offset
|
19
|
+
// this._length
|
20
|
+
};
|
21
|
+
|
22
|
+
// XXX non interoperable: create and use an Object.defineProperty stub.
|
23
|
+
Binary.prototype.__defineGetter__("length", function() {
|
24
|
+
return this._length;
|
25
|
+
});
|
26
|
+
Binary.prototype.__defineSetter__("length", function(length) {
|
27
|
+
print("x trying to set length: " + length);
|
28
|
+
});
|
29
|
+
|
30
|
+
// toArray() - n array of the byte values
|
31
|
+
// toArray(charset) - an array of the code points, decoded
|
32
|
+
Binary.prototype.toArray = function(charset) {
|
33
|
+
if (arguments.length === 0) {
|
34
|
+
var array = new Array(this._length);
|
35
|
+
|
36
|
+
for (var i = 0; i < this._length; i++)
|
37
|
+
array[i] = this.get(i);
|
38
|
+
|
39
|
+
return array;
|
40
|
+
}
|
41
|
+
else if (arguments.length === 1) {
|
42
|
+
var string = B_DECODE(this._bytes, this._offset, this._length, charset),
|
43
|
+
length = string.length,
|
44
|
+
array = new Array(length);
|
45
|
+
|
46
|
+
for (var i = 0; i < length; i++)
|
47
|
+
array[i] = string.charCodeAt(i);
|
48
|
+
|
49
|
+
return array;
|
50
|
+
}
|
51
|
+
else
|
52
|
+
throw new Error("Illegal arguments to toArray()");
|
53
|
+
};
|
54
|
+
|
55
|
+
// toByteArray() - just a copy
|
56
|
+
// toByteArray(sourceCharset, targetCharset) - transcoded
|
57
|
+
Binary.prototype.toByteArray = function(sourceCodec, targetCodec) {
|
58
|
+
if (arguments.length < 2)
|
59
|
+
return new ByteArray(this);
|
60
|
+
else if (arguments.length === 2 && typeof sourceCodec === "string" && typeof targetCodec === "string") {
|
61
|
+
var bytes = B_TRANSCODE(this._bytes, this._offset, this._length, sourceCodec, targetCodec);
|
62
|
+
return new ByteArray(bytes, 0, B_LENGTH(bytes));
|
63
|
+
}
|
64
|
+
|
65
|
+
throw new Error("Illegal arguments to ByteArray toByteArray");
|
66
|
+
};
|
67
|
+
|
68
|
+
// toByteString() - byte for byte copy
|
69
|
+
// toByteString(sourceCharset, targetCharset) - transcoded
|
70
|
+
Binary.prototype.toByteString = function(sourceCodec, targetCodec) {
|
71
|
+
if (arguments.length < 2)
|
72
|
+
return new ByteString(this);
|
73
|
+
else if (arguments.length === 2 && typeof sourceCodec === "string" && typeof targetCodec === "string") {
|
74
|
+
var bytes = B_TRANSCODE(this._bytes, this._offset, this._length, sourceCodec, targetCodec);
|
75
|
+
return new ByteString(bytes, 0, B_LENGTH(bytes));
|
76
|
+
}
|
77
|
+
|
78
|
+
throw new Error("Illegal arguments to ByteArray toByteString");
|
79
|
+
};
|
80
|
+
|
81
|
+
// decodeToString()
|
82
|
+
// decodeToString(charset) - returns a String from its decoded bytes in a given charset. If no charset is provided, or if the charset is "undefined", assumes the default system encoding.
|
83
|
+
// decodeToString(number) - returns a String from its decoded bytes in a given base, like 64, 32, 16, 8, 2
|
84
|
+
Binary.prototype.decodeToString = function(charset) {
|
85
|
+
if (charset) {
|
86
|
+
if (typeof charset == "number")
|
87
|
+
return require("base" + charset).encode(this);
|
88
|
+
else if (charset.begins("base"))
|
89
|
+
return require(charset).encode(this);
|
90
|
+
else
|
91
|
+
return B_DECODE(this._bytes, this._offset, this._length, charset);
|
92
|
+
}
|
93
|
+
return B_DECODE_DEFAULT(this._bytes, this._offset, this._length);
|
94
|
+
};
|
95
|
+
|
96
|
+
// get(offset) - Return the byte at offset as a Number.
|
97
|
+
Binary.prototype.get = function(offset) {
|
98
|
+
if (offset < 0 || offset >= this._length)
|
99
|
+
return NaN;
|
100
|
+
|
101
|
+
//var b = this._bytes[this._offset + offset];
|
102
|
+
//return (b >= 0) ? b : -1 * ((b ^ 0xFF) + 1);
|
103
|
+
return B_GET(this._bytes, this._offset + offset)
|
104
|
+
};
|
105
|
+
|
106
|
+
Binary.prototype.indexOf = function(byteValue, start, stop) {
|
107
|
+
// HACK: use ByteString's slice since we know we won't be modifying result
|
108
|
+
var array = ByteString.prototype.slice.apply(this, [start, stop]).toArray(),
|
109
|
+
result = array.indexOf(byteValue);
|
110
|
+
return (result < 0) ? -1 : result + (start || 0);
|
111
|
+
};
|
112
|
+
|
113
|
+
Binary.prototype.lastIndexOf = function(byteValue, start, stop) {
|
114
|
+
// HACK: use ByteString's slice since we know we won't be modifying result
|
115
|
+
var array = ByteString.prototype.slice.apply(this, [start, stop]).toArray(),
|
116
|
+
result = array.lastIndexOf(byteValue);
|
117
|
+
return (result < 0) ? -1 : result + (start || 0);
|
118
|
+
};
|
119
|
+
|
120
|
+
// valueOf()
|
121
|
+
Binary.prototype.valueOf = function() {
|
122
|
+
return this;
|
123
|
+
};
|
124
|
+
|
125
|
+
/* ByteString */
|
126
|
+
|
127
|
+
var ByteString = exports.ByteString = function() {
|
128
|
+
if (!(this instanceof ByteString)) {
|
129
|
+
if (arguments.length == 0)
|
130
|
+
return new ByteString();
|
131
|
+
if (arguments.length == 1)
|
132
|
+
return new ByteString(arguments[0]);
|
133
|
+
if (arguments.length == 2)
|
134
|
+
return new ByteString(arguments[0], arguments[1]);
|
135
|
+
if (arguments.length == 3)
|
136
|
+
return new ByteString(arguments[0], arguments[1], arguments[2]);
|
137
|
+
}
|
138
|
+
|
139
|
+
// ByteString() - Construct an empty byte string.
|
140
|
+
if (arguments.length === 0) {
|
141
|
+
this._bytes = B_ALLOC(0); // null;
|
142
|
+
this._offset = 0;
|
143
|
+
this._length = 0;
|
144
|
+
}
|
145
|
+
// ByteString(byteString) - Copies byteString.
|
146
|
+
else if (arguments.length === 1 && arguments[0] instanceof ByteString) {
|
147
|
+
return arguments[0];
|
148
|
+
}
|
149
|
+
// ByteString(byteArray) - Use the contents of byteArray.
|
150
|
+
else if (arguments.length === 1 && arguments[0] instanceof ByteArray) {
|
151
|
+
var copy = arguments[0].toByteArray();
|
152
|
+
this._bytes = copy._bytes;
|
153
|
+
this._offset = copy._offset;
|
154
|
+
this._length = copy._length;
|
155
|
+
}
|
156
|
+
// ByteString(arrayOfNumbers) - Use the numbers in arrayOfNumbers as the bytes.
|
157
|
+
else if (arguments.length === 1 && Array.isArray(arguments[0])) {
|
158
|
+
var array = arguments[0];
|
159
|
+
this._bytes = B_ALLOC(array.length);
|
160
|
+
for (var i = 0; i < array.length; i++) {
|
161
|
+
var b = array[i];
|
162
|
+
// If any element is outside the range 0...255, an exception (TODO) is thrown.
|
163
|
+
if (b < -0x80 || b > 0xFF)
|
164
|
+
throw new Error("ByteString constructor argument Array of integers must be -128 - 255 ("+b+")");
|
165
|
+
// Java "bytes" are interpreted as 2's complement
|
166
|
+
//this._bytes[i] = (b < 128) ? b : -1 * ((b ^ 0xFF) + 1);
|
167
|
+
B_SET(this._bytes, i, b);
|
168
|
+
}
|
169
|
+
this._offset = 0;
|
170
|
+
this._length = B_LENGTH(this._bytes);
|
171
|
+
}
|
172
|
+
// ByteString(string, charset) - Convert a string. The ByteString will contain string encoded with charset.
|
173
|
+
else if ((arguments.length === 1 || (arguments.length === 2 && arguments[1] === undefined)) && typeof arguments[0] === "string") {
|
174
|
+
this._bytes = B_ENCODE_DEFAULT(arguments[0]);
|
175
|
+
this._offset = 0;
|
176
|
+
this._length = B_LENGTH(this._bytes);
|
177
|
+
}
|
178
|
+
else if (arguments.length === 2 && typeof arguments[0] === "string" && typeof arguments[1] === "string") {
|
179
|
+
this._bytes = B_ENCODE(arguments[0], arguments[1]);
|
180
|
+
this._offset = 0;
|
181
|
+
this._length = B_LENGTH(this._bytes);
|
182
|
+
}
|
183
|
+
// private: ByteString(bytes, offset, length)
|
184
|
+
else if (arguments.length === 3 && typeof arguments[1] === "number" && typeof arguments[2] === "number") {
|
185
|
+
this._bytes = arguments[0];
|
186
|
+
this._offset = arguments[1];
|
187
|
+
this._length = arguments[2];
|
188
|
+
}
|
189
|
+
else {
|
190
|
+
var util = require("util");
|
191
|
+
throw new Error("Illegal arguments to ByteString constructor: " + util.repr(arguments));
|
192
|
+
}
|
193
|
+
|
194
|
+
if (engine.ByteStringWrapper)
|
195
|
+
return engine.ByteStringWrapper(this);
|
196
|
+
else
|
197
|
+
return this;
|
198
|
+
};
|
199
|
+
|
200
|
+
ByteString.prototype = new Binary();
|
201
|
+
|
202
|
+
ByteString.prototype.__defineGetter__("length", function() {
|
203
|
+
return this._length;
|
204
|
+
});
|
205
|
+
ByteString.prototype.__defineSetter__("length", function(length) {
|
206
|
+
});
|
207
|
+
|
208
|
+
// toByteArray() - Returns a byte for byte copy in a ByteArray.
|
209
|
+
// toByteArray(sourceCharset, targetCharset) - Returns a transcoded copy in a ByteArray.
|
210
|
+
// - implemented on Binary
|
211
|
+
|
212
|
+
// toByteString() - Returns itself, since there's no need to copy an immutable ByteString.
|
213
|
+
// toByteString(sourceCharset, targetCharset) - Returns a transcoded copy.
|
214
|
+
// - implemented on Binary
|
215
|
+
|
216
|
+
// toArray() - Returns an array containing the bytes as numbers.
|
217
|
+
// toArray(charset) - Returns an array containing the decoded Unicode code points.
|
218
|
+
// - implemented on Binary
|
219
|
+
|
220
|
+
// toString()
|
221
|
+
ByteString.prototype.toString = function(charset) {
|
222
|
+
if (charset)
|
223
|
+
return this.decodeToString(charset);
|
224
|
+
|
225
|
+
return "[ByteString "+this._length+"]";
|
226
|
+
};
|
227
|
+
|
228
|
+
// decodeToString(charset) - Returns the decoded ByteArray as a string.
|
229
|
+
// - implemented on Binary
|
230
|
+
|
231
|
+
ByteString.prototype.byteAt =
|
232
|
+
ByteString.prototype.charAt = function(offset) {
|
233
|
+
var byteValue = this.get(offset);
|
234
|
+
|
235
|
+
if (isNaN(byteValue))
|
236
|
+
return new ByteString();
|
237
|
+
|
238
|
+
return new ByteString([byteValue]);
|
239
|
+
};
|
240
|
+
|
241
|
+
// indexOf() - implemented on Binary
|
242
|
+
// lastIndexOf() - implemented on Binary
|
243
|
+
|
244
|
+
// charCodeAt(offset)
|
245
|
+
ByteString.prototype.charCodeAt = Binary.prototype.get;
|
246
|
+
|
247
|
+
// get(offset) - implemented on Binary
|
248
|
+
|
249
|
+
// byteAt(offset) ByteString - implemented on Binary
|
250
|
+
// charAt(offset) ByteString - implemented on Binary
|
251
|
+
|
252
|
+
// split(delimiter, [options])
|
253
|
+
ByteString.prototype.split = function(delimiters, options) {
|
254
|
+
var options = options || {},
|
255
|
+
count = options.count === undefined ? -1 : options.count,
|
256
|
+
includeDelimiter = options.includeDelimiter || false;
|
257
|
+
|
258
|
+
// standardize delimiters into an array of ByteStrings:
|
259
|
+
if (!Array.isArray(delimiters))
|
260
|
+
delimiters = [delimiters];
|
261
|
+
|
262
|
+
delimiters = delimiters.map(function(delimiter) {
|
263
|
+
if (typeof delimiter === "number")
|
264
|
+
delimiter = [delimiter];
|
265
|
+
return new ByteString(delimiter);
|
266
|
+
});
|
267
|
+
|
268
|
+
var components = [],
|
269
|
+
startOffset = this._offset,
|
270
|
+
currentOffset = this._offset;
|
271
|
+
|
272
|
+
// loop until there's no more bytes to consume
|
273
|
+
bytes_loop :
|
274
|
+
while (currentOffset < this._offset + this._length) {
|
275
|
+
|
276
|
+
// try each delimiter until we find a match
|
277
|
+
delimiters_loop :
|
278
|
+
for (var i = 0; i < delimiters.length; i++) {
|
279
|
+
var d = delimiters[i];
|
280
|
+
|
281
|
+
for (var j = 0; j < d._length; j++) {
|
282
|
+
// reached the end of the bytes, OR bytes not equal
|
283
|
+
if (currentOffset + j > this._offset + this._length ||
|
284
|
+
B_GET(this._bytes, currentOffset + j) !== B_GET(d._bytes, d._offset + j)) {
|
285
|
+
continue delimiters_loop;
|
286
|
+
}
|
287
|
+
}
|
288
|
+
|
289
|
+
// push the part before the delimiter
|
290
|
+
components.push(new ByteString(this._bytes, startOffset, currentOffset - startOffset));
|
291
|
+
|
292
|
+
// optionally push the delimiter
|
293
|
+
if (includeDelimiter)
|
294
|
+
components.push(new ByteString(this._bytes, currentOffset, d._length))
|
295
|
+
|
296
|
+
// reset the offsets
|
297
|
+
startOffset = currentOffset = currentOffset + d._length;
|
298
|
+
|
299
|
+
continue bytes_loop;
|
300
|
+
}
|
301
|
+
|
302
|
+
// if there was no match, increment currentOffset to try the next one
|
303
|
+
currentOffset++;
|
304
|
+
}
|
305
|
+
|
306
|
+
// push the remaining part, if any
|
307
|
+
if (currentOffset > startOffset)
|
308
|
+
components.push(new ByteString(this._bytes, startOffset, currentOffset - startOffset));
|
309
|
+
|
310
|
+
return components;
|
311
|
+
};
|
312
|
+
|
313
|
+
// slice()
|
314
|
+
// slice(begin)
|
315
|
+
// slice(begin, end)
|
316
|
+
ByteString.prototype.slice = function(begin, end) {
|
317
|
+
if (begin === undefined)
|
318
|
+
begin = 0;
|
319
|
+
else if (begin < 0)
|
320
|
+
begin = this._length + begin;
|
321
|
+
|
322
|
+
if (end === undefined)
|
323
|
+
end = this._length;
|
324
|
+
else if (end < 0)
|
325
|
+
end = this._length + end;
|
326
|
+
|
327
|
+
begin = Math.min(this._length, Math.max(0, begin));
|
328
|
+
end = Math.min(this._length, Math.max(0, end));
|
329
|
+
|
330
|
+
return new ByteString(this._bytes, this._offset + begin, end - begin);
|
331
|
+
};
|
332
|
+
|
333
|
+
// substr(start)
|
334
|
+
// substr(start, length)
|
335
|
+
ByteString.prototype.substr = function(start, length) {
|
336
|
+
if (start !== undefined) {
|
337
|
+
if (length !== undefined)
|
338
|
+
return this.slice(start);
|
339
|
+
else
|
340
|
+
return this.slice(start, start + length);
|
341
|
+
}
|
342
|
+
return this.slice();
|
343
|
+
};
|
344
|
+
|
345
|
+
// substring(first)
|
346
|
+
// substring(first, last)
|
347
|
+
ByteString.prototype.substring = function(from, to) {
|
348
|
+
if (from !== undefined) {
|
349
|
+
if (to !== undefined)
|
350
|
+
return this.slice(Math.max(Math.min(from, this._length), 0));
|
351
|
+
else
|
352
|
+
return this.slice(Math.max(Math.min(from, this._length), 0),
|
353
|
+
Math.max(Math.min(to, this._length), 0));
|
354
|
+
}
|
355
|
+
return this.slice();
|
356
|
+
};
|
357
|
+
|
358
|
+
// [] ByteString - TODO
|
359
|
+
|
360
|
+
// toSource()
|
361
|
+
ByteString.prototype.toSource = function() {
|
362
|
+
return "ByteString(["+this.toArray().join(",")+"])";
|
363
|
+
};
|
364
|
+
|
365
|
+
/* ByteArray */
|
366
|
+
|
367
|
+
// ByteArray() - New, empty ByteArray.
|
368
|
+
// ByteArray(length) - New ByteArray filled with length zero bytes.
|
369
|
+
// ByteArray(byteArray) - Copy byteArray.
|
370
|
+
// ByteArray(byteString) - Copy contents of byteString.
|
371
|
+
// ByteArray(arrayOfBytes) - Use numbers in arrayOfBytes as contents.
|
372
|
+
// Throws an exception if any element is outside the range 0...255 (TODO).
|
373
|
+
// ByteArray(string, charset) - Create a ByteArray from a Javascript string, the result being encoded with charset.
|
374
|
+
var ByteArray = exports.ByteArray = function() {
|
375
|
+
if (!this instanceof ByteArray) {
|
376
|
+
if (arguments.length == 0)
|
377
|
+
return new ByteArray();
|
378
|
+
if (arguments.length == 1)
|
379
|
+
return new ByteArray(arguments[0]);
|
380
|
+
if (arguments.length == 2)
|
381
|
+
return new ByteArray(arguments[0], arguments[1]);
|
382
|
+
if (arguments.length == 3)
|
383
|
+
return new ByteArray(arguments[0], arguments[1], arguments[2]);
|
384
|
+
}
|
385
|
+
|
386
|
+
// ByteArray() - New, empty ByteArray.
|
387
|
+
if (arguments.length === 0) {
|
388
|
+
this._bytes = B_ALLOC(0); // null;
|
389
|
+
this._offset = 0;
|
390
|
+
this._length = 0;
|
391
|
+
}
|
392
|
+
// ByteArray(length) - New ByteArray filled with length zero bytes.
|
393
|
+
else if (arguments.length === 1 && typeof arguments[0] === "number") {
|
394
|
+
this._bytes = B_ALLOC(arguments[0]); // null;
|
395
|
+
this._offset = 0;
|
396
|
+
this._length = B_LENGTH(this._bytes);
|
397
|
+
}
|
398
|
+
// ByteArray(byteArray) - Copy byteArray.
|
399
|
+
// ByteArray(byteString) - Copy contents of byteString.
|
400
|
+
else if (arguments.length === 1 && (arguments[0] instanceof ByteArray || arguments[0] instanceof ByteString)) {
|
401
|
+
var byteArray = new ByteArray(arguments[0]._length);
|
402
|
+
B_COPY(arguments[0]._bytes, arguments[0]._offset, byteArray._bytes, byteArray._offset, byteArray._length);
|
403
|
+
return byteArray;
|
404
|
+
}
|
405
|
+
// ByteArray(arrayOfBytes) - Use numbers in arrayOfBytes as contents.
|
406
|
+
// Throws an exception if any element is outside the range 0...255 (TODO).
|
407
|
+
else if (arguments.length === 1 && Array.isArray(arguments[0])) {
|
408
|
+
var array = arguments[0];
|
409
|
+
this._bytes = B_ALLOC(array.length);
|
410
|
+
for (var i = 0; i < array.length; i++) {
|
411
|
+
var b = array[i];
|
412
|
+
// If any element is outside the range 0...255, an exception (TODO) is thrown.
|
413
|
+
if (b < 0 || b > 0xFF)
|
414
|
+
throw new Error("ByteString constructor argument Array of integers must be 0 - 255 ("+b+")");
|
415
|
+
// Java "bytes" are interpreted as 2's complement
|
416
|
+
//this._bytes[i] = (b < 128) ? b : -1 * ((b ^ 0xFF) + 1);
|
417
|
+
B_SET(this._bytes, i, b);
|
418
|
+
}
|
419
|
+
this._offset = 0;
|
420
|
+
this._length = B_LENGTH(this._bytes);
|
421
|
+
}
|
422
|
+
// ByteArray(string, charset) - Create a ByteArray from a Javascript string, the result being encoded with charset.
|
423
|
+
else if ((arguments.length === 1 || (arguments.length === 2 && arguments[1] === undefined)) && typeof arguments[0] === "string") {
|
424
|
+
this._bytes = B_ENCODE_DEFAULT(arguments[0]);
|
425
|
+
this._offset = 0;
|
426
|
+
this._length = B_LENGTH(this._bytes);
|
427
|
+
}
|
428
|
+
else if (arguments.length === 2 && typeof arguments[0] === "string" && typeof arguments[1] === "string") {
|
429
|
+
this._bytes = B_ENCODE(arguments[0], arguments[1]);
|
430
|
+
this._offset = 0;
|
431
|
+
this._length = B_LENGTH(this._bytes);
|
432
|
+
}
|
433
|
+
// private: ByteArray(bytes, offset, length)
|
434
|
+
else if (arguments.length === 3 && typeof arguments[1] === "number" && typeof arguments[2] === "number") {
|
435
|
+
this._bytes = arguments[0];
|
436
|
+
this._offset = arguments[1];
|
437
|
+
this._length = arguments[2];
|
438
|
+
}
|
439
|
+
else
|
440
|
+
throw new Error("Illegal arguments to ByteString constructor: [" +
|
441
|
+
Array.prototype.join.apply(arguments, [","]) + "] ("+arguments.length+")");
|
442
|
+
|
443
|
+
if (engine.ByteArrayWrapper)
|
444
|
+
return engine.ByteArrayWrapper(this);
|
445
|
+
else
|
446
|
+
return this;
|
447
|
+
};
|
448
|
+
|
449
|
+
ByteArray.prototype = new Binary();
|
450
|
+
|
451
|
+
ByteArray.prototype.__defineGetter__("length", function() {
|
452
|
+
return this._length;
|
453
|
+
});
|
454
|
+
ByteArray.prototype.__defineSetter__("length", function(length) {
|
455
|
+
if (typeof length !== "number")
|
456
|
+
return;
|
457
|
+
|
458
|
+
// same length
|
459
|
+
if (length === this._length) {
|
460
|
+
return;
|
461
|
+
}
|
462
|
+
// new length is less, truncate
|
463
|
+
else if (length < this._length) {
|
464
|
+
this._length = length;
|
465
|
+
}
|
466
|
+
// new length is more, but fits without moving, just clear new bytes
|
467
|
+
else if (this._offset + length <= B_LENGTH(this._bytes)) {
|
468
|
+
B_FILL(this._bytes, this._length, this._offset + length - 1, 0);
|
469
|
+
this._length = length;
|
470
|
+
}
|
471
|
+
// new length is more, but fits if we shift to bottom, so do that.
|
472
|
+
else if (length <= B_LENGTH(this._bytes)) {
|
473
|
+
B_COPY(this._bytes, this._offset, this._bytes, 0, this._length);
|
474
|
+
this._offset = 0;
|
475
|
+
B_FILL(this._bytes, this._length, this._offset + length - 1, 0);
|
476
|
+
this._length = length;
|
477
|
+
}
|
478
|
+
// new length is more than the allocated bytes array, allocate a new one and copy the data
|
479
|
+
else {
|
480
|
+
var newBytes = B_ALLOC(length);
|
481
|
+
B_COPY(this._bytes, this._offset, newBytes, 0, this._length);
|
482
|
+
this._bytes = newBytes;
|
483
|
+
this._offset = 0;
|
484
|
+
this._length = length;
|
485
|
+
}
|
486
|
+
});
|
487
|
+
|
488
|
+
// FIXME: array notation for set and get
|
489
|
+
ByteArray.prototype.set = function(index, b) {
|
490
|
+
// If any element is outside the range 0...255, an exception (TODO) is thrown.
|
491
|
+
if (b < 0 || b > 0xFF)
|
492
|
+
throw new Error("ByteString constructor argument Array of integers must be 0 - 255 ("+b+")");
|
493
|
+
|
494
|
+
if (index < 0 || index >= this._length)
|
495
|
+
throw new Error("Out of range");
|
496
|
+
|
497
|
+
// Java "bytes" are interpreted as 2's complement
|
498
|
+
//this._bytes[this._offset + index] = (b < 128) ? b : -1 * ((b ^ 0xFF) + 1);
|
499
|
+
B_SET(this._bytes, this._offset + index, b);
|
500
|
+
};
|
501
|
+
|
502
|
+
// toArray()
|
503
|
+
// toArray(charset)
|
504
|
+
// - implemented on Binary
|
505
|
+
|
506
|
+
// toByteArray() - just a copy
|
507
|
+
// toByteArray(sourceCharset, targetCharset) - transcoded
|
508
|
+
// - implemented on Binary
|
509
|
+
|
510
|
+
// toByteString() - byte for byte copy
|
511
|
+
// toByteString(sourceCharset, targetCharset) - transcoded
|
512
|
+
// - implemented on Binary
|
513
|
+
|
514
|
+
// toString() - a string representation like "[ByteArray 10]"
|
515
|
+
// toString(charset) - an alias for decodeToString(charset)
|
516
|
+
ByteArray.prototype.toString = function(charset) {
|
517
|
+
if (charset)
|
518
|
+
return this.decodeToString(charset);
|
519
|
+
|
520
|
+
return "[ByteArray "+this._length+"]";
|
521
|
+
};
|
522
|
+
|
523
|
+
// decodeToString(charset) - implemented on Binary
|
524
|
+
|
525
|
+
// byteAt(offset) ByteString - Return the byte at offset as a ByteString.
|
526
|
+
// - implemented on Binary
|
527
|
+
|
528
|
+
// get(offset) Number - Return the byte at offset as a Number.
|
529
|
+
// - implemented on Binary
|
530
|
+
|
531
|
+
// concat(other ByteArray|ByteString|Array)
|
532
|
+
// TODO: I'm assuming Array means an array of ByteStrings/ByteArrays, not an array of integers.
|
533
|
+
ByteArray.prototype.concat = function() {
|
534
|
+
var components = [this],
|
535
|
+
totalLength = this._length;
|
536
|
+
|
537
|
+
for (var i = 0; i < arguments.length; i++) {
|
538
|
+
var component = Array.isArray(arguments[i]) ? arguments[i] : [arguments[i]];
|
539
|
+
|
540
|
+
for (var j = 0; j < component.length; j++) {
|
541
|
+
var subcomponent = component[j];
|
542
|
+
if (!(subcomponent instanceof ByteString) && !(subcomponent instanceof ByteArray))
|
543
|
+
throw "Arguments to ByteArray.concat() must be ByteStrings, ByteArrays, or Arrays of those.";
|
544
|
+
|
545
|
+
components.push(subcomponent);
|
546
|
+
totalLength += subcomponent.length;
|
547
|
+
}
|
548
|
+
}
|
549
|
+
|
550
|
+
var result = new ByteArray(totalLength),
|
551
|
+
offset = 0;
|
552
|
+
|
553
|
+
components.forEach(function(component) {
|
554
|
+
B_COPY(component._bytes, component._offset, result._bytes, offset, component._length);
|
555
|
+
offset += component._length;
|
556
|
+
});
|
557
|
+
|
558
|
+
return result;
|
559
|
+
};
|
560
|
+
|
561
|
+
// pop() -> byte Number
|
562
|
+
ByteArray.prototype.pop = function() {
|
563
|
+
if (this._length === 0)
|
564
|
+
return undefined;
|
565
|
+
|
566
|
+
this._length--;
|
567
|
+
|
568
|
+
return B_GET(this._bytes, this._offset + this._length);
|
569
|
+
};
|
570
|
+
|
571
|
+
// push(...variadic Numbers...)-> count Number
|
572
|
+
ByteArray.prototype.push = function() {
|
573
|
+
var length, newLength = this.length += length = arguments.length;
|
574
|
+
try {
|
575
|
+
for (var i = 0; i < length; i++)
|
576
|
+
this.set(newLength - length + i, arguments[i]);
|
577
|
+
} catch (e) {
|
578
|
+
this.length -= length;
|
579
|
+
throw e;
|
580
|
+
}
|
581
|
+
return newLength;
|
582
|
+
};
|
583
|
+
|
584
|
+
// extendRight(...variadic Numbers / Arrays / ByteArrays / ByteStrings ...)
|
585
|
+
ByteArray.prototype.extendRight = function() {
|
586
|
+
throw "NYI";
|
587
|
+
};
|
588
|
+
|
589
|
+
// shift() -> byte Number
|
590
|
+
ByteArray.prototype.shift = function() {
|
591
|
+
if (this._length === 0)
|
592
|
+
return undefined;
|
593
|
+
|
594
|
+
this._length--;
|
595
|
+
this._offset++;
|
596
|
+
|
597
|
+
return B_GET(this._bytes, this._offset - 1);
|
598
|
+
};
|
599
|
+
|
600
|
+
// unshift(...variadic Numbers...) -> count Number
|
601
|
+
ByteArray.prototype.unshift = function() {
|
602
|
+
var copy = this.slice();
|
603
|
+
this.length = 0;
|
604
|
+
try {
|
605
|
+
this.push.apply(this, arguments);
|
606
|
+
this.push.apply(this, copy.toArray());
|
607
|
+
return this.length;
|
608
|
+
} catch(e) {
|
609
|
+
B_COPY(copy._bytes, copy._offset, this._bytes, this._offset, copy.length);
|
610
|
+
this.length = copy.length;
|
611
|
+
throw e;
|
612
|
+
}
|
613
|
+
};
|
614
|
+
|
615
|
+
// extendLeft(...variadic Numbers / Arrays / ByteArrays / ByteStrings ...)
|
616
|
+
ByteArray.prototype.extendLeft = function() {
|
617
|
+
throw "NYI";
|
618
|
+
};
|
619
|
+
|
620
|
+
// reverse() in place reversal
|
621
|
+
ByteArray.prototype.reverse = function() {
|
622
|
+
// "limit" is halfway, rounded down. "top" is the last index.
|
623
|
+
var limit = Math.floor(this._length/2) + this._offset,
|
624
|
+
top = this._length - 1;
|
625
|
+
|
626
|
+
// swap each pair of bytes, up to the halfway point
|
627
|
+
for (var i = this._offset; i < limit; i++) {
|
628
|
+
var tmp = B_GET(this._bytes, i);
|
629
|
+
B_SET(this._bytes, i, B_GET(this._bytes, top - i));
|
630
|
+
B_SET(this._bytes, top - i, tmp);
|
631
|
+
}
|
632
|
+
|
633
|
+
return this;
|
634
|
+
};
|
635
|
+
|
636
|
+
// slice()
|
637
|
+
ByteArray.prototype.slice = function() {
|
638
|
+
return new ByteArray(ByteString.prototype.slice.apply(this, arguments));
|
639
|
+
};
|
640
|
+
|
641
|
+
var numericCompareFunction = function(o1, o2) { return o1 - o2; };
|
642
|
+
|
643
|
+
// sort([compareFunction])
|
644
|
+
ByteArray.prototype.sort = function(compareFunction) {
|
645
|
+
// FIXME: inefficient?
|
646
|
+
|
647
|
+
var array = this.toArray();
|
648
|
+
|
649
|
+
if (arguments.length)
|
650
|
+
array.sort(compareFunction);
|
651
|
+
else
|
652
|
+
array.sort(numericCompareFunction);
|
653
|
+
|
654
|
+
for (var i = 0; i < array.length; i++)
|
655
|
+
this.set(i, array[i]);
|
656
|
+
};
|
657
|
+
|
658
|
+
// splice()
|
659
|
+
ByteArray.prototype.splice = function(index, howMany /*, elem1, elem2 */) {
|
660
|
+
if (index === undefined) return;
|
661
|
+
if (index < 0) index += this.length;
|
662
|
+
if (howMany === undefined) howMany = this._length - index;
|
663
|
+
var end = index + howMany;
|
664
|
+
var remove = this.slice(index, end);
|
665
|
+
var keep = this.slice(end);
|
666
|
+
var inject = Array.prototype.slice.call(arguments, 2);
|
667
|
+
this._length = index;
|
668
|
+
this.push.apply(this, inject);
|
669
|
+
this.push.apply(this, keep.toArray());
|
670
|
+
return remove;
|
671
|
+
};
|
672
|
+
|
673
|
+
// indexOf() - implemented on Binary
|
674
|
+
// lastIndexOf() - implemented on Binary
|
675
|
+
|
676
|
+
// split() Returns an array of ByteArrays instead of ByteStrings.
|
677
|
+
ByteArray.prototype.split = function() {
|
678
|
+
var components = ByteString.prototype.split.apply(this.toByteString(), arguments);
|
679
|
+
|
680
|
+
// convert ByteStrings to ByteArrays
|
681
|
+
for (var i = 0; i < components.length; i++) {
|
682
|
+
// we know we can use these byte buffers directly since we copied them above
|
683
|
+
components[i] = new ByteArray(components[i]._bytes, components[i]._offset, components[i]._length);
|
684
|
+
}
|
685
|
+
|
686
|
+
return components;
|
687
|
+
};
|
688
|
+
|
689
|
+
// filter(callback[, thisObject])
|
690
|
+
ByteArray.prototype.filter = function(callback, thisObject) {
|
691
|
+
var result = new ByteArray(this._length);
|
692
|
+
for (var i = 0, length = this._length; i < length; i++) {
|
693
|
+
var value = this.get(i);
|
694
|
+
if (callback.apply(thisObject, [value, i, this]))
|
695
|
+
result.push(value);
|
696
|
+
}
|
697
|
+
return result;
|
698
|
+
};
|
699
|
+
|
700
|
+
// forEach(callback[, thisObject]);
|
701
|
+
ByteArray.prototype.forEach = function(callback, thisObject) {
|
702
|
+
for (var i = 0, length = this._length; i < length; i++)
|
703
|
+
callback.apply(thisObject, [this.get(i), i, this]);
|
704
|
+
};
|
705
|
+
|
706
|
+
// every(callback[, thisObject])
|
707
|
+
ByteArray.prototype.every = function(callback, thisObject) {
|
708
|
+
for (var i = 0, length = this._length; i < length; i++)
|
709
|
+
if (!callback.apply(thisObject, [this.get(i), i, this]))
|
710
|
+
return false;
|
711
|
+
return true;
|
712
|
+
};
|
713
|
+
|
714
|
+
// some(callback[, thisObject])
|
715
|
+
ByteArray.prototype.some = function(callback, thisObject) {
|
716
|
+
for (var i = 0, length = this._length; i < length; i++)
|
717
|
+
if (callback.apply(thisObject, [this.get(i), i, this]))
|
718
|
+
return true;
|
719
|
+
return false;
|
720
|
+
};
|
721
|
+
|
722
|
+
// map(callback[, thisObject]);
|
723
|
+
ByteArray.prototype.map = function(callback, thisObject) {
|
724
|
+
var result = new ByteArray(this._length);
|
725
|
+
for (var i = 0, length = this._length; i < length; i++)
|
726
|
+
result.set(i, callback.apply(thisObject, [this.get(i), i, this]));
|
727
|
+
return result;
|
728
|
+
};
|
729
|
+
|
730
|
+
// reduce(callback[, initialValue])
|
731
|
+
ByteArray.prototype.reduce = function(callback, initialValue) {
|
732
|
+
var value = initialValue;
|
733
|
+
for (var i = 0, length = this._length; i < length; i++)
|
734
|
+
value = callback(value, this.get(i), i, this);
|
735
|
+
return value;
|
736
|
+
};
|
737
|
+
|
738
|
+
// reduceRight(callback[, initialValue])
|
739
|
+
ByteArray.prototype.reduceRight = function(callback, initialValue) {
|
740
|
+
var value = initialValue;
|
741
|
+
for (var i = this._length-1; i > 0; i--)
|
742
|
+
value = callback(value, this.get(i), i, this);
|
743
|
+
return value;
|
744
|
+
};
|
745
|
+
|
746
|
+
// displace(begin, end, values/ByteStrings/ByteArrays/Arrays...) -> length
|
747
|
+
// begin/end are specified like for slice. Can be used like splice but does not return the removed elements.
|
748
|
+
ByteArray.prototype.displace = function(begin, end) {
|
749
|
+
throw "NYI";
|
750
|
+
};
|
751
|
+
|
752
|
+
// toSource() returns a string like "ByteArray([])" for a null byte-array.
|
753
|
+
ByteArray.prototype.toSource = function() {
|
754
|
+
return "ByteArray(["+this.toArray().join(",")+"])";
|
755
|
+
};
|