home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / msdos / pibterm / pibt41s2.arc / LTRIM.MOD < prev    next >
Encoding:
Text File  |  1987-11-11  |  5.7 KB  |  87 lines

  1. (*--------------------------------------------------------------------------*)
  2. (*               LTrim --- LTrim leading blanks from a string               *)
  3. (*--------------------------------------------------------------------------*)
  4.  
  5. FUNCTION LTrim( S : AnyStr ) : AnyStr;
  6.  
  7. (*--------------------------------------------------------------------------*)
  8. (*                                                                          *)
  9. (*     Function:   LTrim                                                    *)
  10. (*                                                                          *)
  11. (*     Purpose:    Trims leading blanks from a string                       *)
  12. (*                                                                          *)
  13. (*     Calling sequence:                                                    *)
  14. (*                                                                          *)
  15. (*         LTrimmed_S := TRIM( S );                                         *)
  16. (*                                                                          *)
  17. (*            S           --- the string to be trimmed                      *)
  18. (*            LTrimmed_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. (*     A Pascal version of this routine could be written as:                *)
  27. (*                                                                          *)
  28. (*        VAR                                                               *)
  29. (*           I:       INTEGER;                                              *)
  30. (*           L:       INTEGER;                                              *)
  31. (*                                                                          *)
  32. (*        BEGIN                                                             *)
  33. (*                                                                          *)
  34. (*           I := 1;                                                        *)
  35. (*           L := ORD( S[0] );                                              *)
  36. (*                                                                          *)
  37. (*           WHILE ( ( I <= L ) AND ( S[I] = ' ' ) ) DO                     *)
  38. (*              I := SUCC( I );                                             *)
  39. (*                                                                          *)
  40. (*           S[0]  := CHR( MAX( L - I + 1 , 0 ) );                          *)
  41. (*                                                                          *)
  42. (*           MOVE( S[I], S[1], ORD( S[0] ) );                               *)
  43. (*                                                                          *)
  44. (*           LTrim := S;                                                    *)
  45. (*                                                                          *)
  46. (*        END;                                                              *)
  47. (*                                                                          *)
  48. (*--------------------------------------------------------------------------*)
  49.  
  50. BEGIN (* LTrim *)
  51.  
  52. INLINE(
  53.   $1E/                   {         PUSH    DS                ; Save DS}
  54.                          {;}
  55.   $C5/$76/$06/           {         LDS     SI,[BP+6]         ; Get address of S}
  56.   $FC/                   {         CLD                       ; Forward search}
  57.   $AC/                   {         LODSB                     ; Get length of S}
  58.   $3C/$00/               {         CMP     AL,0              ; See if length 0}
  59.   $74/$20/               {         JE      LTrim2            ; If so, no trimming required}
  60.                          {;}
  61.   $31/$C9/               {         XOR     CX,CX}
  62.   $88/$C1/               {         MOV     CL,AL             ; Remember length for search loop}
  63.                          {;}
  64.   $B0/$20/               {         MOV     AL,' '            ; Blank to AL}
  65.                          {;}
  66.   $C4/$7E/$06/           {         LES     DI,[BP+6]         ; Get address of S}
  67.   $47/                   {         INC     DI                ; Skip length}
  68.   $F3/$AE/               {         REPE    SCASB             ; Scan over blanks}
  69.   $74/$01/               {         JE      LTrim1            ; If CX=0, entire string is blank.}
  70.   $41/                   {         INC     CX}
  71.                          {;}
  72.   $88/$C8/               {LTrim1:  MOV     AL,CL             ; Length to copy}
  73.   $89/$FE/               {         MOV     SI,DI             ; Offset of first non-blank}
  74.   $4E/                   {         DEC     SI}
  75.   $8E/$5E/$08/           {         MOV     DS,[BP+8]         ; Segment for S}
  76.   $C4/$7E/$0A/           {         LES     DI,[BP+10]        ; Result string address}
  77.   $AA/                   {         STOSB                     ; Set length in result}
  78.   $F2/$A4/               {         REP     MOVSB             ; Move trimmed result}
  79.   $E9/$04/$00/           {         JMP     Exit}
  80.                          {;}
  81.   $C4/$7E/$0A/           {LTrim2:  LES     DI,[BP+10]        ; Result string address}
  82.   $AA/                   {         STOSB                     ; Set length=0 in result}
  83.                          {;}
  84.   $1F);                  {Exit:    POP     DS                ; Restore DS}
  85.  
  86. END   (* LTrim *);
  87.