-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_utils.cc
More file actions
56 lines (44 loc) · 1.3 KB
/
buffer_utils.cc
File metadata and controls
56 lines (44 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
using namespace v8;
using namespace node;
Handle<Value> IndexOf(const Arguments &args) {
HandleScope scope;
if (args.Length() < 3) {
ThrowException(Exception::TypeError(
String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!Buffer::HasInstance(args[0])) {
ThrowException(Exception::TypeError(
String::New("Argument 1 must be a Buffer")));
return scope.Close(Undefined());
}
if (!args[1]->IsNumber()) {
ThrowException(Exception::TypeError(
String::New("Argument 2 must be a number")));
return scope.Close(Undefined());
}
if (!args[2]->IsNumber()) {
ThrowException(Exception::TypeError(
String::New("Argument 3 must be a number")));
return scope.Close(Undefined());
}
Local<Object> bufferObj = args[0]->ToObject();
char *data = Buffer::Data(bufferObj);
size_t length = Buffer::Length(bufferObj);
int64_t needle = args[1]->IntegerValue();
int64_t start = args[2]->IntegerValue();
for (size_t i=start; i < length; i++) {
if (data[i] == needle) {
return scope.Close(Number::New(i));
}
}
return scope.Close(False());
}
void init(Handle<Object> target) {
target->Set(String::NewSymbol("indexOf"),
FunctionTemplate::New(IndexOf)->GetFunction());
}
NODE_MODULE(buffer_utils, init)