diff --git a/src/cstrings.d b/src/cstrings.d index 1134b36..5fce9b2 100644 --- a/src/cstrings.d +++ b/src/cstrings.d @@ -1,7 +1,7 @@ (**************************************************************************** * File name: CSTRINGS.D * Created On: Fri 02 Oct 2020 07:27:00 PM CEST - * Last Modified: Fri 02 Oct 2020 07:46:49 PM CEST + * Last Modified: Sat 03 Oct 2020 01:59:46 PM CEST * Author: Jali **************************************************************************** * Header file for an implementation of C-Like strings @@ -20,20 +20,88 @@ CONST TYPE CString : POINTER TO CHAR; + CompareResult = ( Before, Equal, After ); (* The sort order for Compare() *) -PROCEDURE Length(str : CString) : LONGCARD - VAR - count : LONGCARD; - countPtr : POINTER TO CHAR; -BEGIN - count = 0; - countPtr = str; - WHILE (countPtr^ <> NULL) BEGIN - count += 1; - END; (* WHILE *) +(* + * Compare two CStrings and return a CompareResult + * that indicates their relative position in the sort order + * + * # Parameters: + * - strA: The first string to compare + * - strB: The second string to compare + * + * # Returns: + * An integer, which is + * - less than zero, if strA is before strB in the sort order + * - zero if both strings are identical + * - greater than zero, if str + *) +PROCEDURE Compare(strA, strB : CString) : CompareResult; - RETURN count; -END Length; +(* + * Concatanates two strings, and returns a new string, that contains of both + * strings. + * + * # Parameters: + * - strA: The original string + * - strB: The second string to add to strA + * + * # Retruns: + * A new CString, that consists of strA followed by strB + *) +PROCEDURE Concat(strA, strB : CString) : CString; + +(* + * Checks if *subString* is contained in *string*, and returns the result. + * + * # Parameters: + * - string: The string to compare to + * - subString: The substring to search + * + * # Returns: + * TRUE, if subString is part of string, else false. + *) +PROCEDURE Contains(string, subString : CString) : BOOLEAN; + +(* + * Checks, if the CString is NIL or the the string only contains a NULL char. + * + * # Parameters: + * - string: The string to check + * + * # Returns: + * TRUE, if the CString is NIL, or only contains the NULL character + *) +PROCEDURE IsNilOrEmpty(string : CString) : BOOLEAN; + +(* + * Checks, if the CString is NIL or the the string only contains a white space + * characters. + * + * # Parameters: + * - string: The string to check + * + * # Returns: + * TRUE, if the CString is NIL, or only contains a white space character + *) +PROCEDURE IsNilOrWhiteSpace(string : CString) : BOOLEAN; + +(* + * Returns the length of the string stored in + * the CString. + *) +PROCEDURE Length(str : CString) : LONGCARD; + +(* + * Converts the string into a dynamic ARRAY OF CHAR. + * + * # Parameters: + * - string: The string to convert + * + * # Returns: + * An ARRAY OF CHAR that contains the original string. + *) +PROCEDURE ToArray(string : CString) : ARRAY OF CHAR; END CStrings; diff --git a/src/cstrings.i b/src/cstrings.i index 9a42203..1e94204 100644 --- a/src/cstrings.i +++ b/src/cstrings.i @@ -1 +1,49 @@ +(**************************************************************************** + * File name: CSTRINGS.I + * Created On: Sat 03 Oct 2020 03:18:19 PM CEST + * Last Modified: Sat 03 Oct 2020 03:18:19 PM CEST + * Author: Jali + **************************************************************************** + * Implementation for handling C-Like strings + ****************************************************************************) + +IMPLEMENTATION MODULE CStrings; + +PROCEDURE Compare(strA : CString, strB : CString) : CompareResult +(* $L- *) +BEGIN + ASSEMBLER + MOVE.L -(A3),A1 ; Read strB from the stack + MOVE.L -(A3),A0 ; Read strA from the stack + MOVE.W 1,D2 ; Preset the return value to CompareResult.Equal + Loop: + MOVE.S D0,(A0)+ ; Read a byte from strA into D0, and increase the pointer + CMP.S 0,D0 ; Check if the value is NULL + BEQ Return ; Reached the end of the first string; break + MOVE.S D1,(A1)+ ; Read a byte from strB into S1 and increase the pointer + CMP.S 0,D0 ; Check if the byte is NULL + BEQ Return ; Reached the end of the second string; break + CMP.S D0,D1 ; Compare the two values for equality + BEQ Loop ; If equal, continue with the next bytes. + MOVE.W 2,D2 ; Set the result register to ComepareResult.After + BHI Return ; Break and retrun result + MOVE.W 0,D2 ; Set the result register to CompareResult.Before + Return: + MOVE D2,(A3)+ ; Push the result to the stack + END; +(* $L+ *) +END Compare; + +PROCEDURE Concat(strA, strB : CString) : CString + VAR + len : LONGCARD; + newString : ARRAY + resultString : POINTER TO CHAR; +BEGIN + len := Length(strA) + Length(strB); + +END Concat; + +END CStrings; + (* vim: set filetype=modula2: *)