Skip to content

Commit

Permalink
Buffer, feat: Buffer.from support byteOffset and length.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Aug 13, 2017
1 parent cd5ca14 commit 4240334
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 60 deletions.
34 changes: 12 additions & 22 deletions fibjs/include/ifs/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ class Buffer_base : public object_base {
static result_t _new(exlib::string str, exlib::string codec, obj_ptr<Buffer_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
static result_t _new(int32_t size, obj_ptr<Buffer_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
static result_t isBuffer(v8::Local<v8::Value> v, bool& retVal);
static result_t from(v8::Local<v8::Array> datas, obj_ptr<Buffer_base>& retVal);
static result_t from(v8::Local<v8::ArrayBuffer> datas, obj_ptr<Buffer_base>& retVal);
static result_t from(v8::Local<v8::TypedArray> datas, obj_ptr<Buffer_base>& retVal);
static result_t from(Buffer_base* buffer, obj_ptr<Buffer_base>& retVal);
static result_t from(Buffer_base* buffer, int32_t byteOffset, int32_t length, obj_ptr<Buffer_base>& retVal);
static result_t from(exlib::string str, int32_t byteOffset, int32_t length, obj_ptr<Buffer_base>& retVal);
static result_t from(exlib::string str, exlib::string codec, obj_ptr<Buffer_base>& retVal);
static result_t concat(v8::Local<v8::Array> buflist, int32_t cutLength, obj_ptr<Buffer_base>& retVal);
static result_t alloc(int32_t size, int32_t fill, exlib::string codec, obj_ptr<Buffer_base>& retVal);
Expand Down Expand Up @@ -352,29 +350,21 @@ inline void Buffer_base::s_from(const v8::FunctionCallbackInfo<v8::Value>& args)

METHOD_ENTER();

METHOD_OVER(1, 1);

ARG(v8::Local<v8::Array>, 0);

hr = from(v0, vr);

METHOD_OVER(1, 1);

ARG(v8::Local<v8::ArrayBuffer>, 0);

hr = from(v0, vr);

METHOD_OVER(1, 1);
METHOD_OVER(3, 1);

ARG(v8::Local<v8::TypedArray>, 0);
ARG(obj_ptr<Buffer_base>, 0);
OPT_ARG(int32_t, 1, 0);
OPT_ARG(int32_t, 2, -1);

hr = from(v0, vr);
hr = from(v0, v1, v2, vr);

METHOD_OVER(1, 1);
METHOD_OVER(3, 1);

ARG(obj_ptr<Buffer_base>, 0);
ARG(exlib::string, 0);
OPT_ARG(int32_t, 1, 0);
OPT_ARG(int32_t, 2, -1);

hr = from(v0, vr);
hr = from(v0, v1, v2, vr);

METHOD_OVER(2, 1);

Expand Down
34 changes: 14 additions & 20 deletions fibjs/src/global/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,28 @@ result_t Buffer_base::isBuffer(v8::Local<v8::Value> v, bool& retVal)
return 0;
}

result_t Buffer_base::from(v8::Local<v8::Array> datas,
result_t Buffer_base::from(Buffer_base* buffer, int32_t byteOffset, int32_t length,
obj_ptr<Buffer_base>& retVal)
{
retVal = new Buffer();
retVal->append(datas);
return 0;
exlib::string str;
buffer->toString(str);
return from(str, byteOffset, length, retVal);
}

result_t Buffer_base::from(v8::Local<v8::ArrayBuffer> datas,
result_t Buffer_base::from(exlib::string str, int32_t byteOffset, int32_t length,
obj_ptr<Buffer_base>& retVal)
{
retVal = new Buffer();
retVal->append(datas);
return 0;
}
if (byteOffset < 0)
return CHECK_ERROR(CALL_E_INVALIDARG);

result_t Buffer_base::from(v8::Local<v8::TypedArray> datas,
obj_ptr<Buffer_base>& retVal)
{
retVal = new Buffer();
return retVal->append(datas);
}
if (byteOffset > str.length())
byteOffset = str.length();

result_t Buffer_base::from(Buffer_base* buffer,
obj_ptr<Buffer_base>& retVal)
{
retVal = new Buffer();
return retVal->append(buffer);
if (length < 0)
length = str.length() - byteOffset;

retVal = new Buffer(str.substr(byteOffset, length));
return 0;
}

result_t Buffer_base::from(exlib::string str, exlib::string codec,
Expand Down
28 changes: 10 additions & 18 deletions idl/zh-cn/Buffer.idl
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,21 @@ interface Buffer : object
*/
static Boolean isBuffer(Value v);

/*! @brief 通过数组创建 Buffer 对象
@param datas 给定数组类型变量用于创建 Buffer 对象
@return 返回 Buffer 实例
*/
static Buffer from(Array datas);

/*! @brief 通过 ArrayBuffer 创建 Buffer 对象
@param datas 给定 ArrayBuffer 类型变量用于创建 Buffer 对象
@return 返回 Buffer 实例
*/
static Buffer from(ArrayBuffer datas);

/*! @brief 通过 TypedArray 创建 Buffer 对象
@param datas 给定 TypedArray 类型变量用于创建 Buffer 对象
/*! @brief 通过其他 Buffer 创建 Buffer 对象
@param buffer 给定 Buffer 类型变量用于创建 Buffer 对象
@param byteOffset 指定数据起始位置,起始为 0
@param length 指定数据长度,起始位 -1,表示剩余所有数据
@return 返回 Buffer 实例
*/
static Buffer from(TypedArray datas);
static Buffer from(Buffer buffer, Integer byteOffset = 0, Integer length = -1);

/*! @brief 通过其他 Buffer 创建 Buffer 对象
@param buffer 给定 Buffer 类型变量用于创建 Buffer 对象
/*! @brief 通过字符串创建 Buffer 对象
@param str 初始化字符串,字符串将以 utf-8 格式写入
@param byteOffset 指定数据起始位置,起始为 0
@param length 指定数据长度,起始位 -1,表示剩余所有数据
@return 返回 Buffer 实例
*/
static Buffer from(Buffer buffer);
static Buffer from(String str, Integer byteOffset = 0, Integer length = -1);

/*! @brief 通过字符串创建 Buffer 对象
@param str 初始化字符串,字符串将以 utf-8 格式写入,缺省则创建一个空对象
Expand Down
13 changes: 13 additions & 0 deletions test/buffer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ describe('Buffer', () => {
assert.equal(buf.toString(), "abcd");
});

it('Buffer.from(Buffer, offset, len)', () => {
var buf = Buffer.from(new Buffer("abcd"), 1, 2);
assert.equal(buf.length, 2);
assert.equal(buf.toString(), "bc");

var buf = Buffer.from(new Buffer("abcd"), 1);
assert.equal(buf.length, 3);
assert.equal(buf.toString(), "bcd");

var buf = Buffer.from(new Buffer("abcd"), 10);
assert.equal(buf.length, 0);
});

it('Buffer.byteLength(String)', () => {
var str1 = "\u00bd + \u00bc = \u00be";
var str2 = "0xffffff";
Expand Down

0 comments on commit 4240334

Please sign in to comment.