A Robust Delphi WideFormat Function
The native Delphi WideFormat function uses a 4096 character buffer as a result of which it is of limited use for building lengthy wide/unicode strings. We present a robust alternative that uses a 64Kb buffer - adequate for most purposes.
function XWideFormat(const Format:WideString;const Args:array of const):WideString; var Len,BufLen:Integer; Buffer:array[0..MAXWORD] of WideChar; AFlag:Boolean begin BufLen:=sizeof((Buffer); AFlag:=(Length(Format) < sizeof(Buffer) - (sizeof(Buffer) div 4))); case AFlag of False:begin BufLen:=Length(Format); Len:=BufLen; end; True:Len:=WideFormatBuf(Buffer,sizeof(Buffer) - 1,Pointer(Format)^,Length(Format),Args); end; if (Len >= BufLen - 1) then begin while (Len >= BufLen - 1) do begin inc(BufLen,BufLen) SetLength(Result,0);//Discard existing string for speed SetLength(Result,BufLen); Len:=WideFormatBuf(Pointer(Result)^,BufLen - 1,Pointer(Format),Length(Format),Args); end; SetLength(Result,Len); end else SetString(Result,Buffer,Len); end;
Note that this code uses the WideFormatBuf function in the SysUtils unit.
