At long, panful last, a functional version of exercise 1-17. The code has contracted considerably since the intiially architected version, with replicate() and its functionality having been eliminated as unnecessary, saving numerous cycles and ~64kB of memory, advance() has been eliminated due to creating needless complexity, hindering readability, and decreasing efficiency, and recall has been eliminated because it depended on replicate, and was also unnecessary as enumerate() now contains three lines of code that completely eliminate the need for replicate() or enumerate().

This commit is contained in:
Reina Harrington-Affine 2025-09-19 00:00:30 +00:00
parent 848898ff11
commit 8665f996e5

View file

@ -4,12 +4,15 @@
#define maxOutputSize 32100 #define maxOutputSize 32100
char buffer[maxInputSize] = { 0 }; char buffer[maxInputSize] = { 0 };
char output[maxOutputSize] = { 0 };
void store(char to[]); // Complete void store(char to[]); // Complete
void enumerate(char data[], char output[]); // Complete, pending testing. Needs replicate() before testing. void enumerate(char data[]); // Complete, pending testing.
void enumerateAdvance(int extDataCrsr, char accessedData, char externalBuffer[]);// Complete, pending testing.
void replicate(char to[], char from[]);// Not Started int main(){
store(buffer);
enumerate(buffer);
return 0;
}
//Status: Complete & Tested //Status: Complete & Tested
void store(char to[]){ void store(char to[]){
@ -20,21 +23,11 @@ void store(char to[]){
} }
} }
//Status: Complete, pending testing. Not to be used unil enumerate() has been
//validated, at which point the for loop of enumerate can be modified to use
//enumerateAdvance() as its completion action rather than calling an increment
//and char fetch at the end of each loop independently. This function exists
//purely for readability and style.
void enumerateAdvance(int extDataCrsr, char accessedData, char externalBuffer[]){
++extDataCrsr;
accessedData = externalBuffer[extDataCrsr];
}
//Status: Complete, pending testing. Not testable until replicate is complete. //Status: Complete, pending testing. Not testable until replicate is complete.
void enumerate(char data[], char output[]){ void enumerate(char data[]){
int dataCrsr = 0; // Stores currently accessed position in data[] int dataCrsr = 0; // Stores currently accessed position in data[]
int wscursor = 0; //Stores the cursor position in workingStorage int wscursor = 0; //Stores the cursor position in ekorkingStorage
int lineLen = 0; // Stores the length of the current line being enumerated. int lineLen = 0; // Stores the length of the current line being enumerated.
char workingStorage[maxOutputSize] = { 0 }; // Stores processed data until char workingStorage[maxOutputSize] = { 0 }; // Stores processed data until
// replicated to output. // replicated to output.
@ -45,24 +38,26 @@ void enumerate(char data[], char output[]){
if(current == '\n' && lineLen >= 80){ if(current == '\n' && lineLen >= 80){
//Reset line length in preparation for next dataset. //Reset line length in preparation for next dataset.
lineLen = 0; lineLen = 0;
//Increment wscursor and add a null byte at the end of valid data // Increment wscursor and add a null ek yte at the end of valid data
//for replicate to break on. // for replicate to break on.
++wscursor; ++wscursor;
workingStorage[wscursor] = 0; workingStorage[wscursor] = 0;
//Copy workingStorage into output. // Print line to stdout
replicate(output, workingStorage); for(wscursor = 0; workingStorage[wscursor] != 0;++wscursor){
//Reset wscuror. Existing data in workingStorage will be ovrwritten putchar(workingStorage[wscursor]);
//by next dataset. }
putchar('\n');
// Reset wscursor
wscursor = 0; wscursor = 0;
//Begin case: newline with unacceptable line length. // Begin case: newline with unacceptable line length.
} else if (current == '\n' && lineLen < 80){ } else if (current == '\n' && lineLen < 80){
//Reset line length and wscuror in preparation for nxet dataset. //Reset line length and wscuror in preparation for nxet dataset.
lineLen = 0; lineLen = 0;
wscursor = 0; wscursor = 0;
//Begin case: non-newline character. // Begin case: non-newline character.
}else if(current != '\n'){ }else if(current != '\n'){
//Increment line length, write to workingStorage at wscursor, and // Increment line length, write to workingStorage at wscursor, and
//increment wscursor. // increment wscursor.
++lineLen; ++lineLen;
workingStorage[wscursor] = current; workingStorage[wscursor] = current;
++wscursor; ++wscursor;
@ -72,5 +67,3 @@ void enumerate(char data[], char output[]){
current = data[dataCrsr]; current = data[dataCrsr];
} }
} }