noVaLue Posted May 23, 2012 Report Posted May 23, 2012 (edited) Desi mi-as fi dorit sa o termin, nu cred ca va fi cu putinta, caci multe planuri mai am in cap, si nu le mai dau de sfarsit. Inceputul e acolo, daca cineva vrea sa continue nu are decat.File String.h#define _CRT_SECURE_NO_WARNINGS#ifndef _STR_IMP#define _STR_IMP#include <malloc.h>#include <list>class String {private: static char *bad_alloc(char*);protected: char *str;public: String(); String(const char *op2); String(String &op2); ~String(); String operator=(char *op2); String operator+=(char *op2); friend String operator+(String &op1, String &op2); friend String operator+(String &op1, char *op2); friend String operator+(char *op1, String &op2); void* operator new(size_t sz); void* operator new[](size_t sz); void operator delete(void *p) { free(p); } void operator delete[](void *p) { free(p); } operator char*() { return str; } int len(); int pLen(); String* listToStrArray(std::list<char*> dataAwait); String* Split(char *delimiters, bool isWord); bool Equals(String op2); bool Equals(char *op2); bool StartsWith(String seqStr); int BruteDataLen();};#endif File String.cpp#include "String.h"String::String() { str = new char[sizeof(char*) + 1]; memset(str, 0, sizeof(char*) + 1);}String::String(const char* op2) { str = new char[sizeof(char*) + 1]; memset(str, 0, sizeof(char*) + 1); if(&this->str != &op2 && op2 != NULL) { str = new char[strlen(op2) + 1 ]; strcpy(str, op2); }}String::String(String &op2) { str = new char[sizeof(char*) + 1]; memset(str, 0, sizeof(char*) + 1); if(&this->str != &op2.str && op2 != NULL) { str = new char[op2.len() + 1 ]; strcpy(str, op2); }}String::~String() { delete[] str; }String String::operator=(char *op2) { if(&this->str != &op2 && op2 != NULL) { delete[] str; str = new char[strlen(op2) + 1]; strcpy(this->str, op2); } return *this;}String String::operator+=(char *op2) { if(op2 != NULL) { String bkUp = *this + op2; delete[] str; str = new char[bkUp.len() + 1]; strcpy(str, bkUp); } return *this;}String operator+(String &op1, String &op2) { String sum; if(op1 != NULL && op2 != NULL) { sum.str = new char[op1.len() + op2.len() + 1]; memset(sum.str, 0, op1.len() + op2.len() + 1); for(int i=0; i<(int)op1.len(); i++) sum[i] = op1[i]; for(int i=0; i<(int)op2.len(); i++) sum[i+op1.len()] = op2[i]; }else if(op1 != NULL) { sum.str = new char[op1.len() + 1]; memset(sum.str, 0, op1.len() + 1); for(int i=0; i<(int)op1.len(); i++) sum[i] = op1[i]; }else if(op2 != NULL) { sum.str = new char[op2.len() + 1]; memset(sum.str, 0, op2.len() + 1); for(int i=0; i<(int)op2.len(); i++) sum[i] = op2[i]; }else { sum.str = new char[sizeof(char*) + 1]; memset(sum.str, 0, sizeof(char*) + 1); } return sum;}String operator+(String &op1, char *op2) { String sum; if(op1 == NULL) { sum.str = new char[strlen(op2) + 1]; memset(sum.str, 0, strlen(op2) + 1); for(int i=0; i<(int)strlen(op2); i++) sum[i] = op2[i]; }else { sum.str = new char[op1.len() + strlen(op2) + 1]; memset(sum.str, 0, op1.len() + strlen(op2) + 1); for(int i=0; i<(int)op1.len(); i++) sum[i] = op1[i]; for(int i=0; i<(int)strlen(op2); i++) sum[i+op1.len()] = op2[i]; } return sum;}String operator+(char *op1, String &op2) { String sum; if(op2 == NULL) { sum.str = new char[strlen(op1) + 1]; memset(sum.str, 0, strlen(op1) + 1); for(int i=0; i<(int)strlen(op1); i++) sum[i] = op1[i]; }else { sum.str = new char[strlen(op1) + op2.len() + 1]; memset(sum.str, 0, strlen(op1) + op2.len() + 1); for(int i=0; i<(int)strlen(op1); i++) sum[i] = op1[i]; for(int i=0; i<(int)op2.len(); i++) sum[i+strlen(op1)] = op2[i]; } return sum;}char* String::bad_alloc(char *msg) { return msg;}void* String::operator new(size_t sz) { void *p = malloc(sz+4); if(p == 0) throw bad_alloc(""); return p;}void* String::operator new[](size_t sz) { void *p = malloc(sz+4); if(p == 0) throw bad_alloc(""); return p;}int String::len() { if(this->str != NULL) return (int)strlen(str); return -1;}int String::pLen() { char len[8]; sprintf_s(len, sizeof(len), "%d", this[-1]); return atoi(len);}String* String::listToStrArray(std::list<char*> dataAwait) { String *arr = new String[dataAwait.size()]; int count = 0; for(std::list<char*>::iterator i=dataAwait.begin(); i!=dataAwait.end(); i++) arr[count++] = *i; return arr;}String* String::Split(char *delimiters, bool isWord) { if(str == NULL || delimiters == NULL) return NULL; std::list<char*> dataAwait; char *argStk = (char*) malloc(strlen(str) + 1); char *argDel = (char*) malloc(strlen(delimiters) + 1); strcpy(argStk, str); strcpy(argDel, delimiters); int n = strlen(argStk); int o = strlen(argDel); if(isWord && o > 1) { if(strlen(argStk) < strlen(argDel)) { dataAwait.insert(dataAwait.begin(), argStk); }else { bool endSearch = true; int *strStk = (int*) calloc(n + 2, sizeof(int*)); strStk[0] = 1; for(int c=0; c<n; c++) { if(c + o <= n) { for(int i=0; i<o; i++) { if(argStk[c+i] != argDel[i]) { endSearch = false; break; } } if(endSearch) { argStk[c] = '\0'; strStk[c+o+1] = 1; c+=strlen(argDel)-1; } }else break; endSearch = true; } for(int j=n; j>=-1; j--) { char *pch2 = (char*) calloc(n + 1, sizeof(char*)); while(strStk[j+1] == 0) { j--; } if(j == -1) strcpy(pch2, argStk+j+1); else strcpy(pch2, argStk+j); if(strlen(pch2) == 0) dataAwait.insert(dataAwait.begin(), (char*)""); else dataAwait.insert(dataAwait.begin(), pch2); } delete []strStk; } }else { int *strStk = (int*) calloc(strlen(argStk) + 2, sizeof(int*)); strStk[0] = 1; char* pch1; for(int i=0; i<(int)strlen(argDel); i++) { pch1 = strchr(argStk, argDel[i]); while (pch1 != NULL) { strStk[pch1-argStk+2] = 1; pch1 = strchr(pch1+1, argDel[i]); } } for(int j=strlen(argStk); j>=-1; j--) { char *pch2 = (char*) calloc(strlen(argStk) + 1, sizeof(char*)); argStk[j] = '\0'; while(strStk[j+1] == 0) { j--; } if(j == -1) strcpy(pch2, argStk+j+1); else strcpy(pch2, argStk+j); if(strlen(pch2) == 0) dataAwait.insert(dataAwait.begin(), (char*)""); else dataAwait.insert(dataAwait.begin(), pch2); } delete []strStk; } return listToStrArray(dataAwait);}bool String::Equals(String op2) { if(this->len() == -1 && op2.len() == -1) return true; else if(this->len() == -1) return false; else if(op2.len() == -1) return false; else { for(int i=0; i<this->len(); i++) if(str[i] != op2[i]) return false; return true; }}bool String::Equals(char *op2) { if(this->len() == -1 && strlen(op2) == -1) return true; else if(this->len() == -1) return false; else if(strlen(op2) == -1) return false; else { for(int i=0; i<this->len(); i++) if(str[i] != op2[i]) return false; return true; }}bool String::StartsWith(String seqStr) { if(len() < seqStr.len()) return false; for(int i=0; i<seqStr.len(); i++) if(str[i] != seqStr[i]) return false; return true;}int String::BruteDataLen() { int dataLen = 0; for(int i=0; i<len(); i++) if(str[i] != '\0' && str[i] != '\n' && str[i] != '\r' && str[i] != ' ') dataLen++; return dataLen;}De asemenea pentru exemplificare va ofer, aceste teste.#include "String.h"int main() { char *c = new char[sizeof(char*)]; memset(c, 0, sizeof(char*)); strcat(c, "bananna"); String x = "Sa%sa0%xd"+ (String)c; String gh; String g = "sa:"; g += "new ch"+x; String *s = x.Split("a%", false); String *as= x.Split("a%", true); for(int i=0; i<s->pLen(); i++) printf("Split To Alphas: %s\n", s[i]); for(int i=0; i<as->pLen(); i++) printf("Split To Alphas: %s\n", as[i]); printf("Res: %s\n", g); getwchar(); return 0;}#Autor ^##PS: In curand voi publica si sursa java la un BruteForce pentru crypt("pass", "hash")(functie cunoscuta in php) . L-am creat pentru ca inloc de 4k hashes/s merge cu 81k, in plus m-a ajutat sa sparg o parola random 8 caractere, in 10 min, cand cu php si 4 threaduri asteptase-m 17 ore. Sursa va fi minimala. La fel cine vrea sa adauge ceva, hf.http://imageshack.us/f/252/sanstitregkk.png Lipseste doar interfata, care o voi face.##PS2: Daca nu intelegeti ce am facut acolo, pot da un edit, si pune comentarii. Edited May 23, 2012 by noVaLue Quote
ionut.hulub Posted May 23, 2012 Report Posted May 23, 2012 da tu sti ca exista deja clasa string in c++ nu? Quote
noVaLue Posted May 23, 2012 Author Report Posted May 23, 2012 dar nu-mi place cum se comporta , de altfel nu as fi incercat o refacere a ei. Quote