From c9e69ededbabb78346eaa362269e89f747323710 Mon Sep 17 00:00:00 2001 From: justuser-31 Date: Fri, 9 May 2025 14:04:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D1=87=D1=91=D1=82=20nulTerminator=20?= =?UTF-8?q?=D0=B8=20=D0=BC=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/string.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/libs/string.h b/libs/string.h index 474d677..cfd8c1b 100644 --- a/libs/string.h +++ b/libs/string.h @@ -2,17 +2,20 @@ #include #include +// 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 * sizeof(char)); + *s = realloc(*s, new_size + nulTerm); } // -------------- END ------------------ @@ -22,15 +25,15 @@ void strResize(string* s, int new_size) { // 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* src, string* dst) { + strcpy(*src, *dst); } // -------------- END ------------------ @@ -44,17 +47,17 @@ int numLen(double num) { } char* int2Str(int* num) { - char* buffer = malloc(numLen(*num) + 2); + char* buffer = malloc(numLen(*num) + nulTerm); sprintf(buffer, "%d", *num); return buffer; } char* float2Str(float* num) { - char* buffer = malloc(numLen(*num) + 2); + char* buffer = malloc(numLen(*num) + nulTerm); sprintf(buffer, "%f", *num); return buffer; } char* double2Str(double* num) { - char* buffer = malloc(numLen(*num) + 2); + char* buffer = malloc(numLen(*num) + nulTerm); sprintf(buffer, "%lf", *num); return buffer; }