home *** CD-ROM | disk | FTP | other *** search
- ;****************************************************************
- ; function Trim(S : String) : String
- ;
- ; Returns string with leading and trailing white space removed
- ; from S. White space is considered to be a space ' ' (ascii
- ; code 20h or 32 decimal), and all characters less than a
- ; space. In other words characters with ascii codes 0..32 are
- ; whitespace. If a line of all white space is passed as S,
- ; then an empty string '' will be returned.
- ;
- ; ex.
- ; Trim(' Hello there ') would return 'Hello there'
- ; Trim(#9#9'if C > 0 then'#9) would return 'if C > 0 then'
- ;
- ; by Richard S. Sadowsky
- ; version .5 7/7/88
- ; Copyright (c) 1988, Richard S. Sadowsky
- ; Los Angeles, California
- ;
- ;****************************************************************
-
-
- CODE SEGMENT BYTE PUBLIC
- ASSUME CS:CODE
-
- PUBLIC Trim
-
- ;****************************************************************
- ; Some equates to refer to the function result, and the function's
- ; string parameter.
- ;****************************************************************
-
- TrimResult EQU DWORD PTR [BP+0Ah]
- TrimStr EQU DWORD PTR [BP+06h]
-
- Trim PROC FAR
- PUSH BP ; save BP
- MOV BP,SP ; set up base pointer
- PUSH DS ; save DS, we use it
- LDS SI,TrimStr ; source str is function param
- LES DI,TrimResult ; dest str is function result
- CLD ; foward string ops
- XOR BX,BX ; use BX as counter
- LODSB ; get the length byte
- STOSB ; copy it to dest str
- XOR CH,CH ; zero high byte of CX
- MOV CL,AL ; mov str length into CL
- JCXZ ExitCode ; all done is length is 0
-
- ;****************************************************************
- ; First we search forward for white space. We continue looking
- ; until the first non white space character is found, or we
- ; have searched the whole string. When a non white space
- ; character is found, we fall into the NotWhiteSpace loop
- ; below.
- ;****************************************************************
-
- Leading:
- LODSB ; get next byte
- CMP AL,' ' ; Is it white space?
- JG NotWhiteSpace ; if not jump to NotWhiteSpace
- LOOP Leading ; repeat for next char
- JMP SHORT AdjLength ; done with string
-
- ;****************************************************************
- ; The follwoing code simply copies however many characters
- ; are left in the string to the destination. BL is used
- ; to count the characters copied. As soon as the first
- ; Non whitespace character is encountered in the string
- ; we fall into this Loop.
- ;****************************************************************
-
- NotWhiteSpace:
- STOSB ; copy the byte to dest
- INC BL ; inc our char count
- LODSB ; get next char in AL
- LOOP NotWhiteSpace ; loop while CX <> 0
-
- ;****************************************************************
- ; Not we adjust the length byte of the destination str to the
- ; number of characters we copied in the NotWhiteSpace loop.
- ;****************************************************************
-
- AdjLength:
- LES DI,TrimResult ; point to length byte in dest
- MOV ES:[DI],BL ; mov in new length
-
- ;****************************************************************
- ; Now we point to the last byte of the destination string and
- ; set the direction flag (backward string ops). We then see how
- ; many trailing white space characters there are, and subtract
- ; this number from the length byte.
- ;****************************************************************
-
- Trailing:
- XOR CH,CH ; zero high byte of CX
- MOV CL,BL ; length of dest string
- JCXZ ExitCode ; if zero we're done
-
- XOR DX,DX ; we use DX as a counter
- LDS SI,TrimResult ; Point to TrimRes
- ADD SI,BX ; point to last char in TrimRes
- STD ; backward string ops
-
- Trail: LODSB ; Get byte in AL
- CMP AL,' ' ; is it whitespace?
- JA AdjLength2 ; if not, then adjust length
- INC DL ; otherwise inc our counter
- LOOP Trail ; repeat for next char
-
- ;****************************************************************
- ; Now we adjust the length byte of the function result to the
- ; number of characters after trailing white space is removed.
- ; BL contains the length of the function result and DL contains
- ; the number of trailing white space characters to be removed.
- ;****************************************************************
-
- AdjLength2:
- OR DL,DL ; is DL zero?
- JZ ExitCode ; yes, so exit
- SUB BL,DL ; subtract DL from BL
- LES DI,TrimResult ; point to length of TrimRes
- MOV ES:[DI],BL ; mov in new length
-
- ExitCode:
- POP DS ; restore DS (or else)
- POP BP ; restore BP
- RET 04h ; pop parameters off stack
- Trim ENDP
-
- CODE ENDS
- END