Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/lib/buffer.js b/lib/buffer.js
- index ecb25b7..f4d03bb 100644
- --- a/lib/buffer.js
- +++ b/lib/buffer.js
- @@ -328,3 +328,18 @@ Buffer.prototype.slice = function (start, end) {
- return new Buffer(this.parent, end - start, +start + this.offset);
- };
- +
- +// indexOf(code|char, start, end)
- +Buffer.prototype.indexOf = function (char_code, start, end) {
- + start || (start = 0);
- + end || (end = this.length);
- +
- + if (this.length < end || end < start) {
- + throw new Error('Range is out of bounds');
- + }
- +
- + start += this.offset;
- + end += this.offset;
- +
- + return this.parent.indexOf(char_code, start, end) - this.offset;
- +};
- diff --git a/src/node_buffer.cc b/src/node_buffer.cc
- index 5c4e3c1..d5ad78a 100644
- --- a/src/node_buffer.cc
- +++ b/src/node_buffer.cc
- @@ -529,6 +529,42 @@ Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
- }
- +// var index = buffer.indexOf(0xFF, start, end);
- +Handle<Value> Buffer::IndexOf(const Arguments &args) {
- + HandleScope scope;
- +
- + char ascii_value;
- +
- + if (args[0]->IsString()) {
- + ascii_value = (*String::Utf8Value(args[0]->ToString()))[0];
- + } else if (args[0]->IsNumber()) {
- + ascii_value = args[0]->Uint32Value();
- + } else {
- + return ThrowException(Exception::TypeError(String::New(
- + "First argument must be a ascii character / code")));
- + }
- +
- + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
- +
- + size_t start = args[1]->IsNumber() ? args[1]->Uint32Value() : 0;
- + size_t length = args[2]->IsNumber() ? args[2]->Uint32Value() - start + 1 :
- + buffer->length_ - start;
- +
- + if (buffer->length_ < (length + start)) {
- + return ThrowException(Exception::TypeError(String::New(
- + "Range out of bounds")));
- + }
- +
- + char *index_ptr = (char*) memchr(buffer->data_ + start, ascii_value, length);
- +
- + if (NULL == index_ptr) {
- + return scope.Close(Integer::New(-1));
- + }
- +
- + return scope.Close(Integer::New(index_ptr - buffer->data_));
- +}
- +
- +
- // var nbytes = Buffer.byteLength("string", "utf8")
- Handle<Value> Buffer::ByteLength(const Arguments &args) {
- HandleScope scope;
- @@ -597,6 +633,7 @@ void Buffer::Initialize(Handle<Object> target) {
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "base64Write", Buffer::Base64Write);
- NODE_SET_PROTOTYPE_METHOD(constructor_template, "copy", Buffer::Copy);
- + NODE_SET_PROTOTYPE_METHOD(constructor_template, "indexOf", Buffer::IndexOf);
- NODE_SET_METHOD(constructor_template->GetFunction(),
- "byteLength",
- diff --git a/src/node_buffer.h b/src/node_buffer.h
- index d191eb6..35fd5c8 100644
- --- a/src/node_buffer.h
- +++ b/src/node_buffer.h
- @@ -72,6 +72,7 @@ class Buffer : public ObjectWrap {
- static v8::Handle<v8::Value> MakeFastBuffer(const v8::Arguments &args);
- static v8::Handle<v8::Value> Unpack(const v8::Arguments &args);
- static v8::Handle<v8::Value> Copy(const v8::Arguments &args);
- + static v8::Handle<v8::Value> IndexOf(const v8::Arguments &args);
- Buffer(size_t length);
- Buffer(Buffer *parent, size_t start, size_t end);
Add Comment
Please, Sign In to add comment