[futurebasic] FN titleCase$

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2001 : Group Archive : Group : All Groups

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Sun, 21 Oct 2001 08:16:20 -0400
RB has a somewhat maligned function called "Titlecase". A critique of 
Titlecase from the RB Web Ring site Rosetta Stone describes it this 
way:

"Titlecase(phrase as String)

"Returns a string in which all letter characters at the start of each 
word are converted to uppercase. The only valid word separator for 
the Titlecase function is the space (character code 32).

"A word preceded by a TAB (character code 9) or RETURN (character 
code 13) will have all its characters converted to lower case, but 
will not be properly capitalised. If the TAB and RETURN characters 
were valid word separators, the Titlecase function would be 
considerably more useful, especially for long entries in EditField 
and StaticText controls."

The Rosetta Stone goes on to compare Titlecase with VB's StrConv 
using the vbProperCase constant:

"StrConv(myString, vbProperCase)

"Returns a string in which all letter characters at the start of each 
word are converted to uppercase. Valid word separators (and their 
character codes) are:

NULL (0)
TAB (9)
LINEFEED (10)
VERTICAL TAB (11)
FORM FEED (12)
RETURN (13)
SPACE (32)"

The following FN titleCase$ for FB^3 is a prototype of an integrated 
string case function shown here strictly for conceptualization. It 
uses code borrowed from several different functions developed by 
Alain Pastor. Unlike the StrConv function I posted here earlier, 
Alain's code bypasses FB's internal case tools in favor of Toolbox 
calls. I am not yet sure if this would solve problems for our 
oriental friends or Unicode users, or whether it would correct 
translation problems pointed out by jonathan in his earlier post.

Alain's sense of humor also shines through in the UFunk and LFunk selections.

Your input concerning features and other suggestions/critiques is valued.

Ken

Alas, this particular code sample is riddled with underscores for 
constants. I fear they will be lost on the Associate server, but they 
should make it through intact in the e-mailed versions of the list. 
Please watch for e-mail line breaks.

'-----------BEGIN FN^3 CODE ------------

'~FN titleCase$ (Function to handle string case)

// Concept based on code contributed
// by Alain Pastor

BEGIN ENUM
_tcUppercase
_tcLowercase
_tcCapWords
_tcCapSentences
_tcUFunk
_tcLFunk
END ENUM

LOCAL FN titleCase$( theStr AS STR255, caseConstant AS INT )
DIM   i       AS INTEGER
DIM   toCap   AS BOOLEAN
DIM @ theChar AS UNSIGNED CHAR

SELECT CASE( caseConstant )

CASE _tcUppercase
UpperCaseText( @theStr[1], theStr[0], _smCurrentScript )

CASE _tcLowercase
LowerCaseText( @theStr[1], theStr[0], _smCurrentScript )

CASE _tcCapWords
LONG IF theStr[0]
LowerCaseText( @theStr[1], theStr[0], _smCurrentScript )
toCap = _zTrue : i = 0
DO
DO
i++
theChar = theStr[i]
UppercaseStripDiacritics( @theChar ,1 , _smCurrentScript )
SELECT
CASE theChar < _"A" OR theChar > _"Z"
toCap = _zTrue
END SELECT
UNTIL ( theChar => _"A" AND theChar <= _"Z" ) OR i = theStr[0]
IF i = theStr[0] THEN EXIT FN
LONG IF toCap
UpperCaseText( @theStr[i], 1, _smCurrentScript )
toCap = NOT(toCap)
END IF
UNTIL _nil
END IF

CASE _tcCapSentences
LONG IF theStr[0]
LowerCaseText( @theStr[1], theStr[0], _smCurrentScript )
toCap = _zTrue : i = 0
DO
DO
i++
theChar = theStr[i]
UppercaseStripDiacritics( @theChar ,1 , _smCurrentScript )
SELECT
CASE theChar < _"A" OR theChar > _"Z"
SELECT theChar
CASE _".", _"?", _"!"
toCap = _zTrue
END SELECT
END SELECT
UNTIL ( theChar => _"A" AND theChar <= _"Z" ) OR i = theStr[0]
IF i = theStr[0] THEN EXIT FN
LONG IF toCap
UpperCaseText( @theStr[i], 1, _smCurrentScript )
toCap = NOT(toCap)
DO
i++
IF i = theStr[0] THEN EXIT FN
SELECT theStr[i]
CASE _" ",_".",_"-",_"?",_"!",_"(",_")"
i-- : EXIT DO
END SELECT
UNTIL _nil
END IF

UNTIL _nil
END IF

CASE _tcUFunk
LONG IF theStr[0]
UpperCaseText( @theStr[1], theStr[0], _smCurrentScript )
FOR i = 2 TO theStr[0] STEP 2
LowerCaseText( @theStr[i], 1, _smCurrentScript )
NEXT
END IF

CASE _tcLFunk
LONG IF theStr[0]
LowerCaseText( @theStr[1], theStr[0], _smCurrentScript )
FOR i = 2 TO theStr[0] STEP 2
UpperCaseText( @theStr[i], 1, _smCurrentScript )
NEXT
END IF

CASE ELSE
PRINT "Sorry, not a valid constant."

END SELECT

END FN = theStr

DIM myStr AS STR255
myStr = " Hello, how are you? fine and you?"

PRINT:PRINT "Uppercase this string:"
PRINT FN titleCase$( myStr, _tcUppercase )
PRINT:PRINT "Lowercase this string:"
PRINT FN titleCase$( myStr, _tcLowercase )
PRINT:PRINT "Capitalize all words in this string:"
PRINT FN titleCase$( myStr, _tcCapWords )
PRINT:PRINT "Capitalize just sentences in this string:"
PRINT FN titleCase$( myStr, _tcCapSentences )
PRINT:PRINT "Do funky uppercase stuff on this string:"
PRINT FN titleCase$( myStr, _tcUFunk )
PRINT:PRINT "Do funky lowercase stuff on this string:"
PRINT FN titleCase$( myStr, _tcLFunk )

'------------ END CODE -----------------