代码整理

This commit is contained in:
nnhy 2016-05-18 02:55:28 +00:00
parent 6a545988e9
commit fc5f7fad4f
8 changed files with 11 additions and 141 deletions

View File

@ -12,20 +12,6 @@
/******************************** Array ********************************/ /******************************** 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) Array::Array(void* data, int len) : Buffer(data, len)
{ {
Init(); Init();
@ -45,16 +31,8 @@ Array::Array(const Buffer& rhs) : Buffer(nullptr, 0)
Init(); Init();
} }
/*Array::Array(const Array& rhs) : Buffer(nullptr, 0)
{
Copy(0, rhs, 0, -1);
Init();
}*/
Array::Array(Array&& rval) : Buffer(nullptr, 0) Array::Array(Array&& rval) : Buffer(nullptr, 0)
{ {
//*this = rval;
move(rval); move(rval);
} }
@ -128,7 +106,6 @@ Array& Array::operator = (const void* p)
Array& Array::operator = (Array&& rval) Array& Array::operator = (Array&& rval)
{ {
//Buffer::operator=(rval);
move(rval); move(rval);
return *this; return *this;

View File

@ -12,9 +12,9 @@ public:
Array(void* data, int len); Array(void* data, int len);
Array(const void* data, int len); Array(const void* data, int len);
explicit Array(const Buffer& rhs);
Array(const Array& rhs) = delete; Array(const Array& rhs) = delete;
Array(Array&& rval); Array(Array&& rval);
explicit Array(const Buffer& rhs);
virtual ~Array(); virtual ~Array();
@ -136,13 +136,6 @@ public:
_Size = sizeof(T); _Size = sizeof(T);
} }
// 重载等号运算符,使用另一个固定数组来初始化
/*TArray& operator=(const TArray& arr)
{
Array::operator=(arr);
return *this;
}*/
TArray& operator=(const TArray& arr) = delete; TArray& operator=(const TArray& arr) = delete;
// 让父类的所有Set函数在这里可见 // 让父类的所有Set函数在这里可见

View File

@ -2,10 +2,7 @@
#include "_Core.h" #include "_Core.h"
#include "Type.h"
#include "Buffer.h" #include "Buffer.h"
#include "Array.h"
#include "ByteArray.h"
#include "SString.h" #include "SString.h"
/******************************** Buffer ********************************/ /******************************** Buffer ********************************/
@ -39,7 +36,6 @@ Buffer& Buffer::operator = (const Buffer& rhs)
{ {
if(!SetLength(rhs.Length())) assert(false, "赋值操作无法扩容"); if(!SetLength(rhs.Length())) assert(false, "赋值操作无法扩容");
//Copy(0, rhs, 0, -1);
Copy(rhs, 0); Copy(rhs, 0);
return *this; return *this;

View File

@ -26,20 +26,7 @@ class Buffer : public Object
public: public:
// 打包一个指针和长度指定的数据区 // 打包一个指针和长度指定的数据区
Buffer(void* ptr, int len); Buffer(void* ptr, int len);
// 不能用简写的模板否则String可能命中自己的构造函数 // 禁用拷贝构造函数
/*template<typename T, int N>
Buffer(T (&arr)[N])
{
_Arr = (char*)arr;
_Length = sizeof(arr);
}
template<typename T>
Buffer(T (&obj))
{
_Arr = (char*)&obj;
_Length = sizeof(obj);
}*/
// 拷贝构造函数。直接把指针和长度拿过来用
Buffer(const Buffer& buf) = delete; Buffer(const Buffer& buf) = delete;
// 对象mov操作指针和长度归我清空对方 // 对象mov操作指针和长度归我清空对方
Buffer(Buffer&& rval); Buffer(Buffer&& rval);
@ -71,6 +58,7 @@ public:
virtual int CopyTo(int srcIndex, void* dest, int len) const; virtual int CopyTo(int srcIndex, void* dest, int len) const;
// 拷贝数据,默认-1长度表示两者最小长度 // 拷贝数据,默认-1长度表示两者最小长度
virtual int Copy(int destIndex, const Buffer& src, int srcIndex, int len); virtual int Copy(int destIndex, const Buffer& src, int srcIndex, int len);
// 从另一个对象拷贝数据和长度,长度不足且扩容失败时报错
int Copy(const Buffer& src, int destIndex = 0); int Copy(const Buffer& src, int destIndex = 0);
// 用指定字节设置初始化一个区域 // 用指定字节设置初始化一个区域

View File

@ -2,9 +2,9 @@
#define __String_H__ #define __String_H__
#include "Array.h" #include "Array.h"
#include "ByteArray.h"
// 字符串助手,主要用于字符串连接 // 字符串助手,主要用于字符串连接
//class StringHelper;
class StringSplit; class StringSplit;
// 字符串 // 字符串
@ -14,7 +14,6 @@ public:
String(const char* cstr = ""); String(const char* cstr = "");
String(const String& str); String(const String& str);
String(String&& rval); String(String&& rval);
//String(StringHelper&& rval);
// 外部传入缓冲区供内部使用,注意长度减去零结束符 // 外部传入缓冲区供内部使用,注意长度减去零结束符
String(char* str, int length); String(char* str, int length);
// 包装静态字符串,直接使用,修改时扩容 // 包装静态字符串,直接使用,修改时扩容
@ -52,7 +51,6 @@ public:
String& operator = (const String& rhs); String& operator = (const String& rhs);
String& operator = (const char* cstr); String& operator = (const char* cstr);
String& operator = (String&& rval); String& operator = (String&& rval);
//String& operator = (StringHelper&& rval);
// 连接内建类型。如果参数无效则认为连接失败 // 连接内建类型。如果参数无效则认为连接失败
bool Concat(const Object& obj); bool Concat(const Object& obj);
@ -83,13 +81,6 @@ public:
String& operator += (float num) {Concat(num); return (*this);} String& operator += (float num) {Concat(num); return (*this);}
String& operator += (double num) {Concat(num); return (*this);} String& operator += (double num) {Concat(num); return (*this);}
/*template<typename T>
friend StringHelper& operator + (const StringHelper& lhs, T rhs)
{
auto& a = const_cast<StringHelper&>(lhs);
if (!a.Concat(rhs)) a.release();
return a;
}*/
friend String& operator + (String& lhs, const Object& rhs); friend String& operator + (String& lhs, const Object& rhs);
friend String& operator + (String& lhs, const String& rhs); friend String& operator + (String& lhs, const String& rhs);
friend String& operator + (String& lhs, const char* cstr); friend String& operator + (String& lhs, const char* cstr);
@ -155,8 +146,6 @@ public:
bool EndsWith(const String& str) const; bool EndsWith(const String& str) const;
bool EndsWith(const char* 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; StringSplit Split(const String& sep) const;
String Substring(int start, int _Length) const; String Substring(int start, int _Length) const;

View File

@ -24,11 +24,6 @@ String::String(const char* cstr) : Array(Arr, ArrayLength(Arr))
{ {
init(); init();
/*
使
*/
//if (cstr) copy(cstr, strlen(cstr));
_Length = strlen(cstr); _Length = strlen(cstr);
if(_Length) if(_Length)
{ {
@ -51,12 +46,6 @@ String::String(String&& rval) : Array(Arr, ArrayLength(Arr))
move(rval); move(rval);
} }
/*String::String(StringHelper&& rval) : Array(Arr, ArrayLength(Arr))
{
init();
move(rval);
}*/
String::String(char c) : Array(Arr, ArrayLength(Arr)) String::String(char c) : Array(Arr, ArrayLength(Arr))
{ {
init(); init();
@ -309,12 +298,6 @@ String& String::operator = (String&& rval)
return *this; return *this;
} }
/*String& String::operator = (StringHelper&& rval)
{
if (this != &rval) move(rval);
return *this;
}*/
String& String::operator = (const char* cstr) String& String::operator = (const char* cstr)
{ {
if (cstr) copy(cstr, strlen(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; 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 StringSplit String::Split(const String& sep) const
{ {
return StringSplit(*this, sep); return StringSplit(*this, sep);
@ -959,6 +915,8 @@ String String::ToUpper() const
return str; return str;
} }
/******************************** 辅助 ********************************/
extern char* itoa(int value, char *string, int radix) extern char* itoa(int value, char *string, int radix)
{ {
return ltoa(value, string, radix) ; return ltoa(value, string, radix) ;
@ -1068,6 +1026,8 @@ char *dtostrf (double val, char width, byte prec, char* sout)
return sout; return sout;
} }
/******************************** StringSplit ********************************/
StringSplit::StringSplit(const String& str, const String& sep) : StringSplit::StringSplit(const String& str, const String& sep) :
_Str(str), _Str(str),
_Sep(sep) _Sep(sep)
@ -1080,33 +1040,6 @@ StringSplit::StringSplit(const String& str, const String& sep) :
if(p >= 0) _Length = p; 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() const String StringSplit::Next()
{ {
auto ptr = _Str.GetBuffer(); auto ptr = _Str.GetBuffer();

View File

@ -1,9 +1,6 @@
#ifndef __Type_H__ #ifndef __Type_H__
#define __Type_H__ #define __Type_H__
/*#include <stdio.h>
#include <stdlib.h>*/
/* 类型定义 */ /* 类型定义 */
typedef char sbyte; typedef char sbyte;
typedef unsigned char byte; typedef unsigned char byte;

View File

@ -18,9 +18,6 @@ extern "C"
#ifdef USE_FULL_ASSERT #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__)) #define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed2("ptr==nullptr", (const char*)__FILE__, __LINE__))
bool assert_ptr_(const void* p); bool assert_ptr_(const void* p);