;string.i
		
strcpy		macro	
		ifnd	\2sw
		lea	100$(pc),a1
		bra	101$
100$		dc.b	\2,0
		ds.w	0
101$
		endc
		ifd	\2sw
		addrtxin	<\2>,a0		;source
		endc
		addrin	\1,a1
		move.l	a1,d0
		jsr	_strcpycode(pc)
		ifnd	Strcpy
		bra	_strcpyover
_strcpycode
141$		move.b	(a0)+,(a1)+
		bne.s	141$
		rts
Strcpy		equ	1
_strcpyover	
		endc
		endm
	
strncpy		macro	
		addrtxin	<\2>,a0		;source
		addrin	\1,a1
		varin	\3,d1
		move.l	a1,d0
		jsr	_strncpycode(pc)
		ifnd	Strncpy
		bra	_strncpyover
_strncpycode
141$		subq.l	#1,d1
		bmi	142$
		move.b	(a0)+,(a1)+
		bne.s	141$
142$		subq.l	#1,a1
		clr.b	(a1)
		rts
Strncpy		equ	1
_strncpyover	
		endc
		endm

strchr		macro			;finds a character
		addrin	\1,a0		;source
		varin	\2,d0
		jsr	_strchrcode(pc)
		ifnd	Strchr
		bra	_strchrover
_strchrcode
141$		move.b	(a0)+,d1
		beq	143$
		cmp.b	d1,d0
		bne.s	141$
142$		subq.l	#1,a0
		move.l	a0,d0
		rts
143$		moveq	#0,d0
		rts
Strchr		equ	1
_strchrover	
		endc
		endm

strstr		macro			;finds a string
		movem.l	d2/a2,-(sp)
		addrin	\1,a0		;source
		addrtxin	<\2>,a1		;find
	
		move.l	a1,a2
		jsr	_strstrcode(pc)
		movem.l	(sp)+,d2/a2
		ifnd	Strstr
		bra	_strstrover
_strstrcode
141$		move.l	#-1,d1
130$		addq.l	#1,d1
		move.b	0(a0,d1.l),d0
		move.b	0(a1,d1.l),d2
		beq	131$		;find
		cmp.b	d2,d0
		beq.s	130$
		tst.b	(a0)+
		beq	143$		;no
		bra	141$
142$	
131$		move.l	a0,d0
		rts
143$		moveq	#0,d0
		rts
Strstr		equ	1
_strstrover	
		endc
		endm

strswap		macro
		addrin	\2,a0
		addrin	\1,a1
		jsr	_strswapcode(pc)
		ifnd	Strswap
		bra	_strswapover
_strswapcode
		movem.l	a2/a3,-(sp)
		move.l	a0,a2
		move.l	a1,a3
		lea	getbuf(a4),a1
150$		move.b	(a0)+,(a1)+		;to buffer
		bne.s	150$
		move.l	a2,a0
		move.l	a3,a1
151$		move.b	(a1)+,(a0)+
		bne.s	151$
		lea	getbuf(a4),a0
		move.l	a3,a1
152$		move.b	(a0)+,(a1)+		;from  buffer
		bne.s	152$
		movem.l	(sp)+,a2/a3
		rts
Strswap		equ	1
_strswapover
		endc
		endm

strcat		macro
		addrtxin	<\2>,a0
		addrin	\1,a1
		jsr	_addstrcode(pc)
		ifnd	Addstr
		bra	_addstrover
_addstrcode	
142$		tst.b	(a1)+
		bne	142$
		sub.l	#1,a1
143$		move.b	(a0)+,(a1)+
		bne	143$
		sub.l	#1,a1
		move.l	a1,d0
		rts
Addstr		equ	1
_addstrover
		endc
		endm	

strncat		macro
		addrtxin	<\2>,a0
		addrin	\1,a1
		varin	\3,d1
		move.l	a1,d0
		jsr	_addnstrcode(pc)
		ifnd	Addnstr
		bra	_addnstrover
_addnstrcode	
142$		tst.b	(a1)+
		bne	142$
		sub.l	#1,a1
143$		subq.l	#1,d1
		bmi	144$
		move.b	(a0)+,(a1)+
		bne	143$
144$		sub.l	#1,a1
		clr.b	(a1)
		move.l	a1,d0
		rts
Addnstr		equ	1
_addnstrover
		endc
		endm	

strcmp		macro
		addrin	\1,a0
		addrtxin	<\2>,a1
		jsr	_strcmpcode(pc)
		ifnd	Strcmpc
		bra	Strcmpover
_strcmpcode	
145$		move.b	(a0)+,d0
		beq	146$			;end of the string
		cmp.b	(a1)+,d0
		beq	145$
		bls	148$
147$		moveq	#1,d0			;less alphabetic
		rts
148$		moveq	#-1,d0			;bigger alphabetic
		rts
146$		tst.b	(a1)
		bne	148$
		moveq	#0,d0			;equal
		rts
Strcmpc		set	1
Strcmpover
		endc
		endm

strcmpa		macro
		addrin	\1,a0
		addrtxin	<\2>,a1
		jsr	_strccmpcode(pc)
		ifnd	Strccmpc
		bra	Strccmpover
_strccmpcode	
145$		move.b	(a0)+,d0
		beq	146$			;end of the string
		cmp.b	#'a',d0
		blt	150$
		cmp.b	#'z',d0
		bhi	150$
		sub.b	#32,d0
150$		move.b	(a1)+,d1
		cmp.b	#'a',d1
		blt	151$
		cmp.b	#'z',d1
		bhi	151$
		sub.b	#32,d1
151$		cmp.b	d1,d0
		beq	145$
		bls	148$
147$		moveq	#1,d0			;less alphabetic
		rts
148$		moveq	#-1,d0			;bigger alphabetic
		rts
146$		tst.b	(a1)
		bne	148$
		moveq	#0,d0			;equal
		rts
Strccmpc		set	1
Strccmpover
		endc
		endm

strncmp		macro
		addrin	\1,a0
		addrtxin	<\2>,a1
		varin	\3,d1
		jsr	_strncmpcode(pc)
		ifnd	Strncmpc
		bra	Strncmpover
_strncmpcode	
145$		subq.l	#1,d1
		bmi	149$
		move.b	(a0)+,d0
		beq	146$			;end of the string
		cmp.b	(a1)+,d0
		beq	145$
		bls	148$
147$		moveq	#1,d0			;less alphabetic
		rts
148$		moveq	#-1,d0			;bigger alphabetic
		rts
146$		tst.b	(a1)
		bne	148$
149$		moveq	#0,d0			;equal
		rts
Strncmpc		set	1
Strncmpover
		endc
		endm

strend	macro
		addrin	\1,a0
200$		tst.b	(a0)+
		bne		200$
		move.l	a0,d0
		subq.l	#1,d0
	    endm

isalnum		macro
		jsr	isalnmcd(pc)
		ifnd	_isalnum
		bra	_isalnumovr
isalnmcd	isalpha	\1
		bne	201$
		isdigit	\1
		bne	201$
		moveq	#0,d0
201$		rts
_isalnum	set	1
_isalphanumovr
		endc
	    endm

isalpha		macro
		varin	\1,d0
		jsr	isalphacd(pc)
		ifnd	_isalpha
		bra	_isalphaovr
isalphacd		cmp.b	#'A',d0
		blt		202$
		cmp.b	#'z',d0
		bhi		202$
		cmp.b	#'a',d0
		bhs		203$
		cmp.b	#'Z',d0
		bls		203$
202$		moveq	#0,d0
203$		rts
_isalpha	set	1
_isalphaovr	
		endc
	    endm

isdigit	macro
		varin	\1,d0
		jsr	isdigitcd(pc)
		ifnd	_isdigit
		bra	_isdigitovr
isdigitcd	cmp.b	#'0',d0
		blt	204$
		cmp.b	#'9',d0
		bhi	204$
		rts
204$		moveq	#0,d0
		rts
_isnumeric1	rts
_isdigit	set	1
_isdigitcd
		endc
	  endm

strupr		macro
		addrin	\1,a0
		jsr	struprcd(pc)
		ifnd	_struprfl
		bra	_struprovr
struprcd
204$		move.b	(a0),d0
		beq	203$
		cmp.b	#'a',d0
		blt	205$
		cmp.b	#'z',d0
		bhi	205$
		sub.b	#32,d0
205$		move.b	d0,(a0)+
		bra	204$
203$		rts
_struprfl	set	1
_struprovr
		endc
	    endm

strlwr	macro
		addrin	\1,a0
		jsr	_strlwrcd(pc)
		ifnd	_strlwrfl
		bra	_strlwrovr
_strlwrcd
204$		move.b	(a0),d0
		beq	205$
		cmp.b	#'A',d0
		blt	206$
		cmp.b	#'Z',d0
		bhi	206$
		add.b	#32,d0		;tolower case
206$		move.b	d0,(a0)+
		bra	204$
205$		rts
_strlwrfl	set	1
_strlwrovr
		endc
	    endm

ucase	macro
		varin	\1,d0
		jsr	ucasecd
		ifnd	_ucasefl	
		bra	_ucaseovr
ucasecd		cmp.b	#'a',d0
		blt	207$
		cmp.b	#'z',d0
		bhi	207$
		sub.b	#32,d0		;toupper case
207$		rts
_ucasefl	set	1
_ucaseovr
		endc
	    endm

locase	macro
		varin	\1,d0
		jsr	_locasecd(pc)
		ifnd	_locasefl
		bra	_locaseovr
_locasecd	cmp.b	#'A',d0
		blt	208$
		cmp.b	#'Z',d0
		bhi	208$
		add.b	#32,d0
208$		rts
_locasefl	set	1
_locaseovr
		endc
	    endm


skippblk	macro			;find space a posotion
_STRskipblk	moveq	#0,d0
_sskipblk1	cmp.b	#32,(a0)
		beq	_sskipblk2
		cmp.b	#10,(a0)
		beq	_sskipblk2
		cmp.b	#9,(a0)
		beq	_sskipblk2
		rts
_sskipblk2	addq.l	#1,a0
		addq.l	#1,d0
		bra	_sskipblk1

	    endm

isspace		macro
		varin	\1,d0
		jsr	_isblankcd
		ifnd	_isspacefl
		bra	_isspaceovr
_isblankcd	cmp.b	#32,d0
		beq	209$
		cmp.b	#10,d0
		beq	209$
		cmp.b	#9,d0
		moveq	#0,d0
209$		rts
_isspacefl	set	1
_isspaceovr
		endc
	    endm

strlen	macro				;lenght	of the string
		addrin	\1,a0
		jsr	strlencd(pc)
		ifnd	_strlenfl
		bra	_strlenovr
strlencd	moveq	#-1,d0
205$		addq.l	#1,d0
		tst.b	(a0)+
		bne	205$
		rts
_strlenfl	set	1
_strlenovr
		endc
	    endm

bcpl_strcpy	macro
		addrin	\1,a0
		addrin	\2,a1
		jsr	bcplcd(pc)
		ifnd	_bcplfl
		bra	_bcplovr
bcplcd		add.l	a1,a1
		add.l	a1,a1
		moveq	#0,d0
		move.b	(a1)+,d0
		subq.w	#1,d0
		bmi	206$
207$		move.b	(a1)+,(a0)+
		dbf	d0,207$
206$		clr.b	(a0)	
		rts
_bcplfl		set	1
_bcplovr
		endc
		endm

strsort		macro	;sort strings to the order
		ifnd	Strcmpc
		bra	strmovr
		strcmp	getbuf,getbuf	;ask	_strcmpcode
strmovr
		endc	
		movem.l	d2-d7/a2-a5,-(sp)
		addrin	\1,a2
		varin	\2,d2
		move.l	#\1dm2,d4
		jsr	qsortcd(pc)
		movem.l	(sp)+,d2-d7/a2-a5
		ifnd	qsortid
		bra	qsortovr
qsortcd		cmp.l	#2,d2
		blt	256$
		move.l	d2,d5
		mulu.w	d4,d5		;size
		asl.l	#2,d2		;4*count
		move.l	d2,d0
		add.l	d5,d0
		move.l	d2,d7
		move.l	#$10001,d1
		move.l	4,a6
		jsr	-198(a6)	;_LVOAllocMem(a6)
		tst.l	d0
		beq	256$		;no mem
		move.l	d0,a3
		move.l	d0,a4
		add.l	d2,a4
		move.l	a2,a0	;the first
		move.l	a3,a1
		move.l	d2,d0
		lsr.l	#2,d0
		subq.l	#1,d0
250$		move.l	a0,(a1)+	;addresses of the strings into a stack
		add.l	d4,a0
		dbf	d0,250$		
254$		moveq	#0,d6		flag
		move.l	#0,d3
		subq.l	#4,d2
253$		move.l	0(a3,d3.l),a1
		move.l	4(a3,d3.l),a0
		jsr	_strcmpcode
		tst.l	d0
		bpl	251$		;no swap
		move.l	0(a3,d3.l),a0		;swap
		move.l	4(a3,d3.l),0(a3,d3.l)
		move.l	a0,4(a3,d3.l)
		addq.l	#1,d6
251$		addq	#4,d3
		cmp.l	d3,d2
		bne	253$		;next
		cmp.l	#1,d6
		bge	254$		;check again
		moveq	#0,d3
		move.l	a4,a5
258$		move.l	0(a3,d3.l),a0
		move.l	a5,a1
257$		move.b	(a0)+,(a1)+
		bne	257$
		addq.l	#4,d3
		add.l	d4,a5
		cmp.l	d3,d7
		bne	258$
		lsr.l	#1,d5
		move.l	a4,a0
		move.l	a2,a1
259$		move.w	(a0)+,(a1)+
		dbf	d5,259$
255$		move.l	a3,a1
		move.l	d7,d0
		move.l	4,a6
		jsr	-210(a6)	;_LVOFreeMem(a6)
		move.l	a2,d0	
		rts
256$		moveq	#0,d0
		rts
qsortid		set	1
qsortovr
		endc		
		endm		

