[futurebasic] Change characters in unicode

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 2009 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Thu, 3 Dec 2009 04:30:51 -0500
In this thread, Eugen wrote to me back channel with questions about 
finding and replacing Unicode characters. He was trying to do the 
replacement in containers, but I suggested he stick CFString 
functions which are designed for Unicode.

Below is the example I sent him which I am posting here for archive purposes.

FYI, an easy way to find the hex number of any Unicode character is 
by adding the Character Pallet found in the International preference 
pane's "Input Menu" tab to your menu bar. Hover over any character 
with your mouse and a help box will pop up with the Unicode and UTF8 
numbers of the character. Rather than converting the hex to long as 
in my earlier example, below I simply use the hex for the characters 
Eugen requested.

BTW, it sure would be nice if the FB Editor window was able to 
directly interact with the Character Pallet a la TextWrangler, and 
display Unicode characters rather than nebulous boxes when characters 
are pasted into the window.

Ken

include "Util_Containers.incl"
include "Tlbx HIView.incl"

local fn GetCharacterUnicode ( whichCharacter as UniChar ) as CFStringRef
'~'1
dim as CFStringRef  cfStr

cfStr = fn CFStringCreateWithCharacters( _kCFAllocatorDefault, 
whichCharacter, 1 )

end fn = cfStr     // User must release

dim as UniChar             nr1, nr2
dim as CFStringRef         cfStr1, cfStr2
dim as CFIndex             count : count = 0
dim as CFMutableStringRef  mutStrTemp, mutStrFinal
dim as ControlFontStyleRec cfs

// Create and fill a temporary CFMutableString
mutStrTemp = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
CFStringAppend( mutStrTemp, @"
Sentence with character to convert: " )

// Add character nr1 to end of temporary string
nr1 = 0x283
cfStr1 = fn GetCharacterUnicode( nr1 )
CFStringAppend( mutStrTemp, cfStr1 )

// Create a final CFMutableString for display and fill with contents 
of temporary string
mutStrFinal = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
CFStringAppend( mutStrFinal, mutStrTemp )

// Create replacement unicode character
nr2 = 0x219
cfStr2 = fn GetCharacterUnicode( nr2 )

// Find and replace unicode character on the temporary string
long if ( mutStrTemp )
count = fn CFStringFindAndReplace( mutStrTemp, cfStr1, cfStr2, fn 
CFRangeMake( 0, fn CFStringGetLength( mutStrTemp ) ), 0 )
end if

// Add contents of converted temporary string to final display string
CFStringAppend( mutStrFinal, @"

Conversion completed.
" )
CFStringAppend( mutStrFinal, mutStrTemp )

// Release temporary string
CFRelease( mutStrTemp )

// Create window and edit field and place final display string into field
window 1,,(0,0)-(400,200)
edit field 1,,(10,10)-(390,190),,_centerJust
cfs.flags = _kControlUseSizeMask
cfs.size = 18
fn SetButtonFontStyle( 1, cfs )

fn HIViewSetText( button&( 1 ), mutStrFinal )
CFRelease( mutStrFinal )

do
HandleEvents
until gFBQuit