BUG: Correction for Rtrim and Ltrim QuickBasic Utilities (58038)






This article was previously published under Q58038

SYMPTOMS

The Ltrim and Rtrim routines, found in the Utility Subprograms file, don't handle all strings. The Rtrim routine does not correctly trim leading spaces, and Ltrim gives an "Illegal Function Call" when passed a blank string of spaces. Below are corrections to handle these cases.

The Utility Subprograms program is in the QB Demos folder on the Examples Disk provided with Microsoft QuickBasic Version 1.00 for the Macintosh .

Microsoft has confirmed this to be a bug in QuickBasic Version 1.00 for the Macintosh . We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Both Rtrim and Ltrim access one extra character beyond the boundaries of the string. In addition, these routines are not equivalent to the MS-DOS QuickBasic Versions 4.00, 4.00b, and 4.50 LTRIM$ and RTRIM$ routines, which trim ANY character with an ASCII code equal to or less than 32, instead of just spaces (ASCII 32).

The following code example illustrates the problems with the existing Rtrim and Ltrim routines and shows the code changes needed to correct them:
CLS

R$ = "test   "
PRINT "Rtrim"
PRINT "|"; R$; "|", "length"; LEN(R$); " -> "
CALL Rtrim(R$)
PRINT "|"; R$; "|", "length"; LEN(R$)
PRINT

L$ = "    "
PRINT "Ltrim"
PRINT "|"; L$; "|", "length"; LEN(L$); " -> "
PRINT "Causes Illegal Function Call if following lines activated."
'CALL Ltrim(L$)                      'Causes Illegal Function Call
'PRINT "|"; L$; "|", "length "; LEN(L$)

'To correct Ltrim, comment out first WHILE and
'uncomment second WHILE.
SUB Ltrim (s$) STATIC
   IF s$ <> "" THEN
     i% = 0: a& = SADD(s$)

     ' comment out this line:
     WHILE PEEK(a& + i%) <= 32: i% = i% + 1: WEND

     ' uncomment this line to correct:
     'WHILE (PEEK(a& + i%) = 32) AND (i% < LEN(s$)): i% = i% + 1: WEND
     s$ = RIGHT$(s$, LEN(s$) - i%)     'Illegal Function Call here
   END IF
END SUB

'To correct Rtrim, comment out first WHILE line and line above it, and
'uncomment the second WHILE line and the preceding line.
SUB Rtrim (s$) STATIC
   IF s$ <> "" THEN

      ' comment out the following two lines:
      i% = LEN(s$) + 1: t% = 0: a& = SADD(s$)
      WHILE t% <= 32: i% = i% - 1: t% = PEEK(a& + i%): WEND

      ' uncomment the following two lines to correct:
      'i% = LEN(s$) - 1: t% = 0: a& = SADD(s$)
      'WHILE PEEK(a& + i%) = 32: i% = i% - 1: : WEND

      s$ = LEFT$(s$, i% + 1)
   END IF
END SUB
				

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: kbbug KB58038