Hola chic@s el otro dia en el grupo de skype de indetectables me di cuenta que un usuario estaba realizando su propia implementacion de la clase string de C++
[Enlace externo eliminado para invitados] asi que me sume a su idea y decidi hacer mi version la cual he de admitir no esta completa ni por mucho pero si nos da una idea de como implementar este tipo de estructuras.
Dentro de esta implementacion he intentado evitar usar funciones o clases tanto de C/C++ todo esta implementado desde cero a base de punteros char asi que por lo tanto es portable y no hago uso de ningun include innecesarios.
Header
Mostrar/Ocultar
#ifndef ZSTRING_H
#define ZSTRING_H
typedef unsigned long zUlong;
class ZString
{
private:
char* zPtrString;
zUlong zLength;
void zCopy(const char* source,char* destination);
void zCopyAlloc(const char* source);
zUlong zStrLen(const char* source) const;
public:
ZString();
ZString(const char* source);
ZString(const ZString& source);
ZString& operator=(const ZString& source)
{
if(this!=&source)
{
zCopyAlloc(source.to_Cstr());
}
return *this;
}
ZString& operator=(const char* source)
{
if(source!=nullptr)
{
zCopyAlloc(source);
}
return *this;
}
char& operator[] (zUlong pos)
{
return zPtrString[pos];
}
const char& operator[] (zUlong pos) const
{
return zPtrString[pos];
}
bool operator==(const ZString& str)const
{
return this->zCompare(str);
}
const char* to_Cstr() const;
zUlong zSize() const;
bool zIsEmpty() const;
void zClear();
bool zCompare(const char *str) const;
bool zCompare(const ZString& str) const;
~ZString();
};
#endif // ZSTRING_H
Implementacion
Mostrar/Ocultar
#include "zstring.h"
ZString::ZString()
{
zPtrString=nullptr;
zLength=0;
}
ZString::ZString(const char* source)
{
zPtrString=nullptr;
if(source!=nullptr)
{
zCopyAlloc(source);
}
}
ZString::ZString(const ZString& source)
{
zPtrString=nullptr;
zCopyAlloc(source.to_Cstr());
}
void ZString::zCopy(const char* source,char* destination)
{
int i=0;
for(i=0;source[i];i++)
{
destination[i]=source[i];
}
destination[i]='\0';
}
void ZString::zCopyAlloc(const char* source)
{
if(zPtrString!=nullptr)
delete[] zPtrString;
zUlong len=zStrLen(source);
zPtrString=new char[len+1];
zCopy(source,zPtrString);
zLength=len;
}
zUlong ZString::zStrLen(const char* source) const
{
zUlong counter=0;
char* stemp=(char*)source;
while(*stemp++)counter++;
return counter;
}
const char* ZString::to_Cstr() const
{
return zPtrString;
}
zUlong ZString::zSize() const
{
return zLength;
}
bool ZString::zIsEmpty() const
{
return zPtrString==nullptr || zLength==0;
}
void ZString::zClear()
{
zCopyAlloc("");
}
bool ZString::zCompare(const char* str) const
{
if(zSize() != zStrLen(str)) return false;
char* stemp1=(char*)str;
char* stemp2=(char*)zPtrString;
while(*stemp1 && *stemp2)
{
if(*stemp1!=*stemp2) return false;
stemp1++;
stemp2++;
}
return true;
}
bool ZString::zCompare(const ZString& str) const
{
if(zSize() != str.zSize()) return false;
char* stemp1=(char*)str.to_Cstr();
char* stemp2=(char*)zPtrString;
while(*stemp1 && *stemp2)
{
if(*stemp1!=*stemp2) return false;
stemp1++;
stemp2++;
}
return true;
}
ZString::~ZString()
{
if(zPtrString!=nullptr)
{
delete[] zPtrString;
}
}
Uso
Mostrar/Ocultar
#include <iostream>
#include "zstring.h"
using namespace std;
int main()
{
ZString msg="hello world";
cout<<msg.to_Cstr()<<endl;
for(unsigned int i=0;i<msg.zSize();i++)
{
cout<<"letter: "<<msg[i]<<endl;
}
ZString other;
other="other string";
if(msg==other)cout<<"it's equal"<<endl;
else cout<<"it's different"<<endl;
return 0;
}