彻底解除对string.h的依赖,针对清零和拷贝,重写快速实现方法
This commit is contained in:
parent
88ba6fee12
commit
fe839afa6e
|
@ -1,8 +1,4 @@
|
||||||
//#include <stdio.h>
|
//#include <string.h>
|
||||||
//#include <stdlib.h>
|
|
||||||
//#include <math.h>
|
|
||||||
//#include <stdarg.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "_Core.h"
|
#include "_Core.h"
|
||||||
|
|
||||||
|
@ -185,16 +181,19 @@ bool Array::SetItem(const void* data, int index, int count)
|
||||||
int len2 = index + count;
|
int len2 = index + count;
|
||||||
CheckCapacity(len2, index);
|
CheckCapacity(len2, index);
|
||||||
|
|
||||||
byte* buf = (byte*)GetBuffer();
|
//byte* buf = (byte*)GetBuffer();
|
||||||
// 如果元素类型大小为1,那么可以直接调用内存设置函数
|
// 如果元素类型大小为1,那么可以直接调用内存设置函数
|
||||||
if(_Size == 1)
|
if(_Size == 1)
|
||||||
memset(&buf[index], *(byte*)data, count);
|
//memset(&buf[index], *(byte*)data, count);
|
||||||
|
Set(*(byte*)data, index, count);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while(count-- > 0)
|
while(count-- > 0)
|
||||||
{
|
{
|
||||||
memcpy(buf, data, _Size);
|
//memcpy(buf, data, _Size);
|
||||||
buf += _Size;
|
//buf += _Size;
|
||||||
|
Copy(index, data, _Size);
|
||||||
|
index += _Size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,7 +234,8 @@ void Array::Clear()
|
||||||
assert(_canWrite, "禁止Clear修改");
|
assert(_canWrite, "禁止Clear修改");
|
||||||
assert(_Arr, "Clear数据不能为空指针");
|
assert(_Arr, "Clear数据不能为空指针");
|
||||||
|
|
||||||
memset(_Arr, 0, _Size * _Length);
|
//memset(_Arr, 0, _Size * _Length);
|
||||||
|
Set(0, 0, _Size * _Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置指定位置的值,不足时自动扩容
|
// 设置指定位置的值,不足时自动扩容
|
||||||
|
@ -248,7 +248,8 @@ void Array::SetItemAt(int i, const void* item)
|
||||||
|
|
||||||
if(i >= _Length) _Length = i + 1;
|
if(i >= _Length) _Length = i + 1;
|
||||||
|
|
||||||
memcpy((byte*)_Arr + _Size * i, item, _Size);
|
//memcpy((byte*)_Arr + _Size * i, item, _Size);
|
||||||
|
Copy(_Size * i, item, _Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重载索引运算符[],返回指定元素的第一个字节
|
// 重载索引运算符[],返回指定元素的第一个字节
|
||||||
|
@ -321,12 +322,14 @@ bool operator==(const Array& bs1, const Array& bs2)
|
||||||
{
|
{
|
||||||
if(bs1.Length() != bs2.Length()) return false;
|
if(bs1.Length() != bs2.Length()) return false;
|
||||||
|
|
||||||
return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) == 0;
|
return bs1.CompareTo(bs2) == 0;
|
||||||
|
//return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const Array& bs1, const Array& bs2)
|
bool operator!=(const Array& bs1, const Array& bs2)
|
||||||
{
|
{
|
||||||
if(bs1.Length() != bs2.Length()) return true;
|
if(bs1.Length() != bs2.Length()) return true;
|
||||||
|
|
||||||
return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) != 0;
|
return bs1.CompareTo(bs2) != 0;
|
||||||
|
//return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) != 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
#include <string.h>
|
//#include <string.h>
|
||||||
|
|
||||||
#include "_Core.h"
|
#include "_Core.h"
|
||||||
|
|
||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
#include "SString.h"
|
#include "SString.h"
|
||||||
|
|
||||||
|
static void memset(byte* ptr, byte item, uint len);
|
||||||
|
static void memcpy(byte* dst, const byte* src, uint len);
|
||||||
|
|
||||||
/******************************** Buffer ********************************/
|
/******************************** Buffer ********************************/
|
||||||
|
|
||||||
Buffer::Buffer(void* ptr, int len)
|
Buffer::Buffer(void* ptr, int len)
|
||||||
|
@ -134,7 +137,7 @@ int Buffer::Copy(int destIndex, const void* src, int len)
|
||||||
if(_Arr == src) return len;
|
if(_Arr == src) return len;
|
||||||
|
|
||||||
// 拷贝数据
|
// 拷贝数据
|
||||||
if(len) memmove((byte*)_Arr + destIndex, src, len);
|
if(len) memcpy((byte*)_Arr + destIndex, (byte*)src, len);
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
@ -172,7 +175,7 @@ int Buffer::CopyTo(int srcIndex, void* data, int len) const
|
||||||
if(len)
|
if(len)
|
||||||
{
|
{
|
||||||
if(data != (byte*)_Arr + srcIndex)
|
if(data != (byte*)_Arr + srcIndex)
|
||||||
memcpy(data, (byte*)_Arr + srcIndex, len);
|
memcpy((byte*)data, (byte*)_Arr + srcIndex, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
|
@ -337,13 +340,40 @@ String Buffer::AsString() const
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Buffer::CompareTo(const Buffer& bs) const
|
||||||
|
{
|
||||||
|
return CompareTo(bs._Arr, bs._Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Buffer::CompareTo(const void* ptr, int len) const
|
||||||
|
{
|
||||||
|
if(len < 0) len = 0xFFFF;
|
||||||
|
|
||||||
|
// 同一个指针,长度决定大小
|
||||||
|
if(_Arr == ptr) return _Length - len;
|
||||||
|
|
||||||
|
// 以最短长度来比较
|
||||||
|
int count = _Length;
|
||||||
|
if(count > len) count = len;
|
||||||
|
|
||||||
|
// 遍历每一个字符
|
||||||
|
for(cstring p1=_Arr, p2=(cstring)ptr; count>0; count--, p1++, p2++)
|
||||||
|
{
|
||||||
|
int n = *p1 - *p2;
|
||||||
|
if(n) return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断剩余长度,以此决定大小
|
||||||
|
return _Length - len;
|
||||||
|
}
|
||||||
|
|
||||||
bool operator == (const Buffer& bs1, const Buffer& bs2)
|
bool operator == (const Buffer& bs1, const Buffer& bs2)
|
||||||
{
|
{
|
||||||
if(bs1._Arr == bs2._Arr) return true;
|
if(bs1._Arr == bs2._Arr) return true;
|
||||||
if(!bs1._Arr || !bs2._Arr) return false;
|
if(!bs1._Arr || !bs2._Arr) return false;
|
||||||
if(bs1.Length() != bs2.Length()) return false;
|
if(bs1.Length() != bs2.Length()) return false;
|
||||||
|
|
||||||
return memcmp(bs1._Arr, bs2._Arr, bs1.Length()) == 0;
|
return bs1.CompareTo(bs2) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator == (const Buffer& bs1, const void* ptr)
|
bool operator == (const Buffer& bs1, const void* ptr)
|
||||||
|
@ -351,7 +381,7 @@ bool operator == (const Buffer& bs1, const void* ptr)
|
||||||
if(bs1._Arr == ptr) return true;
|
if(bs1._Arr == ptr) return true;
|
||||||
if(!bs1._Arr || !ptr) return false;
|
if(!bs1._Arr || !ptr) return false;
|
||||||
|
|
||||||
return memcmp(bs1._Arr, ptr, bs1.Length()) == 0;
|
return bs1.CompareTo(ptr) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator != (const Buffer& bs1, const Buffer& bs2)
|
bool operator != (const Buffer& bs1, const Buffer& bs2)
|
||||||
|
@ -360,7 +390,7 @@ bool operator != (const Buffer& bs1, const Buffer& bs2)
|
||||||
if(!bs1._Arr || !bs2._Arr) return true;
|
if(!bs1._Arr || !bs2._Arr) return true;
|
||||||
if(bs1.Length() != bs2.Length()) return true;
|
if(bs1.Length() != bs2.Length()) return true;
|
||||||
|
|
||||||
return memcmp(bs1._Arr, bs2._Arr, bs1.Length()) != 0;
|
return bs1.CompareTo(bs2) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator != (const Buffer& bs1, const void* ptr)
|
bool operator != (const Buffer& bs1, const void* ptr)
|
||||||
|
@ -368,5 +398,60 @@ bool operator != (const Buffer& bs1, const void* ptr)
|
||||||
if(bs1._Arr == ptr) return false;
|
if(bs1._Arr == ptr) return false;
|
||||||
if(!bs1._Arr || !ptr) return true;
|
if(!bs1._Arr || !ptr) return true;
|
||||||
|
|
||||||
return memcmp(bs1._Arr, ptr, bs1.Length()) != 0;
|
return bs1.CompareTo(ptr) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void memset(byte* ptr, byte item, uint len)
|
||||||
|
{
|
||||||
|
// 为了加快速度,分头中尾三部分
|
||||||
|
byte* p = ptr;
|
||||||
|
|
||||||
|
// 为了让中间部分凑够4字节对齐
|
||||||
|
int n = (uint)p & 0x03;
|
||||||
|
for(int i=0; i<n && len>0; i++, len--)
|
||||||
|
*p++ = item;
|
||||||
|
|
||||||
|
// 中间部分,4字节对齐
|
||||||
|
if(len > 4)
|
||||||
|
{
|
||||||
|
int v = (item << 24) | (item << 16) | (item << 8) | item;
|
||||||
|
int* pi = (int*)p;
|
||||||
|
for(; len>0; len-=4)
|
||||||
|
*pi++ = v;
|
||||||
|
p = (byte*)pi;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结尾部分
|
||||||
|
while(len--)
|
||||||
|
*p++ = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
void memcpy(byte* dst, const byte* src, uint len)
|
||||||
|
{
|
||||||
|
// 为了加快速度,分头中尾三部分
|
||||||
|
|
||||||
|
// 如果两个不能同时对齐,那么无法使用快速拷贝
|
||||||
|
int nd = (uint)dst & 0x03;
|
||||||
|
int ns = (uint)src & 0x03;
|
||||||
|
if(nd == ns)
|
||||||
|
{
|
||||||
|
// 为了让中间部分凑够4字节对齐
|
||||||
|
for(int i=0; i<nd && len>0; i++, len--)
|
||||||
|
*dst++ = *src++;
|
||||||
|
|
||||||
|
// 中间部分,4字节对齐
|
||||||
|
if(len > 4)
|
||||||
|
{
|
||||||
|
int* pd = (int*)dst;
|
||||||
|
int* ps = (int*)src;
|
||||||
|
for(; len>0; len-=4)
|
||||||
|
*pd++ = *ps++;
|
||||||
|
dst = (byte*)pd;
|
||||||
|
src = (byte*)ps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结尾部分
|
||||||
|
while(len--)
|
||||||
|
*dst++ = *src++;
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,6 +91,8 @@ public:
|
||||||
|
|
||||||
explicit operator bool() const { return _Length > 0; }
|
explicit operator bool() const { return _Length > 0; }
|
||||||
bool operator !() const { return _Length == 0; }
|
bool operator !() const { return _Length == 0; }
|
||||||
|
int CompareTo(const Buffer& bs) const;
|
||||||
|
int CompareTo(const void* ptr, int len = -1) const;
|
||||||
friend bool operator == (const Buffer& bs1, const Buffer& bs2);
|
friend bool operator == (const Buffer& bs1, const Buffer& bs2);
|
||||||
friend bool operator == (const Buffer& bs1, const void* ptr);
|
friend bool operator == (const Buffer& bs1, const void* ptr);
|
||||||
friend bool operator != (const Buffer& bs1, const Buffer& bs2);
|
friend bool operator != (const Buffer& bs1, const Buffer& bs2);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "BinaryPair.h"
|
#include "BinaryPair.h"
|
||||||
|
|
||||||
#include <string.h>
|
//#include <string.h>
|
||||||
|
|
||||||
// 初始化消息,各字段为0
|
// 初始化消息,各字段为0
|
||||||
/*BinaryPair::BinaryPair(Buffer& bs)
|
/*BinaryPair::BinaryPair(Buffer& bs)
|
||||||
|
@ -34,7 +34,8 @@ Buffer BinaryPair::Get(cstring name) const
|
||||||
// 从当前位置开始向后找,如果找不到,再从头开始找到当前位置。
|
// 从当前位置开始向后找,如果找不到,再从头开始找到当前位置。
|
||||||
// 这样子安排,如果是顺序读取,将会提升性能
|
// 这样子安排,如果是顺序读取,将会提升性能
|
||||||
|
|
||||||
int slen = strlen(name);
|
//int slen = strlen(name);
|
||||||
|
String sn = name;
|
||||||
|
|
||||||
auto& ms = *_s;
|
auto& ms = *_s;
|
||||||
uint p = ms.Position();
|
uint p = ms.Position();
|
||||||
|
@ -53,7 +54,7 @@ Buffer BinaryPair::Get(cstring name) const
|
||||||
if(ln2 <0 || ln2 > ms.Remain()) return err;
|
if(ln2 <0 || ln2 > ms.Remain()) return err;
|
||||||
auto dt = ms.ReadBytes(ln2);
|
auto dt = ms.ReadBytes(ln2);
|
||||||
|
|
||||||
if(len == slen && strncmp(name, (cstring)nm, len) == 0) return Buffer(dt, ln2);
|
if(sn == String((cstring)nm, len)) return Buffer(dt, ln2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从头开始再来一次
|
// 从头开始再来一次
|
||||||
|
|
2
Sys.h
2
Sys.h
|
@ -4,8 +4,6 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
/*#include <stdlib.h>
|
|
||||||
#include <string.h>*/
|
|
||||||
|
|
||||||
#include "Core\Type.h"
|
#include "Core\Type.h"
|
||||||
#include "Core\Buffer.h"
|
#include "Core\Buffer.h"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "Sys.h"
|
#include "Sys.h"
|
||||||
|
|
||||||
#include <string.h>
|
//#include <string.h>
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
static void TestAssign()
|
static void TestAssign()
|
||||||
|
@ -97,8 +97,8 @@ void Buffer::Test()
|
||||||
|
|
||||||
// 拷贝数据,默认-1长度表示当前长度
|
// 拷贝数据,默认-1长度表示当前长度
|
||||||
char abc[] = "abcd";
|
char abc[] = "abcd";
|
||||||
bs.Copy(5, abc, strlen(abc));
|
bs.Copy(5, abc, sizeof(abc));
|
||||||
debug_printf("Copy(5, \"abcd\", %d) => %s\r\n", strlen(abc), cs);
|
debug_printf("Copy(5, \"abcd\", %d) => %s\r\n", sizeof(abc), cs);
|
||||||
assert(cs[5] == 'a' && cs[8] == 'd', "int Copy(int destIndex, const void* src, int len)");
|
assert(cs[5] == 'a' && cs[8] == 'd', "int Copy(int destIndex, const void* src, int len)");
|
||||||
|
|
||||||
// 把数据复制到目标缓冲区,默认-1长度表示当前长度
|
// 把数据复制到目标缓冲区,默认-1长度表示当前长度
|
||||||
|
|
Loading…
Reference in New Issue