More AMTL conversion - 🔥 CString and CVector
This commit is contained in:
@ -24,130 +24,132 @@
|
||||
#include <stdlib.h>
|
||||
#include <am-string.h>
|
||||
|
||||
using namespace ke;
|
||||
namespace ke {
|
||||
|
||||
class AutoString
|
||||
{
|
||||
public:
|
||||
AutoString() : ptr_(NULL), length_(0)
|
||||
{
|
||||
}
|
||||
AutoString(AutoString &&other)
|
||||
: ptr_(other.ptr_),
|
||||
length_(other.length_)
|
||||
{
|
||||
other.ptr_ = nullptr;
|
||||
other.length_ = 0;
|
||||
}
|
||||
AutoString(const char *ptr)
|
||||
{
|
||||
assign(ptr);
|
||||
}
|
||||
AutoString(const AString &str)
|
||||
{
|
||||
assign(str.chars(), str.length());
|
||||
}
|
||||
AutoString(const char *ptr, size_t len)
|
||||
{
|
||||
assign(ptr, len);
|
||||
}
|
||||
AutoString(const AutoString &other)
|
||||
{
|
||||
assign(other.ptr(), other.length());
|
||||
}
|
||||
~AutoString()
|
||||
{
|
||||
free(ptr_);
|
||||
}
|
||||
public:
|
||||
AutoString() : ptr_(nullptr), length_(0)
|
||||
{
|
||||
}
|
||||
AutoString(AutoString &&other)
|
||||
: ptr_(other.ptr_),
|
||||
length_(other.length_)
|
||||
{
|
||||
other.ptr_ = nullptr;
|
||||
other.length_ = 0;
|
||||
}
|
||||
AutoString(const char *ptr)
|
||||
{
|
||||
assign(ptr);
|
||||
}
|
||||
AutoString(const AString &str)
|
||||
{
|
||||
assign(str.chars(), str.length());
|
||||
}
|
||||
AutoString(const char *ptr, size_t len)
|
||||
{
|
||||
assign(ptr, len);
|
||||
}
|
||||
AutoString(const AutoString &other)
|
||||
{
|
||||
assign(other.ptr(), other.length());
|
||||
}
|
||||
~AutoString()
|
||||
{
|
||||
free(ptr_);
|
||||
}
|
||||
|
||||
AutoString &operator =(const char *ptr) {
|
||||
free(ptr_);
|
||||
assign(ptr);
|
||||
return *this;
|
||||
}
|
||||
AutoString &operator =(const AutoString &other) {
|
||||
free(ptr_);
|
||||
assign(other.ptr(), other.length());
|
||||
return *this;
|
||||
}
|
||||
AutoString &operator =(AutoString &&other) {
|
||||
Swap(other.ptr_, ptr_);
|
||||
Swap(other.length_, length_);
|
||||
return *this;
|
||||
}
|
||||
AutoString &operator =(const char *ptr) {
|
||||
free(ptr_);
|
||||
assign(ptr);
|
||||
return *this;
|
||||
}
|
||||
AutoString &operator =(const AutoString &other) {
|
||||
free(ptr_);
|
||||
assign(other.ptr(), other.length());
|
||||
return *this;
|
||||
}
|
||||
AutoString &operator =(AutoString &&other) {
|
||||
Swap(other.ptr_, ptr_);
|
||||
Swap(other.length_, length_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
AutoString operator +(const AutoString &other) const {
|
||||
size_t len = length() + other.length();
|
||||
char *buf = (char *)malloc(len + 1);
|
||||
memcpy(buf, ptr(), length());
|
||||
memcpy(buf + length(), other.ptr(), other.length());
|
||||
buf[len] = '\0';
|
||||
AutoString operator +(const AutoString &other) const {
|
||||
size_t len = length() + other.length();
|
||||
char *buf = (char *)malloc(len + 1);
|
||||
memcpy(buf, ptr(), length());
|
||||
memcpy(buf + length(), other.ptr(), other.length());
|
||||
buf[len] = '\0';
|
||||
|
||||
AutoString r;
|
||||
r.ptr_ = buf;
|
||||
r.length_ = len;
|
||||
return r;
|
||||
}
|
||||
AutoString r;
|
||||
r.ptr_ = buf;
|
||||
r.length_ = len;
|
||||
return r;
|
||||
}
|
||||
|
||||
AutoString operator +(const char *other) const {
|
||||
size_t other_length = strlen(other);
|
||||
size_t len = length() + other_length;
|
||||
char *buf = (char *)malloc(len + 1);
|
||||
memcpy(buf, ptr(), length());
|
||||
memcpy(buf + length(), other, other_length);
|
||||
buf[len] = '\0';
|
||||
AutoString operator +(const char *other) const {
|
||||
size_t other_length = strlen(other);
|
||||
size_t len = length() + other_length;
|
||||
char *buf = (char *)malloc(len + 1);
|
||||
memcpy(buf, ptr(), length());
|
||||
memcpy(buf + length(), other, other_length);
|
||||
buf[len] = '\0';
|
||||
|
||||
AutoString r;
|
||||
r.ptr_ = buf;
|
||||
r.length_ = len;
|
||||
return r;
|
||||
}
|
||||
AutoString r;
|
||||
r.ptr_ = buf;
|
||||
r.length_ = len;
|
||||
return r;
|
||||
}
|
||||
|
||||
AutoString operator +(unsigned val) const {
|
||||
char buffer[24];
|
||||
_snprintf(buffer, sizeof(buffer), "%d", val);
|
||||
return *this + buffer;
|
||||
}
|
||||
AutoString operator +(unsigned val) const {
|
||||
char buffer[24];
|
||||
_snprintf(buffer, sizeof(buffer), "%d", val);
|
||||
return *this + buffer;
|
||||
}
|
||||
|
||||
size_t length() const {
|
||||
return length_;
|
||||
}
|
||||
size_t length() const {
|
||||
return length_;
|
||||
}
|
||||
|
||||
bool operator !() const {
|
||||
return !length_;
|
||||
}
|
||||
bool operator !() const {
|
||||
return !length_;
|
||||
}
|
||||
|
||||
const char *ptr() const {
|
||||
return ptr_ ? ptr_ : "";
|
||||
}
|
||||
operator const char *() const {
|
||||
return ptr();
|
||||
}
|
||||
const char *ptr() const {
|
||||
return ptr_ ? ptr_ : "";
|
||||
}
|
||||
operator const char *() const {
|
||||
return ptr();
|
||||
}
|
||||
|
||||
private:
|
||||
void assign(const char *ptr) {
|
||||
if (!ptr) {
|
||||
ptr_ = NULL;
|
||||
length_ = 0;
|
||||
return;
|
||||
}
|
||||
assign(ptr, strlen(ptr));
|
||||
private:
|
||||
void assign(const char *ptr) {
|
||||
if (!ptr) {
|
||||
ptr_ = nullptr;
|
||||
length_ = 0;
|
||||
return;
|
||||
}
|
||||
void assign(const char *ptr, size_t length) {
|
||||
if (!ptr) {
|
||||
ptr_ = NULL;
|
||||
length_ = 0;
|
||||
return;
|
||||
}
|
||||
length_ = length;
|
||||
ptr_ = (char *)malloc(length_ + 1);
|
||||
memcpy(ptr_, ptr, length_);
|
||||
ptr_[length_] = '\0';
|
||||
assign(ptr, strlen(ptr));
|
||||
}
|
||||
void assign(const char *ptr, size_t length) {
|
||||
if (!ptr) {
|
||||
ptr_ = nullptr;
|
||||
length_ = 0;
|
||||
return;
|
||||
}
|
||||
length_ = length;
|
||||
ptr_ = (char *)malloc(length_ + 1);
|
||||
memcpy(ptr_, ptr, length_);
|
||||
ptr_[length_] = '\0';
|
||||
}
|
||||
|
||||
private:
|
||||
char *ptr_;
|
||||
size_t length_;
|
||||
};
|
||||
|
||||
private:
|
||||
char *ptr_;
|
||||
size_t length_;
|
||||
};
|
||||
|
||||
#endif // _include_spcomp_auto_string_h_
|
||||
|
Reference in New Issue
Block a user