Guest User

Untitled

a guest
Jun 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. diff --git a/lib/buffer.js b/lib/buffer.js
  2. index ecb25b7..f4d03bb 100644
  3. --- a/lib/buffer.js
  4. +++ b/lib/buffer.js
  5. @@ -328,3 +328,18 @@ Buffer.prototype.slice = function (start, end) {
  6.  
  7. return new Buffer(this.parent, end - start, +start + this.offset);
  8. };
  9. +
  10. +// indexOf(code|char, start, end)
  11. +Buffer.prototype.indexOf = function (char_code, start, end) {
  12. + start || (start = 0);
  13. + end || (end = this.length);
  14. +
  15. + if (this.length < end || end < start) {
  16. + throw new Error('Range is out of bounds');
  17. + }
  18. +
  19. + start += this.offset;
  20. + end += this.offset;
  21. +
  22. + return this.parent.indexOf(char_code, start, end) - this.offset;
  23. +};
  24. diff --git a/src/node_buffer.cc b/src/node_buffer.cc
  25. index 5c4e3c1..d5ad78a 100644
  26. --- a/src/node_buffer.cc
  27. +++ b/src/node_buffer.cc
  28. @@ -529,6 +529,42 @@ Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
  29. }
  30.  
  31.  
  32. +// var index = buffer.indexOf(0xFF, start, end);
  33. +Handle<Value> Buffer::IndexOf(const Arguments &args) {
  34. + HandleScope scope;
  35. +
  36. + char ascii_value;
  37. +
  38. + if (args[0]->IsString()) {
  39. + ascii_value = (*String::Utf8Value(args[0]->ToString()))[0];
  40. + } else if (args[0]->IsNumber()) {
  41. + ascii_value = args[0]->Uint32Value();
  42. + } else {
  43. + return ThrowException(Exception::TypeError(String::New(
  44. + "First argument must be a ascii character / code")));
  45. + }
  46. +
  47. + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
  48. +
  49. + size_t start = args[1]->IsNumber() ? args[1]->Uint32Value() : 0;
  50. + size_t length = args[2]->IsNumber() ? args[2]->Uint32Value() - start + 1 :
  51. + buffer->length_ - start;
  52. +
  53. + if (buffer->length_ < (length + start)) {
  54. + return ThrowException(Exception::TypeError(String::New(
  55. + "Range out of bounds")));
  56. + }
  57. +
  58. + char *index_ptr = (char*) memchr(buffer->data_ + start, ascii_value, length);
  59. +
  60. + if (NULL == index_ptr) {
  61. + return scope.Close(Integer::New(-1));
  62. + }
  63. +
  64. + return scope.Close(Integer::New(index_ptr - buffer->data_));
  65. +}
  66. +
  67. +
  68. // var nbytes = Buffer.byteLength("string", "utf8")
  69. Handle<Value> Buffer::ByteLength(const Arguments &args) {
  70. HandleScope scope;
  71. @@ -597,6 +633,7 @@ void Buffer::Initialize(Handle<Object> target) {
  72. NODE_SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite);
  73. NODE_SET_PROTOTYPE_METHOD(constructor_template, "base64Write", Buffer::Base64Write);
  74. NODE_SET_PROTOTYPE_METHOD(constructor_template, "copy", Buffer::Copy);
  75. + NODE_SET_PROTOTYPE_METHOD(constructor_template, "indexOf", Buffer::IndexOf);
  76.  
  77. NODE_SET_METHOD(constructor_template->GetFunction(),
  78. "byteLength",
  79. diff --git a/src/node_buffer.h b/src/node_buffer.h
  80. index d191eb6..35fd5c8 100644
  81. --- a/src/node_buffer.h
  82. +++ b/src/node_buffer.h
  83. @@ -72,6 +72,7 @@ class Buffer : public ObjectWrap {
  84. static v8::Handle<v8::Value> MakeFastBuffer(const v8::Arguments &args);
  85. static v8::Handle<v8::Value> Unpack(const v8::Arguments &args);
  86. static v8::Handle<v8::Value> Copy(const v8::Arguments &args);
  87. + static v8::Handle<v8::Value> IndexOf(const v8::Arguments &args);
  88.  
  89. Buffer(size_t length);
  90. Buffer(Buffer *parent, size_t start, size_t end);
Add Comment
Please, Sign In to add comment