Add some const qualifiers, removed MALLOC() null checks, fixed Float_modf(), use memcpy() instead of memmove().

This commit is contained in:
Jorge Acereda 2019-06-14 14:38:19 +02:00
parent 8550f0e243
commit bbee702f5e
10 changed files with 48 additions and 67 deletions

View File

@ -1,7 +1,7 @@
#include "carp_stdbool.h"
// Bool
bool Bool_copy(bool* b) {
bool Bool_copy(const bool* b) {
return *b;
}

View File

@ -20,17 +20,12 @@ char Char_from_MINUS_int(int i) {
return (char)i;
}
char Char_copy(char *c) {
char Char_copy(const char *c) {
return *c;
}
String PtrChar_str(char *c) {
String PtrChar_str(const char *c) {
size_t len = strlen(c) + 1;
String ptr = CARP_MALLOC(len);
if (ptr == NULL) {
return NULL;
}
return (String) memcpy(ptr, c, len);
}

View File

@ -11,7 +11,7 @@ bool Double__GT_(double x, double y) { return x > y; }
bool Double__EQ_(double x, double y) { return x == y; }
double Double_neg(double x) { return -x; }
double Double_copy(double *x) { return *x; }
double Double_copy(const double *x) { return *x; }
// Double.toInt : Double -> Int
int Double_to_MINUS_int(double x) {

View File

@ -11,7 +11,7 @@ bool Float__GT_(float x, float y) { return x > y; }
bool Float__EQ_(float x, float y) { return x == y; }
float Float_neg(float x) { return -x; }
float Float_copy(float *x) { return *x; }
float Float_copy(const float *x) { return *x; }
int Float_to_MINUS_int(float x) {
return (int)x;
@ -72,7 +72,7 @@ float Float_exp(float x) {
}
float Float_frexp(float x, int* exponent) {
return frexp(x, exponent);
return frexpf(x, exponent);
}
float Float_ldexp(float x, int exponent) {
@ -87,8 +87,8 @@ float Float_log10(float x) {
return log10(x);
}
float Float_modf(float x, float* integer) {
return modf(x, (double*) integer);
float Float_modf(float x, float * integer) {
return modff(x, integer);
}
float Float_pow(float x, float y) {

View File

@ -24,7 +24,7 @@ int Int_bit_MINUS_or(int x, int y) { return x | y; }
int Int_bit_MINUS_xor(int x, int y) { return x ^ y; }
int Int_bit_MINUS_not(int x) { return ~x; }
int Int_copy(int *x) { return *x; }
int Int_copy(const int *x) { return *x; }
int Int_mod(int x, int divider) {
return x % divider;

View File

@ -70,7 +70,7 @@ String IO_get_MINUS_line() {
return buffer;
}
String IO_read_MINUS_file(String *filename) {
String IO_read_MINUS_file(const String *filename) {
String buffer = 0;
long length;
FILE *f = fopen(*filename, "rb");
@ -105,6 +105,6 @@ void IO_fclose(FILE *f) {
fclose(f);
}
FILE *IO_fopen(String *filename, String *mode) {
FILE *IO_fopen(const String *filename, const String *mode) {
return fopen(*filename, *mode);
}

View File

@ -25,7 +25,7 @@ long Long_bit_MINUS_or(long x, long y) { return x | y; }
long Long_bit_MINUS_xor(long x, long y) { return x ^ y; }
long Long_bit_MINUS_not(long x) { return ~x; }
long Long_copy(long *x) { return *x; }
long Long_copy(const long *x) { return *x; }
long Long_mod(long x, long divider) {
return x % divider;

View File

@ -355,9 +355,6 @@ String Pattern_internal_lmemfind(String s1, size_t l1, String s2, size_t l2) {
String String_copy_len(String s, int len) {
String ptr = CARP_MALLOC(len+1);
if (!ptr) return NULL;
memcpy(ptr, s, len);
ptr[len] = '\0';
return ptr;
@ -573,6 +570,7 @@ PatternGMatchRes Pattern_internal_gmatch_aux(PatternGMatchState* gm) {
return (PatternGMatchRes){.valid=true, .data=a};
}
}
memset(&a, 0, sizeof(a));
return (PatternGMatchRes){.valid=false, .data=a}; /* not found */
}
@ -682,9 +680,6 @@ String Pattern_substitute(Pattern* p, String *s, String *t, int ns) {
Pattern Pattern_copy(Pattern *p) {
size_t len = strlen(*p) + 1;
Pattern ptr = CARP_MALLOC(len);
if (!ptr) return NULL;
return (Pattern) memcpy(ptr, *p, len);
}

View File

@ -29,7 +29,7 @@ void String_string_MINUS_set_BANG_(String *s, int i, char ch) {
(*s)[i] = ch;
}
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, String *src) {
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, const String *src) {
char *dest = (*into) + i;
int lsrc = strlen(*src);
@ -71,30 +71,25 @@ void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i, String *src) {
strncpy(dest, *src, lsrc);
}
String String_copy(String *s) {
String String_copy(const String *s) {
size_t len = strlen(*s) + 1;
String ptr = CARP_MALLOC(len);
if (ptr == NULL) {
return NULL;
}
return (String) memcpy(ptr, *s, len);
}
bool String__EQ_(String *a, String *b) {
bool String__EQ_(const String *a, const String *b) {
return strcmp(*a, *b) == 0;
}
bool String__GT_(String *a, String *b) {
bool String__GT_(const String *a, const String *b) {
return strcmp(*a, *b) > 0;
}
bool String__LT_(String *a, String *b) {
bool String__LT_(const String *a, const String *b) {
return strcmp(*a, *b) < 0;
}
String String_append(String *a, String *b) {
String String_append(const String *a, const String *b) {
int la = strlen(*a);
int lb = strlen(*b);
int total = la + lb + 1;
@ -103,41 +98,37 @@ String String_append(String *a, String *b) {
return buffer;
}
int String_length(String *s) {
int String_length(const String *s) {
return strlen(*s);
}
char* String_cstr(String *s) {
char* String_cstr(const String *s) {
return *s;
}
String String_str(String *s) {
int n = strlen(*s) + 1;
String buffer = CARP_MALLOC(n);
snprintf(buffer, n, "%s", *s);
return buffer;
String String_str(const String *s) {
return String_copy(s);
}
String String_prn(String *s) {
String String_prn(const String *s) {
int n = strlen(*s) + 4;
String buffer = CARP_MALLOC(n);
snprintf(buffer, n, "@\"%s\"", *s);
return buffer;
}
char String_char_MINUS_at(String* s, int i) {
char String_char_MINUS_at(const String* s, int i) {
return (*s)[i];
}
String String_format(String *str, String *s) {
String String_format(const String *str, const String *s) {
int size = snprintf(NULL, 0, *str, *s)+1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, *s);
return buffer;
}
Array String_chars(String *s) {
Array String_chars(const String *s) {
Array chars;
chars.len = strlen(*s);
chars.capacity = chars.len;
@ -145,19 +136,19 @@ Array String_chars(String *s) {
return chars;
}
String String_from_MINUS_chars(Array *a) {
String String_from_MINUS_chars(const Array *a) {
String s = CARP_MALLOC(a->len+1);
memmove(s, a->data, a->len);
memcpy(s, a->data, a->len);
s[a->len] = '\0';
return s;
}
String String_tail(String* s) {
int len = strlen(*s);
String news = CARP_MALLOC(len);
memcpy(news, (*s)+1, len-1);
news[len-1] = '\0';
return news;
String String_tail(const String* s) {
int len = strlen(*s);
String news = CARP_MALLOC(len);
memcpy(news, (*s)+1, len-1);
news[len-1] = '\0';
return news;
}
String String_empty() {
@ -167,8 +158,8 @@ String String_empty() {
}
String Bool_str(bool b) {
String true_str = "true";
String false_str = "false";
const String true_str = "true";
const String false_str = "false";
if(b) {
return String_copy(&true_str);
} else {
@ -176,7 +167,7 @@ String Bool_str(bool b) {
}
}
String Bool_format(String* str, bool b) {
String Bool_format(const String* str, bool b) {
int size = snprintf(NULL, 0, *str, b)+1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
@ -195,7 +186,7 @@ String Char_prn(char c) {
return buffer;
}
String Char_format(String* str, char b) {
String Char_format(const String* str, char b) {
int size = snprintf(NULL, 0, *str, b)+1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, b);
@ -209,7 +200,7 @@ String Double_str(double x) {
return buffer;
}
String Double_format(String* s, double x) {
String Double_format(const String* s, double x) {
int size = snprintf(NULL, 0, *s, x)+1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *s, x);
@ -223,7 +214,7 @@ String Float_str(float x) {
return buffer;
}
String Float_format(String* str, float x) {
String Float_format(const String* str, float x) {
int size = snprintf(NULL, 0, *str, x)+1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
@ -237,14 +228,14 @@ String Int_str(int x) {
return buffer;
}
String Int_format(String* str, int x) {
String Int_format(const String* str, int x) {
int size = snprintf(NULL, 0, *str, x)+1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
int Int_from_MINUS_string(String *s) {
int Int_from_MINUS_string(const String *s) {
return atoi(*s);
}
@ -255,18 +246,18 @@ String Long_str(long x) {
return buffer;
}
String Long_format(String* str, long x) {
String Long_format(const String* str, long x) {
int size = snprintf(NULL, 0, *str, x)+1;
String buffer = CARP_MALLOC(size);
snprintf(buffer, size, *str, x);
return buffer;
}
long Long_from_MINUS_string(String *s) {
long Long_from_MINUS_string(const String *s) {
return atol(*s);
}
int String_index_MINUS_of_MINUS_from(String *s, char c, int i) {
int String_index_MINUS_of_MINUS_from(const String *s, char c, int i) {
/* Return index of first occurrence of `c` in `s` AFTER index i
* Returns -1 if not found
*/
@ -280,7 +271,7 @@ int String_index_MINUS_of_MINUS_from(String *s, char c, int i) {
return -1;
}
int String_index_MINUS_of(String *s, char c) {
int String_index_MINUS_of(const String *s, char c) {
/* Return index of first occurrence of `c` in `s`
* Returns -1 if not found
*/

View File

@ -42,7 +42,7 @@ double System_nanotime() {
}
#endif
void System_system(String *command) {
void System_system(const String *command) {
system(*command);
}