I found this in another thread but the Windows part gives an OutOfBound error here:
return ConvertEncoding(DefineEncoding(m.stringValue(0, newSize*2), Encodings.UTF16), Encodings.UTF8)
I’ll try to solve this (full function below).
In the meantime I have a solution for macOS thanks to @Thomas Eckert…
Thanks.
[code]Function Normalize(extends s as String, form as UInt32) As String
// Normalizes characters of a text string according to Unicode 4.0 TR#15
// can’t normalize a string with unknown encoding
if Encoding(s) is nil then
return s
end if
// can’t normalize a string not encoded in UTF8
if Encoding(s) <> Encodings.UTF8 then
return ConvertEncoding(s, Encodings.UTF8)
end if
#if targetMacOS
soft declare function CFStringCreateMutableCopy lib "Carbon.framework" ( _
alloc as Ptr, _
maxLength as UInt32, _
theString as CFStringRef) as CFStringRef
soft declare sub CFStringNormalize lib "Carbon.framework" (theString as CFStringRef, theForm as UInt32)
dim mutableStringRef as CFStringRef = CFStringCreateMutableCopy(nil, 0, s)
CFStringNormalize mutableStringRef, form
return mutableStringRef
'enum CFStringNormalizationForm {
'kCFStringNormalizationFormD = 0,
'kCFStringNormalizationFormKD = 1,
'kCFStringNormalizationFormC = 2,
'kCFStringNormalizationFormKC = 3
#elseif targetWin32
// not available on Windows < Vista
if not system.isFunctionAvailable("NormalizeString", "Normaliz.dll") then
return s
end if
'soft declare function GetLastError lib "Kernel32.dll" () as UInt32
soft declare function NormalizeString lib "Normaliz.dll" ( _
NormForm as Int32, _
lpSrcString as WString, _
cwSrcLength as integer, _
lpDstString as Ptr, _
cwDstLength as integer) as integer
dim estimatedSize as integer = NormalizeString(form, s, len(s), nil, 0)
if estimatedSize > 0 then
dim m as new memoryBlock(estimatedSize)
dim newSize as integer = NormalizeString(form, s, lenb(s), m, m.size)
return ConvertEncoding(DefineEncoding(m.stringValue(0, newSize*2), Encodings.UTF16), Encodings.UTF8)
else
return ""
end if
' if estimatedSize <= 0 then
' err = GetLastError
' end if
' typedef enum _NORM_FORM {
' NormalizationOther = 0,
' NormalizationC = 0x1,
' NormalizationD = 0x2,
' NormalizationKC = 0x5,
' NormalizationKD = 0x6
' } NORM_FORM;
#else
dim exc as new UnsupportedOperationException
exc.message = "Normalize is not supported on this platform"
raise exc
#endif
End Function
[/code]