From 216c0e4972c6f7b66db2f51783bd47f2c0e11d83 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Wed, 21 Jan 2026 21:44:41 +0100 Subject: [PATCH 01/19] Redo string stdlib --- src/structures/String.sac | 181 +++++----- src/structures/src/String/StringC.h | 35 -- src/structures/src/String/btos.c | 27 -- src/structures/src/String/copystr.c | 20 -- src/structures/src/String/ctos.c | 22 -- src/structures/src/String/dtos.c | 22 -- src/structures/src/String/freestr.c | 17 - src/structures/src/String/ftos.c | 22 -- src/structures/src/String/itos.c | 22 -- src/structures/src/String/sprintf.c | 48 --- src/structures/src/String/sscanf.c | 23 -- src/structures/src/String/sscanfstr.c | 24 -- src/structures/src/String/str.c | 437 ++++++++++++++++++++++++ src/structures/src/String/str.h | 50 +++ src/structures/src/String/strcasecmp.c | 20 -- src/structures/src/String/strcat.c | 27 -- src/structures/src/String/strchr.c | 25 -- src/structures/src/String/strcmp.c | 20 -- src/structures/src/String/strcspn.c | 20 -- src/structures/src/String/strdrop.c | 23 -- src/structures/src/String/strext.c | 36 -- src/structures/src/String/strins.c | 28 -- src/structures/src/String/strlen.c | 16 - src/structures/src/String/strmod.c | 43 --- src/structures/src/String/strncasecmp.c | 20 -- src/structures/src/String/strncat.c | 23 -- src/structures/src/String/strncmp.c | 20 -- src/structures/src/String/strovwt.c | 64 ---- src/structures/src/String/strrchr.c | 25 -- src/structures/src/String/strsel.c | 18 - src/structures/src/String/strspn.c | 20 -- src/structures/src/String/strstr.c | 17 - src/structures/src/String/strtake.c | 43 --- src/structures/src/String/strtod.c | 31 -- src/structures/src/String/strtof.c | 31 -- src/structures/src/String/strtoi.c | 31 -- src/structures/src/String/strtok.c | 51 --- src/structures/src/String/tostring.c | 32 -- src/structures/src/String/trim.c | 105 ------ 39 files changed, 588 insertions(+), 1151 deletions(-) delete mode 100644 src/structures/src/String/StringC.h delete mode 100644 src/structures/src/String/btos.c delete mode 100644 src/structures/src/String/copystr.c delete mode 100644 src/structures/src/String/ctos.c delete mode 100644 src/structures/src/String/dtos.c delete mode 100644 src/structures/src/String/freestr.c delete mode 100644 src/structures/src/String/ftos.c delete mode 100644 src/structures/src/String/itos.c delete mode 100644 src/structures/src/String/sprintf.c delete mode 100644 src/structures/src/String/sscanf.c delete mode 100644 src/structures/src/String/sscanfstr.c create mode 100644 src/structures/src/String/str.c create mode 100644 src/structures/src/String/str.h delete mode 100644 src/structures/src/String/strcasecmp.c delete mode 100644 src/structures/src/String/strcat.c delete mode 100644 src/structures/src/String/strchr.c delete mode 100644 src/structures/src/String/strcmp.c delete mode 100644 src/structures/src/String/strcspn.c delete mode 100644 src/structures/src/String/strdrop.c delete mode 100644 src/structures/src/String/strext.c delete mode 100644 src/structures/src/String/strins.c delete mode 100644 src/structures/src/String/strlen.c delete mode 100644 src/structures/src/String/strmod.c delete mode 100644 src/structures/src/String/strncasecmp.c delete mode 100644 src/structures/src/String/strncat.c delete mode 100644 src/structures/src/String/strncmp.c delete mode 100644 src/structures/src/String/strovwt.c delete mode 100644 src/structures/src/String/strrchr.c delete mode 100644 src/structures/src/String/strsel.c delete mode 100644 src/structures/src/String/strspn.c delete mode 100644 src/structures/src/String/strstr.c delete mode 100644 src/structures/src/String/strtake.c delete mode 100644 src/structures/src/String/strtod.c delete mode 100644 src/structures/src/String/strtof.c delete mode 100644 src/structures/src/String/strtoi.c delete mode 100644 src/structures/src/String/strtok.c delete mode 100644 src/structures/src/String/tostring.c delete mode 100644 src/structures/src/String/trim.c diff --git a/src/structures/String.sac b/src/structures/String.sac index 7167ebfe..5b4c2b05 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -12,115 +12,128 @@ export all except { indent }; external typedef string; #pragma freefun "free_string" - #pragma linkobj "src/String/freestr.o" + #pragma linkobj "src/String/str.o" #pragma copyfun "copy_string" - #pragma linkobj "src/String/copystr.o" + #pragma linkobj "src/String/str.o" external string to_string(char[.] A, int LENGTH); - #pragma linksign [1,2,3] - #pragma refcounting [0,1] - #pragma linkobj "src/String/tostring.o" + #pragma linkname "SACtostring" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" + /* Convert character array to string. */ + +external string to_string(char[.] A); + #pragma linkname "SACautotostring" + #pragma sacarg [1] + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert character array to string. */ external string strmod(string str, int P, char C); - #pragma linksign [1,2,3,4] - #pragma refcounting [0,1] - #pragma linkobj "src/String/strmod.o" + #pragma linkname "SACstrmod" + #pragma linksign [1,1,2,3] + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Change position P of string str to character C. */ external string strins(string S1, int P, string S2); - #pragma linkobj "src/String/strins.o" - #pragma linksign [0,1,2,3] + #pragma linkname "SACstrins" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Insert string S2 into string S1 starting at position P. */ external string strovwt(string S1, int P, string S2); - #pragma linksign [1,2,3,4] - #pragma refcounting [0,1] - #pragma linkobj "src/String/strovwt.o" + #pragma linkname "SACstrovwt" + #pragma linksign [1,1,2,3] + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Overwrite string S2 with string S1 starting at position P. */ external char strsel(string str, int P); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strsel.o" + #pragma linkname "SACstrsel" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Select character P of string str. */ external string strcat(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcat.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Concatenate strings S1 and S2. */ external string +(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcat.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Infix version of strcat. */ external string strncat(string S1, string S2, int N); #pragma linkname "SACstrncat" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strncat.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Concatenate string S1 and the first N characters of string S2. */ external int strcmp(string S1, string S2); #pragma linkname "SACstrcmp" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcmp.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Compare strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strncmp(string S1, string S2, int N); #pragma linkname "SACstrncmp" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strncmp.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Compare the first N characters of strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strcasecmp(string S1, string S2); #pragma linkname "SACstrcasecmp" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcasecmp.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Compare strings S1 and S2 while ignoring upper/lower case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strncasecmp(string S1, string S2, int N); #pragma linkname "SACstrncasecmp" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strncasecmp.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Compare the first N characters of strings S1 and S2 ignoring case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strlen(string str); - #pragma linksign [0,1] #pragma linkname "SACstrlen" - #pragma linkobj "src/String/strlen.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Return the length of string str. */ external string strtake(string str, int N); - #pragma linksign [1,2,3] - #pragma refcounting [0,1] - #pragma linkobj "src/String/strtake.o" + #pragma linkname "SACstrtake" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Return the first N characters of string str. */ external string strdrop(string str, int N); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strdrop.o" + #pragma linkname "SACstrdrop" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Return all but the first N characters of string str. */ external string strext(string str, int FIRST, int LEN); - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strext.o" + #pragma linkname "SACstrext" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Extract the substring beginning at character FIRST with length LEN from string str. */ external string sprintf(string FORMAT, ...); #pragma linkname "SACsprintf" - #pragma linksign [0,1] - #pragma linkobj "src/String/sprintf.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Generate a string depending on the format string and a variable number of arguments similar to the printf and fprintf functions. */ @@ -128,7 +141,8 @@ external string sprintf(string FORMAT, ...); external int, ... sscanf(string str, string FORMAT); #pragma linkname "SACsscanf" #pragma linksign [0,1,2] - #pragma linkobj "src/String/sscanf.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Scan the string str concerning the format string FORMAT. The format string is the same as for fscanf. The int result gives the maximum number of successfully performed @@ -138,54 +152,56 @@ external int, ... sscanf(string str, string FORMAT); errors. Use sscanf_str instead. */ external string sscanf_str(string str, string FORMAT); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/sscanfstr.o" + #pragma linkname "SACsscanf_str" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Scan the string str concerning the format string FORMAT. FORMAT must have exactly one string conversion specifier. The scanned substring is returned. */ external int strchr(string str, char C); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strchr.o" #pragma linkname "SACstrchr" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Return the position of the first occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strrchr(string str, char C); #pragma linkname "SACstrrchr" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strrchr.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Return the position of the last occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strcspn(string str, string REJECT); #pragma linkname "SACstrcspn" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcspn.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Return the length of the initial segment of str which consists entirely of characters not in REJECT. */ external int strspn(string str, string ACCEPT); #pragma linkname "SACstrspn" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strspn.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Return the length of the initial segment of str which consists entirely of characters in ACCEPT. */ external int strstr(string HAYSTACK, string NEEDLE); #pragma linkname "SACstrstr" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strstr.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Find the first occurrence of the string NEEDLE in the string HAYSTACK. Return the index to this occurrence. If NEEDLE could not be found then return -1. */ external string, string strtok(string str, string SEP); #pragma linkname "SACstrtok" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strtok.o" + #linksign [1, 2, 3, 4] + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Split string str into a token and the remainder string using the characters from SEP as delimiters. If str starts with characters from SEP then these are skipped until @@ -193,32 +209,35 @@ external string, string strtok(string str, string SEP); external string chomp(string str); #pragma linkname "SACchomp" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linksign [1,1] + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Remove trailing line terminators from str. */ external string rtrim(string str); #pragma linkname "SACrtrim" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linksign [1,1] + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Remove trailing whitespace from str. */ external string ltrim(string str); #pragma linkname "SACltrim" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Remove leading whitespace from str. */ external string trim(string str); #pragma linkname "SACtrim" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Remove leading and trailing whitespace from str. */ external int, string strtoi(string str, int BASE); #pragma linkname "SACstrtoi" #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strtoi.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert the string str into an integer value. BASE specifies the format and must be in the range from 2 to 32. This feature allows for scanning hexadecimal or octal numbers as well as decimal @@ -228,58 +247,60 @@ external int, string strtoi(string str, int BASE); external float, string strtof(string str); #pragma linkname "SACstrtof" #pragma linksign [0,1,2] - #pragma linkobj "src/String/strtof.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert the string str into a float value. The string result contains the remainder of the input string. */ external double, string strtod(string str); #pragma linkname "SACstrtod" #pragma linksign [0,1,2] - #pragma linkobj "src/String/strtod.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert the string str into a double value. The string result contains the remainder of the input string. */ external int toi(string str); #pragma linkname "SACtoi" - #pragma linksign [0,1] - #pragma linkobj "src/String/strtoi.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert the string str into an integer value. In contrast to strtoi the number must always be in decimal representation. */ external float tof(string str); #pragma linkname "SACtof" - #pragma linksign [0,1] - #pragma linkobj "src/String/strtof.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert the string str into a float value. */ external double tod(string str); #pragma linkname "SACtod" - #pragma linksign [0,1] - #pragma linkobj "src/String/strtod.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert the string str into a double value. */ external string tos(int N); #pragma linkname "SACitos" - #pragma linksign [0,1] - #pragma linkobj "src/String/itos.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert integer into string. */ external string tos(float N); #pragma linkname "SACftos" - #pragma linksign [0,1] - #pragma linkobj "src/String/ftos.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert float into string. */ external string tos(double N); #pragma linkname "SACdtos" - #pragma linksign [0,1] - #pragma linkobj "src/String/dtos.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert double into string. */ external string tos(bool B); #pragma linkname "SACbtos" - #pragma linksign [0,1] - #pragma linkobj "src/String/btos.o" + #pragma linkobj "src/String/str.o" + #pragma header "src/String/str.h" /* Convert boolean into string. */ inline string indent(int indent, string str) diff --git a/src/structures/src/String/StringC.h b/src/structures/src/String/StringC.h deleted file mode 100644 index 0cb23b93..00000000 --- a/src/structures/src/String/StringC.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Implementation of standard module StringC - */ - - -#include -#include -#include -#include - -#include "sac.h" - - -#ifdef CHECK - -#define RANGECHECK(check, lower, upper, str) \ - if ((checkupper)) \ - SAC_RuntimeError("Range violation upon string access:\n" \ - "tried to access character %d of string\n" \ - "\"%s\"", check, str); - - -#else /* CHECK */ - -#define RANGECHECK(check, lower, upper, str) - -#endif /* CHECK */ - - -#define STRDUP(new, old) new=(string)SAC_MALLOC(strlen(old)+1); \ - strcpy(new, old); - -#define STRFREE(str) SAC_FREE(str); - -typedef char* string; diff --git a/src/structures/src/String/btos.c b/src/structures/src/String/btos.c deleted file mode 100644 index 6902c6a5..00000000 --- a/src/structures/src/String/btos.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACbtos( bool n) -{ - char *res; - - res = (char *) SAC_MALLOC( 6); - - if (n) { - strcpy( res, "true"); - } - else { - strcpy( res, "false"); - } - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/copystr.c b/src/structures/src/String/copystr.c deleted file mode 100644 index 35171ce2..00000000 --- a/src/structures/src/String/copystr.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string copy_string( string s) -{ - string new; - - STRDUP( new, s); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/ctos.c b/src/structures/src/String/ctos.c deleted file mode 100644 index bf8a2199..00000000 --- a/src/structures/src/String/ctos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACctos( double *n) -{ - char *res; - - res = (char *) SAC_MALLOC( 120); - - sprintf( res, "(%g,%g)", n[0], n[1]); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/dtos.c b/src/structures/src/String/dtos.c deleted file mode 100644 index 14de6ab4..00000000 --- a/src/structures/src/String/dtos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACdtos( double n) -{ - char *res; - - res = (char *) SAC_MALLOC( 60); - - sprintf( res, "%g", n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/freestr.c b/src/structures/src/String/freestr.c deleted file mode 100644 index 40bb4931..00000000 --- a/src/structures/src/String/freestr.c +++ /dev/null @@ -1,17 +0,0 @@ - -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -void free_string( string s) -{ - STRFREE( s); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/ftos.c b/src/structures/src/String/ftos.c deleted file mode 100644 index 115ff796..00000000 --- a/src/structures/src/String/ftos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACftos( float n) -{ - char *res; - - res = (char *) SAC_MALLOC( 60); - - sprintf( res, "%g", n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/itos.c b/src/structures/src/String/itos.c deleted file mode 100644 index 35b9c410..00000000 --- a/src/structures/src/String/itos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACitos( int n) -{ - char *res; - - res = (char *) SAC_MALLOC( 40); - - sprintf( res, "%d", n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/sprintf.c b/src/structures/src/String/sprintf.c deleted file mode 100644 index 8aeb46e2..00000000 --- a/src/structures/src/String/sprintf.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" -#include - -/* Allow for long path names and URLs with long parameters. */ -#if defined(PATH_MAX) && PATH_MAX >= 2048 -#define BUFFER_SIZE PATH_MAX -#else -#define BUFFER_SIZE 2048 -#endif - - -/*****************************************************************/ - -string SACsprintf( string format, ...) -{ - va_list arg_p; - char buffer[BUFFER_SIZE]; - int n; - string new; - - buffer[0] = '\0'; - va_start( arg_p, format); - n = vsnprintf( buffer, sizeof buffer, format, arg_p); - va_end( arg_p); - if ((size_t)n >= sizeof buffer) { - new = (string) SAC_MALLOC( n + 1); - va_start( arg_p, format); - n = vsnprintf( new, n + 1, format, arg_p); - va_end( arg_p); - } - else if (n >= 0) { - new = (string) SAC_MALLOC( strlen( buffer) + 1); - strcpy( new, buffer); - } - else { - new = (string) SAC_MALLOC( 1); - new[0] = '\0'; - } - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/sscanf.c b/src/structures/src/String/sscanf.c deleted file mode 100644 index b5703003..00000000 --- a/src/structures/src/String/sscanf.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACsscanf( string s, string format, ...) -{ - int res; - va_list arg_p; - - va_start( arg_p, format); - res = vsscanf( s, format, arg_p); - va_end( arg_p); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/sscanfstr.c b/src/structures/src/String/sscanfstr.c deleted file mode 100644 index 2dd446fc..00000000 --- a/src/structures/src/String/sscanfstr.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string sscanf_str( string s, string format) -{ - string new; - - new = (string) SAC_MALLOC( strlen( s) + 1); - - new[0] = 0; - - sscanf( s, format, new); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/str.c b/src/structures/src/String/str.c new file mode 100644 index 00000000..5528709f --- /dev/null +++ b/src/structures/src/String/str.c @@ -0,0 +1,437 @@ +/* + * C implementation of standard module StringC + */ + + +#include +#include +#include +#include +#include +#include "str.h" + +string copy_string (string s) +{ + string s2 = malloc ((strlen(s) + 1) * sizeof (char)); + strcpy (s2, s); + return s2; +} + +void free_string (string s) +{ + free (s); +} + + + + +string SACtostring (string arr, sac_int length) +{ + string res = malloc ((size_t) length + 1); + + strncpy (res, arr, (size_t)length); + res[length] = '\0'; + + return res; +} + +string SACautotostring (SACarg *sarr) +{ + string *parr = SACARGgetSharedData(SACTYPE__MAIN__char, sarr); + string arr = parr[0]; + sac_int length = SACARGgetShape(sarr, 0); + + string res = malloc ((size_t) length + 1); + + strncpy (res, arr, (size_t)length); + res[length] = '\0'; + + return res; +} + +void SACstrmod (string str, sac_int pos, char c) +{ + if (strlen(str) <= pos) + ; // TODO runtime error + + str[pos] = c; +} + +string SACstrins (string outer, sac_int pos, string inner) +{ + size_t outer_length = strlen (outer); + size_t inner_length = strlen (inner); + + string res = malloc(outer_length + inner_length + 1); + + strncpy(res, outer, pos); + strcpy(res+pos, inner); + strcpy(res+pos+inner_length, outer+pos); + + return res; +} + +void SACstrovwt (string outer, sac_int pos, string inner) +{ + size_t outer_length = strlen (outer); + size_t inner_length = strlen (inner); + + if (pos+inner_length > outer_length) + ; // TODO runtime error + + strncpy(outer+pos, inner, inner_length); +} + +char SACstrsel (string str, sac_int pos) +{ + int strlength = strlen(str); + if (pos >= strlength) + ; // TODO runtime error + + return str[pos]; +} + +string SACstrcat (string fst, string snd) +{ + size_t fstlength = strlen (fst); + size_t sndlength = sndlen (fst); + + string res = malloc (fstlength + sndlength + 1); + + strcpy(res, fst); + strcpy(res+fstlength, snd); + + return res; +} + +string SACstrncat (string fst, string snd, sac_int n) +{ + size_t fstlength = strlen (fst); + size_t sndlength = sndlen (fst); + + string res = malloc (fstlength + sndlength + 1); + + strcpy(res, fst); + strncpy(res+fstlength, snd, n); + + return res; +} + +sac_int SACstrcmp (string fst, string snd) +{ + return (sac_int) strcmp (fst, snd); +} + +sac_int SACstrncmp (string fst, string snd, sac_int n) +{ + return (sac_int) strncmp (fst, snd, (size_t) n); +} + +sac_int SACstrcasecmp (string fst, string snd) +{ + return (sac_int) strcasecmp (fst, snd); +} + +sac_int SACstrncasecmp (string fst, string snd, sac_int n) +{ + return (sac_int) strncasecmp (fst, snd, (size_t) n); +} + +sac_int SACstrlen (string fst) +{ + return (sac_int) strlen (fst); +} + +char SACstrtake(string str, sac_int pos) +{ + size_t strlength = strlen (str); + + if (pos >= strlength) + ; // TODO runtime error + + return str[pos]; +} + +string SACstrdrop (string str, sac_int pos) +{ + size_t strlength = strlen (str); + + string res = malloc(strlength - (size_t)pos + 1); + + strcpy(res, str+pos); + + return res; +} + +string SACstrext (string str, sac_int pos, sac_int len) +{ + size_t strlength = strlen(str); + + string res = malloc(strlength - (size_t)len + 1); + + if (pos + strlength >= len) + ; // TODO runtime error + + strncpy(res, str+pos, len); + + return res; +} + +string SACsprintf (string format, ...) +{ + va_list args; + + // Compute the length of the string + va_start (args, format); + int lengthwo0 = vsnprintf (NULL, 0, format, args); + va_end (args); + + if (lengthwo0 < 0) + ; // TODO runtime error + + size_t length = lengthwo0 + 1; + + // Allocate result string + string res = malloc(length); + + // Printf string + va_start (args, format); + vsnprintf (res, length, format, args); + va_end (args); + + return res; +} + +sac_int SACsscanf (string s, string format, ...) +{ + va_list arg_p; + + va_start( arg_p, format); + sac_int res = (sac_int)vsscanf( s, format, arg_p); + va_end( arg_p); + + return res; +} + +string SACsscanf_str (string source, string format) +{ + string res = malloc (strlen (source) + 1); + res[0] = '\0'; + + sscanf (source, format, res); + + return res; +} + +sac_int SACstrchr (string str, char c) +{ + string occurrence = strchr (str, c); + + if (occurrence == NULL) + return -1; + return (sac_int)(occurrence - str); +} + +sac_int SACstrrchr (string str, char c) +{ + string occurrence = strrchr (str, c); + + if (occurrence == NULL) + return -1; + return (sac_int)(occurrence - str); +} + +sac_int SACstrcspn(string str, string reject) +{ + return (sac_int) strcspn(str, reject); +} + +sac_int SACstrspn(string str, string accept) +{ + return (sac_int) strspn(str, accept); +} + +sac_int SACstrstr (string haystack, string needle) +{ + return (sac_int) strstr (haystack, needle); +} + +bool chr_in_delims (char c, string delimiters) +{ + while (true) + { + if (delimiters[0] == '\0') + return false; + if (delimiters[0] == c) + return true; + delimiters ++; + } +} + +string next_in_delims (string str, string delimiters, bool is_delimiter) +{ + while (true) + { + if (str[0] == '\0') + return NULL; + if (chr_in_delims(str[0], delimiters) == is_delimiter) + return str; + str ++; + } +} + +void SACstrtok (string* out_token, string* out_rest, string str, string delimiters) +{ + string start = next_in_delims(str, delimiters, false); + string end = next_in_delims(start, delimiters, true); + + size_t tokenlength = end - start; + *out_token = malloc((tokenlength + 1) * sizeof (char)); + strncpy(*out_token, start, tokenlength); + *out_token[tokenlength] = '\0'; + + *out_rest = malloc((strlend(end) + 1) * sizeof (char)); + strcpy(*out_rest, end); +} + +void SACchomp (string str) +{ + string end = str + strlen(str); + + while (end > str) + { + end --; + if (*end == '\n' || *end == '\r') + *end = '\0'; + else + break; + } +} + +void SACrtrim (string str) +{ + string end = str + strlen(str); + + while (end > str) + { + end --; + if (isspace(*end)) + *end = '\0'; + else + break; + } +} + +string SACltrim (string str) +{ + string end = str + strlen(str); + + while (str < end) + { + if (!isspace(*end)) + *end = '\0'; + else + break; + str ++; + } + + string res = malloc ((strlen(str) + 1) * sizeof (char)); + strcpy(res, str); + + return res; +} + +string SACtrim (string str) +{ + string res = SACltrim(str); + SACrtrim (res); + return res; +} + +sac_int SACstrtoi (string* remain, string input, sac_int base) +{ + string rem; + sac_int res = (sac_int) strtol (input, &rem, base); + + *remain = malloc ((strlen(rem) + 1) * sizeof (char)); + strcpy (*remain, rem); + + return res; +} + + +float SACstrtof (string* remain, string input) +{ + string rem; + float res = (float) strtod (input, &rem); + + *remain = malloc ((strlen(rem) + 1) * sizeof (char)); + strcpy (*remain, rem); + + return res; +} + +double SACstrtod (string* remain, string input) +{ + string rem; + double res = strtod (input, &rem); + + *remain = malloc ((strlen(rem) + 1) * sizeof (char)); + strcpy (*remain, rem); + + return res; +} + +sac_int SACtoi (string input) +{ + return (sac_int) strtol (input, NULL, 0); +} + +float SACtof (string input) +{ + return (float) strtod (input, NULL); +} + +double SACtod (string input) +{ + return strtod (input, NULL); +} + +string SACitos (sac_int n) +{ + string res = malloc(40); + + sprintf (res, "%"PRIisac, n); + + return res; +} + +string SACftos (float n) +{ + string res = malloc(40); + + sprintf (res, "%g", n); + + return res; +} + +string SACdtos (double n) +{ + string res = malloc(40); + + sprintf (res, "%g", n); + + return res; +} + +string SACbtos (bool n) +{ + string res = malloc(6 * sizeof(char)); + + if (n) + strcpy (res, "true"); + else + strcpy (res, "false"); + + return res; +} \ No newline at end of file diff --git a/src/structures/src/String/str.h b/src/structures/src/String/str.h new file mode 100644 index 00000000..7ba7f757 --- /dev/null +++ b/src/structures/src/String/str.h @@ -0,0 +1,50 @@ +#ifndef STDLIB__STR__H +#define STDLIB__STR__H + +#include "sacinterface.h" + +typedef char* string; + +string SACtostring (string arr, sac_int length); +string SACautotostring (SACarg *sarr); +void SACstrmod (string str, sac_int pos, char c); +string SACstrins (string outer, sac_int pos, string inner); +void SACstrovwt (string outer, sac_int pos, string inner); +char SACstrsel (string str, sac_int pos); +string SACstrcat (string fst, string snd); +string SACstrncat (string fst, string snd, sac_int n); +sac_int SACstrcmp (string fst, string snd); +sac_int SACstrncmp (string fst, string snd, sac_int n); +sac_int SACstrcasecmp (string fst, string snd); +sac_int SACstrncasecmp (string fst, string snd, sac_int n); +sac_int SACstrlen (string fst); +char SACstrtake(string str, sac_int pos); +string SACstrdrop (string str, sac_int pos); +string SACstrext (string str, sac_int pos, sac_int len); +string SACsprintf (string format, ...); +sac_int SACsscanf( string s, string format, ...); +string SACsscanf_str (string s, string format); +sac_int SACstrchr (string str, char c); +sac_int SACstrrchr (string str, char c); +sac_int SACstrcspn(string str, string reject); +sac_int SACstrspn(string str, string accept); +sac_int SACstrstr (string haystack, string needle); +bool chr_in_delims (char c, string delimiters); +string next_in_delims (string str, string delimiters, bool is_delimiter); +void SACstrtok (string* out_token, string* out_rest, string str, string delimiters); +void SACchomp (string str) ; +void SACrtrim (string str) ; +string SACltrim (string str) ; +string SACtrim (string str); +sac_int SACstrtoi (string* remain, string input, sac_int base); +float SACstrtof (string* remain, string input); +double SACstrtod (string* remain, string input); +sac_int SACtoi (string input); +float SACtof (string input); +double SACtod (string input); +string SACitos (sac_int n); +string SACftos (float n); +string SACdtos (double n); +string SACbtos (bool n); + +#endif // STDLIB__TOSTRING__H \ No newline at end of file diff --git a/src/structures/src/String/strcasecmp.c b/src/structures/src/String/strcasecmp.c deleted file mode 100644 index bf7a01aa..00000000 --- a/src/structures/src/String/strcasecmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrcasecmp( string first, string second) -{ - int res; - - res = strcasecmp( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strcat.c b/src/structures/src/String/strcat.c deleted file mode 100644 index 05d89f4d..00000000 --- a/src/structures/src/String/strcat.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACstrcat( string first, string second) -{ - size_t len1; - size_t len2; - string new; - - len1 = strlen( first); - len2 = strlen( second); - new = (string) SAC_MALLOC( len1 + len2 + 1); - - strcpy( new, first); - strcpy( new + len1, second); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strchr.c b/src/structures/src/String/strchr.c deleted file mode 100644 index 32de4f93..00000000 --- a/src/structures/src/String/strchr.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrchr( string str, char c) -{ - char *occur; - - occur = strchr( str, c); - - if (occur == NULL) { - return( -1); - } - else { - return( occur - str); - } -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strcmp.c b/src/structures/src/String/strcmp.c deleted file mode 100644 index 9598e989..00000000 --- a/src/structures/src/String/strcmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrcmp( string first, string second) -{ - int res; - - res = strcmp( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strcspn.c b/src/structures/src/String/strcspn.c deleted file mode 100644 index bcc7ab07..00000000 --- a/src/structures/src/String/strcspn.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrcspn( string first, string second) -{ - int res; - - res = strcspn( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strdrop.c b/src/structures/src/String/strdrop.c deleted file mode 100644 index aed250ed..00000000 --- a/src/structures/src/String/strdrop.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string strdrop( string old, int n) -{ - string new; - - RANGECHECK( (size_t) n, 0, strlen( old), old); - - new = (string) SAC_MALLOC( strlen( old) - n + 1); - strcpy( new, old + n); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strext.c b/src/structures/src/String/strext.c deleted file mode 100644 index 92681ad1..00000000 --- a/src/structures/src/String/strext.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string strext( string old, int first, int len) -{ - string new; - - RANGECHECK( (size_t) first, 0, strlen( old) - 1, old); - - if (len <= 0) { - new = (string) SAC_MALLOC( 1); - new[ 0] = 0; - } - else { - new = (string) SAC_MALLOC( len + 1); - strncpy( new, old + first, len); - - if ((size_t) (first + len) <= strlen( old)) { - new[ len] = 0; - } - else { - new[ strlen( old) - first] = 0; - } - } - - return(new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strins.c b/src/structures/src/String/strins.c deleted file mode 100644 index 7070afca..00000000 --- a/src/structures/src/String/strins.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string strins( string old, int pos, string insert) -{ - string new; - - RANGECHECK( (size_t) pos, 0, strlen( old), old); - - new = (string) SAC_MALLOC( strlen( old) + strlen( insert) + 1); - - strncpy( new, old, pos); - new[pos] = 0; - - strcat( new, insert); - strcat( new, old + pos); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strlen.c b/src/structures/src/String/strlen.c deleted file mode 100644 index bc24f338..00000000 --- a/src/structures/src/String/strlen.c +++ /dev/null @@ -1,16 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrlen(string s) -{ - return( strlen( s)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strmod.c b/src/structures/src/String/strmod.c deleted file mode 100644 index 8b9f6c8d..00000000 --- a/src/structures/src/String/strmod.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define old_nt (old, T_OLD((SCL, (HID, (NUQ,))))) - -void strmod( SAC_ND_PARAM_out( new_nt, string), - SAC_ND_PARAM_in( old_nt, string), - int pos, char c) -{ - SAC_ND_DECL__DATA( new_nt, string, ) - SAC_ND_DECL__DESC( new_nt, ) - - RANGECHECK( (size_t)pos, 0, strlen( SAC_ND_A_FIELD( old_nt)) - 1, - SAC_ND_A_FIELD( old_nt)); - - if (SAC_ND_A_RC( old_nt) == 1) { - SAC_ND_A_DESC( new_nt) = SAC_ND_A_DESC( old_nt); - SAC_ND_A_FIELD( new_nt) = SAC_ND_A_FIELD( old_nt); - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - STRDUP( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt)); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - SAC_ND_A_FIELD( new_nt)[pos] = c; - - SAC_ND_RET_out( new_nt, new_nt) -} - -#undef new_nt -#undef old_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/strncasecmp.c b/src/structures/src/String/strncasecmp.c deleted file mode 100644 index 38db2151..00000000 --- a/src/structures/src/String/strncasecmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrncasecmp( string first, string second, int n) -{ - int res; - - res = strncasecmp( first, second, n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strncat.c b/src/structures/src/String/strncat.c deleted file mode 100644 index a5b6c31e..00000000 --- a/src/structures/src/String/strncat.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACstrncat( string first, string second, int n) -{ - string new; - - new = (string) SAC_MALLOC( strlen( first) + n + 1); - - strcpy( new, first); - strncat( new, second, n); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strncmp.c b/src/structures/src/String/strncmp.c deleted file mode 100644 index 58783d2b..00000000 --- a/src/structures/src/String/strncmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrncmp( string first, string second, int n) -{ - int res; - - res = strncmp( first, second, n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strovwt.c b/src/structures/src/String/strovwt.c deleted file mode 100644 index b86bae2b..00000000 --- a/src/structures/src/String/strovwt.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define old_nt (old, T_OLD((SCL, (HID, (NUQ,))))) - -void strovwt( SAC_ND_PARAM_out( new_nt, string), - SAC_ND_PARAM_in( old_nt, string), - int pos, string insert) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, string, ) - int len_old = strlen( SAC_ND_A_FIELD( old_nt)); - int len_insert = strlen( insert); - int len_insert_pos = len_insert+pos; - char store; - - RANGECHECK( (size_t)pos, 0, strlen( SAC_ND_A_FIELD( old_nt)), - SAC_ND_A_FIELD( old_nt)); - - if (len_insert_pos <= len_old) { - store = SAC_ND_A_FIELD( old_nt)[len_insert_pos]; - - if (SAC_ND_A_RC( old_nt) == 1) { - SAC_ND_A_DESC( new_nt) = SAC_ND_A_DESC( old_nt); - SAC_ND_A_FIELD( new_nt) = SAC_ND_A_FIELD( old_nt); - strcpy( SAC_ND_A_FIELD( new_nt) + pos, insert); - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - STRDUP( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt)); - strcpy( SAC_ND_A_FIELD( new_nt) + pos, insert); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - SAC_ND_A_FIELD( new_nt)[len_insert_pos] = store; - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - SAC_ND_A_FIELD( new_nt) = (string) SAC_MALLOC( len_insert_pos + 1); - strncpy( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt), pos); - SAC_ND_A_FIELD( new_nt)[pos] = '\0'; - - strcat( SAC_ND_A_FIELD( new_nt), insert); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - - SAC_ND_RET_out( new_nt, new_nt) -} - -#undef new_nt -#undef old_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/strrchr.c b/src/structures/src/String/strrchr.c deleted file mode 100644 index 903607e3..00000000 --- a/src/structures/src/String/strrchr.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrrchr( string str, char c) -{ - char *occur; - - occur = strrchr( str, c); - - if (occur == NULL) { - return( -1); - } - else { - return( occur-str); - } -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strsel.c b/src/structures/src/String/strsel.c deleted file mode 100644 index 82233e9e..00000000 --- a/src/structures/src/String/strsel.c +++ /dev/null @@ -1,18 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -char strsel( string s, int pos) -{ - RANGECHECK( (size_t) pos, 0, strlen( s) - 1, s); - - return( s[pos]); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strspn.c b/src/structures/src/String/strspn.c deleted file mode 100644 index 95dbf4fd..00000000 --- a/src/structures/src/String/strspn.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrspn( string first, string second) -{ - int res; - - res = strspn( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strstr.c b/src/structures/src/String/strstr.c deleted file mode 100644 index 59ef530b..00000000 --- a/src/structures/src/String/strstr.c +++ /dev/null @@ -1,17 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrstr( string haystack, string needle) -{ - string found = strstr( haystack, needle); - return (found) ? (found - haystack) : -1; -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtake.c b/src/structures/src/String/strtake.c deleted file mode 100644 index 297564d0..00000000 --- a/src/structures/src/String/strtake.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define old_nt (old, T_OLD((SCL, (HID, (NUQ,))))) - -void strtake( SAC_ND_PARAM_out( new_nt, string), - SAC_ND_PARAM_in( old_nt, string), - int n) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, string, ) - - RANGECHECK( (size_t)n, 0, strlen( SAC_ND_A_FIELD( old_nt)), SAC_ND_A_FIELD( old_nt)); - - if (SAC_ND_A_RC( old_nt) == 1) { - SAC_ND_A_DESC( new_nt) = SAC_ND_A_DESC( old_nt); - SAC_ND_A_FIELD( new_nt) = SAC_ND_A_FIELD( old_nt); - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - SAC_ND_A_FIELD( new_nt) = (string) SAC_MALLOC( n + 1); - strncpy( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt), n); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - SAC_ND_A_FIELD( new_nt)[n] = '\0'; - - SAC_ND_RET_out( new_nt, new_nt) -} - -#undef new_nt -#undef old_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/strtod.c b/src/structures/src/String/strtod.c deleted file mode 100644 index 140302e1..00000000 --- a/src/structures/src/String/strtod.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -double SACstrtod( string *remain, string input) -{ - double res; - char *rem; - - res = strtod( input, &rem); - - *remain = (string) SAC_MALLOC( strlen( rem) + 1); - strcpy( *remain, rem); - - return( res); -} - -/*****************************************************************/ - -double SACtod( string input) -{ - return( strtod( input, NULL)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtof.c b/src/structures/src/String/strtof.c deleted file mode 100644 index 723fad7f..00000000 --- a/src/structures/src/String/strtof.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -float SACstrtof( string *remain, string input) -{ - float res; - char *rem; - - res = (float) strtod( input, &rem); - - *remain = (string) SAC_MALLOC( strlen( rem) + 1); - strcpy( *remain, rem); - - return( res); -} - -/*****************************************************************/ - -float SACtof( string input) -{ - return( (float) strtod( input, NULL)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtoi.c b/src/structures/src/String/strtoi.c deleted file mode 100644 index 51239568..00000000 --- a/src/structures/src/String/strtoi.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrtoi( string *remain, string input, int base) -{ - int res; - char *rem; - - res = (int) strtol( input, &rem, base); - - *remain = (string) SAC_MALLOC( strlen( rem) + 1); - strcpy( *remain, rem); - - return( res); -} - -/*****************************************************************/ - -int SACtoi( string input) -{ - return( (int) strtol( input, NULL, 0)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtok.c b/src/structures/src/String/strtok.c deleted file mode 100644 index dd9fbbd8..00000000 --- a/src/structures/src/String/strtok.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACstrtok( string *remain, string input, string sep) -{ - int i, j, k; - string token; - - /* increment 'i' as long as input[i] is in "sep". */ - i = k = 0; - while (input[i] && sep[k]) { - k = 0; - while (sep[k] && input[i] != sep[k]) { - ++k; - } - if (sep[k]) { - ++i; - } - } - - /* increment 'j' as long as input[j] is not in "sep". */ - j = i; - k = 0; - while (input[j] && sep[k] != input[j]) { - k = 0; - while (sep[k] && input[j] != sep[k]) { - ++k; - } - if (!sep[k]) { - ++j; - } - } - - token = (string) SAC_MALLOC( j - i + 1); - strncpy(token, input + i, j - i); - token[j - i] = '\0'; - - *remain = (string) SAC_MALLOC( strlen( input + j) + 1); - strcpy( *remain, input + j); - - return( token); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/tostring.c b/src/structures/src/String/tostring.c deleted file mode 100644 index bacf8fd1..00000000 --- a/src/structures/src/String/tostring.c +++ /dev/null @@ -1,32 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - -/*****************************************************************/ - -#define str_nt (str, T_OLD((SCL, (HID, (NUQ,))))) -#define ar_nt (ar, T_OLD((AUD, (NHD, (NUQ,))))) - -void to_string( SAC_ND_PARAM_out( str_nt, string), - SAC_ND_PARAM_in( ar_nt, char), - int length) -{ - SAC_ND_DECL__DESC( str_nt, ) - SAC_ND_DECL__DATA( str_nt, string, ) - - SAC_ND_ALLOC__DESC( str_nt, 0) - SAC_ND_SET__RC( str_nt, 1) - SAC_ND_A_FIELD( str_nt) = (string) SAC_MALLOC( length + 1); - strncpy( SAC_ND_A_FIELD( str_nt), SAC_ND_A_FIELD( ar_nt), length); - SAC_ND_A_FIELD( str_nt)[length] = '\0'; - - SAC_ND_RET_out( str_nt, str_nt) -} - -#undef str_nt -#undef ar_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/trim.c b/src/structures/src/String/trim.c deleted file mode 100644 index bca1aa1c..00000000 --- a/src/structures/src/String/trim.c +++ /dev/null @@ -1,105 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include -#include "StringC.h" - - -/*****************************************************************/ - -string SACchomp(string in) -{ - size_t len = strlen(in); - size_t i; - string out; - - for (i = len; i > 0; ) { - --i; - if (in[i] != '\r' && in[i] != '\n') { - ++i; - break; - } - } - - out = (string) SAC_MALLOC( i + 1); - strncpy(out, in, i); - out[i] = '\0'; - - return out; -} - -/*****************************************************************/ - -string SACrtrim(string in) -{ - size_t len = strlen(in); - size_t i; - string out; - - for (i = len; i > 0; ) { - --i; - if (!isspace((unsigned char) in[i])) { - ++i; - break; - } - } - - out = (string) SAC_MALLOC( i + 1); - strncpy(out, in, i); - out[i] = '\0'; - - return out; -} - -/*****************************************************************/ - -string SACltrim(string in) -{ - size_t len = strlen(in); - size_t i; - string out; - - for (i = 0; i < len; ++i) { - if (!isspace((unsigned char) in[i])) { - break; - } - } - - out = (string) SAC_MALLOC( len - i + 1); - strncpy(out, in + i, len - i); - out[len - i] = '\0'; - - return out; -} - -/*****************************************************************/ - -string SACtrim(string in) -{ - size_t len = strlen(in); - size_t i, j; - string out; - - for (i = len; i > 0; ) { - --i; - if (!isspace((unsigned char) in[i])) { - ++i; - break; - } - } - for (j = 0; j < i; ++j) { - if (!isspace((unsigned char) in[j])) { - break; - } - } - - out = (string) SAC_MALLOC( i - j + 1); - strncpy(out, in + j, i - j); - out[i - j] = '\0'; - - return out; -} - -/*****************************************************************/ From 319bf0b408e332d4e826971e1bb3dcc099a8ddc3 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 13:53:54 +0100 Subject: [PATCH 02/19] added ctype --- src/structures/String.sac | 1 + 1 file changed, 1 insertion(+) diff --git a/src/structures/String.sac b/src/structures/String.sac index 5b4c2b05..cbb49f97 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -15,6 +15,7 @@ external typedef string; #pragma linkobj "src/String/str.o" #pragma copyfun "copy_string" #pragma linkobj "src/String/str.o" + #pragma ctype "char*" external string to_string(char[.] A, int LENGTH); #pragma linkname "SACtostring" From 6027c053609a28b251b26405176f5c8876cec6a8 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 16:09:12 +0100 Subject: [PATCH 03/19] Fix cmakelists --- src/CMakeLists.txt | 40 +---- src/structures/String.sac | 167 +++++++++--------- src/structures/src/String/{str.c => String.c} | 54 +++--- src/structures/src/String/{str.h => String.h} | 15 +- 4 files changed, 124 insertions(+), 152 deletions(-) rename src/structures/src/String/{str.c => String.c} (82%) rename src/structures/src/String/{str.h => String.h} (80%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 71787321..f46933bb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -195,41 +195,7 @@ SET (C_DEPS_SRC structures/src/StringArray/dim.c structures/src/StringArray/copy.c structures/src/StringArray/alloc.c - structures/src/String/trim.c - structures/src/String/tostring.c - structures/src/String/strtok.c - structures/src/String/strtoi.c - structures/src/String/strtof.c - structures/src/String/strtod.c - structures/src/String/strtake.c - structures/src/String/strstr.c - structures/src/String/strsel.c - structures/src/String/strrchr.c - structures/src/String/strovwt.c - structures/src/String/strncat.c - structures/src/String/strmod.c - structures/src/String/strlen.c - structures/src/String/strins.c - structures/src/String/strext.c - structures/src/String/strdrop.c - structures/src/String/strcmp.c - structures/src/String/strncmp.c - structures/src/String/strcasecmp.c - structures/src/String/strncasecmp.c - structures/src/String/strchr.c - structures/src/String/strcat.c - structures/src/String/sscanf.c - structures/src/String/sscanfstr.c - structures/src/String/strcspn.c - structures/src/String/strspn.c - structures/src/String/sprintf.c - structures/src/String/itos.c - structures/src/String/ftos.c - structures/src/String/dtos.c - structures/src/String/ctos.c - structures/src/String/copystr.c - structures/src/String/freestr.c - structures/src/String/btos.c + structures/src/String/String.c structures/src/List/tl.c structures/src/List/take.c structures/src/List/nth.c @@ -253,7 +219,6 @@ SET (C_DEPS_SRC structures/src/Constants/epidouble.c structures/src/Constants/randmax.c structures/src/Char/ctype.c - structures/src/Char/Char.c stdio/src/TermFile/stdstreams.c stdio/src/TermFile/printf.c stdio/src/TermFile/fputc.c @@ -435,6 +400,7 @@ FOREACH (name ${SAC_SRC}) # of the call to sac2c. GET_FILENAME_COMPONENT (dir "${CMAKE_CURRENT_BINARY_DIR}/${name}" DIRECTORY) GET_FILENAME_COMPONENT (dst ${name} NAME_WE) + GET_FILENAME_COMPONENT (src_dir "${src}" DIRECTORY) SET (mod "${DLL_BUILD_DIR}/${TARGET_ENV}/${SBI}/lib${dst}Mod${VARIANT}${MODEXT}") SET (tree @@ -473,7 +439,7 @@ FOREACH (name ${SAC_SRC}) ADD_CUSTOM_COMMAND ( OUTPUT "${mod}" "${tree}" COMMAND - ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -o ${DLL_BUILD_DIR} "${src}" + ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -Xc -I"${src_dir}" -o ${DLL_BUILD_DIR} "${src}" WORKING_DIRECTORY "${dir}" MAIN_DEPENDENCY "${src}" diff --git a/src/structures/String.sac b/src/structures/String.sac index cbb49f97..d09bd7ef 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -12,129 +12,130 @@ export all except { indent }; external typedef string; #pragma freefun "free_string" - #pragma linkobj "src/String/str.o" + #pragma linkobj "src/String/String.o" #pragma copyfun "copy_string" - #pragma linkobj "src/String/str.o" + #pragma linkobj "src/String/String.o" #pragma ctype "char*" external string to_string(char[.] A, int LENGTH); #pragma linkname "SACtostring" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert character array to string. */ external string to_string(char[.] A); #pragma linkname "SACautotostring" #pragma sacarg [1] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert character array to string. */ external string strmod(string str, int P, char C); #pragma linkname "SACstrmod" #pragma linksign [1,1,2,3] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Change position P of string str to character C. */ external string strins(string S1, int P, string S2); #pragma linkname "SACstrins" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Insert string S2 into string S1 starting at position P. */ external string strovwt(string S1, int P, string S2); #pragma linkname "SACstrovwt" #pragma linksign [1,1,2,3] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Overwrite string S2 with string S1 starting at position P. */ external char strsel(string str, int P); #pragma linkname "SACstrsel" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Select character P of string str. */ external string strcat(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Concatenate strings S1 and S2. */ external string +(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Infix version of strcat. */ external string strncat(string S1, string S2, int N); #pragma linkname "SACstrncat" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Concatenate string S1 and the first N characters of string S2. */ external int strcmp(string S1, string S2); #pragma linkname "SACstrcmp" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Compare strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strncmp(string S1, string S2, int N); #pragma linkname "SACstrncmp" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Compare the first N characters of strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strcasecmp(string S1, string S2); #pragma linkname "SACstrcasecmp" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Compare strings S1 and S2 while ignoring upper/lower case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strncasecmp(string S1, string S2, int N); #pragma linkname "SACstrncasecmp" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Compare the first N characters of strings S1 and S2 ignoring case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strlen(string str); #pragma linkname "SACstrlen" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Return the length of string str. */ external string strtake(string str, int N); #pragma linkname "SACstrtake" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linksign [1, 1, 2] + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Return the first N characters of string str. */ external string strdrop(string str, int N); #pragma linkname "SACstrdrop" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Return all but the first N characters of string str. */ external string strext(string str, int FIRST, int LEN); #pragma linkname "SACstrext" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Extract the substring beginning at character FIRST with length LEN from string str. */ external string sprintf(string FORMAT, ...); #pragma linkname "SACsprintf" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Generate a string depending on the format string and a variable number of arguments similar to the printf and fprintf functions. */ @@ -142,8 +143,8 @@ external string sprintf(string FORMAT, ...); external int, ... sscanf(string str, string FORMAT); #pragma linkname "SACsscanf" #pragma linksign [0,1,2] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Scan the string str concerning the format string FORMAT. The format string is the same as for fscanf. The int result gives the maximum number of successfully performed @@ -154,55 +155,55 @@ external int, ... sscanf(string str, string FORMAT); external string sscanf_str(string str, string FORMAT); #pragma linkname "SACsscanf_str" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Scan the string str concerning the format string FORMAT. FORMAT must have exactly one string conversion specifier. The scanned substring is returned. */ external int strchr(string str, char C); #pragma linkname "SACstrchr" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Return the position of the first occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strrchr(string str, char C); #pragma linkname "SACstrrchr" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Return the position of the last occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strcspn(string str, string REJECT); #pragma linkname "SACstrcspn" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Return the length of the initial segment of str which consists entirely of characters not in REJECT. */ external int strspn(string str, string ACCEPT); #pragma linkname "SACstrspn" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Return the length of the initial segment of str which consists entirely of characters in ACCEPT. */ external int strstr(string HAYSTACK, string NEEDLE); #pragma linkname "SACstrstr" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Find the first occurrence of the string NEEDLE in the string HAYSTACK. Return the index to this occurrence. If NEEDLE could not be found then return -1. */ external string, string strtok(string str, string SEP); #pragma linkname "SACstrtok" - #linksign [1, 2, 3, 4] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linksign [1, 2, 3, 4] + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Split string str into a token and the remainder string using the characters from SEP as delimiters. If str starts with characters from SEP then these are skipped until @@ -211,34 +212,34 @@ external string, string strtok(string str, string SEP); external string chomp(string str); #pragma linkname "SACchomp" #pragma linksign [1,1] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Remove trailing line terminators from str. */ external string rtrim(string str); #pragma linkname "SACrtrim" #pragma linksign [1,1] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Remove trailing whitespace from str. */ external string ltrim(string str); #pragma linkname "SACltrim" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Remove leading whitespace from str. */ external string trim(string str); #pragma linkname "SACtrim" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Remove leading and trailing whitespace from str. */ external int, string strtoi(string str, int BASE); #pragma linkname "SACstrtoi" #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert the string str into an integer value. BASE specifies the format and must be in the range from 2 to 32. This feature allows for scanning hexadecimal or octal numbers as well as decimal @@ -248,60 +249,60 @@ external int, string strtoi(string str, int BASE); external float, string strtof(string str); #pragma linkname "SACstrtof" #pragma linksign [0,1,2] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert the string str into a float value. The string result contains the remainder of the input string. */ external double, string strtod(string str); #pragma linkname "SACstrtod" #pragma linksign [0,1,2] - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert the string str into a double value. The string result contains the remainder of the input string. */ external int toi(string str); #pragma linkname "SACtoi" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert the string str into an integer value. In contrast to strtoi the number must always be in decimal representation. */ external float tof(string str); #pragma linkname "SACtof" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert the string str into a float value. */ external double tod(string str); #pragma linkname "SACtod" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert the string str into a double value. */ external string tos(int N); #pragma linkname "SACitos" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert integer into string. */ external string tos(float N); #pragma linkname "SACftos" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert float into string. */ external string tos(double N); #pragma linkname "SACdtos" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert double into string. */ external string tos(bool B); #pragma linkname "SACbtos" - #pragma linkobj "src/String/str.o" - #pragma header "src/String/str.h" + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.h" /* Convert boolean into string. */ inline string indent(int indent, string str) diff --git a/src/structures/src/String/str.c b/src/structures/src/String/String.c similarity index 82% rename from src/structures/src/String/str.c rename to src/structures/src/String/String.c index 5528709f..93c4c4e3 100644 --- a/src/structures/src/String/str.c +++ b/src/structures/src/String/String.c @@ -8,7 +8,9 @@ #include #include #include -#include "str.h" +#include +#include +#include "String.h" string copy_string (string s) { @@ -25,11 +27,11 @@ void free_string (string s) -string SACtostring (string arr, sac_int length) +string SACtostring (unsigned char* arr, sac_int length) { string res = malloc ((size_t) length + 1); - strncpy (res, arr, (size_t)length); + memcpy (res, arr, (size_t)length); res[length] = '\0'; return res; @@ -37,7 +39,7 @@ string SACtostring (string arr, sac_int length) string SACautotostring (SACarg *sarr) { - string *parr = SACARGgetSharedData(SACTYPE__MAIN__char, sarr); + const string *parr = SACARGgetSharedData(SACTYPE__MAIN__char, sarr); string arr = parr[0]; sac_int length = SACARGgetShape(sarr, 0); @@ -49,10 +51,12 @@ string SACautotostring (SACarg *sarr) return res; } -void SACstrmod (string str, sac_int pos, char c) +void SACstrmod (string str, sac_int pos, unsigned char c) { - if (strlen(str) <= pos) - ; // TODO runtime error + size_t strlength = strlen (str); + + if (strlength <= pos) + SAC_RuntimeError("strmod: pos ("PRIisac") outside of string (length %zu)", pos, strlength); str[pos] = c; } @@ -77,16 +81,16 @@ void SACstrovwt (string outer, sac_int pos, string inner) size_t inner_length = strlen (inner); if (pos+inner_length > outer_length) - ; // TODO runtime error + SAC_RuntimeError("strovwt: Overwriting string ends at position "PRIisac" while string is of length %zu", pos+inner_length, outer_length); - strncpy(outer+pos, inner, inner_length); + memcpy(outer+pos, inner, inner_length * sizeof (char)); } -char SACstrsel (string str, sac_int pos) +unsigned char SACstrsel (string str, sac_int pos) { int strlength = strlen(str); if (pos >= strlength) - ; // TODO runtime error + SAC_RuntimeError("strsel: pos ("PRIisac") outside of string (length %zu)", pos, strlength); return str[pos]; } @@ -94,7 +98,7 @@ char SACstrsel (string str, sac_int pos) string SACstrcat (string fst, string snd) { size_t fstlength = strlen (fst); - size_t sndlength = sndlen (fst); + size_t sndlength = strlen (snd); string res = malloc (fstlength + sndlength + 1); @@ -107,12 +111,12 @@ string SACstrcat (string fst, string snd) string SACstrncat (string fst, string snd, sac_int n) { size_t fstlength = strlen (fst); - size_t sndlength = sndlen (fst); - string res = malloc (fstlength + sndlength + 1); + string res = malloc (fstlength + n + 1); strcpy(res, fst); strncpy(res+fstlength, snd, n); + res[fstlength+n] = '\n'; return res; } @@ -142,14 +146,14 @@ sac_int SACstrlen (string fst) return (sac_int) strlen (fst); } -char SACstrtake(string str, sac_int pos) +void SACstrtake(string str, sac_int pos) { size_t strlength = strlen (str); if (pos >= strlength) - ; // TODO runtime error - - return str[pos]; + SAC_RuntimeError("strtake: pos ("PRIisac") outside of string (length %zu)", pos, strlength); + + str[pos] = '\0'; } string SACstrdrop (string str, sac_int pos) @@ -169,8 +173,8 @@ string SACstrext (string str, sac_int pos, sac_int len) string res = malloc(strlength - (size_t)len + 1); - if (pos + strlength >= len) - ; // TODO runtime error + if (pos + len >= strlength) + SAC_RuntimeError("strext: Selecting substring ends at position "PRIisac" while string is of length %zu", pos+len, strlength); strncpy(res, str+pos, len); @@ -187,7 +191,7 @@ string SACsprintf (string format, ...) va_end (args); if (lengthwo0 < 0) - ; // TODO runtime error + SAC_RuntimeError("printf error %d", lengthwo0); size_t length = lengthwo0 + 1; @@ -223,7 +227,7 @@ string SACsscanf_str (string source, string format) return res; } -sac_int SACstrchr (string str, char c) +sac_int SACstrchr (string str, unsigned char c) { string occurrence = strchr (str, c); @@ -232,7 +236,7 @@ sac_int SACstrchr (string str, char c) return (sac_int)(occurrence - str); } -sac_int SACstrrchr (string str, char c) +sac_int SACstrrchr (string str, unsigned char c) { string occurrence = strrchr (str, c); @@ -256,7 +260,7 @@ sac_int SACstrstr (string haystack, string needle) return (sac_int) strstr (haystack, needle); } -bool chr_in_delims (char c, string delimiters) +bool chr_in_delims (unsigned char c, string delimiters) { while (true) { @@ -290,7 +294,7 @@ void SACstrtok (string* out_token, string* out_rest, string str, string delimite strncpy(*out_token, start, tokenlength); *out_token[tokenlength] = '\0'; - *out_rest = malloc((strlend(end) + 1) * sizeof (char)); + *out_rest = malloc((strlen(end) + 1) * sizeof (char)); strcpy(*out_rest, end); } diff --git a/src/structures/src/String/str.h b/src/structures/src/String/String.h similarity index 80% rename from src/structures/src/String/str.h rename to src/structures/src/String/String.h index 7ba7f757..20c9290e 100644 --- a/src/structures/src/String/str.h +++ b/src/structures/src/String/String.h @@ -2,15 +2,16 @@ #define STDLIB__STR__H #include "sacinterface.h" +#include typedef char* string; -string SACtostring (string arr, sac_int length); +string SACtostring (unsigned char* arr, sac_int length); string SACautotostring (SACarg *sarr); -void SACstrmod (string str, sac_int pos, char c); +void SACstrmod (string str, sac_int pos, unsigned char c); string SACstrins (string outer, sac_int pos, string inner); void SACstrovwt (string outer, sac_int pos, string inner); -char SACstrsel (string str, sac_int pos); +unsigned char SACstrsel (string str, sac_int pos); string SACstrcat (string fst, string snd); string SACstrncat (string fst, string snd, sac_int n); sac_int SACstrcmp (string fst, string snd); @@ -18,18 +19,18 @@ sac_int SACstrncmp (string fst, string snd, sac_int n); sac_int SACstrcasecmp (string fst, string snd); sac_int SACstrncasecmp (string fst, string snd, sac_int n); sac_int SACstrlen (string fst); -char SACstrtake(string str, sac_int pos); +void SACstrtake(string str, sac_int pos); string SACstrdrop (string str, sac_int pos); string SACstrext (string str, sac_int pos, sac_int len); string SACsprintf (string format, ...); sac_int SACsscanf( string s, string format, ...); string SACsscanf_str (string s, string format); -sac_int SACstrchr (string str, char c); -sac_int SACstrrchr (string str, char c); +sac_int SACstrchr (string str, unsigned char c); +sac_int SACstrrchr (string str, unsigned char c); sac_int SACstrcspn(string str, string reject); sac_int SACstrspn(string str, string accept); sac_int SACstrstr (string haystack, string needle); -bool chr_in_delims (char c, string delimiters); +bool chr_in_delims (unsigned char c, string delimiters); string next_in_delims (string str, string delimiters, bool is_delimiter); void SACstrtok (string* out_token, string* out_rest, string str, string delimiters); void SACchomp (string str) ; From 3247e21aa80b7592cedf5236bcffc9af1506b234 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 17:05:21 +0100 Subject: [PATCH 04/19] Some magic to replace int format string specifiers with SaC int format string specifiers --- src/structures/String.sac | 2 +- src/structures/src/String/String.c | 123 ++++++++++++++++++++++------- 2 files changed, 97 insertions(+), 28 deletions(-) diff --git a/src/structures/String.sac b/src/structures/String.sac index d09bd7ef..88dcd37b 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -23,7 +23,7 @@ external string to_string(char[.] A, int LENGTH); #pragma header "src/String/String.h" /* Convert character array to string. */ -external string to_string(char[.] A); +external string to_auto_string(char[.] A); #pragma linkname "SACautotostring" #pragma sacarg [1] #pragma linkobj "src/String/String.o" diff --git a/src/structures/src/String/String.c b/src/structures/src/String/String.c index 93c4c4e3..6f98ed9a 100644 --- a/src/structures/src/String/String.c +++ b/src/structures/src/String/String.c @@ -181,8 +181,97 @@ string SACstrext (string str, sac_int pos, sac_int len) return res; } -string SACsprintf (string format, ...) +bool chr_in_delims (char c, string delimiters) { + while (true) + { + if (delimiters[0] == '\0') + return false; + if (delimiters[0] == c) + return true; + delimiters ++; + } +} + +string next_in_delims (string str, string delimiters, bool is_delimiter) +{ + while (true) + { + if (str[0] == '\0') + return NULL; + if (chr_in_delims(str[0], delimiters) == is_delimiter) + return str; + str ++; + } +} + +static char fmtstart = '%'; +static char fmtspecifiers[] = "di"; +static char fmtmodifiers[] = "+- #.*0123456789"; + +string find_fmtstr_spec_loc(string format) +{ + do + { + // Find % + format = strchr(fmtstart, format); + // Skip modifiers + format = next_in_delims(format, fmtmodifiers, false); + } + // Check if at end, or delimiter has been found + while (format != NULL && !chr_in_delims(*format, fmtspecifiers)); + return format; +} + +string fix_fmtstr_sac_int (string format) +{ + // Count the number of replaces that need to occur, to comput the format string new size + size_t replaces = 0; + string fmt = find_fmtstr_spec_loc(format); + while (fmt != NULL) + { + replaces ++; + fmt = find_fmtstr_spec_loc(fmt); + } + + // Compute the size of the new string, and allocat enough memory + // For each replace, we remove 1 character and add the length of PRIisac + size_t priisaclength = strlen(PRIisac); + size_t formatlength = strlen(format); + string res = malloc ((formatlength + replaces * (formatlength-1) + 1) * sizeof (char)); + + // Copy parts of strings into the result + string start = format; + string respos = res; + while (format != NULL) + { + // Find the next occurence of a format string specifier to be found + format = find_fmtstr_spec_loc(start); + if (format == NULL) + { + // If at end, simply copy over the rest of the string + strcpy(respos, start); + } + else + { + // If found, copy the part of the string until the specifier, including the % and modifiers + strncpy(respos, start, format-start); + respos += format-start; + // Set new start to just after the i or d character + start = format+1; + // Add the format string specifier for our SaC ints + strcpy(respos, PRIisac); + respos += priisaclength; + } + } + + return res; +} + +string SACsprintf (string format_raw, ...) +{ + string format = fix_fmtstr_sac_int(format_raw); + va_list args; // Compute the length of the string @@ -206,8 +295,10 @@ string SACsprintf (string format, ...) return res; } -sac_int SACsscanf (string s, string format, ...) +sac_int SACsscanf (string s, string format_raw, ...) { + string format = fix_fmtstr_sac_int(format_raw); + va_list arg_p; va_start( arg_p, format); @@ -217,8 +308,10 @@ sac_int SACsscanf (string s, string format, ...) return res; } -string SACsscanf_str (string source, string format) +string SACsscanf_str (string source, string format_raw) { + string format = fix_fmtstr_sac_int(format_raw); + string res = malloc (strlen (source) + 1); res[0] = '\0'; @@ -260,30 +353,6 @@ sac_int SACstrstr (string haystack, string needle) return (sac_int) strstr (haystack, needle); } -bool chr_in_delims (unsigned char c, string delimiters) -{ - while (true) - { - if (delimiters[0] == '\0') - return false; - if (delimiters[0] == c) - return true; - delimiters ++; - } -} - -string next_in_delims (string str, string delimiters, bool is_delimiter) -{ - while (true) - { - if (str[0] == '\0') - return NULL; - if (chr_in_delims(str[0], delimiters) == is_delimiter) - return str; - str ++; - } -} - void SACstrtok (string* out_token, string* out_rest, string str, string delimiters) { string start = next_in_delims(str, delimiters, false); From 7142554886692209a9772329914267a3caa30be0 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 17:09:25 +0100 Subject: [PATCH 05/19] Renamed Char.C to actually Char.c --- src/CMakeLists.txt | 2 +- src/structures/Char.sac | 30 ++++++++++----------- src/structures/src/Char/{ctype.c => Char.c} | 0 3 files changed, 16 insertions(+), 16 deletions(-) rename src/structures/src/Char/{ctype.c => Char.c} (100%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f46933bb..53bda6ba 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -218,7 +218,7 @@ SET (C_DEPS_SRC structures/src/Constants/maxdouble.c structures/src/Constants/epidouble.c structures/src/Constants/randmax.c - structures/src/Char/ctype.c + structures/src/Char/Char.c stdio/src/TermFile/stdstreams.c stdio/src/TermFile/printf.c stdio/src/TermFile/fputc.c diff --git a/src/structures/Char.sac b/src/structures/Char.sac index e222811e..8934dc63 100644 --- a/src/structures/Char.sac +++ b/src/structures/Char.sac @@ -14,57 +14,57 @@ export all; ******************************************************************************/ external bool isalpha(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisalpha" #pragma linksign [0,1] external bool isupper(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisupper" #pragma linksign [0,1] external bool islower(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACislower" #pragma linksign [0,1] external bool isdigit(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisdigit" #pragma linksign [0,1] external bool isxdigit(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisxdigit" #pragma linksign [0,1] external bool isspace(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisspace" #pragma linksign [0,1] external bool ispunct(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACispunct" #pragma linksign [0,1] external bool isalnum(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisalnum" #pragma linksign [0,1] external bool isprint(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisprint" #pragma linksign [0,1] external bool isgraph(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisgraph" #pragma linksign [0,1] external bool iscntrl(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACiscntrl" #pragma linksign [0,1] @@ -76,12 +76,12 @@ external bool iscntrl(char c); ******************************************************************************/ external bool isascii(int N); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisascii" #pragma linksign [0,1] external char toascii(int N); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtoascii" #pragma linksign [0,1] @@ -92,12 +92,12 @@ external char toascii(int N); ******************************************************************************/ external char tolower(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtolower" #pragma linksign [0,1] external char toupper(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtoupper" #pragma linksign [0,1] diff --git a/src/structures/src/Char/ctype.c b/src/structures/src/Char/Char.c similarity index 100% rename from src/structures/src/Char/ctype.c rename to src/structures/src/Char/Char.c From 8c0791f33bcf7dfd259605b575e69b11864ac896 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 17:14:03 +0100 Subject: [PATCH 06/19] it builds again :) --- src/structures/src/String/String.c | 8 ++++---- src/structures/src/String/String.h | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/structures/src/String/String.c b/src/structures/src/String/String.c index 6f98ed9a..fd74be5f 100644 --- a/src/structures/src/String/String.c +++ b/src/structures/src/String/String.c @@ -214,7 +214,7 @@ string find_fmtstr_spec_loc(string format) do { // Find % - format = strchr(fmtstart, format); + format = strchr(format, fmtstart); // Skip modifiers format = next_in_delims(format, fmtmodifiers, false); } @@ -275,7 +275,7 @@ string SACsprintf (string format_raw, ...) va_list args; // Compute the length of the string - va_start (args, format); + va_start (args, format_raw); int lengthwo0 = vsnprintf (NULL, 0, format, args); va_end (args); @@ -288,7 +288,7 @@ string SACsprintf (string format_raw, ...) string res = malloc(length); // Printf string - va_start (args, format); + va_start (args, format_raw); vsnprintf (res, length, format, args); va_end (args); @@ -301,7 +301,7 @@ sac_int SACsscanf (string s, string format_raw, ...) va_list arg_p; - va_start( arg_p, format); + va_start( arg_p, format_raw); sac_int res = (sac_int)vsscanf( s, format, arg_p); va_end( arg_p); diff --git a/src/structures/src/String/String.h b/src/structures/src/String/String.h index 20c9290e..ad33a94a 100644 --- a/src/structures/src/String/String.h +++ b/src/structures/src/String/String.h @@ -30,8 +30,6 @@ sac_int SACstrrchr (string str, unsigned char c); sac_int SACstrcspn(string str, string reject); sac_int SACstrspn(string str, string accept); sac_int SACstrstr (string haystack, string needle); -bool chr_in_delims (unsigned char c, string delimiters); -string next_in_delims (string str, string delimiters, bool is_delimiter); void SACstrtok (string* out_token, string* out_rest, string str, string delimiters); void SACchomp (string str) ; void SACrtrim (string str) ; From 44e0edad0a490b765535d58aa93c36b66496947c Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 17:20:20 +0100 Subject: [PATCH 07/19] fixed casts --- src/structures/src/String/String.c | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/structures/src/String/String.c b/src/structures/src/String/String.c index fd74be5f..4968b3b9 100644 --- a/src/structures/src/String/String.c +++ b/src/structures/src/String/String.c @@ -55,7 +55,7 @@ void SACstrmod (string str, sac_int pos, unsigned char c) { size_t strlength = strlen (str); - if (strlength <= pos) + if (strlength <= (size_t)pos) SAC_RuntimeError("strmod: pos ("PRIisac") outside of string (length %zu)", pos, strlength); str[pos] = c; @@ -68,9 +68,9 @@ string SACstrins (string outer, sac_int pos, string inner) string res = malloc(outer_length + inner_length + 1); - strncpy(res, outer, pos); - strcpy(res+pos, inner); - strcpy(res+pos+inner_length, outer+pos); + strncpy(res, outer, (size_t)pos); + strcpy(res+(size_t)pos, inner); + strcpy(res+(size_t)pos+inner_length, outer+(size_t)pos); return res; } @@ -80,16 +80,16 @@ void SACstrovwt (string outer, sac_int pos, string inner) size_t outer_length = strlen (outer); size_t inner_length = strlen (inner); - if (pos+inner_length > outer_length) + if ((size_t)pos+inner_length > outer_length) SAC_RuntimeError("strovwt: Overwriting string ends at position "PRIisac" while string is of length %zu", pos+inner_length, outer_length); - memcpy(outer+pos, inner, inner_length * sizeof (char)); + memcpy(outer+(size_t)pos, inner, inner_length * sizeof (char)); } unsigned char SACstrsel (string str, sac_int pos) { - int strlength = strlen(str); - if (pos >= strlength) + size_t strlength = strlen(str); + if ((size_t)pos >= strlength) SAC_RuntimeError("strsel: pos ("PRIisac") outside of string (length %zu)", pos, strlength); return str[pos]; @@ -112,11 +112,11 @@ string SACstrncat (string fst, string snd, sac_int n) { size_t fstlength = strlen (fst); - string res = malloc (fstlength + n + 1); + string res = malloc (fstlength + (size_t)n + 1); strcpy(res, fst); - strncpy(res+fstlength, snd, n); - res[fstlength+n] = '\n'; + strncpy(res+fstlength, snd, (size_t)n); + res[fstlength+(size_t)n] = '\n'; return res; } @@ -150,7 +150,7 @@ void SACstrtake(string str, sac_int pos) { size_t strlength = strlen (str); - if (pos >= strlength) + if ((size_t)pos >= strlength) SAC_RuntimeError("strtake: pos ("PRIisac") outside of string (length %zu)", pos, strlength); str[pos] = '\0'; @@ -162,7 +162,7 @@ string SACstrdrop (string str, sac_int pos) string res = malloc(strlength - (size_t)pos + 1); - strcpy(res, str+pos); + strcpy(res, str+(size_t)pos); return res; } @@ -173,10 +173,10 @@ string SACstrext (string str, sac_int pos, sac_int len) string res = malloc(strlength - (size_t)len + 1); - if (pos + len >= strlength) + if ((size_t)(pos + len) >= strlength) SAC_RuntimeError("strext: Selecting substring ends at position "PRIisac" while string is of length %zu", pos+len, strlength); - strncpy(res, str+pos, len); + strncpy(res, str+(size_t)pos, (size_t)len); return res; } @@ -424,7 +424,7 @@ string SACtrim (string str) sac_int SACstrtoi (string* remain, string input, sac_int base) { string rem; - sac_int res = (sac_int) strtol (input, &rem, base); + sac_int res = (sac_int) strtol (input, &rem, (size_t)base); *remain = malloc ((strlen(rem) + 1) * sizeof (char)); strcpy (*remain, rem); From 36483252933c95e18978ba69ed618e68ea7e5da4 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 17:34:54 +0100 Subject: [PATCH 08/19] Fix structures/char --- src/structures/Char.sac | 60 +++++++++++++++++----------------- src/structures/src/Char/Char.c | 2 +- src/structures/src/Char/Char.h | 36 ++++++++++++++++++++ 3 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 src/structures/src/Char/Char.h diff --git a/src/structures/Char.sac b/src/structures/Char.sac index 8934dc63..0346cedd 100644 --- a/src/structures/Char.sac +++ b/src/structures/Char.sac @@ -14,59 +14,59 @@ export all; ******************************************************************************/ external bool isalpha(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisalpha" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool isupper(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisupper" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool islower(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACislower" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool isdigit(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisdigit" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool isxdigit(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisxdigit" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool isspace(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisspace" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool ispunct(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACispunct" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool isalnum(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisalnum" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool isprint(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisprint" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool isgraph(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisgraph" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external bool iscntrl(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACiscntrl" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" /****************************************************************************** * @@ -76,14 +76,14 @@ external bool iscntrl(char c); ******************************************************************************/ external bool isascii(int N); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisascii" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external char toascii(int N); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtoascii" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" /****************************************************************************** * @@ -92,14 +92,14 @@ external char toascii(int N); ******************************************************************************/ external char tolower(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtolower" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" external char toupper(char c); - #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtoupper" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + #pragma header "src/Char/Char.h" /****************************************************************************** * diff --git a/src/structures/src/Char/Char.c b/src/structures/src/Char/Char.c index 301e5ae3..a1a05640 100644 --- a/src/structures/src/Char/Char.c +++ b/src/structures/src/Char/Char.c @@ -9,8 +9,8 @@ * They may return any non-zero value when true, but a SAC bool is true if 1. */ -#include #include +#include "Char.h" typedef unsigned char uchar; diff --git a/src/structures/src/Char/Char.h b/src/structures/src/Char/Char.h new file mode 100644 index 00000000..806c3445 --- /dev/null +++ b/src/structures/src/Char/Char.h @@ -0,0 +1,36 @@ +#ifndef STDLIB__CHAR__H +#define STDLIB__CHAR__H + +/* + * Implementation of standard module Char ctype functions. + * It is sort of unfortunate that we have to use these + * functions as they incur a per-character overhead. + * However, without them incorrect values were produced. + * There were two problems: + * The isxxx(.) functions work on unsigned chars and not on chars. + * They may segfault when not in the range [-1,255]. + * They may return any non-zero value when true, but a SAC bool is true if 1. + */ + +#include +#include + +typedef unsigned char uchar; + +int SACisalpha(uchar c); +int SACisupper(uchar c); +int SACislower(uchar c); +int SACisdigit(uchar c); +int SACisxdigit(uchar c); +int SACisspace(uchar c); +int SACispunct(uchar c); +int SACisalnum(uchar c); +int SACisprint(uchar c); +int SACisgraph(uchar c); +int SACiscntrl(uchar c); +int SACisascii(sac_int c); +int SACtoascii(sac_int c); +int SACtolower(uchar c); +int SACtoupper(uchar c); + +#endif // STDLIB__CHAR__H \ No newline at end of file From 212ab5d54d313e124691a29e76723ea4c9fd6258 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 18:08:45 +0100 Subject: [PATCH 09/19] Fixed Constants --- src/CMakeLists.txt | 11 +-- src/structures/Constants.sac | 108 +++++++++++----------- src/structures/src/Char/Char.c | 32 +++---- src/structures/src/Char/Char.h | 34 +++---- src/structures/src/Constants/Constants.c | 79 ++++++++++++++++ src/structures/src/Constants/Constants.h | 25 +++++ src/structures/src/Constants/epidouble.c | 6 -- src/structures/src/Constants/maxdouble.c | 7 -- src/structures/src/Constants/maxfloat.c | 7 -- src/structures/src/Constants/maxint.c | 15 --- src/structures/src/Constants/mindouble.c | 18 ---- src/structures/src/Constants/minfloat.c | 7 -- src/structures/src/Constants/minint.c | 8 -- src/structures/src/Constants/minmax.c | 28 ------ src/structures/src/Constants/minmax.mac | 9 ++ src/structures/src/Constants/tinydouble.c | 9 -- 16 files changed, 202 insertions(+), 201 deletions(-) create mode 100644 src/structures/src/Constants/Constants.c create mode 100644 src/structures/src/Constants/Constants.h delete mode 100644 src/structures/src/Constants/epidouble.c delete mode 100644 src/structures/src/Constants/maxdouble.c delete mode 100644 src/structures/src/Constants/maxfloat.c delete mode 100644 src/structures/src/Constants/maxint.c delete mode 100644 src/structures/src/Constants/mindouble.c delete mode 100644 src/structures/src/Constants/minfloat.c delete mode 100644 src/structures/src/Constants/minint.c delete mode 100644 src/structures/src/Constants/minmax.c create mode 100644 src/structures/src/Constants/minmax.mac delete mode 100644 src/structures/src/Constants/tinydouble.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 53bda6ba..594b40f8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -208,16 +208,7 @@ SET (C_DEPS_SRC structures/src/List/cons.c structures/src/List/append.c structures/src/Format/wordsize.c - structures/src/Constants/tinydouble.c - structures/src/Constants/minmax.c - structures/src/Constants/minint.c - structures/src/Constants/minfloat.c - structures/src/Constants/mindouble.c - structures/src/Constants/maxint.c - structures/src/Constants/maxfloat.c - structures/src/Constants/maxdouble.c - structures/src/Constants/epidouble.c - structures/src/Constants/randmax.c + structures/src/Constants/Constants.c structures/src/Char/Char.c stdio/src/TermFile/stdstreams.c stdio/src/TermFile/printf.c diff --git a/src/structures/Constants.sac b/src/structures/Constants.sac index 062f4263..4dfd3e7b 100644 --- a/src/structures/Constants.sac +++ b/src/structures/Constants.sac @@ -7,107 +7,111 @@ export all; #ifdef FULLTYPES external byte minbyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external byte maxbyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external short minshort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external short maxshort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external longlong minlonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external longlong maxlonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ubyte minubyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ubyte maxubyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ushort minushort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ushort maxushort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ulonglong minulonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ulonglong maxulonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" #endif /* FULLTYPES */ external int minint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minint.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external int maxint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/maxint.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external long minlong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external long maxlong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external uint minuint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external uint maxuint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ulong minulong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external ulong maxulong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external float minfloat(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minfloat.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external float maxfloat(); - #pragma linksign[0] - #pragma linkobj "src/Constants/maxfloat.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external double mindouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/mindouble.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external double tinydouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/tinydouble.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external double maxdouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/maxdouble.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" external double epidouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/epidouble.o" + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" + +external int randmax(); + #pragma linkobj "src/Constants/Constants.o" + #pragma header "src/Constants/Constants.h" \ No newline at end of file diff --git a/src/structures/src/Char/Char.c b/src/structures/src/Char/Char.c index a1a05640..23993cf4 100644 --- a/src/structures/src/Char/Char.c +++ b/src/structures/src/Char/Char.c @@ -12,79 +12,77 @@ #include #include "Char.h" -typedef unsigned char uchar; - -int SACisalpha(uchar c) +bool SACisalpha(uchar c) { return isalpha(c) != 0; } -int SACisupper(uchar c) +bool SACisupper(uchar c) { return isupper(c) != 0; } -int SACislower(uchar c) +bool SACislower(uchar c) { return islower(c) != 0; } -int SACisdigit(uchar c) +bool SACisdigit(uchar c) { return isdigit(c) != 0; } -int SACisxdigit(uchar c) +bool SACisxdigit(uchar c) { return isxdigit(c) != 0; } -int SACisspace(uchar c) +bool SACisspace(uchar c) { return isspace(c) != 0; } -int SACispunct(uchar c) +bool SACispunct(uchar c) { return ispunct(c) != 0; } -int SACisalnum(uchar c) +bool SACisalnum(uchar c) { return isalnum(c) != 0; } -int SACisprint(uchar c) +bool SACisprint(uchar c) { return isprint(c) != 0; } -int SACisgraph(uchar c) +bool SACisgraph(uchar c) { return isgraph(c) != 0; } -int SACiscntrl(uchar c) +bool SACiscntrl(uchar c) { return iscntrl(c) != 0; } -int SACisascii(sac_int c) +bool SACisascii(sac_int c) { return c >= 0 && c < 256 && isascii(c) != 0; } -int SACtoascii(sac_int c) +uchar SACtoascii(sac_int c) { return toascii(c & 0xFF); } -int SACtolower(uchar c) +uchar SACtolower(uchar c) { return tolower(c); } -int SACtoupper(uchar c) +uchar SACtoupper(uchar c) { return toupper(c); } diff --git a/src/structures/src/Char/Char.h b/src/structures/src/Char/Char.h index 806c3445..774bd085 100644 --- a/src/structures/src/Char/Char.h +++ b/src/structures/src/Char/Char.h @@ -12,25 +12,25 @@ * They may return any non-zero value when true, but a SAC bool is true if 1. */ -#include -#include +#include "sacinterface.h" +#include typedef unsigned char uchar; -int SACisalpha(uchar c); -int SACisupper(uchar c); -int SACislower(uchar c); -int SACisdigit(uchar c); -int SACisxdigit(uchar c); -int SACisspace(uchar c); -int SACispunct(uchar c); -int SACisalnum(uchar c); -int SACisprint(uchar c); -int SACisgraph(uchar c); -int SACiscntrl(uchar c); -int SACisascii(sac_int c); -int SACtoascii(sac_int c); -int SACtolower(uchar c); -int SACtoupper(uchar c); +bool SACisalpha(uchar c); +bool SACisupper(uchar c); +bool SACislower(uchar c); +bool SACisdigit(uchar c); +bool SACisxdigit(uchar c); +bool SACisspace(uchar c); +bool SACispunct(uchar c); +bool SACisalnum(uchar c); +bool SACisprint(uchar c); +bool SACisgraph(uchar c); +bool SACiscntrl(uchar c); +bool SACisascii(sac_int c); +uchar SACtoascii(sac_int c); +uchar SACtolower(uchar c); +uchar SACtoupper(uchar c); #endif // STDLIB__CHAR__H \ No newline at end of file diff --git a/src/structures/src/Constants/Constants.c b/src/structures/src/Constants/Constants.c new file mode 100644 index 00000000..92be8450 --- /dev/null +++ b/src/structures/src/Constants/Constants.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include "Constants.h" + +#define UCHAR_MIN 0 +#define USHRT_MIN 0 +#define UINT_MIN 0 +#define ULONG_MIN 0 +#define ULLONG_MIN 0 + +//---------- +#define minmax(rtype, SACtype, typeucase) \ +rtype max##SACtype( void ) \ +{ \ + return( typeucase##_MAX); \ +} \ + \ +rtype min##SACtype( void ) \ +{ \ + return( typeucase##_MIN); \ +} + +#include "minmax.mac" +#undef minmax +//---------- + +sac_int maxint( void) +{ + sac_int num_bits = 8 * sizeof(sac_int); + /** + * To avoid overflow: + * 2^(num_bits - 1) - 1 = 2 * 2^(num_bits - 2) - 1 = + * 2^(num_bits - 2) + (2^(num_bits - 2) - 1) + **/ + sac_int half = (sac_int)1 << (num_bits - 2); + return half + (half - 1); +} + +sac_int minint( void) +{ + sac_int num_bits = 8 * sizeof(sac_int); + return -((sac_int)1 << (num_bits - 1)); +} + +float minfloat( void) +{ + return( -FLT_MAX); /* do not use FLT_MIN here!!! */ +} + +float maxfloat( void) +{ + return( FLT_MAX); +} + +double mindouble( void) +{ + return( -DBL_MAX); +} + +double maxdouble( void) +{ + return( DBL_MAX); +} + +double tinydouble( void) +{ + return( DBL_MIN); +} + +double epidouble( void) +{ + return( DBL_EPSILON); +} + +sac_int randmax( void) +{ + return (sac_int)RAND_MAX; +} \ No newline at end of file diff --git a/src/structures/src/Constants/Constants.h b/src/structures/src/Constants/Constants.h new file mode 100644 index 00000000..bf6f1021 --- /dev/null +++ b/src/structures/src/Constants/Constants.h @@ -0,0 +1,25 @@ +#ifndef STDLIB__CONSTANTS__H +#define STDLIB__CONSTANTS__H + +#include "sacinterface.h" + +//---------- +#define minmax(rtype, SACtype, typeucase) \ +rtype max##SACtype( void ); \ +rtype min##SACtype( void ); + +#include "minmax.mac" +#undef minmax +//---------- + +sac_int maxint( void); +sac_int minint( void); +float minfloat( void); +float maxfloat( void); +double mindouble( void); +double maxdouble( void); +double tinydouble( void); +double epidouble( void); +sac_int randmax( void); + +#endif // STDLIB__CONSTANTS__H diff --git a/src/structures/src/Constants/epidouble.c b/src/structures/src/Constants/epidouble.c deleted file mode 100644 index b32593a3..00000000 --- a/src/structures/src/Constants/epidouble.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -double epidouble( void) -{ - return( DBL_EPSILON); -} diff --git a/src/structures/src/Constants/maxdouble.c b/src/structures/src/Constants/maxdouble.c deleted file mode 100644 index f70d7542..00000000 --- a/src/structures/src/Constants/maxdouble.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - - -double maxdouble( void) -{ - return( DBL_MAX); -} diff --git a/src/structures/src/Constants/maxfloat.c b/src/structures/src/Constants/maxfloat.c deleted file mode 100644 index b7689521..00000000 --- a/src/structures/src/Constants/maxfloat.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - - -float maxfloat( void) -{ - return( FLT_MAX); -} diff --git a/src/structures/src/Constants/maxint.c b/src/structures/src/Constants/maxint.c deleted file mode 100644 index f5080f5a..00000000 --- a/src/structures/src/Constants/maxint.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#include - -sac_int maxint( void) -{ - sac_int num_bits = 8 * sizeof(sac_int); - /** - * To avoid overflow: - * 2^(num_bits - 1) - 1 = 2 * 2^(num_bits - 2) - 1 = - * 2^(num_bits - 2) + (2^(num_bits - 2) - 1) - **/ - sac_int half = (sac_int)1 << (num_bits - 2); - return half + (half - 1); -} diff --git a/src/structures/src/Constants/mindouble.c b/src/structures/src/Constants/mindouble.c deleted file mode 100644 index 1027eeaf..00000000 --- a/src/structures/src/Constants/mindouble.c +++ /dev/null @@ -1,18 +0,0 @@ -#include - -/* !! This function does not return DBL_MIN !! - * The reason for this is that DBL_MIN is the smallest - * normalised value that can represented. For consistency - * with maxint() and minint(), this function returns the - * lowest negative number that can be represented, which - * is the exact opposite of DBL_MAX: -DBL_MAX. - * - * To get DBL_MIN, look at tinydouble() - * - * More information can be found at: - * http://forums.codeguru.com/showthread.php?260921-DBL_MIN-and-DBL_MAX&p=799431#post799431 - */ -double mindouble( void) -{ - return( -DBL_MAX); -} diff --git a/src/structures/src/Constants/minfloat.c b/src/structures/src/Constants/minfloat.c deleted file mode 100644 index 2c587e2b..00000000 --- a/src/structures/src/Constants/minfloat.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - - -float minfloat( void) -{ - return( -FLT_MAX); /* do not use FLT_MIN here!!! */ -} diff --git a/src/structures/src/Constants/minint.c b/src/structures/src/Constants/minint.c deleted file mode 100644 index b5127486..00000000 --- a/src/structures/src/Constants/minint.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -sac_int minint( void) -{ - sac_int num_bits = 8 * sizeof(sac_int); - return -((sac_int)1 << (num_bits - 1)); -} diff --git a/src/structures/src/Constants/minmax.c b/src/structures/src/Constants/minmax.c deleted file mode 100644 index 94876ebb..00000000 --- a/src/structures/src/Constants/minmax.c +++ /dev/null @@ -1,28 +0,0 @@ -#include - -#define UCHAR_MIN 0 -#define USHRT_MIN 0 -#define UINT_MIN 0 -#define ULONG_MIN 0 -#define ULLONG_MIN 0 - -#define minmax(rtype, SACtype, typeucase) \ -rtype max##SACtype( void ) \ -{ \ - return( typeucase##_MAX); \ -} \ - \ -rtype min##SACtype( void ) \ -{ \ - return( typeucase##_MIN); \ -} - -minmax(char, byte, CHAR) -minmax(short, short, SHRT) -minmax(long, long, LONG) -minmax(long long, longlong, LLONG) -minmax(unsigned char, ubyte, UCHAR) -minmax(unsigned short, ushort, USHRT) -minmax(unsigned int, uint, UINT) -minmax(unsigned long, ulong, ULONG) -minmax(unsigned long long, ulonglong, ULLONG) diff --git a/src/structures/src/Constants/minmax.mac b/src/structures/src/Constants/minmax.mac new file mode 100644 index 00000000..678fb0da --- /dev/null +++ b/src/structures/src/Constants/minmax.mac @@ -0,0 +1,9 @@ +minmax(char, byte, CHAR) +minmax(short, short, SHRT) +minmax(long, long, LONG) +minmax(long long, longlong, LLONG) +minmax(unsigned char, ubyte, UCHAR) +minmax(unsigned short, ushort, USHRT) +minmax(unsigned int, uint, UINT) +minmax(unsigned long, ulong, ULONG) +minmax(unsigned long long, ulonglong, ULLONG) diff --git a/src/structures/src/Constants/tinydouble.c b/src/structures/src/Constants/tinydouble.c deleted file mode 100644 index 458acf02..00000000 --- a/src/structures/src/Constants/tinydouble.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -/* DBL_MIN represents the smallest normilised - * value that can be represented. - */ -double tinydouble( void) -{ - return( DBL_MIN); -} From 696c8aecf905284066bc09cde4f8758e3fad3bbb Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 21:35:01 +0100 Subject: [PATCH 10/19] Re-iplementation of List --- src/CMakeLists.txt | 12 +- src/structures/List.sac | 79 +++++---- src/structures/String.sac | 3 +- src/structures/src/Constants/Constants.c | 2 +- src/structures/src/Constants/Constants.h | 2 +- .../Constants/{minmax.mac => Constants.mac} | 0 src/structures/src/Constants/randmax.c | 6 - src/structures/src/List/List.c | 153 ++++++++++++++++++ src/structures/src/List/List.h | 42 +++-- src/structures/src/List/append.c | 98 ----------- src/structures/src/List/cons.c | 37 ----- src/structures/src/List/drop.c | 44 ----- src/structures/src/List/empty.c | 24 --- src/structures/src/List/free.c | 24 --- src/structures/src/List/hd.c | 27 ---- src/structures/src/List/length.c | 27 ---- src/structures/src/List/nil.c | 29 ---- src/structures/src/List/nth.c | 37 ----- src/structures/src/List/take.c | 130 --------------- src/structures/src/List/tl.c | 34 ---- src/structures/src/String/String.c | 1 + 21 files changed, 223 insertions(+), 588 deletions(-) rename src/structures/src/Constants/{minmax.mac => Constants.mac} (100%) delete mode 100644 src/structures/src/Constants/randmax.c create mode 100644 src/structures/src/List/List.c delete mode 100644 src/structures/src/List/append.c delete mode 100644 src/structures/src/List/cons.c delete mode 100644 src/structures/src/List/drop.c delete mode 100644 src/structures/src/List/empty.c delete mode 100644 src/structures/src/List/free.c delete mode 100644 src/structures/src/List/hd.c delete mode 100644 src/structures/src/List/length.c delete mode 100644 src/structures/src/List/nil.c delete mode 100644 src/structures/src/List/nth.c delete mode 100644 src/structures/src/List/take.c delete mode 100644 src/structures/src/List/tl.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 594b40f8..fd1fdf87 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -196,17 +196,7 @@ SET (C_DEPS_SRC structures/src/StringArray/copy.c structures/src/StringArray/alloc.c structures/src/String/String.c - structures/src/List/tl.c - structures/src/List/take.c - structures/src/List/nth.c - structures/src/List/nil.c - structures/src/List/length.c - structures/src/List/hd.c - structures/src/List/free.c - structures/src/List/empty.c - structures/src/List/drop.c - structures/src/List/cons.c - structures/src/List/append.c + structures/src/List/List.c structures/src/Format/wordsize.c structures/src/Constants/Constants.c structures/src/Char/Char.c diff --git a/src/structures/List.sac b/src/structures/List.sac index edf0e95b..a6188bc5 100644 --- a/src/structures/List.sac +++ b/src/structures/List.sac @@ -10,56 +10,53 @@ export all; */ external typedef list; - #pragma linkobj "src/List/free.o" - #pragma freefun "SAC_List_free_list" + #pragma ctype "list*" + #pragma copyfun "SAClistcopy" + #pragma freefun "SAClistfree" + #pragma linkobj "src/List/List.o" external list nil(); - #pragma linkname "SAC_List_nil" - #pragma linksign [1] - #pragma refcounting [0] - #pragma linkobj "src/List/nil.o" + #pragma linkname "SAClistnil" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external list cons( int ELEM, list LIST); - #pragma linkname "SAC_List_cons" - #pragma linksign [1,2,3] - #pragma refcounting [0,2] - #pragma linkobj "src/List/cons.o" + #pragma linkname "SAClistcons" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external int hd( list LIST); - #pragma linkname "SAC_List_hd" - #pragma linksign [0,1] - #pragma refcounting [1] - #pragma linkobj "src/List/hd.o" + #pragma linkname "SAClisthead" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external list tl( list LIST); - #pragma linkname "SAC_List_tl" - #pragma linksign [1,2] - #pragma refcounting [0,1] - #pragma linkobj "src/List/tl.o" + #pragma linkname "SAClisttail" + #pragma sacarg [0] + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external bool empty( list LIST); #pragma linkname "SAC_List_empty" - #pragma linksign [0,1] - #pragma refcounting [1] - #pragma linkobj "src/List/empty.o" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external list append( list LIST1, list LIST2); - #pragma linkname "SAC_List_append" - #pragma linksign [1,2,3] - #pragma refcounting [0,1,2] - #pragma linkobj "src/List/append.o" + #pragma linkname "SAClistappend" + #pragma sacarg [0, 1, 2] + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external int nth( int N, list LIST); - #pragma linkname "SAC_List_nth" - #pragma linksign [0,1,2] - #pragma refcounting [2] - #pragma linkobj "src/List/nth.o" + #pragma linkname "SAClistnth" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external int length( list LIST); - #pragma linkname "SAC_List_length" - #pragma linksign [0,1] - #pragma refcounting [1] - #pragma linkobj "src/List/length.o" + #pragma linkname "SAClistlength" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external list drop( int N, list LIST); - #pragma linkname "SAC_List_drop" - #pragma linksign [1,2,3] - #pragma refcounting [0,2] - #pragma linkobj "src/List/drop.o" + #pragma linkname "SAClistdrop" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" external list take( int N, list LIST); - #pragma linkname "SAC_List_take" - #pragma linksign [1,2,3] - #pragma refcounting [0,2] - #pragma linkobj "src/List/take.o" + #pragma linkname "SAClisttake" + #pragma linkobj "src/List/List.o" + #pragma header "src/List/List.h" + + + diff --git a/src/structures/String.sac b/src/structures/String.sac index 88dcd37b..6545c786 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -11,11 +11,10 @@ module String; export all except { indent }; external typedef string; + #pragma ctype "char*" #pragma freefun "free_string" - #pragma linkobj "src/String/String.o" #pragma copyfun "copy_string" #pragma linkobj "src/String/String.o" - #pragma ctype "char*" external string to_string(char[.] A, int LENGTH); #pragma linkname "SACtostring" diff --git a/src/structures/src/Constants/Constants.c b/src/structures/src/Constants/Constants.c index 92be8450..31e453e8 100644 --- a/src/structures/src/Constants/Constants.c +++ b/src/structures/src/Constants/Constants.c @@ -21,7 +21,7 @@ rtype min##SACtype( void ) \ return( typeucase##_MIN); \ } -#include "minmax.mac" +#include "Constants.mac" #undef minmax //---------- diff --git a/src/structures/src/Constants/Constants.h b/src/structures/src/Constants/Constants.h index bf6f1021..23bb4c75 100644 --- a/src/structures/src/Constants/Constants.h +++ b/src/structures/src/Constants/Constants.h @@ -8,7 +8,7 @@ rtype max##SACtype( void ); \ rtype min##SACtype( void ); -#include "minmax.mac" +#include "Constants.mac" #undef minmax //---------- diff --git a/src/structures/src/Constants/minmax.mac b/src/structures/src/Constants/Constants.mac similarity index 100% rename from src/structures/src/Constants/minmax.mac rename to src/structures/src/Constants/Constants.mac diff --git a/src/structures/src/Constants/randmax.c b/src/structures/src/Constants/randmax.c deleted file mode 100644 index 7325aacf..00000000 --- a/src/structures/src/Constants/randmax.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int randmax( void) -{ - return RAND_MAX; -} diff --git a/src/structures/src/List/List.c b/src/structures/src/List/List.c new file mode 100644 index 00000000..4cc57ce7 --- /dev/null +++ b/src/structures/src/List/List.c @@ -0,0 +1,153 @@ +/* + * Implementation of SAC standard module List + */ + + +#include +#include "List.h" + +list* SAClistcopy (list* lst) +{ + if (lst == NULL) return NULL; + + list* cpy = malloc(sizeof(list)); + cpy->elem = lst->elem; + cpy->cons = SACARGduplicateSaCArray(lst->cons); +} + +void SAClistfree (list* lst) +{ + if (lst == NULL) return; + + // Check whether this triggers a refcount decrease + SACARGdeleteSacArray(lst->cons); + free (lst); +} + + + +list* SAClistnil () +{ + return NULL; +} + +list* SAClistcons (sac_int elem, list* cons) +{ + list* res = malloc(sizeof(list)); + res->elem = elem; + res->cons = SACARGduplicateSaCArray(cons); + return res; +} + +sac_int SAClisthead (list* lst) +{ + if (lst == NULL) + SAC_RuntimeError ("Tried to get head of empty list"); + return lst->elem; +} + +SACarg* SAClisttail (list* lst) +{ + if (lst == NULL) + SAC_RuntimeError ("Tried to get tail of empty list"); + return SACARGduplicateSaCArray(lst->cons); +} + +bool SAClistempty (list* lst) +{ + return lst != NULL; +} + +SACarg* SAClistappend (SACarg* sa, SACarg* sb) +{ + list* a = SACARGgetSharedData(SACTYPE__LIST_list, sa); + if (a == NULL) + { + SACARGdeleteSacArray(sa); + return sb; + } + + list* child; + while (true) + { + child = SACARGgetSharedData(SACTYPE__LIST__list, a->cons); + if (child == NULL) break; + a = child; + } + + SACARGdeleteSacArray(a->cons); + a->cons = sb; + return sa; +} + +sac_int SAClistnth (sac_int n, list* lst) +{ + if (n < 0) + SAC_RuntimeError("Cannot get negative nth element of list"); + + while (lst != NULL) + { + if (n == 0) + return lst->elem; + n--; + lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); + } + + SAC_RuntimeError("Nth element outside of list"); +} + +sac_int SAClistlength (list* lst) { + sac_int length = 0; + while (lst != NULL) + { + length++; + lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); + } + return length; +} + +SACarg* SAClistdrop(sac_int n, SACarg* slst_old) +{ + if (n < 0) + SAC_RuntimeError("Cannot get negative nth element of list"); + + SACarg* slst = SACARGduplicateSaCArray(slst_old); + + while (true) + { + if (n == 0) break; + slst = ((list*)SACARGgetSharedData(SACTYPE_LIST_list, slst))->cons; + n--; + } + + if (n > 0) + SAC_RuntimeError("Nth element outside of list"); + + SACARGdeleteSacArray(slst_old); + return slst; +} + +list* SAClisttake(sac_int n, list* lst) +{ + if (n == 0) return NULL; + if (n < 0) + SAC_RuntimeError("Cannot get a negative amount of elements from a list"); + + list* result = malloc(sizeof(list)); + list* builder = result; + + while (true) + { + builder-> elem = lst->elem; + lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); + n --; + if (n == 0) break; + if (lst == NULL) + SAC_RuntimeError("Nth element outside of list"); + list* next = malloc(sizeof(list)); + builder->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, next, 0); + builder = next; + } + builder->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0); + return result; +} \ No newline at end of file diff --git a/src/structures/src/List/List.h b/src/structures/src/List/List.h index 218b70ac..ec27a10d 100644 --- a/src/structures/src/List/List.h +++ b/src/structures/src/List/List.h @@ -1,22 +1,34 @@ -/* - * Implementation of SAC standard module List +#ifndef STDLIB__LIST__H +#define STDLIB__LIST__H + +/** + * Implementation of SAC standard module List + * Do not try to touch this, it will fuck with your mind + * If you decide to go ahead anyway, good luck + * Nil = NULL inside the SACarg :) */ -#include -#include - -#include "sac.h" - - -#define TRACE 0 - +#include +#include "sacinterface.h" typedef struct LIST { - int elem; - struct LIST *rest; - SAC_array_descriptor_t desc; + sac_int elem; + SACarg* cons; } list; - -extern void SAC_List_free_list( list *elems); +list* SAClistcopy (list* lst); +void SAClistfree (list* lst); + +list* SAClistnil (); +list* SAClistcons (sac_int elem, list* cons); +sac_int SAClisthead (list* lst); +SACarg* SAClisttail (list* lst); +bool SAClistempty (list* lst); +SACarg* SAClistappend (SACarg* sa, SACarg* sb); +sac_int SAClistnth (sac_int n, list* lst); +sac_int SAClistlength (list* lst); +SACarg* SAClistdrop(sac_int n, SACarg* slst_old); +list* SAClisttake(sac_int n, list* lst); + +#endif // STDLIB__LIST__H \ No newline at end of file diff --git a/src/structures/src/List/append.c b/src/structures/src/List/append.c deleted file mode 100644 index 45a40d48..00000000 --- a/src/structures/src/List/append.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elemsA_nt (elemsA, T_OLD((SCL, (HID, (NUQ,))))) -#define elemsB_nt (elemsB, T_OLD((SCL, (HID, (NUQ,))))) -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_append( SAC_ND_PARAM_out( res_nt, list *), - SAC_ND_PARAM_in( elemsA_nt, list *), - SAC_ND_PARAM_in( elemsB_nt, list *)) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, list *, ) - - if (elemsA->rest == NULL) { /* elemsA == NIL! */ - SAC_ND_RET_out( res_nt, elemsB_nt) - if (--(DESC_RC( elemsA->desc)) == 0) { - SAC_List_free_list( elemsA); - } - } - else { - - if (DESC_RC( elemsA->desc) == 1) { /* re-use all elems while (rc == 1)! */ - SAC_ND_RET_out( res_nt, elemsA_nt) - - do { - new = elemsA; - elemsA = elemsA->rest; - } - while ((elemsA->rest != NULL) && (DESC_RC( elemsA->desc) == 1)); - /* - * Now, we decrement the "rest" of elemsA. - * Although this may lead to a 0 rc in case of NIL, - * we do NOT free that node, since we have to make - * sure that it survives the copying while-loop. - * After that loop we check, whether the rc is 0. - * If that is the case, we know that there were - * no copies to be done and we can free the NIL! - */ - (DESC_RC( elemsA->desc))--; -#if TRACE - fprintf( stderr, "changing CONS at (%p)\n", new); -#endif - } - - else { /* copy first elem & decrement rc of 'elemsA'! */ - new = (list *) SAC_MALLOC( sizeof( list)); - new->elem = elemsA->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); -#if TRACE - fprintf( stderr, "creating CONS at (%p)\n", new); -#endif - SAC_ND_RET_out( res_nt, new_nt) - (DESC_RC( elemsA->desc))--; - - elemsA = elemsA->rest; /* 'elemsA' has to be one in advance of 'new'! */ - } - - /* - * 'new' points to the last elem reused/copied - * 'elemsA' is one in front - */ - while (elemsA->rest != NULL) { - new->rest = (list *) SAC_MALLOC( sizeof( list)); -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); - fprintf( stderr, "creating CONS at (%p)\n", new->rest); -#endif - new = new->rest; - new->elem = elemsA->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); - elemsA = elemsA->rest; - } - new->rest = elemsB; -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); -#endif - /* Finally, we have to do some housekeeping! (see comment above!) */ - if (DESC_RC( elemsA->desc) == 0) { - SAC_List_free_list( elemsA); - } - } -} - -#undef res_nt -#undef elemsA_nt -#undef elemsB_nt -#undef new_nt diff --git a/src/structures/src/List/cons.c b/src/structures/src/List/cons.c deleted file mode 100644 index 33ae4fc5..00000000 --- a/src/structures/src/List/cons.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_cons( SAC_ND_PARAM_out( res_nt, list *), - int elem, - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - res = (list *) SAC_MALLOC( sizeof( list)); - res->elem = elem; - res->rest = elems; - SAC_ND_ALLOC__DESC( res_nt, 0) - SAC_ND_SET__RC( res_nt, 1) - res->desc = SAC_ND_A_DESC( res_nt); - -#if TRACE - fprintf( stderr, "creating CONS at (%p)\n", res); - fprintf( stderr, " [ %d . (%p)]\n", elem, elems); -#endif - - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt -#undef elemsA_nt -#undef elemsB_nt -#undef new_nt diff --git a/src/structures/src/List/drop.c b/src/structures/src/List/drop.c deleted file mode 100644 index 55909d65..00000000 --- a/src/structures/src/List/drop.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_drop( SAC_ND_PARAM_out( res_nt, list *), - int n, - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - if (n < 0) { - SAC_RuntimeError( "negative first arg of drop\n"); - } - - SAC_ND_A_DESC( res_nt) = SAC_ND_A_DESC( elems_nt); - SAC_ND_A_FIELD( res_nt) = SAC_ND_A_FIELD( elems_nt); - - while (n > 0) { - if (res->rest == NULL) { - SAC_RuntimeError( "first arg of drop %d larger than length of list\n", n); - } - res=res->rest; - n--; - } - (DESC_RC( res->desc))++; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - SAC_ND_A_DESC( res_nt) = res->desc; - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt -#undef elems_nt diff --git a/src/structures/src/List/empty.c b/src/structures/src/List/empty.c deleted file mode 100644 index f109b50a..00000000 --- a/src/structures/src/List/empty.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_empty( SAC_ND_PARAM_in( elems_nt, list *)) -{ - int res; - - res = (elems->rest == NULL); - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/free.c b/src/structures/src/List/free.c deleted file mode 100644 index ea300f9f..00000000 --- a/src/structures/src/List/free.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -void SAC_List_free_list( list *elems) -{ - list *next; - - do { - next = elems->rest; - -#if TRACE - fprintf( stderr, "freeing (%p)\n", elems); -#endif - - SAC_FREE( elems); - elems = next; - } - while ((elems != NULL) && (--(DESC_RC( elems->desc)) == 0)); -} diff --git a/src/structures/src/List/hd.c b/src/structures/src/List/hd.c deleted file mode 100644 index 4d557a27..00000000 --- a/src/structures/src/List/hd.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_hd( SAC_ND_PARAM_in( elems_nt, list *)) -{ - int res; - - if (elems->rest == NULL) { - SAC_RuntimeError( "hd applied to NIL\n"); - } - res = elems->elem; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/length.c b/src/structures/src/List/length.c deleted file mode 100644 index d2b5f18e..00000000 --- a/src/structures/src/List/length.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_length( SAC_ND_PARAM_in( elems_nt, list *)) -{ - list *ptr = elems; - int res = 0; - - while (ptr->rest != NULL) { - ptr = ptr->rest; - res++; - } - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/nil.c b/src/structures/src/List/nil.c deleted file mode 100644 index 9ed0b168..00000000 --- a/src/structures/src/List/nil.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_nil( SAC_ND_PARAM_out( res_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - res = (list *) SAC_MALLOC( sizeof( list)); - res->rest = NULL; - SAC_ND_ALLOC__DESC( res_nt, 0) - SAC_ND_SET__RC( res_nt, 1) - res->desc = SAC_ND_A_DESC( res_nt); - -#if TRACE - fprintf( stderr, "creating NIL at (%p)\n", res); -#endif - - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt diff --git a/src/structures/src/List/nth.c b/src/structures/src/List/nth.c deleted file mode 100644 index b4fac9bf..00000000 --- a/src/structures/src/List/nth.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_nth( int n, SAC_ND_PARAM_in( elems_nt, list *)) -{ - list *ptr; - int res; - - if (n < 0) { - SAC_RuntimeError( "negative first arg of nth\n"); - } - - ptr = elems; - while (n > 0) { - ptr = ptr->rest; - if (ptr->rest == NULL) { - SAC_RuntimeError( "first arg of nth %d larger than length of list\n", n); - } - n--; - } - res = ptr->elem; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/take.c b/src/structures/src/List/take.c deleted file mode 100644 index 5b6d0ac0..00000000 --- a/src/structures/src/List/take.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define last_nt (last, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_take( SAC_ND_PARAM_out( res_nt, list *), - int n, - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, list *, ) - SAC_ND_DECL__DESC( last_nt, ) - SAC_ND_DECL__DATA( last_nt, list *, ) - - if (n < 0) { - SAC_RuntimeError( "negative first arg of take\n"); - } - - last = (list *) SAC_MALLOC( sizeof( list)); - last->rest = NULL; - SAC_ND_ALLOC__DESC( last_nt, 0) - SAC_ND_SET__RC( last_nt, 1) - last->desc = SAC_ND_A_DESC( last_nt); -#if TRACE - fprintf( stderr, "creating NIL at (%p)\n", last); -#endif - - if (n == 0) { - SAC_ND_RET_out( res_nt, last_nt) - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - } - else { - /* - * Now, we do know that we have to take at least one element! - */ - - if (DESC_RC( elems->desc) == 1) { - /* - * re-use all elems while ((rc == 1) && (n > 0)) ! - */ - SAC_ND_RET_out( res_nt, elems_nt) - - do { - if (elems->rest == NULL) - SAC_RuntimeError( "first arg of take %d larger than length of list\n", - n); - new = elems; - elems = elems->rest; - } - while ((--n > 0) && (DESC_RC( elems->desc) == 1)); - /* - * Now, we decrement the "rest" of elems. - */ - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } -#if TRACE - fprintf( stderr, "changing CONS at (%p)\n", new); -#endif - } - - else { - if (elems->rest == NULL) { - SAC_RuntimeError( "first arg of take %d larger than length of list\n", - n); - } - /* - * Now, we do know that we have at least one element to copy. - * After doing so, we have to decrement the rc of elems! - */ - new = (list *) SAC_MALLOC( sizeof( list)); - new->elem = elems->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); -#if TRACE - fprintf( stderr, "creating CONS at (%p)\n", new); -#endif - SAC_ND_RET_out( res_nt, new_nt); - - (DESC_RC( elems->desc))--; - elems = elems->rest; /* elems has to be one in advance of new! */ - n--; - } - - /* - * 'new' points to the last elem reused/copied - * 'elems' is one in front! - */ - while (n > 0) { - if (elems->rest == NULL) - SAC_RuntimeError( "first arg of take %d larger than length of list\n", - n); - new->rest = (list *) SAC_MALLOC( sizeof( list)); -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); - fprintf( stderr, "creating CONS at (%p)\n", new->rest); -#endif - new = new->rest; - new->elem = elems->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); - - elems = elems->rest; - n--; - } - new->rest = last; - -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); -#endif - } -} - -#undef res_nt -#undef elems_nt -#undef new_nt -#undef last_nt diff --git a/src/structures/src/List/tl.c b/src/structures/src/List/tl.c deleted file mode 100644 index e3fe5677..00000000 --- a/src/structures/src/List/tl.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_tl( SAC_ND_PARAM_out( res_nt, list *), - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - if (elems->rest == NULL) { - SAC_RuntimeError( "tl applied to NIL\n"); - } - res = elems->rest; - - (DESC_RC( res->desc))++; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - SAC_ND_A_DESC( res_nt) = res->desc; - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt -#undef elems_nt diff --git a/src/structures/src/String/String.c b/src/structures/src/String/String.c index 4968b3b9..220ed6b9 100644 --- a/src/structures/src/String/String.c +++ b/src/structures/src/String/String.c @@ -48,6 +48,7 @@ string SACautotostring (SACarg *sarr) strncpy (res, arr, (size_t)length); res[length] = '\0'; + SACARGdeleteSacArray(sarr); return res; } From e00c7138785b44fbb478420a9f78f48129f0aae3 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 22:38:15 +0100 Subject: [PATCH 11/19] more structures stuff --- src/structures/Char.sac | 30 +++++------ src/structures/Constants.sac | 54 +++++++++---------- src/structures/List.sac | 21 ++++---- src/structures/String.sac | 82 ++++++++++++++-------------- src/structures/src/List/List.c | 87 +++++++++++++++++------------- src/structures/src/List/List.h | 6 ++- src/structures/src/String/String.c | 5 +- src/structures/src/String/String.h | 3 ++ 8 files changed, 155 insertions(+), 133 deletions(-) diff --git a/src/structures/Char.sac b/src/structures/Char.sac index 0346cedd..dda232e0 100644 --- a/src/structures/Char.sac +++ b/src/structures/Char.sac @@ -16,57 +16,57 @@ export all; external bool isalpha(char c); #pragma linkname "SACisalpha" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool isupper(char c); #pragma linkname "SACisupper" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool islower(char c); #pragma linkname "SACislower" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool isdigit(char c); #pragma linkname "SACisdigit" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool isxdigit(char c); #pragma linkname "SACisxdigit" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool isspace(char c); #pragma linkname "SACisspace" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool ispunct(char c); #pragma linkname "SACispunct" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool isalnum(char c); #pragma linkname "SACisalnum" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool isprint(char c); #pragma linkname "SACisprint" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool isgraph(char c); #pragma linkname "SACisgraph" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external bool iscntrl(char c); #pragma linkname "SACiscntrl" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" /****************************************************************************** * @@ -78,12 +78,12 @@ external bool iscntrl(char c); external bool isascii(int N); #pragma linkname "SACisascii" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external char toascii(int N); #pragma linkname "SACtoascii" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" /****************************************************************************** * @@ -94,12 +94,12 @@ external char toascii(int N); external char tolower(char c); #pragma linkname "SACtolower" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" external char toupper(char c); #pragma linkname "SACtoupper" #pragma linkobj "src/Char/Char.o" - #pragma header "src/Char/Char.h" + // #pragma header "src/Char/Char.h" /****************************************************************************** * diff --git a/src/structures/Constants.sac b/src/structures/Constants.sac index 4dfd3e7b..35f30b45 100644 --- a/src/structures/Constants.sac +++ b/src/structures/Constants.sac @@ -8,110 +8,110 @@ export all; external byte minbyte(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external byte maxbyte(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external short minshort(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external short maxshort(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external longlong minlonglong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external longlong maxlonglong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ubyte minubyte(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ubyte maxubyte(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ushort minushort(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ushort maxushort(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ulonglong minulonglong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ulonglong maxulonglong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" #endif /* FULLTYPES */ external int minint(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external int maxint(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external long minlong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external long maxlong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external uint minuint(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external uint maxuint(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ulong minulong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external ulong maxulong(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external float minfloat(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external float maxfloat(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external double mindouble(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external double tinydouble(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external double maxdouble(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external double epidouble(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" + // #pragma header "src/Constants/Constants.h" external int randmax(); #pragma linkobj "src/Constants/Constants.o" - #pragma header "src/Constants/Constants.h" \ No newline at end of file + // #pragma header "src/Constants/Constants.h" \ No newline at end of file diff --git a/src/structures/List.sac b/src/structures/List.sac index a6188bc5..ad1a847d 100644 --- a/src/structures/List.sac +++ b/src/structures/List.sac @@ -18,45 +18,46 @@ external typedef list; external list nil(); #pragma linkname "SAClistnil" #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external list cons( int ELEM, list LIST); #pragma linkname "SAClistcons" #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external int hd( list LIST); #pragma linkname "SAClisthead" #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external list tl( list LIST); #pragma linkname "SAClisttail" #pragma sacarg [0] #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external bool empty( list LIST); #pragma linkname "SAC_List_empty" #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external list append( list LIST1, list LIST2); #pragma linkname "SAClistappend" #pragma sacarg [0, 1, 2] #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external int nth( int N, list LIST); #pragma linkname "SAClistnth" #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external int length( list LIST); #pragma linkname "SAClistlength" #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external list drop( int N, list LIST); #pragma linkname "SAClistdrop" + #pragma sacarg [0, 2] #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" external list take( int N, list LIST); #pragma linkname "SAClisttake" #pragma linkobj "src/List/List.o" - #pragma header "src/List/List.h" + // #pragma header "src/List/List.h" diff --git a/src/structures/String.sac b/src/structures/String.sac index 6545c786..5f6d485a 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -12,71 +12,71 @@ export all except { indent }; external typedef string; #pragma ctype "char*" - #pragma freefun "free_string" #pragma copyfun "copy_string" + #pragma freefun "free_string" #pragma linkobj "src/String/String.o" external string to_string(char[.] A, int LENGTH); #pragma linkname "SACtostring" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert character array to string. */ external string to_auto_string(char[.] A); #pragma linkname "SACautotostring" #pragma sacarg [1] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert character array to string. */ external string strmod(string str, int P, char C); #pragma linkname "SACstrmod" #pragma linksign [1,1,2,3] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Change position P of string str to character C. */ external string strins(string S1, int P, string S2); #pragma linkname "SACstrins" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Insert string S2 into string S1 starting at position P. */ external string strovwt(string S1, int P, string S2); #pragma linkname "SACstrovwt" #pragma linksign [1,1,2,3] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Overwrite string S2 with string S1 starting at position P. */ external char strsel(string str, int P); #pragma linkname "SACstrsel" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Select character P of string str. */ external string strcat(string S1, string S2); #pragma linkname "SACstrcat" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Concatenate strings S1 and S2. */ external string +(string S1, string S2); #pragma linkname "SACstrcat" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Infix version of strcat. */ external string strncat(string S1, string S2, int N); #pragma linkname "SACstrncat" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Concatenate string S1 and the first N characters of string S2. */ external int strcmp(string S1, string S2); #pragma linkname "SACstrcmp" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Compare strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ @@ -84,7 +84,7 @@ external int strcmp(string S1, string S2); external int strncmp(string S1, string S2, int N); #pragma linkname "SACstrncmp" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Compare the first N characters of strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ @@ -92,7 +92,7 @@ external int strncmp(string S1, string S2, int N); external int strcasecmp(string S1, string S2); #pragma linkname "SACstrcasecmp" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Compare strings S1 and S2 while ignoring upper/lower case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ @@ -100,7 +100,7 @@ external int strcasecmp(string S1, string S2); external int strncasecmp(string S1, string S2, int N); #pragma linkname "SACstrncasecmp" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Compare the first N characters of strings S1 and S2 ignoring case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ @@ -108,33 +108,33 @@ external int strncasecmp(string S1, string S2, int N); external int strlen(string str); #pragma linkname "SACstrlen" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Return the length of string str. */ external string strtake(string str, int N); #pragma linkname "SACstrtake" #pragma linksign [1, 1, 2] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Return the first N characters of string str. */ external string strdrop(string str, int N); #pragma linkname "SACstrdrop" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Return all but the first N characters of string str. */ external string strext(string str, int FIRST, int LEN); #pragma linkname "SACstrext" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Extract the substring beginning at character FIRST with length LEN from string str. */ external string sprintf(string FORMAT, ...); #pragma linkname "SACsprintf" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Generate a string depending on the format string and a variable number of arguments similar to the printf and fprintf functions. */ @@ -143,7 +143,7 @@ external int, ... sscanf(string str, string FORMAT); #pragma linkname "SACsscanf" #pragma linksign [0,1,2] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Scan the string str concerning the format string FORMAT. The format string is the same as for fscanf. The int result gives the maximum number of successfully performed @@ -155,7 +155,7 @@ external int, ... sscanf(string str, string FORMAT); external string sscanf_str(string str, string FORMAT); #pragma linkname "SACsscanf_str" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Scan the string str concerning the format string FORMAT. FORMAT must have exactly one string conversion specifier. The scanned substring is returned. */ @@ -163,7 +163,7 @@ external string sscanf_str(string str, string FORMAT); external int strchr(string str, char C); #pragma linkname "SACstrchr" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Return the position of the first occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ @@ -171,7 +171,7 @@ external int strchr(string str, char C); external int strrchr(string str, char C); #pragma linkname "SACstrrchr" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Return the position of the last occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ @@ -179,21 +179,21 @@ external int strrchr(string str, char C); external int strcspn(string str, string REJECT); #pragma linkname "SACstrcspn" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Return the length of the initial segment of str which consists entirely of characters not in REJECT. */ external int strspn(string str, string ACCEPT); #pragma linkname "SACstrspn" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Return the length of the initial segment of str which consists entirely of characters in ACCEPT. */ external int strstr(string HAYSTACK, string NEEDLE); #pragma linkname "SACstrstr" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Find the first occurrence of the string NEEDLE in the string HAYSTACK. Return the index to this occurrence. If NEEDLE could not be found then return -1. */ @@ -202,7 +202,7 @@ external string, string strtok(string str, string SEP); #pragma linkname "SACstrtok" #pragma linksign [1, 2, 3, 4] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Split string str into a token and the remainder string using the characters from SEP as delimiters. If str starts with characters from SEP then these are skipped until @@ -212,33 +212,33 @@ external string chomp(string str); #pragma linkname "SACchomp" #pragma linksign [1,1] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Remove trailing line terminators from str. */ external string rtrim(string str); #pragma linkname "SACrtrim" #pragma linksign [1,1] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Remove trailing whitespace from str. */ external string ltrim(string str); #pragma linkname "SACltrim" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Remove leading whitespace from str. */ external string trim(string str); #pragma linkname "SACtrim" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Remove leading and trailing whitespace from str. */ external int, string strtoi(string str, int BASE); #pragma linkname "SACstrtoi" #pragma linksign [0,1,2,3] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert the string str into an integer value. BASE specifies the format and must be in the range from 2 to 32. This feature allows for scanning hexadecimal or octal numbers as well as decimal @@ -249,7 +249,7 @@ external float, string strtof(string str); #pragma linkname "SACstrtof" #pragma linksign [0,1,2] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert the string str into a float value. The string result contains the remainder of the input string. */ @@ -257,51 +257,51 @@ external double, string strtod(string str); #pragma linkname "SACstrtod" #pragma linksign [0,1,2] #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert the string str into a double value. The string result contains the remainder of the input string. */ external int toi(string str); #pragma linkname "SACtoi" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert the string str into an integer value. In contrast to strtoi the number must always be in decimal representation. */ external float tof(string str); #pragma linkname "SACtof" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert the string str into a float value. */ external double tod(string str); #pragma linkname "SACtod" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert the string str into a double value. */ external string tos(int N); #pragma linkname "SACitos" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert integer into string. */ external string tos(float N); #pragma linkname "SACftos" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert float into string. */ external string tos(double N); #pragma linkname "SACdtos" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert double into string. */ external string tos(bool B); #pragma linkname "SACbtos" #pragma linkobj "src/String/String.o" - #pragma header "src/String/String.h" + // #pragma header "src/String/String.h" /* Convert boolean into string. */ inline string indent(int indent, string str) diff --git a/src/structures/src/List/List.c b/src/structures/src/List/List.c index 4cc57ce7..67422b26 100644 --- a/src/structures/src/List/List.c +++ b/src/structures/src/List/List.c @@ -4,6 +4,7 @@ #include +#include #include "List.h" list* SAClistcopy (list* lst) @@ -13,15 +14,39 @@ list* SAClistcopy (list* lst) list* cpy = malloc(sizeof(list)); cpy->elem = lst->elem; cpy->cons = SACARGduplicateSaCArray(lst->cons); + return cpy; } -void SAClistfree (list* lst) +list* SAClistfree (list* lst) { - if (lst == NULL) return; + if (lst == NULL) return NULL; // Check whether this triggers a refcount decrease - SACARGdeleteSacArray(lst->cons); + SACARGdeleteSacArray(&(lst->cons)); free (lst); + return NULL; +} + +// n = 0 for unlimited +// Last element will not have cons initialized! +void deepcopy(list** out_root, list** out_last, const list* lst, sac_int n) +{ + *out_root = malloc(sizeof(list)); + *out_last = *out_root; + (*out_last)->elem = lst->elem; + n--; + while (n != 0) + { + lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); + if (lst == NULL && n > 0) + SAC_RuntimeError("Nth element outside of list"); + if (lst == NULL) break; + list* next = malloc(sizeof(list)); + (*out_last)->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, next, 0); + *out_last = next; + (*out_last)->elem = lst->elem; + n--; + } } @@ -35,7 +60,7 @@ list* SAClistcons (sac_int elem, list* cons) { list* res = malloc(sizeof(list)); res->elem = elem; - res->cons = SACARGduplicateSaCArray(cons); + res->cons = SACARGduplicateSaCArray(cons->cons); return res; } @@ -60,28 +85,26 @@ bool SAClistempty (list* lst) SACarg* SAClistappend (SACarg* sa, SACarg* sb) { - list* a = SACARGgetSharedData(SACTYPE__LIST_list, sa); + const list* a = SACARGgetSharedData(SACTYPE__LIST__list, sa); if (a == NULL) { - SACARGdeleteSacArray(sa); + SACARGdeleteSacArray(&sa); return sb; } - list* child; - while (true) - { - child = SACARGgetSharedData(SACTYPE__LIST__list, a->cons); - if (child == NULL) break; - a = child; - } + list* res = NULL; + list* taila = NULL; + deepcopy(&res, &taila, a, 0); + taila->cons = sb; + + SACarg* sres = SACARGcreateFromPointer(SACTYPE__LIST__list, res, 0); - SACARGdeleteSacArray(a->cons); - a->cons = sb; - return sa; + return sres; } -sac_int SAClistnth (sac_int n, list* lst) +sac_int SAClistnth (sac_int n, list* lstnc) { + const list* lst = lstnc; if (n < 0) SAC_RuntimeError("Cannot get negative nth element of list"); @@ -94,9 +117,12 @@ sac_int SAClistnth (sac_int n, list* lst) } SAC_RuntimeError("Nth element outside of list"); + return 0; } -sac_int SAClistlength (list* lst) { +sac_int SAClistlength (list* lstnc) +{ + const list* lst = lstnc; sac_int length = 0; while (lst != NULL) { @@ -116,14 +142,14 @@ SACarg* SAClistdrop(sac_int n, SACarg* slst_old) while (true) { if (n == 0) break; - slst = ((list*)SACARGgetSharedData(SACTYPE_LIST_list, slst))->cons; + slst = ((list*)SACARGgetSharedData(SACTYPE__LIST__list, slst))->cons; n--; } if (n > 0) SAC_RuntimeError("Nth element outside of list"); - SACARGdeleteSacArray(slst_old); + SACARGdeleteSacArray(&slst_old); return slst; } @@ -133,21 +159,10 @@ list* SAClisttake(sac_int n, list* lst) if (n < 0) SAC_RuntimeError("Cannot get a negative amount of elements from a list"); - list* result = malloc(sizeof(list)); - list* builder = result; + list* res = NULL; + list* taila = NULL; + deepcopy(&res, &taila, lst, n); + taila -> cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0); - while (true) - { - builder-> elem = lst->elem; - lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); - n --; - if (n == 0) break; - if (lst == NULL) - SAC_RuntimeError("Nth element outside of list"); - list* next = malloc(sizeof(list)); - builder->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, next, 0); - builder = next; - } - builder->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0); - return result; + return res; } \ No newline at end of file diff --git a/src/structures/src/List/List.h b/src/structures/src/List/List.h index ec27a10d..d68610d5 100644 --- a/src/structures/src/List/List.h +++ b/src/structures/src/List/List.h @@ -12,15 +12,17 @@ #include #include "sacinterface.h" +extern SACtypes* SACTYPE__LIST__list; + typedef struct LIST { sac_int elem; SACarg* cons; } list; list* SAClistcopy (list* lst); -void SAClistfree (list* lst); +list* SAClistfree (list* lst); -list* SAClistnil (); +list* SAClistnil (void); list* SAClistcons (sac_int elem, list* cons); sac_int SAClisthead (list* lst); SACarg* SAClisttail (list* lst); diff --git a/src/structures/src/String/String.c b/src/structures/src/String/String.c index 220ed6b9..acc39d77 100644 --- a/src/structures/src/String/String.c +++ b/src/structures/src/String/String.c @@ -19,9 +19,10 @@ string copy_string (string s) return s2; } -void free_string (string s) +string free_string (string s) { free (s); + return NULL; } @@ -48,7 +49,7 @@ string SACautotostring (SACarg *sarr) strncpy (res, arr, (size_t)length); res[length] = '\0'; - SACARGdeleteSacArray(sarr); + SACARGdeleteSacArray(&sarr); return res; } diff --git a/src/structures/src/String/String.h b/src/structures/src/String/String.h index ad33a94a..f84ce263 100644 --- a/src/structures/src/String/String.h +++ b/src/structures/src/String/String.h @@ -6,6 +6,9 @@ typedef char* string; +string copy_string (string s); +string free_string (string s); + string SACtostring (unsigned char* arr, sac_int length); string SACautotostring (SACarg *sarr); void SACstrmod (string str, sac_int pos, unsigned char c); From e9378dce6e6f9201daf7648ae77ea2b53e681f77 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 22:39:38 +0100 Subject: [PATCH 12/19] undone non-complete fix --- src/CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fd1fdf87..71b0bf6a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -381,7 +381,7 @@ FOREACH (name ${SAC_SRC}) # of the call to sac2c. GET_FILENAME_COMPONENT (dir "${CMAKE_CURRENT_BINARY_DIR}/${name}" DIRECTORY) GET_FILENAME_COMPONENT (dst ${name} NAME_WE) - GET_FILENAME_COMPONENT (src_dir "${src}" DIRECTORY) + //GET_FILENAME_COMPONENT (src_dir "${src}" DIRECTORY) SET (mod "${DLL_BUILD_DIR}/${TARGET_ENV}/${SBI}/lib${dst}Mod${VARIANT}${MODEXT}") SET (tree @@ -420,7 +420,8 @@ FOREACH (name ${SAC_SRC}) ADD_CUSTOM_COMMAND ( OUTPUT "${mod}" "${tree}" COMMAND - ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -Xc -I"${src_dir}" -o ${DLL_BUILD_DIR} "${src}" + //${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -Xc -I"${src_dir}" -o ${DLL_BUILD_DIR} "${src}" + ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -o ${DLL_BUILD_DIR} "${src}" WORKING_DIRECTORY "${dir}" MAIN_DEPENDENCY "${src}" From aad6dbfd0d136042b0960e7adf3bf80a9f8a4fb4 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 22:48:54 +0100 Subject: [PATCH 13/19] fix build system --- src/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 71b0bf6a..e8a80b55 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -381,7 +381,7 @@ FOREACH (name ${SAC_SRC}) # of the call to sac2c. GET_FILENAME_COMPONENT (dir "${CMAKE_CURRENT_BINARY_DIR}/${name}" DIRECTORY) GET_FILENAME_COMPONENT (dst ${name} NAME_WE) - //GET_FILENAME_COMPONENT (src_dir "${src}" DIRECTORY) + # GET_FILENAME_COMPONENT (src_dir "${src}" DIRECTORY) SET (mod "${DLL_BUILD_DIR}/${TARGET_ENV}/${SBI}/lib${dst}Mod${VARIANT}${MODEXT}") SET (tree @@ -420,7 +420,7 @@ FOREACH (name ${SAC_SRC}) ADD_CUSTOM_COMMAND ( OUTPUT "${mod}" "${tree}" COMMAND - //${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -Xc -I"${src_dir}" -o ${DLL_BUILD_DIR} "${src}" + # ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -Xc -I"${src_dir}" -o ${DLL_BUILD_DIR} "${src}" ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -o ${DLL_BUILD_DIR} "${src}" WORKING_DIRECTORY "${dir}" From 1769dc945a8dde13b706a3b91578158c9c6f5940 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 22:54:55 +0100 Subject: [PATCH 14/19] fix ctype in list --- src/structures/List.sac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/List.sac b/src/structures/List.sac index ad1a847d..a203dfb5 100644 --- a/src/structures/List.sac +++ b/src/structures/List.sac @@ -10,7 +10,7 @@ export all; */ external typedef list; - #pragma ctype "list*" + //#pragma ctype "list*" #pragma copyfun "SAClistcopy" #pragma freefun "SAClistfree" #pragma linkobj "src/List/List.o" From 849c64ab456b11faf15895f6e8e57466d97a3213 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 23:15:41 +0100 Subject: [PATCH 15/19] someone changed the fucking functions from underneath me :c --- src/structures/src/List/List.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/structures/src/List/List.c b/src/structures/src/List/List.c index 67422b26..f36341cb 100644 --- a/src/structures/src/List/List.c +++ b/src/structures/src/List/List.c @@ -42,7 +42,7 @@ void deepcopy(list** out_root, list** out_last, const list* lst, sac_int n) SAC_RuntimeError("Nth element outside of list"); if (lst == NULL) break; list* next = malloc(sizeof(list)); - (*out_last)->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, next, 0); + (*out_last)->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, next, 0, NULL); *out_last = next; (*out_last)->elem = lst->elem; n--; @@ -97,7 +97,7 @@ SACarg* SAClistappend (SACarg* sa, SACarg* sb) deepcopy(&res, &taila, a, 0); taila->cons = sb; - SACarg* sres = SACARGcreateFromPointer(SACTYPE__LIST__list, res, 0); + SACarg* sres = SACARGcreateFromPointer(SACTYPE__LIST__list, res, 0, NULL); return sres; } @@ -162,7 +162,7 @@ list* SAClisttake(sac_int n, list* lst) list* res = NULL; list* taila = NULL; deepcopy(&res, &taila, lst, n); - taila -> cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0); + taila -> cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0, NULL); return res; } \ No newline at end of file From 69e086ef17a4b1a1f89c21124d41dae37f2375e9 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 23:25:03 +0100 Subject: [PATCH 16/19] styling warnings --- src/structures/src/Char/Char.h | 2 +- src/structures/src/Constants/Constants.c | 2 +- src/structures/src/List/List.c | 4 ++-- src/structures/src/List/List.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/structures/src/Char/Char.h b/src/structures/src/Char/Char.h index 774bd085..71fd39a1 100644 --- a/src/structures/src/Char/Char.h +++ b/src/structures/src/Char/Char.h @@ -33,4 +33,4 @@ uchar SACtoascii(sac_int c); uchar SACtolower(uchar c); uchar SACtoupper(uchar c); -#endif // STDLIB__CHAR__H \ No newline at end of file +#endif // STDLIB__CHAR__H diff --git a/src/structures/src/Constants/Constants.c b/src/structures/src/Constants/Constants.c index 31e453e8..e30a8877 100644 --- a/src/structures/src/Constants/Constants.c +++ b/src/structures/src/Constants/Constants.c @@ -76,4 +76,4 @@ double epidouble( void) sac_int randmax( void) { return (sac_int)RAND_MAX; -} \ No newline at end of file +} diff --git a/src/structures/src/List/List.c b/src/structures/src/List/List.c index f36341cb..7c637377 100644 --- a/src/structures/src/List/List.c +++ b/src/structures/src/List/List.c @@ -51,7 +51,7 @@ void deepcopy(list** out_root, list** out_last, const list* lst, sac_int n) -list* SAClistnil () +list* SAClistnil (void) { return NULL; } @@ -165,4 +165,4 @@ list* SAClisttake(sac_int n, list* lst) taila -> cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0, NULL); return res; -} \ No newline at end of file +} diff --git a/src/structures/src/List/List.h b/src/structures/src/List/List.h index d68610d5..a4939a6e 100644 --- a/src/structures/src/List/List.h +++ b/src/structures/src/List/List.h @@ -33,4 +33,4 @@ sac_int SAClistlength (list* lst); SACarg* SAClistdrop(sac_int n, SACarg* slst_old); list* SAClisttake(sac_int n, list* lst); -#endif // STDLIB__LIST__H \ No newline at end of file +#endif // STDLIB__LIST__H From 8e3aa751c878530d3de65d623d4637e24500fe85 Mon Sep 17 00:00:00 2001 From: Niek Janssen Date: Thu, 22 Jan 2026 23:48:22 +0100 Subject: [PATCH 17/19] renamed String to String because the pipeline complained somehow --- src/CMakeLists.txt | 2 +- src/structures/String.sac | 162 +++++++++--------- src/structures/src/String/{String.c => Str.c} | 0 src/structures/src/String/{String.h => Str.h} | 0 4 files changed, 82 insertions(+), 82 deletions(-) rename src/structures/src/String/{String.c => Str.c} (100%) rename src/structures/src/String/{String.h => Str.h} (100%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e8a80b55..36e9e284 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -195,7 +195,7 @@ SET (C_DEPS_SRC structures/src/StringArray/dim.c structures/src/StringArray/copy.c structures/src/StringArray/alloc.c - structures/src/String/String.c + structures/src/String/Str.c structures/src/List/List.c structures/src/Format/wordsize.c structures/src/Constants/Constants.c diff --git a/src/structures/String.sac b/src/structures/String.sac index 5f6d485a..1f84cf4c 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -14,127 +14,127 @@ external typedef string; #pragma ctype "char*" #pragma copyfun "copy_string" #pragma freefun "free_string" - #pragma linkobj "src/String/String.o" + #pragma linkobj "src/String/Str.o" external string to_string(char[.] A, int LENGTH); #pragma linkname "SACtostring" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert character array to string. */ external string to_auto_string(char[.] A); #pragma linkname "SACautotostring" #pragma sacarg [1] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert character array to string. */ external string strmod(string str, int P, char C); #pragma linkname "SACstrmod" #pragma linksign [1,1,2,3] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Change position P of string str to character C. */ external string strins(string S1, int P, string S2); #pragma linkname "SACstrins" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Insert string S2 into string S1 starting at position P. */ external string strovwt(string S1, int P, string S2); #pragma linkname "SACstrovwt" #pragma linksign [1,1,2,3] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Overwrite string S2 with string S1 starting at position P. */ external char strsel(string str, int P); #pragma linkname "SACstrsel" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Select character P of string str. */ external string strcat(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Concatenate strings S1 and S2. */ external string +(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Infix version of strcat. */ external string strncat(string S1, string S2, int N); #pragma linkname "SACstrncat" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Concatenate string S1 and the first N characters of string S2. */ external int strcmp(string S1, string S2); #pragma linkname "SACstrcmp" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strncmp(string S1, string S2, int N); #pragma linkname "SACstrncmp" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare the first N characters of strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strcasecmp(string S1, string S2); #pragma linkname "SACstrcasecmp" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare strings S1 and S2 while ignoring upper/lower case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strncasecmp(string S1, string S2, int N); #pragma linkname "SACstrncasecmp" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare the first N characters of strings S1 and S2 ignoring case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strlen(string str); #pragma linkname "SACstrlen" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the length of string str. */ external string strtake(string str, int N); #pragma linkname "SACstrtake" #pragma linksign [1, 1, 2] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the first N characters of string str. */ external string strdrop(string str, int N); #pragma linkname "SACstrdrop" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return all but the first N characters of string str. */ external string strext(string str, int FIRST, int LEN); #pragma linkname "SACstrext" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Extract the substring beginning at character FIRST with length LEN from string str. */ external string sprintf(string FORMAT, ...); #pragma linkname "SACsprintf" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Generate a string depending on the format string and a variable number of arguments similar to the printf and fprintf functions. */ @@ -142,8 +142,8 @@ external string sprintf(string FORMAT, ...); external int, ... sscanf(string str, string FORMAT); #pragma linkname "SACsscanf" #pragma linksign [0,1,2] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Scan the string str concerning the format string FORMAT. The format string is the same as for fscanf. The int result gives the maximum number of successfully performed @@ -154,46 +154,46 @@ external int, ... sscanf(string str, string FORMAT); external string sscanf_str(string str, string FORMAT); #pragma linkname "SACsscanf_str" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Scan the string str concerning the format string FORMAT. FORMAT must have exactly one string conversion specifier. The scanned substring is returned. */ external int strchr(string str, char C); #pragma linkname "SACstrchr" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the position of the first occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strrchr(string str, char C); #pragma linkname "SACstrrchr" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the position of the last occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strcspn(string str, string REJECT); #pragma linkname "SACstrcspn" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the length of the initial segment of str which consists entirely of characters not in REJECT. */ external int strspn(string str, string ACCEPT); #pragma linkname "SACstrspn" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the length of the initial segment of str which consists entirely of characters in ACCEPT. */ external int strstr(string HAYSTACK, string NEEDLE); #pragma linkname "SACstrstr" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Find the first occurrence of the string NEEDLE in the string HAYSTACK. Return the index to this occurrence. If NEEDLE could not be found then return -1. */ @@ -201,8 +201,8 @@ external int strstr(string HAYSTACK, string NEEDLE); external string, string strtok(string str, string SEP); #pragma linkname "SACstrtok" #pragma linksign [1, 2, 3, 4] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Split string str into a token and the remainder string using the characters from SEP as delimiters. If str starts with characters from SEP then these are skipped until @@ -211,34 +211,34 @@ external string, string strtok(string str, string SEP); external string chomp(string str); #pragma linkname "SACchomp" #pragma linksign [1,1] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove trailing line terminators from str. */ external string rtrim(string str); #pragma linkname "SACrtrim" #pragma linksign [1,1] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove trailing whitespace from str. */ external string ltrim(string str); #pragma linkname "SACltrim" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove leading whitespace from str. */ external string trim(string str); #pragma linkname "SACtrim" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove leading and trailing whitespace from str. */ external int, string strtoi(string str, int BASE); #pragma linkname "SACstrtoi" #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into an integer value. BASE specifies the format and must be in the range from 2 to 32. This feature allows for scanning hexadecimal or octal numbers as well as decimal @@ -248,60 +248,60 @@ external int, string strtoi(string str, int BASE); external float, string strtof(string str); #pragma linkname "SACstrtof" #pragma linksign [0,1,2] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a float value. The string result contains the remainder of the input string. */ external double, string strtod(string str); #pragma linkname "SACstrtod" #pragma linksign [0,1,2] - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a double value. The string result contains the remainder of the input string. */ external int toi(string str); #pragma linkname "SACtoi" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into an integer value. In contrast to strtoi the number must always be in decimal representation. */ external float tof(string str); #pragma linkname "SACtof" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a float value. */ external double tod(string str); #pragma linkname "SACtod" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a double value. */ external string tos(int N); #pragma linkname "SACitos" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert integer into string. */ external string tos(float N); #pragma linkname "SACftos" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert float into string. */ external string tos(double N); #pragma linkname "SACdtos" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert double into string. */ external string tos(bool B); #pragma linkname "SACbtos" - #pragma linkobj "src/String/String.o" - // #pragma header "src/String/String.h" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert boolean into string. */ inline string indent(int indent, string str) diff --git a/src/structures/src/String/String.c b/src/structures/src/String/Str.c similarity index 100% rename from src/structures/src/String/String.c rename to src/structures/src/String/Str.c diff --git a/src/structures/src/String/String.h b/src/structures/src/String/Str.h similarity index 100% rename from src/structures/src/String/String.h rename to src/structures/src/String/Str.h From 80435015302184cc9bd9e2420c05792379f151d2 Mon Sep 17 00:00:00 2001 From: Jordy Aaldering <33897257+JordyAaldering@users.noreply.github.com> Date: Fri, 23 Jan 2026 14:13:00 +0100 Subject: [PATCH 18/19] Wrong header name --- src/structures/src/String/Str.c | 67 ++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/structures/src/String/Str.c b/src/structures/src/String/Str.c index acc39d77..c2d86632 100644 --- a/src/structures/src/String/Str.c +++ b/src/structures/src/String/Str.c @@ -2,7 +2,6 @@ * C implementation of standard module StringC */ - #include #include #include @@ -10,7 +9,8 @@ #include #include #include -#include "String.h" + +#include "Str.h" string copy_string (string s) { @@ -25,9 +25,6 @@ string free_string (string s) return NULL; } - - - string SACtostring (unsigned char* arr, sac_int length) { string res = malloc ((size_t) length + 1); @@ -152,8 +149,9 @@ void SACstrtake(string str, sac_int pos) { size_t strlength = strlen (str); - if ((size_t)pos >= strlength) + if ((size_t)pos >= strlength) { SAC_RuntimeError("strtake: pos ("PRIisac") outside of string (length %zu)", pos, strlength); + } str[pos] = '\0'; } @@ -175,8 +173,9 @@ string SACstrext (string str, sac_int pos, sac_int len) string res = malloc(strlength - (size_t)len + 1); - if ((size_t)(pos + len) >= strlength) + if ((size_t)(pos + len) >= strlength) { SAC_RuntimeError("strext: Selecting substring ends at position "PRIisac" while string is of length %zu", pos+len, strlength); + } strncpy(res, str+(size_t)pos, (size_t)len); @@ -187,23 +186,30 @@ bool chr_in_delims (char c, string delimiters) { while (true) { - if (delimiters[0] == '\0') + if (delimiters[0] == '\0') { return false; - if (delimiters[0] == c) + } + + if (delimiters[0] == c) { return true; - delimiters ++; + } + + delimiters++; } } string next_in_delims (string str, string delimiters, bool is_delimiter) { - while (true) - { - if (str[0] == '\0') + while (true) { + if (str[0] == '\0') { return NULL; - if (chr_in_delims(str[0], delimiters) == is_delimiter) + } + + if (chr_in_delims(str[0], delimiters) == is_delimiter) { return str; - str ++; + } + + str++; } } @@ -326,8 +332,10 @@ sac_int SACstrchr (string str, unsigned char c) { string occurrence = strchr (str, c); - if (occurrence == NULL) + if (occurrence == NULL) { return -1; + } + return (sac_int)(occurrence - str); } @@ -335,8 +343,10 @@ sac_int SACstrrchr (string str, unsigned char c) { string occurrence = strrchr (str, c); - if (occurrence == NULL) + if (occurrence == NULL) { return -1; + } + return (sac_int)(occurrence - str); } @@ -376,10 +386,11 @@ void SACchomp (string str) while (end > str) { end --; - if (*end == '\n' || *end == '\r') + if (*end == '\n' || *end == '\r') { *end = '\0'; - else + } else { break; + } } } @@ -390,10 +401,11 @@ void SACrtrim (string str) while (end > str) { end --; - if (isspace(*end)) + if (isspace(*end)) { *end = '\0'; - else + } else { break; + } } } @@ -403,10 +415,11 @@ string SACltrim (string str) while (str < end) { - if (!isspace(*end)) + if (!isspace(*end)) { *end = '\0'; - else + } else { break; + } str ++; } @@ -434,7 +447,6 @@ sac_int SACstrtoi (string* remain, string input, sac_int base) return res; } - float SACstrtof (string* remain, string input) { string rem; @@ -503,10 +515,11 @@ string SACbtos (bool n) { string res = malloc(6 * sizeof(char)); - if (n) + if (n) { strcpy (res, "true"); - else + } else { strcpy (res, "false"); + } return res; -} \ No newline at end of file +} From ea1c25a2664cba3b05ea28c0df619571a5ececec Mon Sep 17 00:00:00 2001 From: Jordy Aaldering Date: Fri, 23 Jan 2026 14:41:18 +0100 Subject: [PATCH 19/19] some styling cleanup whilst I wait for my build --- src/structures/List.sac | 40 ++-- src/structures/src/Char/Char.c | 43 ++--- src/structures/src/Char/Char.h | 24 +-- src/structures/src/List/List.c | 142 ++++++++------- src/structures/src/List/List.h | 40 ++-- src/structures/src/String/Str.c | 314 ++++++++++++++++---------------- src/structures/src/String/Str.h | 91 ++++----- 7 files changed, 356 insertions(+), 338 deletions(-) diff --git a/src/structures/List.sac b/src/structures/List.sac index a203dfb5..75d6b6dd 100644 --- a/src/structures/List.sac +++ b/src/structures/List.sac @@ -5,59 +5,65 @@ module List deprecated "The List module contains a space leak."; export all; /* - * The SAC standard module List provides lists of integers as an - * additional data type offering access functions as usual. + * The SAC standard module List provides lists of integers as an + * additional data type offering access functions as usual. */ external typedef list; - //#pragma ctype "list*" #pragma copyfun "SAClistcopy" #pragma freefun "SAClistfree" #pragma linkobj "src/List/List.o" + // #pragma ctype "list*" external list nil(); #pragma linkname "SAClistnil" #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external list cons( int ELEM, list LIST); + +external list cons(int ELEM, list LIST); #pragma linkname "SAClistcons" #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external int hd( list LIST); + +external int hd(list LIST); #pragma linkname "SAClisthead" #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external list tl( list LIST); + +external list tl(list LIST); #pragma linkname "SAClisttail" #pragma sacarg [0] #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external bool empty( list LIST); + +external bool empty(list LIST); #pragma linkname "SAC_List_empty" #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external list append( list LIST1, list LIST2); + +external list append(list LIST1, list LIST2); #pragma linkname "SAClistappend" - #pragma sacarg [0, 1, 2] + #pragma sacarg [0,1,2] #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external int nth( int N, list LIST); + +external int nth(int N, list LIST); #pragma linkname "SAClistnth" #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external int length( list LIST); + +external int length(list LIST); #pragma linkname "SAClistlength" #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external list drop( int N, list LIST); + +external list drop(int N, list LIST); #pragma linkname "SAClistdrop" - #pragma sacarg [0, 2] + #pragma sacarg [0,2] #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" -external list take( int N, list LIST); + +external list take(int N, list LIST); #pragma linkname "SAClisttake" #pragma linkobj "src/List/List.o" // #pragma header "src/List/List.h" - - - diff --git a/src/structures/src/Char/Char.c b/src/structures/src/Char/Char.c index 23993cf4..503308ef 100644 --- a/src/structures/src/Char/Char.c +++ b/src/structures/src/Char/Char.c @@ -1,89 +1,78 @@ -/* - * Implementation of standard module Char ctype functions. - * It is sort of unfortunate that we have to use these - * functions as they incur a per-character overhead. - * However, without them incorrect values were produced. - * There were two problems: - * The isxxx(.) functions work on unsigned chars and not on chars. - * They may segfault when not in the range [-1,255]. - * They may return any non-zero value when true, but a SAC bool is true if 1. - */ - #include + #include "Char.h" bool SACisalpha(uchar c) { - return isalpha(c) != 0; + return isalpha(c) != 0; } bool SACisupper(uchar c) { - return isupper(c) != 0; + return isupper(c) != 0; } bool SACislower(uchar c) { - return islower(c) != 0; + return islower(c) != 0; } bool SACisdigit(uchar c) { - return isdigit(c) != 0; + return isdigit(c) != 0; } bool SACisxdigit(uchar c) { - return isxdigit(c) != 0; + return isxdigit(c) != 0; } bool SACisspace(uchar c) { - return isspace(c) != 0; + return isspace(c) != 0; } bool SACispunct(uchar c) { - return ispunct(c) != 0; + return ispunct(c) != 0; } bool SACisalnum(uchar c) { - return isalnum(c) != 0; + return isalnum(c) != 0; } bool SACisprint(uchar c) { - return isprint(c) != 0; + return isprint(c) != 0; } bool SACisgraph(uchar c) { - return isgraph(c) != 0; + return isgraph(c) != 0; } bool SACiscntrl(uchar c) { - return iscntrl(c) != 0; + return iscntrl(c) != 0; } bool SACisascii(sac_int c) { - return c >= 0 && c < 256 && isascii(c) != 0; + return c >= 0 && c < 256 && isascii(c) != 0; } uchar SACtoascii(sac_int c) { - return toascii(c & 0xFF); + return toascii(c & 0xFF); } uchar SACtolower(uchar c) { - return tolower(c); + return tolower(c); } uchar SACtoupper(uchar c) { - return toupper(c); + return toupper(c); } - diff --git a/src/structures/src/Char/Char.h b/src/structures/src/Char/Char.h index 71fd39a1..41241ec2 100644 --- a/src/structures/src/Char/Char.h +++ b/src/structures/src/Char/Char.h @@ -1,20 +1,20 @@ -#ifndef STDLIB__CHAR__H -#define STDLIB__CHAR__H +#ifndef _SAC_CHAR_H_ +#define _SAC_CHAR_H_ /* - * Implementation of standard module Char ctype functions. - * It is sort of unfortunate that we have to use these - * functions as they incur a per-character overhead. - * However, without them incorrect values were produced. - * There were two problems: - * The isxxx(.) functions work on unsigned chars and not on chars. - * They may segfault when not in the range [-1,255]. - * They may return any non-zero value when true, but a SAC bool is true if 1. + * Implementation of standard module Char ctype functions. It is sort of + * unfortunate that we have to use these functions as they incur a per-character + * overhead. However, without them incorrect values were produced. + * There were two problems: + * - The isxxx(.) functions work on unsigned chars and not on chars. + * They may segfault when not in the range [-1,255]. + * - They may return any non-zero value when true, but a SaC bool is true if 1. */ -#include "sacinterface.h" #include +#include "sacinterface.h" + typedef unsigned char uchar; bool SACisalpha(uchar c); @@ -33,4 +33,4 @@ uchar SACtoascii(sac_int c); uchar SACtolower(uchar c); uchar SACtoupper(uchar c); -#endif // STDLIB__CHAR__H +#endif /* _SAC_CHAR_H_ */ diff --git a/src/structures/src/List/List.c b/src/structures/src/List/List.c index 7c637377..38a9940a 100644 --- a/src/structures/src/List/List.c +++ b/src/structures/src/List/List.c @@ -1,25 +1,25 @@ -/* - * Implementation of SAC standard module List - */ - - #include #include + #include "List.h" -list* SAClistcopy (list* lst) +list *SAClistcopy(list *lst) { - if (lst == NULL) return NULL; + if (lst == NULL) { + return NULL; + } - list* cpy = malloc(sizeof(list)); + list *cpy = malloc(sizeof(list)); cpy->elem = lst->elem; cpy->cons = SACARGduplicateSaCArray(lst->cons); return cpy; } -list* SAClistfree (list* lst) +list *SAClistfree(list *lst) { - if (lst == NULL) return NULL; + if (lst == NULL) { + return NULL; + } // Check whether this triggers a refcount decrease SACARGdeleteSacArray(&(lst->cons)); @@ -29,90 +29,98 @@ list* SAClistfree (list* lst) // n = 0 for unlimited // Last element will not have cons initialized! -void deepcopy(list** out_root, list** out_last, const list* lst, sac_int n) +void deepcopy(list **out_root, list **out_last, const list *lst, sac_int n) { *out_root = malloc(sizeof(list)); *out_last = *out_root; (*out_last)->elem = lst->elem; - n--; - while (n != 0) - { + n -= 1; + + while (n != 0) { lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); - if (lst == NULL && n > 0) + if (lst == NULL && n > 0) { SAC_RuntimeError("Nth element outside of list"); - if (lst == NULL) break; - list* next = malloc(sizeof(list)); + } + + if (lst == NULL) { + break; + } + + list *next = malloc(sizeof(list)); (*out_last)->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, next, 0, NULL); *out_last = next; (*out_last)->elem = lst->elem; - n--; + n -= 1; } } - - -list* SAClistnil (void) +list *SAClistnil(void) { return NULL; } -list* SAClistcons (sac_int elem, list* cons) +list *SAClistcons(sac_int elem, list *cons) { - list* res = malloc(sizeof(list)); + list *res = malloc(sizeof(list)); res->elem = elem; res->cons = SACARGduplicateSaCArray(cons->cons); return res; } -sac_int SAClisthead (list* lst) +sac_int SAClisthead(list *lst) { - if (lst == NULL) + if (lst == NULL) { SAC_RuntimeError ("Tried to get head of empty list"); + } + return lst->elem; } -SACarg* SAClisttail (list* lst) +SACarg *SAClisttail(list *lst) { - if (lst == NULL) + if (lst == NULL) { SAC_RuntimeError ("Tried to get tail of empty list"); + } + return SACARGduplicateSaCArray(lst->cons); } -bool SAClistempty (list* lst) +bool SAClistempty(list *lst) { return lst != NULL; } -SACarg* SAClistappend (SACarg* sa, SACarg* sb) +SACarg *SAClistappend(SACarg *sa, SACarg *sb) { - const list* a = SACARGgetSharedData(SACTYPE__LIST__list, sa); - if (a == NULL) - { + const list *a = SACARGgetSharedData(SACTYPE__LIST__list, sa); + if (a == NULL) { SACARGdeleteSacArray(&sa); return sb; } - list* res = NULL; - list* taila = NULL; + list *res = NULL; + list *taila = NULL; deepcopy(&res, &taila, a, 0); taila->cons = sb; - SACarg* sres = SACARGcreateFromPointer(SACTYPE__LIST__list, res, 0, NULL); + SACarg *sres = SACARGcreateFromPointer(SACTYPE__LIST__list, res, 0, NULL); return sres; } -sac_int SAClistnth (sac_int n, list* lstnc) +sac_int SAClistnth(sac_int n, list *lstnc) { - const list* lst = lstnc; - if (n < 0) + const list *lst = lstnc; + if (n < 0) { SAC_RuntimeError("Cannot get negative nth element of list"); + } - while (lst != NULL) - { - if (n == 0) + while (lst != NULL) { + if (n == 0) { return lst->elem; - n--; + } + + n -= 1; lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); } @@ -120,49 +128,61 @@ sac_int SAClistnth (sac_int n, list* lstnc) return 0; } -sac_int SAClistlength (list* lstnc) +sac_int SAClistlength(list *lstnc) { - const list* lst = lstnc; + const list *lst = lstnc; sac_int length = 0; - while (lst != NULL) - { - length++; + + while (lst != NULL) { + length += 1; lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); } + return length; } -SACarg* SAClistdrop(sac_int n, SACarg* slst_old) + +SACarg *SAClistdrop(sac_int n, SACarg *slst_old) { - if (n < 0) + if (n < 0) { SAC_RuntimeError("Cannot get negative nth element of list"); + } + + SACarg *slst = SACARGduplicateSaCArray(slst_old); - SACarg* slst = SACARGduplicateSaCArray(slst_old); + while (true) { + if (n == 0) { + break; + } - while (true) - { - if (n == 0) break; slst = ((list*)SACARGgetSharedData(SACTYPE__LIST__list, slst))->cons; - n--; + n -= 1; } - if (n > 0) + // This seems wrong? + // The above loop only breaks when n == 0, so this should never be possible. + if (n > 0) { SAC_RuntimeError("Nth element outside of list"); + } SACARGdeleteSacArray(&slst_old); return slst; } -list* SAClisttake(sac_int n, list* lst) +list *SAClisttake(sac_int n, list *lst) { - if (n == 0) return NULL; - if (n < 0) + if (n == 0) { + return NULL; + } + + if (n < 0) { SAC_RuntimeError("Cannot get a negative amount of elements from a list"); + } - list* res = NULL; - list* taila = NULL; + list *res = NULL; + list *taila = NULL; deepcopy(&res, &taila, lst, n); - taila -> cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0, NULL); + taila->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0, NULL); return res; } diff --git a/src/structures/src/List/List.h b/src/structures/src/List/List.h index a4939a6e..2f13cfbb 100644 --- a/src/structures/src/List/List.h +++ b/src/structures/src/List/List.h @@ -1,15 +1,15 @@ -#ifndef STDLIB__LIST__H -#define STDLIB__LIST__H +#ifndef _SAC_LIST_H_ +#define _SAC_LIST_H_ -/** - * Implementation of SAC standard module List - * Do not try to touch this, it will fuck with your mind - * If you decide to go ahead anyway, good luck +/* + * Implementation of SAC standard module List. + * Do not try to touch this, it will fuck with your mind. + * If you decide to go ahead anyway, good luck. * Nil = NULL inside the SACarg :) */ - #include + #include "sacinterface.h" extern SACtypes* SACTYPE__LIST__list; @@ -19,18 +19,18 @@ typedef struct LIST { SACarg* cons; } list; -list* SAClistcopy (list* lst); -list* SAClistfree (list* lst); - -list* SAClistnil (void); -list* SAClistcons (sac_int elem, list* cons); -sac_int SAClisthead (list* lst); -SACarg* SAClisttail (list* lst); -bool SAClistempty (list* lst); -SACarg* SAClistappend (SACarg* sa, SACarg* sb); -sac_int SAClistnth (sac_int n, list* lst); -sac_int SAClistlength (list* lst); +list *SAClistcopy(list *lst); +list *SAClistfree(list *lst); + +list *SAClistnil(void); +list *SAClistcons(sac_int elem, list *cons); +sac_int SAClisthead(list *lst); +SACarg* SAClisttail(list *lst); +bool SAClistempty(list *lst); +SACarg* SAClistappend(SACarg* sa, SACarg* sb); +sac_int SAClistnth(sac_int n, list *lst); +sac_int SAClistlength(list *lst); SACarg* SAClistdrop(sac_int n, SACarg* slst_old); -list* SAClisttake(sac_int n, list* lst); +list *SAClisttake(sac_int n, list *lst); -#endif // STDLIB__LIST__H +#endif /* _SAC_LIST_H_ */ diff --git a/src/structures/src/String/Str.c b/src/structures/src/String/Str.c index c2d86632..1ec9dd79 100644 --- a/src/structures/src/String/Str.c +++ b/src/structures/src/String/Str.c @@ -1,117 +1,117 @@ -/* - * C implementation of standard module StringC - */ - -#include -#include +#include #include -#include -#include +#include #include -#include +#include +#include #include "Str.h" -string copy_string (string s) +string copy_string(string s) { - string s2 = malloc ((strlen(s) + 1) * sizeof (char)); - strcpy (s2, s); + string s2 = malloc((strlen(s) + 1) * sizeof(char)); + strcpy(s2, s); return s2; } -string free_string (string s) +string free_string(string s) { - free (s); + free(s); return NULL; } -string SACtostring (unsigned char* arr, sac_int length) +string SACtostring(unsigned char *arr, sac_int length) { - string res = malloc ((size_t) length + 1); + string res = malloc((size_t) length + 1); - memcpy (res, arr, (size_t)length); + memcpy(res, arr,(size_t)length); res[length] = '\0'; return res; } -string SACautotostring (SACarg *sarr) +string SACautotostring(SACarg *sarr) { const string *parr = SACARGgetSharedData(SACTYPE__MAIN__char, sarr); string arr = parr[0]; sac_int length = SACARGgetShape(sarr, 0); - string res = malloc ((size_t) length + 1); + string res = malloc((size_t) length + 1); - strncpy (res, arr, (size_t)length); + strncpy(res, arr,(size_t)length); res[length] = '\0'; SACARGdeleteSacArray(&sarr); return res; } -void SACstrmod (string str, sac_int pos, unsigned char c) +void SACstrmod(string str, sac_int pos, unsigned char c) { - size_t strlength = strlen (str); + size_t strlength = strlen(str); - if (strlength <= (size_t)pos) - SAC_RuntimeError("strmod: pos ("PRIisac") outside of string (length %zu)", pos, strlength); + if (strlength <=(size_t)pos) + SAC_RuntimeError("strmod: pos("PRIisac") outside of string(length %zu)", + pos, strlength); str[pos] = c; } -string SACstrins (string outer, sac_int pos, string inner) +string SACstrins(string outer, sac_int pos, string inner) { - size_t outer_length = strlen (outer); - size_t inner_length = strlen (inner); + size_t outer_length = strlen(outer); + size_t inner_length = strlen(inner); string res = malloc(outer_length + inner_length + 1); - strncpy(res, outer, (size_t)pos); + strncpy(res, outer,(size_t)pos); strcpy(res+(size_t)pos, inner); strcpy(res+(size_t)pos+inner_length, outer+(size_t)pos); return res; } -void SACstrovwt (string outer, sac_int pos, string inner) +void SACstrovwt(string outer, sac_int pos, string inner) { - size_t outer_length = strlen (outer); - size_t inner_length = strlen (inner); + size_t outer_length = strlen(outer); + size_t inner_length = strlen(inner); - if ((size_t)pos+inner_length > outer_length) - SAC_RuntimeError("strovwt: Overwriting string ends at position "PRIisac" while string is of length %zu", pos+inner_length, outer_length); + if ((size_t)pos+inner_length > outer_length) { + SAC_RuntimeError("strovwt: Overwriting string ends at position "PRIisac + " while string is of length %zu", + pos + inner_length, outer_length); + } - memcpy(outer+(size_t)pos, inner, inner_length * sizeof (char)); + memcpy(outer+(size_t)pos, inner, inner_length * sizeof(char)); } -unsigned char SACstrsel (string str, sac_int pos) +unsigned char SACstrsel(string str, sac_int pos) { size_t strlength = strlen(str); if ((size_t)pos >= strlength) - SAC_RuntimeError("strsel: pos ("PRIisac") outside of string (length %zu)", pos, strlength); + SAC_RuntimeError("strsel: pos("PRIisac") outside of string(length %zu)", + pos, strlength); return str[pos]; } -string SACstrcat (string fst, string snd) +string SACstrcat(string fst, string snd) { - size_t fstlength = strlen (fst); - size_t sndlength = strlen (snd); + size_t fstlength = strlen(fst); + size_t sndlength = strlen(snd); - string res = malloc (fstlength + sndlength + 1); + string res = malloc(fstlength + sndlength + 1); strcpy(res, fst); - strcpy(res+fstlength, snd); + strcpy(res + fstlength, snd); return res; } -string SACstrncat (string fst, string snd, sac_int n) +string SACstrncat(string fst, string snd, sac_int n) { - size_t fstlength = strlen (fst); + size_t fstlength = strlen(fst); - string res = malloc (fstlength + (size_t)n + 1); + string res = malloc(fstlength +(size_t)n + 1); strcpy(res, fst); strncpy(res+fstlength, snd, (size_t)n); @@ -120,95 +120,97 @@ string SACstrncat (string fst, string snd, sac_int n) return res; } -sac_int SACstrcmp (string fst, string snd) +sac_int SACstrcmp(string fst, string snd) { - return (sac_int) strcmp (fst, snd); + return (sac_int)strcmp(fst, snd); } -sac_int SACstrncmp (string fst, string snd, sac_int n) +sac_int SACstrncmp(string fst, string snd, sac_int n) { - return (sac_int) strncmp (fst, snd, (size_t) n); + return (sac_int)strncmp(fst, snd,(size_t) n); } -sac_int SACstrcasecmp (string fst, string snd) +sac_int SACstrcasecmp(string fst, string snd) { - return (sac_int) strcasecmp (fst, snd); + return (sac_int)strcasecmp(fst, snd); } -sac_int SACstrncasecmp (string fst, string snd, sac_int n) +sac_int SACstrncasecmp(string fst, string snd, sac_int n) { - return (sac_int) strncasecmp (fst, snd, (size_t) n); + return (sac_int)strncasecmp(fst, snd, (size_t)n); } -sac_int SACstrlen (string fst) +sac_int SACstrlen(string fst) { - return (sac_int) strlen (fst); + return (sac_int)strlen(fst); } void SACstrtake(string str, sac_int pos) { - size_t strlength = strlen (str); + size_t strlength = strlen(str); if ((size_t)pos >= strlength) { - SAC_RuntimeError("strtake: pos ("PRIisac") outside of string (length %zu)", pos, strlength); + SAC_RuntimeError("strtake: pos("PRIisac") outside of string(length %zu)", + pos, strlength); } str[pos] = '\0'; } -string SACstrdrop (string str, sac_int pos) +string SACstrdrop(string str, sac_int pos) { - size_t strlength = strlen (str); + size_t strlength = strlen(str); - string res = malloc(strlength - (size_t)pos + 1); + string res = malloc(strlength -(size_t)pos + 1); - strcpy(res, str+(size_t)pos); + strcpy(res, str + (size_t)pos); return res; } -string SACstrext (string str, sac_int pos, sac_int len) +string SACstrext(string str, sac_int pos, sac_int len) { size_t strlength = strlen(str); - string res = malloc(strlength - (size_t)len + 1); + string res = malloc(strlength -(size_t)len + 1); if ((size_t)(pos + len) >= strlength) { - SAC_RuntimeError("strext: Selecting substring ends at position "PRIisac" while string is of length %zu", pos+len, strlength); + SAC_RuntimeError("strext: Selecting substring ends at position "PRIisac + " while string is of length %zu", + pos + len, strlength); } - strncpy(res, str+(size_t)pos, (size_t)len); + strncpy(res, str + (size_t)pos, (size_t)len); return res; } -bool chr_in_delims (char c, string delimiters) +bool chr_in_delims(char c, string delimiters) { - while (true) - { + while (true) { if (delimiters[0] == '\0') { return false; } - + if (delimiters[0] == c) { return true; } - + delimiters++; } } -string next_in_delims (string str, string delimiters, bool is_delimiter) +string next_in_delims(string str, string delimiters, bool is_delimiter) { while (true) { if (str[0] == '\0') { return NULL; } - + if (chr_in_delims(str[0], delimiters) == is_delimiter) { return str; } - + str++; } } @@ -219,8 +221,7 @@ static char fmtmodifiers[] = "+- #.*0123456789"; string find_fmtstr_spec_loc(string format) { - do - { + do { // Find % format = strchr(format, fmtstart); // Skip modifiers @@ -228,17 +229,17 @@ string find_fmtstr_spec_loc(string format) } // Check if at end, or delimiter has been found while (format != NULL && !chr_in_delims(*format, fmtspecifiers)); + return format; } -string fix_fmtstr_sac_int (string format) +string fix_fmtstr_sac_int(string format) { // Count the number of replaces that need to occur, to comput the format string new size size_t replaces = 0; string fmt = find_fmtstr_spec_loc(format); - while (fmt != NULL) - { - replaces ++; + while (fmt != NULL) { + replaces++; fmt = find_fmtstr_spec_loc(fmt); } @@ -246,22 +247,19 @@ string fix_fmtstr_sac_int (string format) // For each replace, we remove 1 character and add the length of PRIisac size_t priisaclength = strlen(PRIisac); size_t formatlength = strlen(format); - string res = malloc ((formatlength + replaces * (formatlength-1) + 1) * sizeof (char)); + string res = malloc((formatlength + replaces *(formatlength-1) + 1) * sizeof(char)); // Copy parts of strings into the result string start = format; string respos = res; - while (format != NULL) - { + while (format != NULL) { // Find the next occurence of a format string specifier to be found format = find_fmtstr_spec_loc(start); - if (format == NULL) - { + + if (format == NULL) { // If at end, simply copy over the rest of the string strcpy(respos, start); - } - else - { + } else { // If found, copy the part of the string until the specifier, including the % and modifiers strncpy(respos, start, format-start); respos += format-start; @@ -276,16 +274,16 @@ string fix_fmtstr_sac_int (string format) return res; } -string SACsprintf (string format_raw, ...) +string SACsprintf(string format_raw, ...) { string format = fix_fmtstr_sac_int(format_raw); va_list args; // Compute the length of the string - va_start (args, format_raw); - int lengthwo0 = vsnprintf (NULL, 0, format, args); - va_end (args); + va_start(args, format_raw); + int lengthwo0 = vsnprintf(NULL, 0, format, args); + va_end(args); if (lengthwo0 < 0) SAC_RuntimeError("printf error %d", lengthwo0); @@ -296,96 +294,96 @@ string SACsprintf (string format_raw, ...) string res = malloc(length); // Printf string - va_start (args, format_raw); - vsnprintf (res, length, format, args); - va_end (args); + va_start(args, format_raw); + vsnprintf(res, length, format, args); + va_end(args); return res; } -sac_int SACsscanf (string s, string format_raw, ...) +sac_int SACsscanf(string s, string format_raw, ...) { string format = fix_fmtstr_sac_int(format_raw); - + va_list arg_p; - va_start( arg_p, format_raw); - sac_int res = (sac_int)vsscanf( s, format, arg_p); - va_end( arg_p); + va_start(arg_p, format_raw); + sac_int res =(sac_int)vsscanf(s, format, arg_p); + va_end(arg_p); return res; } -string SACsscanf_str (string source, string format_raw) +string SACsscanf_str(string source, string format_raw) { string format = fix_fmtstr_sac_int(format_raw); - string res = malloc (strlen (source) + 1); + string res = malloc(strlen(source) + 1); res[0] = '\0'; - sscanf (source, format, res); + sscanf(source, format, res); return res; } -sac_int SACstrchr (string str, unsigned char c) +sac_int SACstrchr(string str, unsigned char c) { - string occurrence = strchr (str, c); + string occurrence = strchr(str, c); if (occurrence == NULL) { return -1; } - - return (sac_int)(occurrence - str); + + return(sac_int)(occurrence - str); } -sac_int SACstrrchr (string str, unsigned char c) +sac_int SACstrrchr(string str, unsigned char c) { - string occurrence = strrchr (str, c); + string occurrence = strrchr(str, c); if (occurrence == NULL) { return -1; } - - return (sac_int)(occurrence - str); + + return(sac_int)(occurrence - str); } sac_int SACstrcspn(string str, string reject) { - return (sac_int) strcspn(str, reject); + return (sac_int)strcspn(str, reject); } sac_int SACstrspn(string str, string accept) { - return (sac_int) strspn(str, accept); + return (sac_int)strspn(str, accept); } -sac_int SACstrstr (string haystack, string needle) +sac_int SACstrstr(string haystack, string needle) { - return (sac_int) strstr (haystack, needle); + return (sac_int)strstr(haystack, needle); } -void SACstrtok (string* out_token, string* out_rest, string str, string delimiters) +void SACstrtok(string* out_token, string* out_rest, string str, string delimiters) { string start = next_in_delims(str, delimiters, false); string end = next_in_delims(start, delimiters, true); size_t tokenlength = end - start; - *out_token = malloc((tokenlength + 1) * sizeof (char)); + *out_token = malloc((tokenlength + 1) * sizeof(char)); strncpy(*out_token, start, tokenlength); *out_token[tokenlength] = '\0'; - *out_rest = malloc((strlen(end) + 1) * sizeof (char)); + *out_rest = malloc((strlen(end) + 1) * sizeof(char)); strcpy(*out_rest, end); } -void SACchomp (string str) +void SACchomp(string str) { string end = str + strlen(str); - while (end > str) - { - end --; + while (end > str) { + end--; + if (*end == '\n' || *end == '\r') { *end = '\0'; } else { @@ -394,13 +392,13 @@ void SACchomp (string str) } } -void SACrtrim (string str) +void SACrtrim(string str) { string end = str + strlen(str); - while (end > str) - { + while (end > str) { end --; + if (isspace(*end)) { *end = '\0'; } else { @@ -409,116 +407,116 @@ void SACrtrim (string str) } } -string SACltrim (string str) +string SACltrim(string str) { string end = str + strlen(str); - while (str < end) - { + while (str < end) { if (!isspace(*end)) { *end = '\0'; } else { break; } - str ++; + + str++; } - string res = malloc ((strlen(str) + 1) * sizeof (char)); + string res = malloc((strlen(str) + 1) * sizeof(char)); strcpy(res, str); - + return res; } -string SACtrim (string str) +string SACtrim(string str) { string res = SACltrim(str); - SACrtrim (res); + SACrtrim(res); return res; } -sac_int SACstrtoi (string* remain, string input, sac_int base) +sac_int SACstrtoi(string* remain, string input, sac_int base) { string rem; - sac_int res = (sac_int) strtol (input, &rem, (size_t)base); + sac_int res =(sac_int) strtol(input, &rem,(size_t)base); - *remain = malloc ((strlen(rem) + 1) * sizeof (char)); - strcpy (*remain, rem); + *remain = malloc((strlen(rem) + 1) * sizeof(char)); + strcpy(*remain, rem); return res; } -float SACstrtof (string* remain, string input) +float SACstrtof(string* remain, string input) { string rem; - float res = (float) strtod (input, &rem); + float res =(float) strtod(input, &rem); - *remain = malloc ((strlen(rem) + 1) * sizeof (char)); - strcpy (*remain, rem); + *remain = malloc((strlen(rem) + 1) * sizeof(char)); + strcpy(*remain, rem); return res; } -double SACstrtod (string* remain, string input) +double SACstrtod(string* remain, string input) { string rem; - double res = strtod (input, &rem); + double res = strtod(input, &rem); - *remain = malloc ((strlen(rem) + 1) * sizeof (char)); - strcpy (*remain, rem); + *remain = malloc((strlen(rem) + 1) * sizeof(char)); + strcpy(*remain, rem); return res; } -sac_int SACtoi (string input) +sac_int SACtoi(string input) { - return (sac_int) strtol (input, NULL, 0); + return (sac_int)strtol(input, NULL, 0); } -float SACtof (string input) +float SACtof(string input) { - return (float) strtod (input, NULL); + return(float) strtod(input, NULL); } -double SACtod (string input) +double SACtod(string input) { - return strtod (input, NULL); + return strtod(input, NULL); } -string SACitos (sac_int n) +string SACitos(sac_int n) { string res = malloc(40); - sprintf (res, "%"PRIisac, n); + sprintf(res, "%"PRIisac, n); return res; } -string SACftos (float n) +string SACftos(float n) { string res = malloc(40); - sprintf (res, "%g", n); + sprintf(res, "%g", n); return res; } -string SACdtos (double n) +string SACdtos(double n) { string res = malloc(40); - sprintf (res, "%g", n); + sprintf(res, "%g", n); return res; } -string SACbtos (bool n) +string SACbtos(bool n) { string res = malloc(6 * sizeof(char)); if (n) { - strcpy (res, "true"); + strcpy(res, "true"); } else { - strcpy (res, "false"); + strcpy(res, "false"); } return res; diff --git a/src/structures/src/String/Str.h b/src/structures/src/String/Str.h index f84ce263..648a546a 100644 --- a/src/structures/src/String/Str.h +++ b/src/structures/src/String/Str.h @@ -1,52 +1,57 @@ -#ifndef STDLIB__STR__H -#define STDLIB__STR__H +#ifndef _SAC_STR_H_ +#define _SAC_STR_H_ + +/* + * C implementation of standard module String. + */ -#include "sacinterface.h" #include -typedef char* string; +#include "sacinterface.h" + +typedef char *string; -string copy_string (string s); -string free_string (string s); +string copy_string(string s); +string free_string(string s); -string SACtostring (unsigned char* arr, sac_int length); -string SACautotostring (SACarg *sarr); -void SACstrmod (string str, sac_int pos, unsigned char c); -string SACstrins (string outer, sac_int pos, string inner); -void SACstrovwt (string outer, sac_int pos, string inner); -unsigned char SACstrsel (string str, sac_int pos); -string SACstrcat (string fst, string snd); -string SACstrncat (string fst, string snd, sac_int n); -sac_int SACstrcmp (string fst, string snd); -sac_int SACstrncmp (string fst, string snd, sac_int n); -sac_int SACstrcasecmp (string fst, string snd); -sac_int SACstrncasecmp (string fst, string snd, sac_int n); -sac_int SACstrlen (string fst); +string SACtostring(unsigned char *arr, sac_int length); +string SACautotostring(SACarg *sarr); +void SACstrmod(string str, sac_int pos, unsigned char c); +string SACstrins(string outer, sac_int pos, string inner); +void SACstrovwt(string outer, sac_int pos, string inner); +unsigned char SACstrsel(string str, sac_int pos); +string SACstrcat(string fst, string snd); +string SACstrncat(string fst, string snd, sac_int n); +sac_int SACstrcmp(string fst, string snd); +sac_int SACstrncmp(string fst, string snd, sac_int n); +sac_int SACstrcasecmp(string fst, string snd); +sac_int SACstrncasecmp(string fst, string snd, sac_int n); +sac_int SACstrlen(string fst); void SACstrtake(string str, sac_int pos); -string SACstrdrop (string str, sac_int pos); -string SACstrext (string str, sac_int pos, sac_int len); -string SACsprintf (string format, ...); -sac_int SACsscanf( string s, string format, ...); -string SACsscanf_str (string s, string format); -sac_int SACstrchr (string str, unsigned char c); -sac_int SACstrrchr (string str, unsigned char c); +string SACstrdrop(string str, sac_int pos); +string SACstrext(string str, sac_int pos, sac_int len); +string SACsprintf(string format, ...); +sac_int SACsscanf(string s, string format, ...); +string SACsscanf_str(string s, string format); +sac_int SACstrchr(string str, unsigned char c); +sac_int SACstrrchr(string str, unsigned char c); sac_int SACstrcspn(string str, string reject); sac_int SACstrspn(string str, string accept); -sac_int SACstrstr (string haystack, string needle); -void SACstrtok (string* out_token, string* out_rest, string str, string delimiters); -void SACchomp (string str) ; -void SACrtrim (string str) ; -string SACltrim (string str) ; -string SACtrim (string str); -sac_int SACstrtoi (string* remain, string input, sac_int base); -float SACstrtof (string* remain, string input); -double SACstrtod (string* remain, string input); -sac_int SACtoi (string input); -float SACtof (string input); -double SACtod (string input); -string SACitos (sac_int n); -string SACftos (float n); -string SACdtos (double n); -string SACbtos (bool n); +sac_int SACstrstr(string haystack, string needle); +void SACstrtok(string *out_token, string *out_rest, string str, string delimiters); +void SACchomp(string str); +void SACrtrim(string str); +string SACltrim(string str); +string SACtrim(string str); +sac_int SACstrtoi(string *remain, string input, sac_int base); +float SACstrtof(string *remain, string input); +double SACstrtod(string *remain, string input); +sac_int SACtoi(string input); +float SACtof(string input); +double SACtod(string input); +string SACitos(sac_int n); +string SACftos(float n); +string SACdtos(double n); +string SACbtos(bool n); -#endif // STDLIB__TOSTRING__H \ No newline at end of file +#endif /* _SAC_STR_H_ */