Added module stringm

This commit is contained in:
Curtis F Kaylor 2021-09-20 20:56:07 -04:00
parent b1eded3399
commit d4d3453b59
3 changed files with 217 additions and 0 deletions

72
doc/stringm.txt Normal file
View File

@ -0,0 +1,72 @@
String Manipulation Module for C02
These functions are not part of the standard C and C++ string libraries.
They are included because they are more efficient than equivalent C02 code.
#include <stddef.h02>
#include <ctype.h02>
#include <string.h02>
The following functions are defined:
n = strapd(c, s); Appends character c to string s.
Returns the new length of the string.
Note: Calls strlen(), then writes c into the end
position and a 0 byte into the following position.
n = straps(s); Appends a space to string s.
Returns the new length of the string.
Note: Executes strspd with arguments ' ' and s.
n = strppd(c, s); Prepends character c to string s.
Returns the new length of the string.
Note: Calls strlen(), shifts the entire string one
character to the right and writes c to position 0.
n = strpps(s); Prepends a space to string s.
Returns the new length of the string.
Note: Executes strppd with arguments ' ' and s.
n = strpad(v,s); Right pads string s with spaces to length v.
If v is less than the current length of the
string. the string is not changed.
Returns the new length of the string.
Note: Calls strlen(), and if the result is less
than v, appends the appropriate number of spaces.
n = strlwr(s); Converts letters in string s to lowercase.
Returns the length of the string.
Note: Sets srcptr to &s, then iterates through
the string, calling tolowr() on each character,
n = strupr(s); Converts letters in string s to uppercase.
Returns the length of the string.
Note: Sets srcptr to &s, then iterates through
the string, calling touppr() on each character,
Note: This module expects the following functions to be defined
setdst(s); Set destination pointer
setsrc(s); Set source pointer and initialize index
along with the zero page variable pairs
srcptr Source String Pointer
dstptr Destination String Pointer

114
include/stringm.a02 Normal file
View File

@ -0,0 +1,114 @@
; C02 module stringm.h02 assembly language subroutines
; Requires external routines SETSRC, TOLOWR, and TOUPPR
; Requires the following RAM locations be defined
; external zero page word SRCPTR
; and external bytes TEMP0 and TEMP1
SUBROUTINE STRINGM
;straps(c, s) - Append space to string s
;Args: A = Character to be appended
; X,Y = Pointer to string
;Affects N,Z
;Returns A,Y = New String Length
STRAPS: LDA ' ' ;Set Append Character to Space
;strapd(c, s) - Append character c to string s
;Args: A = Character to be appended
; X,Y = Pointer to string
;Affects N,Z
;Returns A,Y = New String Length
STRAPD: STA TEMP0 ;Save Character to Append
JSR STRLEN ;Get Length of String
BMI .ERROR ;Return 255 if > 127
LDA TEMP0 ;Restore Character to Append
.APDLOP STA (SRCPTR),Y ;Store at End of String
BEQ .RETLEN ;Exit if NUL
INY ;Increment Past New Character
LDA #0 ;Terminate String
STA (SRCPTR),Y
BEQ .RETLEN ;and Append to String
;strpps(c, s) - Prepend space to string s
;Args: A = Character to be appended
; X,Y = Pointer to string
;Affects N,Z
;Returns A,Y = New String Length
STRPPS: LDA ' ' ;Set Append Character to Space
;strppd(c, s) - Prepend Character to String
;Args: A = Character to be appended
; X,Y = Pointer to string
;Sets: N,Z
;Returns A = New String Length
STRPPD: STA TEMP0 ;Save Character to Append
JSR STRLEN ;Get Length of String
BMI .ERROR ;Return 255 if > 127
INY ;Bump up string length
TYA ;Push string length onto stack
PHA
.PPDLOP DEY ;Copy Preceding Character
LDA (SRCPTR),Y
INY ;into Current Position
STA (SRCPTR),Y
DEY ;Move to Preceding Position
BNE .PPDLOP ;and Loop
LDA TEMP0 ;Retrieve Character
STA (SRCPTR),Y ;and Store in Position 0
PLA ;Retrieve new string length
RTS
.ERROR LDA #$FF ;Return Error Code FALSE
RTS
;STRPAD(n, &s) - Pad or Truncate String
;Args: A - New Length of String
; X,Y - Pointer to string
;Sets: SRCPTR = Pointer to source string
;Returns: A,Y = Length of string
; N,Z based on A
STRPAD: STA TEMP0 ;Save New Length
JSR STRLEN ;Get Current Length of String
CMP TEMP0
BCS .PDONE ;If Shorter than New Length
LDA #' '
.PLOOP STA (SRCPTR),Y ; Fill with Spaces
INY
BMI .PDONE ; Until 128 or
CPY TEMP0 ; New Length is Reached
BNE .PLOOP
.PDONE LDA #0 ;Terminate String
STA (SRCPTR),Y
TYA ;and Return Length
RTS
;STRLWR(&s) - Convert String to Lower Case
;Args: X,Y - Pointer to string
;Sets: SRCPTR = Pointer to source string
;Returns: A,Y = Length of string
; N,Z based on A
STRLWR: JSR SETSRC ;Initialize Source String
.LLOOP LDA (SRCPTR),Y ;Get Next Character
BEQ .RETLEN ;If <> NUL
JSR TOLOWR ; Convert to Lower Case
STA (SRCPTR),Y ; and Replace in String
INY ; Increment Index
BPL .LLOOP ; and Loop if < 128
TYA ;Transfer Index to Accumulator
RTS ;and Return
;STRUPR(&s) - Convert String to Lower Case
;Args: X,Y - Pointer to string
;Sets: SRCPTR = Pointer to source string
;Returns: A,Y = Length of string
; N,Z based on A
STRUPR: JSR SETSRC ;Initialize Source String
.ULOOP LDA (SRCPTR),Y ;Get Next Character
BEQ .RETLEN ;If <> NUL
JSR TOUPPR ; Convert to Lower Case
STA (SRCPTR),Y ; and Replace in String
INY ; Increment Index
BPL .ULOOP ; and Loop if < 128
.RETLEN TYA ;Transfer Index to Accumulator
RTS ;and Return
ENDSUBROUTINE

31
include/stringm.h02 Normal file
View File

@ -0,0 +1,31 @@
/**************************************************
* stringm - String Modification Routines for C02 *
**************************************************/
/* Append Character to String *
* Args: c - Character to append *
* s - String to append to *
* Returns: New length of string */
char strapd();
/* Convert String to Lowercase *
* Args: int &s - String *
* Returns: char l - Length */
char strlwr();
/* Pad or Truncate String *
* Args: char n - New Length *
* int &s - string *
* Returns: char l - Length */
char strpad();
/* Prepend Character to String *
* Args: c - Character to prepend *
* s - String to prepend to *
* Returns: New length of string */
char strppd();
/* Convert String to Uppercase *
* Args: int &s - String *
* Returns: char l - Length */
char strupr();