string class for C
1
Name:
Anonymous
2012-05-14 16:25
Design a string ``class'' for C by using structs and function pointers
2
Name:
no
2012-05-14 16:31
no
3
Name:
VIPPER
2012-05-14 16:32
Your not the boss of me now.
4
Name:
Anonymous
2012-05-14 19:04
typedef struct{
char *str;
int len;
}string;
string operator=(string,char *str){
string t;
t.str=str;
t.len=strlen(str);
}
char * operator=(char *,string str){
char *t=malloc(str.len+1);
memcpy(t,str.str,str.len);
t[str.len]=0;
return t;
}
string operator+(string a1,string a2){
string t;
char *tmp=malloc(a1.len+a2.len+2);
memset(tmp,0,a1.len+a2.len+2);
memcpy(tmp,a1.str,a1.len);
memcpy(tmp+strlen(tmp)+1,a2.str,a2.len);
t.len=strlen(tmp);
t.str=tmp;
return tmp;
}
5
Name:
Anonymous
2012-05-14 19:23
typedef struct __string_{
int length;
char * value;
} internal_string, * String;
String new_string(int size){
String s= (string)malloc(size * sizeof(char) + sizeof(internal_string));
s->length = size;
s->value = ((char *)s) + sizeof(internal_string);
return s;
}
void destroy_string(String s){
free(s);
}
String add_string(String s1, String s2){
String s = new_string(s1->length + s2->length);
memcpy(s->value, s1->value, s1->length);
memcpy(s->value, s2->value, s2->length);
return s;
}
#define true 1;
#define false 0;
int equals_string(String s1, String s2){
char * p1 = s1->value;
char * p2 = s2->value;
while(*p1 && *p2){
if(*p1 != *p2)
return false;
}
if(*p1 || *p2)
return false;
return true;
}
6
Name:
Anonymous
2012-05-14 19:26
...
while(*p1 && *p2){
if(*p1 != *p2)
return false;
++p1;
++p2;
}
...
ofc
7
Name:
Anonymous
2012-05-14 19:27
...
memcpy(s->value, s1->value, s1->length);
memcpy(s->value + s1->length, s2->value, s2->length);
...
8
Name:
Anonymous
2012-05-14 19:44
>>4
C
operator
what in the actual fuck
9
Name:
Anonymous
2012-05-14 19:49
>>1
You know what? I might just do that. But believe me, I will surely not post your homework here when I'm done.
Also, anyone here ever heard of
size_t?
10
Name:
matt
2012-05-14 21:18
11
Name:
Anonymous
2012-05-14 22:13
Look into std::basic_string and convert that to structs and this pointers.
12
Name:
Anonymous
2012-05-14 22:13
>>9
size_t is
ENTERPRISE BLOAT .
13
Name:
Anonymous
2012-05-14 22:15
ENTERPRISE
14
Name:
Anonymous
2012-05-14 23:00
>>12
No it isn't. It's defined in stddef.h, which is a very small header file. If you don't use size_t for holding the size of an allocated chunk of memory, you're doing it wrong. int, long, unsigned int, unsigned long, etc. are wrong.
15
Name:
Anonymous
2012-05-15 3:39
>>1
and function pointers
Why would you bloat it with function pointers?
16
Name:
Anonymous
2012-05-15 8:16
>>15
Because he's not very intelligent.
17
Name:
Anonymous
2012-05-15 8:37
>>15,16
No, no, no, you see, one strncpy for each character encoding, and multi-byte and fixed-byte strings can live happily together. You obviously don't have experience with large, multi-platform projects where you can't predict the future need of strings and encodings. When you can't control what's fed into your program, you have to take a submissive stance
like your mother did last night .
18
Name:
Anonymous
2012-05-15 9:00
>>17
You don't need function pointers to cater for different character encodings.
19
Name:
Anonymous
2012-05-15 9:20
>>17
Yeah you're still a fucking idiot.
20
Name:
bampu pantsu
2012-05-29 4:57
bampu pantsu