Compare commits
6 Commits
f9a5352b6e
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 957177e9ec | |||
| c9e69ededb | |||
| 2f6bba5ce7 | |||
| 0d5a86a158 | |||
| e55dd2bf96 | |||
| ed13b8d624 |
+19
-9
@@ -2,15 +2,21 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// It's base. NUL terminator.
|
||||
int nulTerm = 1;
|
||||
|
||||
// -------------------------------------
|
||||
// BASE - String and init of this
|
||||
// -------------------------------------
|
||||
typedef char* string;
|
||||
string strInit(char* s) {
|
||||
string str = malloc(strlen(s) * sizeof(char));
|
||||
string str = malloc(strlen(s) + nulTerm);
|
||||
strcpy(str, s);
|
||||
return str;
|
||||
}
|
||||
void strResize(string* s, int new_size) {
|
||||
*s = realloc(*s, new_size + nulTerm);
|
||||
}
|
||||
// -------------- END ------------------
|
||||
|
||||
|
||||
@@ -19,15 +25,15 @@ string strInit(char* s) {
|
||||
// FUNCTION - basic operations
|
||||
// -------------------------------------
|
||||
void strAdd(string* s, char* str_new) {
|
||||
*s = realloc(*s, strlen(*s) + strlen(str_new)*sizeof(char));
|
||||
*s = realloc(*s, strlen(*s) + strlen(str_new) + nulTerm);
|
||||
strcat(*s, str_new);
|
||||
}
|
||||
void strSet(string* s, char* str_new) {
|
||||
*s = realloc(*s, strlen(str_new)*sizeof(char));
|
||||
*s = realloc(*s, strlen(str_new) + nulTerm);
|
||||
strcpy(*s, str_new);
|
||||
}
|
||||
void strCopy(string* s1, string* s2) {
|
||||
strcpy(*s2, *s1);
|
||||
void strCopy(string* dst, string* src) {
|
||||
strcpy(*dst, *src);
|
||||
}
|
||||
// -------------- END ------------------
|
||||
|
||||
@@ -36,19 +42,23 @@ void strCopy(string* s1, string* s2) {
|
||||
// -------------------------------------
|
||||
// CONVERT - different convert to str
|
||||
// -------------------------------------
|
||||
int numLen(double num) {
|
||||
return snprintf(NULL, 0, "%lf", num);
|
||||
}
|
||||
|
||||
char* int2Str(int* num) {
|
||||
char* buffer = malloc(12);
|
||||
char* buffer = malloc(numLen(*num) + nulTerm);
|
||||
sprintf(buffer, "%d", *num);
|
||||
return buffer;
|
||||
}
|
||||
char* float2Str(float* num) {
|
||||
char* buffer = malloc(12);
|
||||
char* buffer = malloc(numLen(*num) + nulTerm);
|
||||
sprintf(buffer, "%f", *num);
|
||||
return buffer;
|
||||
}
|
||||
char* double2Str(double* num) {
|
||||
char* buffer = malloc(12);
|
||||
sprintf(buffer, "%f", *num);
|
||||
char* buffer = malloc(numLen(*num) + nulTerm);
|
||||
sprintf(buffer, "%lf", *num);
|
||||
return buffer;
|
||||
}
|
||||
// -------------- END ------------------
|
||||
|
||||
Reference in New Issue
Block a user