diff --git a/Core/Array.cpp b/Core/Array.cpp index 223ed463..6a8a7f94 100644 --- a/Core/Array.cpp +++ b/Core/Array.cpp @@ -12,20 +12,6 @@ /******************************** Array ********************************/ -// 数组最大容量。初始化时决定,后面不允许改变 -//int Array::Capacity() const { return _Capacity; } - -/*int MemLen(const void* data) -{ - if(!data) return 0; - - // 自动计算长度,\0结尾,单字节大小时才允许 - int len = 0; - const byte* p =(const byte*)data; - while(*p++) len++; - return len; -}*/ - Array::Array(void* data, int len) : Buffer(data, len) { Init(); @@ -45,16 +31,8 @@ Array::Array(const Buffer& rhs) : Buffer(nullptr, 0) Init(); } -/*Array::Array(const Array& rhs) : Buffer(nullptr, 0) -{ - Copy(0, rhs, 0, -1); - - Init(); -}*/ - Array::Array(Array&& rval) : Buffer(nullptr, 0) { - //*this = rval; move(rval); } @@ -128,7 +106,6 @@ Array& Array::operator = (const void* p) Array& Array::operator = (Array&& rval) { - //Buffer::operator=(rval); move(rval); return *this; diff --git a/Core/Array.h b/Core/Array.h index 2dcd1408..3a88c11e 100644 --- a/Core/Array.h +++ b/Core/Array.h @@ -12,9 +12,9 @@ public: Array(void* data, int len); Array(const void* data, int len); - explicit Array(const Buffer& rhs); Array(const Array& rhs) = delete; Array(Array&& rval); + explicit Array(const Buffer& rhs); virtual ~Array(); @@ -136,13 +136,6 @@ public: _Size = sizeof(T); } - // 重载等号运算符,使用另一个固定数组来初始化 - /*TArray& operator=(const TArray& arr) - { - Array::operator=(arr); - - return *this; - }*/ TArray& operator=(const TArray& arr) = delete; // 让父类的所有Set函数在这里可见 diff --git a/Core/Buffer.cpp b/Core/Buffer.cpp index d4c9f177..a562d594 100644 --- a/Core/Buffer.cpp +++ b/Core/Buffer.cpp @@ -2,10 +2,7 @@ #include "_Core.h" -#include "Type.h" #include "Buffer.h" -#include "Array.h" -#include "ByteArray.h" #include "SString.h" /******************************** Buffer ********************************/ @@ -39,7 +36,6 @@ Buffer& Buffer::operator = (const Buffer& rhs) { if(!SetLength(rhs.Length())) assert(false, "赋值操作无法扩容"); - //Copy(0, rhs, 0, -1); Copy(rhs, 0); return *this; diff --git a/Core/Buffer.h b/Core/Buffer.h index 145df0e3..0b8fe137 100644 --- a/Core/Buffer.h +++ b/Core/Buffer.h @@ -26,20 +26,7 @@ class Buffer : public Object public: // 打包一个指针和长度指定的数据区 Buffer(void* ptr, int len); - // 不能用简写的模板,否则String可能命中自己的构造函数 - /*template - Buffer(T (&arr)[N]) - { - _Arr = (char*)arr; - _Length = sizeof(arr); - } - template - Buffer(T (&obj)) - { - _Arr = (char*)&obj; - _Length = sizeof(obj); - }*/ - // 拷贝构造函数。直接把指针和长度拿过来用 + // 禁用拷贝构造函数 Buffer(const Buffer& buf) = delete; // 对象mov操作,指针和长度归我,清空对方 Buffer(Buffer&& rval); @@ -52,9 +39,9 @@ public: Buffer& operator = (Buffer&& rval); // 拿出指针供外部使用 - inline byte* GetBuffer() { return (byte*)_Arr; } - inline const byte* GetBuffer() const { return (byte*)_Arr; } - inline int Length() const { return _Length; } + inline byte* GetBuffer() { return (byte*)_Arr; } + inline const byte* GetBuffer() const { return (byte*)_Arr; } + inline int Length() const { return _Length; } // 设置数组长度。只能缩小不能扩大,子类可以扩展以实现自动扩容 virtual bool SetLength(int len); @@ -71,6 +58,7 @@ public: virtual int CopyTo(int srcIndex, void* dest, int len) const; // 拷贝数据,默认-1长度表示两者最小长度 virtual int Copy(int destIndex, const Buffer& src, int srcIndex, int len); + // 从另一个对象拷贝数据和长度,长度不足且扩容失败时报错 int Copy(const Buffer& src, int destIndex = 0); // 用指定字节设置初始化一个区域 diff --git a/Core/SString.h b/Core/SString.h index 271420ab..f8a6abda 100644 --- a/Core/SString.h +++ b/Core/SString.h @@ -2,9 +2,9 @@ #define __String_H__ #include "Array.h" +#include "ByteArray.h" // 字符串助手,主要用于字符串连接 -//class StringHelper; class StringSplit; // 字符串 @@ -14,7 +14,6 @@ public: String(const char* cstr = ""); String(const String& str); String(String&& rval); - //String(StringHelper&& rval); // 外部传入缓冲区供内部使用,注意长度减去零结束符 String(char* str, int length); // 包装静态字符串,直接使用,修改时扩容 @@ -52,7 +51,6 @@ public: String& operator = (const String& rhs); String& operator = (const char* cstr); String& operator = (String&& rval); - //String& operator = (StringHelper&& rval); // 连接内建类型。如果参数无效则认为连接失败 bool Concat(const Object& obj); @@ -83,13 +81,6 @@ public: String& operator += (float num) {Concat(num); return (*this);} String& operator += (double num) {Concat(num); return (*this);} - /*template - friend StringHelper& operator + (const StringHelper& lhs, T rhs) - { - auto& a = const_cast(lhs); - if (!a.Concat(rhs)) a.release(); - return a; - }*/ friend String& operator + (String& lhs, const Object& rhs); friend String& operator + (String& lhs, const String& rhs); friend String& operator + (String& lhs, const char* cstr); @@ -155,8 +146,6 @@ public: bool EndsWith(const String& str) const; bool EndsWith(const char* str) const; - //typedef void (*StringItem)(const String& item); - //int Split(const String& str, StringItem callback); StringSplit Split(const String& sep) const; String Substring(int start, int _Length) const; diff --git a/Core/String.cpp b/Core/String.cpp index 91eb0d13..3b1f1abc 100644 --- a/Core/String.cpp +++ b/Core/String.cpp @@ -24,11 +24,6 @@ String::String(const char* cstr) : Array(Arr, ArrayLength(Arr)) { init(); - /* - 其实这里可以不用拷贝,内部直接使用这个指针,等第一次修改的时候再拷贝,不过那样过于复杂了 - */ - //if (cstr) copy(cstr, strlen(cstr)); - _Length = strlen(cstr); if(_Length) { @@ -51,12 +46,6 @@ String::String(String&& rval) : Array(Arr, ArrayLength(Arr)) move(rval); } -/*String::String(StringHelper&& rval) : Array(Arr, ArrayLength(Arr)) -{ - init(); - move(rval); -}*/ - String::String(char c) : Array(Arr, ArrayLength(Arr)) { init(); @@ -309,12 +298,6 @@ String& String::operator = (String&& rval) return *this; } -/*String& String::operator = (StringHelper&& rval) -{ - if (this != &rval) move(rval); - return *this; -}*/ - String& String::operator = (const char* cstr) { if (cstr) copy(cstr, strlen(cstr)); @@ -862,33 +845,6 @@ bool String::EndsWith(const char* str) const return strncmp(&_Arr[_Length - slen], str, slen) == 0; } -/*int String::Split(const String& str, StringItem callback) -{ - if(str.Length() == 0) return 0; - - int n = 0; - int p = 0; - int e = 0; - while(p < _Length) - { - // 找到下一个位置。如果找不到,直接移到末尾 - e = IndexOf(str, p); - if(e < 0) e = _Length; - - n++; - - auto item = Substring(p, e - p); - callback(item); - - // 如果在末尾,说明没有找到 - if(e == _Length) break; - - p = e + str.Length(); - } - - return n; -}*/ - StringSplit String::Split(const String& sep) const { return StringSplit(*this, sep); @@ -959,6 +915,8 @@ String String::ToUpper() const return str; } +/******************************** 辅助 ********************************/ + extern char* itoa(int value, char *string, int radix) { return ltoa(value, string, radix) ; @@ -1068,6 +1026,8 @@ char *dtostrf (double val, char width, byte prec, char* sout) return sout; } +/******************************** StringSplit ********************************/ + StringSplit::StringSplit(const String& str, const String& sep) : _Str(str), _Sep(sep) @@ -1080,33 +1040,6 @@ StringSplit::StringSplit(const String& str, const String& sep) : if(p >= 0) _Length = p; } -/*int String::Split(const String& str, StringItem callback) -{ - if(str.Length() == 0) return 0; - - int n = 0; - int p = 0; - int e = 0; - while(p < _Length) - { - // 找到下一个位置。如果找不到,直接移到末尾 - e = IndexOf(str, p); - if(e < 0) e = _Length; - - n++; - - auto item = Substring(p, e - p); - callback(item); - - // 如果在末尾,说明没有找到 - if(e == _Length) break; - - p = e + str.Length(); - } - - return n; -}*/ - const String StringSplit::Next() { auto ptr = _Str.GetBuffer(); diff --git a/Core/Type.h b/Core/Type.h index d49be966..2e0f8c53 100644 --- a/Core/Type.h +++ b/Core/Type.h @@ -1,9 +1,6 @@ #ifndef __Type_H__ #define __Type_H__ -/*#include -#include */ - /* 类型定义 */ typedef char sbyte; typedef unsigned char byte; diff --git a/Core/_Core.h b/Core/_Core.h index 79bdb21c..821da50f 100644 --- a/Core/_Core.h +++ b/Core/_Core.h @@ -18,9 +18,6 @@ extern "C" #ifdef USE_FULL_ASSERT -// 验证确保对象不为空,并且在有效的内存范围内 -//extern void assert_failed(uint8_t* file, uint32_t line); - #define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed2("ptr==nullptr", (const char*)__FILE__, __LINE__)) bool assert_ptr_(const void* p);