Created
October 11, 2010 09:42
-
-
Save ThisIsMissEm/620277 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/buffer.js b/lib/buffer.js | |
index ecb25b7..b68e13b 100644 | |
--- a/lib/buffer.js | |
+++ b/lib/buffer.js | |
@@ -91,6 +91,9 @@ SlowBuffer.prototype.slice = function (start, end) { | |
return new Buffer(this, end - start, +start); | |
}; | |
+SlowBuffer.prototype.indexOf = function indexOf(pattern, offset, max_offset) { | |
+ return this.indexOf(pattern, offset, max_offset) | |
+}; | |
// Buffer | |
@@ -319,6 +322,10 @@ Buffer.prototype.copy = function copy (target, target_start, start, end) { | |
end + this.offset); | |
}; | |
+Buffer.prototype.indexOf = function indexOf(pattern, offset, max_offset) { | |
+ return this.parent.indexOf(pattern, offset, max_offset) | |
+}; | |
+ | |
// slice(start, end) | |
Buffer.prototype.slice = function (start, end) { | |
diff --git a/src/node_buffer.cc b/src/node_buffer.cc | |
index 5c4e3c1..f934022 100644 | |
--- a/src/node_buffer.cc | |
+++ b/src/node_buffer.cc | |
@@ -362,6 +362,33 @@ Handle<Value> Buffer::Copy(const Arguments &args) { | |
} | |
+Handle<Value> Buffer::IndexOf(const Arguments &args) { | |
+ HandleScope scope; | |
+ | |
+ Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This()); | |
+ | |
+ int pattern = args[0]->Int32Value(); | |
+ size_t offset = args[1]->IsUndefined() ? 0 : args[1]->Int32Value(); | |
+ size_t length = args[2]->IsUndefined() ? buffer->length_ : args[1]->Int32Value(); | |
+ | |
+ if (offset < 0 || offset > buffer->length_) { | |
+ return ThrowException(Exception::Error(String::New("offset out of bounds"))); | |
+ } | |
+ | |
+ if (length < offset || length > buffer->length_) { | |
+ return ThrowException(Exception::Error(String::New("length out of bounds"))); | |
+ } | |
+ | |
+ char *result_ptr = (char*) memchr(buffer->data_, pattern, buffer->length_); | |
+ | |
+ if (NULL != result_ptr) { | |
+ return Integer::New(result_ptr - buffer->data_); | |
+ } | |
+ | |
+ return Undefined(); | |
+} | |
+ | |
+ | |
// var charsWritten = buffer.utf8Write(string, offset, [maxLength]); | |
Handle<Value> Buffer::Utf8Write(const Arguments &args) { | |
HandleScope scope; | |
@@ -597,6 +624,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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment