[comp.sys.apple] String case conversion

rich@pro-exchange.cts.COM (Rich Sims) (05/17/88)

A previous message gave a method for converting a string to all upper case. It
will work, but it's slow and generates *tons* of string garbage, especially if
yuo are doing frequent conversions.  Here's a method that will work a bit more
rapidly, and won't generate any string garbage at all.  It works in DOS 3.3 or
ProDOS.

CAVEAT:  The string to be converted must be the last string referenced before
         this routine is called... even if you have to do something like
         "S$ = S$" before calling the routine.

----- code ------

rich@pro-exchange.cts.COM (Rich Sims) (05/18/88)

(Apparently my earlier message on this subject got "eaten" somewhere along
the way, so I'm re-sending it for those who may be interested.)

A previous message gave a method for converting a string to all upper case. It
will work, but it's slow and generates *tons* of string garbage, especially if
yuo are doing frequent conversions.  Here's a method that will work a bit more
rapidly, and won't generate any string garbage at all.  It works in DOS 3.3 or
ProDOS.

CAVEAT:  The string to be converted must be the last string referenced before
         this routine is called... even if you have to do something like
         "S$ = S$" before calling the routine.

With that in mind, your program should look something like:

------ code -------
  do stuff
  more stuff
  more stuff : INPUT A$ : PRINT A$ :GOSUB <conversion subroutine> : PRINT A$
  more stuff
  more stuff
--- end of code ---

When the program returns from the conversion subroutine to the *second*
"PRINT A$" statement, the string will all be in upper (or lower) case.  How
you reference the string is not important, I used the above example because
you can use that to test the subroutine both for accuracy and speed.

Here is the actual code for the subroutine.  I've separated the individual
statements but you could (and should!) put them all on one line.

-- conversion subroutine --

POKE 8, PEEK (131)
POKE 9, PEEK (132)
ADRS = PEEK (8) + 256 * PEEK (9)
SIZE = PEEK (ADRS)
ADRS = PEEK (ADRS + 1) + 256 * PEEK (ADRS + 2)
FOR TRY = 0 TO SIZE - 1
CHAR = PEEK (ADRS + TRY)
POKE ADRS + TRY, CHAR - 32 * (CHAR > 93 AND CHAR < 123)
NEXT
RETURN

-- end of subroutine --

As shown, the example will convert the entrie string to upper case.  If you
need to convert to lower case, change the third-from-last statement to:

POKE ADRS + TRY, CHAR + 32 * (CHAR > 64 AND CHAR < 91)

I hope this will be useful to some of you.

--------------------------------------------------------------------------
DISCLAIMER:  If your computer erupts in smoke, forget where you read this!
--------------------------------------------------------------------------

UUCP: [ ihnp4 cbosgd sdcsvax nosc ] !crash!pnet01!pro-exchange!rich
ARPA: crash!pnet01!pro-exchange!rich@nosc.mil
INET: rich@pro-exchange.cts.com
pro-exchange:  305-431-3203  :  300-1200-2400-9600/ARQ  :  login as 'register'