Thursday, January 14, 2010

String Class - Implementation

The String class will consist of two variables, a length (an integer, which will support any length strings) and a character pointer to the string. As mentioned, this is not a C/C++ 0 terminated character array, the length value determines the length of the string. A zero length string will have the length set to zero and the character pointer set to null (i.e. zero). There will also be allowance for a String to hold a C string constant (i.e. const char *), where the length will be set to -1.

The functions to implement now are ones that will be needed for the Parser, mainly constructors for creating String instances from a character array including one from two pointers (one pointing to the beginning of the string and one pointing to the character after the last one of the same string), one from a pointer and a length, one from a just a length (just allocates the character array), and one from a C string constant pointer (for holding error strings). There are also constructors that create a null or zero length string (length is zero, pointer is zero) and one from another string instance. There is a destructor that deallocates the character array if there is one (i.e. allocated, for which length is greater than zero). Finally there are access functions for returning the length and the character array pointer.

The next group of functions to implement are for comparing strings. These include one for comparison two strings ignoring case, one for comparing a string to a character array ignoring case (needed for the Parser when searching the Operator Table), one for comparing two strings for equality, and one for comparing two strings for less than, greater than or equal to (needed for the BASIC string relational operators).

There is a group of functions for working with sub-strings, like the LEFT$, MID$ and RIGHT$ BASIC string functions. These include a set one character within a string (that was previously allocated; need by the Parser for creating string constants), one that gets a reference to a sub-string, one that sets a sub-string of a reference and a function that resets the length and pointer (so that no deallocation is performed when the instance is deleted).

Finally there is a group of functions that are expected to be needed for the run time module. These include one that copies one string to another, one that moves (or transfers) one string to another (resetting the source string afterward), one for concatenating one string to another and one for concatenating two strings creating a new string instance in the process.

New functions will be added or these functions will be modified as needed as the interactive BASIC compiler is developed.

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)