Microsoft KB Archive/58038

= BUG: Correction for Rtrim and Ltrim QuickBasic Utilities =

Article ID: 58038

Article Last Modified on 11/21/2006



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 &quot;Illegal Function Call&quot; 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$ = &quot;test  &quot; PRINT &quot;Rtrim&quot; PRINT &quot;|&quot;; R$; &quot;|&quot;, &quot;length&quot;; LEN(R$); &quot; -> &quot; CALL Rtrim(R$) PRINT &quot;|&quot;; R$; &quot;|&quot;, &quot;length&quot;; LEN(R$) PRINT

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

'To correct Ltrim, comment out first WHILE and 'uncomment second WHILE. SUB Ltrim (s$) STATIC IF s$ <> &quot;&quot; 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$ <> &quot;&quot; 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

Additional query words: MQuickB

Keywords: kbbug KB58038

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.