- Added CStrings function definitions
- Added code for CString.Compare
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
(****************************************************************************
|
(****************************************************************************
|
||||||
* File name: CSTRINGS.D
|
* File name: CSTRINGS.D
|
||||||
* Created On: Fri 02 Oct 2020 07:27:00 PM CEST
|
* 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 <jali@orca-central.de>
|
* Author: Jali <jali@orca-central.de>
|
||||||
****************************************************************************
|
****************************************************************************
|
||||||
* Header file for an implementation of C-Like strings
|
* Header file for an implementation of C-Like strings
|
||||||
@@ -20,20 +20,88 @@ CONST
|
|||||||
|
|
||||||
TYPE
|
TYPE
|
||||||
CString : POINTER TO CHAR;
|
CString : POINTER TO CHAR;
|
||||||
|
CompareResult = ( Before, Equal, After ); (* The sort order for Compare() *)
|
||||||
|
|
||||||
PROCEDURE Length(str : CString) : LONGCARD
|
(*
|
||||||
VAR
|
* Compare two CStrings and return a CompareResult
|
||||||
count : LONGCARD;
|
* that indicates their relative position in the sort order
|
||||||
countPtr : POINTER TO CHAR;
|
*
|
||||||
BEGIN
|
* # Parameters:
|
||||||
count = 0;
|
* - strA: The first string to compare
|
||||||
countPtr = str;
|
* - strB: The second string to compare
|
||||||
WHILE (countPtr^ <> NULL) BEGIN
|
*
|
||||||
count += 1;
|
* # Returns:
|
||||||
END; (* WHILE *)
|
* 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;
|
END CStrings;
|
||||||
|
|
||||||
|
|||||||
@@ -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 <jali@orca-central.de>
|
||||||
|
****************************************************************************
|
||||||
|
* 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: *)
|
(* vim: set filetype=modula2: *)
|
||||||
|
|||||||
Reference in New Issue
Block a user