home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / pibterm / pibt41s4.arc / TRIM.MOD < prev    next >
Text File  |  1987-11-10  |  6KB  |  84 lines

  1. (*--------------------------------------------------------------------------*)
  2. (*               Trim --- Trim trailing blanks from a string                *)
  3. (*--------------------------------------------------------------------------*)
  4.  
  5. FUNCTION Trim( S : AnyStr ) : AnyStr;
  6.  
  7. (*--------------------------------------------------------------------------*)
  8. (*                                                                          *)
  9. (*     Function:   Trim                                                     *)
  10. (*                                                                          *)
  11. (*     Purpose:    Trims trailing blanks from a string                      *)
  12. (*                                                                          *)
  13. (*     Calling sequence:                                                    *)
  14. (*                                                                          *)
  15. (*         Trimmed_S := TRIM( S );                                          *)
  16. (*                                                                          *)
  17. (*            S          --- the string to be trimmed                       *)
  18. (*            Trimmed_S  --- the trimmed version of S                       *)
  19. (*                                                                          *)
  20. (*     Calls:  None                                                         *)
  21. (*                                                                          *)
  22. (*     Remarks:                                                             *)
  23. (*                                                                          *)
  24. (*        Note that the original string itself is left untrimmed.           *)
  25. (*                                                                          *)
  26. (*     Pascal version might be written as:                                  *)
  27. (*                                                                          *)
  28. (*        VAR                                                               *)
  29. (*           I:       INTEGER;                                              *)
  30. (*                                                                          *)
  31. (*        BEGIN                                                             *)
  32. (*                                                                          *)
  33. (*           I := ORD( S[0] );                                              *)
  34. (*                                                                          *)
  35. (*           WHILE ( I > 0 ) AND ( S[I] = ' ' ) DO                          *)
  36. (*              I := PRED( I );                                             *)
  37. (*                                                                          *)
  38. (*           S[0] := CHR( I );                                              *)
  39. (*           Trim := S;                                                     *)
  40. (*                                                                          *)
  41. (*        END;                                                              *)
  42. (*                                                                          *)
  43. (*--------------------------------------------------------------------------*)
  44.  
  45. BEGIN (* Trim *)
  46.  
  47. INLINE(
  48.   $1E/                   {         PUSH    DS                ; Save DS}
  49.                          {;}
  50.   $C5/$76/$06/           {         LDS     SI,[BP+6]         ; Get address of S}
  51.   $FC/                   {         CLD                       ; Forward search}
  52.   $AC/                   {         LODSB                     ; Get length of S}
  53.   $3C/$00/               {         CMP     AL,0              ; See if length 0}
  54.   $74/$21/               {         JE      Trim2             ; If so, no trimming required}
  55.                          {;}
  56.   $30/$ED/               {         XOR     CH,CH}
  57.   $88/$C1/               {         MOV     CL,AL             ; Remember length for search loop}
  58.                          {;}
  59.   $B0/$20/               {         MOV     AL,' '            ; Blank to AL}
  60.                          {;}
  61.   $C4/$7E/$06/           {         LES     DI,[BP+6]         ; Get address of S}
  62.   $01/$CF/               {         ADD     DI,CX             ; Point to end of source string}
  63.                          {;}
  64.   $FD/                   {         STD                       ; Backwards search}
  65.   $F3/$AE/               {         REPE    SCASB             ; Scan over blanks}
  66.   $74/$01/               {         JE      Trim1             ; If CX=0, entire string is blank.}
  67.   $41/                   {         INC     CX}
  68.                          {;}
  69.   $88/$C8/               {Trim1:   MOV     AL,CL             ; Length to copy}
  70.   $C5/$76/$06/           {         LDS     SI,[BP+6]         ; Source string address}
  71.   $46/                   {         INC     SI                ; Skip length}
  72.   $C4/$7E/$0A/           {         LES     DI,[BP+10]        ; Result string address}
  73.   $FC/                   {         CLD                       ; Forward move}
  74.   $AA/                   {         STOSB                     ; Set length in result}
  75.   $F2/$A4/               {         REP     MOVSB             ; Move trimmed result}
  76.   $E9/$04/$00/           {         JMP     Exit}
  77.                          {;}
  78.   $C4/$7E/$0A/           {Trim2:   LES     DI,[BP+10]        ; Result string address}
  79.   $AA/                   {         STOSB                     ; Set length=0 in result}
  80.                          {;}
  81.   $1F);                  {Exit:    POP     DS                ; Restore DS}
  82.  
  83. END   (* Trim *);
  84.