Skip to content

Commit cd5ca14

Browse files
committed
Buffer, feat: support Buffer.reverse.
1 parent 6e9f705 commit cd5ca14

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

fibjs/include/Buffer.h

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Buffer : public Buffer_base {
104104
virtual result_t writeDoubleBE(double value, int32_t offset, bool noAssert);
105105
virtual result_t slice(int32_t start, obj_ptr<Buffer_base>& retVal);
106106
virtual result_t slice(int32_t start, int32_t end, obj_ptr<Buffer_base>& retVal);
107+
virtual result_t reverse(obj_ptr<Buffer_base>& retVal);
107108
virtual result_t hex(exlib::string& retVal);
108109
virtual result_t base64(exlib::string& retVal);
109110
virtual result_t keys(v8::Local<v8::Object>& retVal);

fibjs/include/ifs/Buffer.h

+17
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class Buffer_base : public object_base {
107107
virtual result_t writeDoubleBE(double value, int32_t offset, bool noAssert) = 0;
108108
virtual result_t slice(int32_t start, obj_ptr<Buffer_base>& retVal) = 0;
109109
virtual result_t slice(int32_t start, int32_t end, obj_ptr<Buffer_base>& retVal) = 0;
110+
virtual result_t reverse(obj_ptr<Buffer_base>& retVal) = 0;
110111
virtual result_t hex(exlib::string& retVal) = 0;
111112
virtual result_t base64(exlib::string& retVal) = 0;
112113
virtual result_t keys(v8::Local<v8::Object>& retVal) = 0;
@@ -179,6 +180,7 @@ class Buffer_base : public object_base {
179180
static void s_writeDoubleLE(const v8::FunctionCallbackInfo<v8::Value>& args);
180181
static void s_writeDoubleBE(const v8::FunctionCallbackInfo<v8::Value>& args);
181182
static void s_slice(const v8::FunctionCallbackInfo<v8::Value>& args);
183+
static void s_reverse(const v8::FunctionCallbackInfo<v8::Value>& args);
182184
static void s_hex(const v8::FunctionCallbackInfo<v8::Value>& args);
183185
static void s_base64(const v8::FunctionCallbackInfo<v8::Value>& args);
184186
static void s_keys(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -249,6 +251,7 @@ inline ClassInfo& Buffer_base::class_info()
249251
{ "writeDoubleLE", s_writeDoubleLE, false },
250252
{ "writeDoubleBE", s_writeDoubleBE, false },
251253
{ "slice", s_slice, false },
254+
{ "reverse", s_reverse, false },
252255
{ "hex", s_hex, false },
253256
{ "base64", s_base64, false },
254257
{ "keys", s_keys, false },
@@ -1404,6 +1407,20 @@ inline void Buffer_base::s_slice(const v8::FunctionCallbackInfo<v8::Value>& args
14041407
METHOD_RETURN();
14051408
}
14061409

1410+
inline void Buffer_base::s_reverse(const v8::FunctionCallbackInfo<v8::Value>& args)
1411+
{
1412+
obj_ptr<Buffer_base> vr;
1413+
1414+
METHOD_INSTANCE(Buffer_base);
1415+
METHOD_ENTER();
1416+
1417+
METHOD_OVER(0, 0);
1418+
1419+
hr = pInst->reverse(vr);
1420+
1421+
METHOD_RETURN();
1422+
}
1423+
14071424
inline void Buffer_base::s_hex(const v8::FunctionCallbackInfo<v8::Value>& args)
14081425
{
14091426
exlib::string vr;

fibjs/src/global/Buffer.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,24 @@ result_t Buffer::slice(int32_t start, int32_t end, obj_ptr<Buffer_base>& retVal)
945945
return 0;
946946
}
947947

948+
result_t Buffer::reverse(obj_ptr<Buffer_base>& retVal)
949+
{
950+
exlib::string strBuf;
951+
toString(strBuf);
952+
953+
char* buf = strBuf.c_buffer();
954+
int32_t len = (int32_t)strBuf.length();
955+
for (int32_t i = 0; i < len / 2; i++) {
956+
char ch = buf[i];
957+
buf[i] = buf[len - i - 1];
958+
buf[len - i - 1] = ch;
959+
}
960+
961+
retVal = new Buffer(strBuf);
962+
963+
return 0;
964+
}
965+
948966
result_t Buffer::toString(exlib::string& retVal)
949967
{
950968
retVal = m_data;

idl/zh-cn/Buffer.idl

+5
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ interface Buffer : object
561561
*/
562562
Buffer slice(Integer start, Integer end);
563563

564+
/*! @brief 返回一个新缓存对象,包含当前对象数据的倒序
565+
@return 返回新的缓存对象
566+
*/
567+
Buffer reverse();
568+
564569
/*! @brief 使用 16 进制编码缓存对象内容
565570
@return 返回编码字符串
566571
*/

test/buffer_test.js

+42
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,48 @@ describe('Buffer', () => {
724724
assert.equal(buf.indexOf(new Buffer([0x00, 0x36])), 5);
725725

726726
});
727+
728+
var fixtures = [{
729+
"a": "ffff00",
730+
"expected": "00ffff"
731+
},
732+
{
733+
"a": "ffff",
734+
"expected": "ffff"
735+
},
736+
{
737+
"a": "0000",
738+
"expected": "0000"
739+
},
740+
{
741+
"a": "0000ff",
742+
"expected": "ff0000"
743+
},
744+
{
745+
"a": "000000",
746+
"expected": "000000"
747+
},
748+
{
749+
"a": "ffffff",
750+
"expected": "ffffff"
751+
},
752+
{
753+
"a": "00ffff00ff",
754+
"expected": "ff00ffff00"
755+
},
756+
{
757+
"a": "0000ff00ffff00ff",
758+
"expected": "ff00ffff00ff0000"
759+
}
760+
];
761+
762+
it('reverse', () => {
763+
fixtures.forEach((f) => {
764+
var a = new Buffer(f.a, 'hex');
765+
assert.equal(a.reverse().toString('hex'), f.expected);
766+
assert.equal(a.toString('hex'), f.a);
767+
})
768+
});
727769
});
728770

729771
repl && test.run(console.DEBUG);

0 commit comments

Comments
 (0)