HandleScope scope(node_isolate);
ARGS_THIS(args.This())
- int value;
+ SLICE_START_END(args[1], args[2], obj_length)
- if (args[0]->IsString()) {
- String::AsciiValue at(args[0]);
- value = (*at)[0];
- } else {
- value = static_cast<char>(args[0]->Int32Value());
+ if (args[0]->IsNumber()) {
+ int value = args[0]->Uint32Value() & 255;
+ memset(obj_data + start, value, length);
+ return args.This();
}
- SLICE_START_END(args[1], args[2], obj_length)
+ String::Utf8Value at(args[0]);
+ size_t at_length = at.length();
+
+ // optimize single ascii character case
+ if (at_length == 1) {
+ int value = static_cast<int>((*at)[0]);
+ memset(obj_data + start, value, length);
+ return args.This();
+ }
+
+ size_t in_there = at_length;
+ char* ptr = obj_data + start + at_length;
- memset(obj_data + start, value, length);
+ memcpy(obj_data + start, *at, MIN(at_length, length));
+
+ if (at_length >= length)
+ return args.This();
+
+ while (in_there < length - in_there) {
+ memcpy(ptr, obj_data + start, in_there);
+ ptr += in_there;
+ in_there *= 2;
+ }
+
+ if (in_there < length) {
+ memcpy(ptr, obj_data + start, length - in_there);
+ in_there = length;
+ }
return args.This();
}
assert.strictEqual(cntr, b[i]);
}
+// copy string longer than buffer length (failure will segfault)
+var bb = new Buffer(10);
+bb.fill('hello crazy world');
+
var caught_error = null;
for (; i < 32; i++) assert.equal(1, b[i]);
for (; i < b.length; i++) assert.equal(0, b[i]);
+var buf = new Buffer(10);
+buf.fill('abc');
+assert.equal(buf.toString(), 'abcabcabca');
+buf.fill('է');
+assert.equal(buf.toString(), 'էէէէէ');
+
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
var b = new Buffer(10);
b.write('あいうえお', encoding);