2008-03-17 07:10:11 +01:00
|
|
|
{
|
|
|
|
:: X2UtNamedFormat implements Format-style functionality using named
|
|
|
|
:: instead of indexed parameters.
|
|
|
|
::
|
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtNamedFormat;
|
|
|
|
|
|
|
|
interface
|
2008-03-17 17:01:34 +01:00
|
|
|
uses
|
2013-04-09 16:56:07 +02:00
|
|
|
Classes,
|
|
|
|
SysUtils;
|
2008-03-17 17:01:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
TNamedFormatStringList = class(TStringList)
|
|
|
|
public
|
|
|
|
procedure AddLn();
|
|
|
|
|
|
|
|
function Format(AParams: array of const): String;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-03-17 07:10:11 +01:00
|
|
|
{
|
|
|
|
AFormat uses the same format strings as SysUtils.Format, where each
|
2008-03-17 17:01:34 +01:00
|
|
|
format specifier may use a named instead of a numeric index, surrounded by
|
|
|
|
<>, eg:
|
|
|
|
|
|
|
|
%<Value1>:s %<Value2>:.2d
|
2013-04-09 16:56:07 +02:00
|
|
|
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
AParams contains alternating the parameter name and it's value.
|
|
|
|
|
|
|
|
Note: NamedFormat works by mapping names to indices and passing the result
|
2008-03-17 17:01:34 +01:00
|
|
|
to SysUtils.Format. Unnamed or existing indexed specifiers will therefore
|
|
|
|
be affected by named specifiers! It is strongly recommended to name all
|
|
|
|
specifiers.
|
2008-03-17 07:10:11 +01:00
|
|
|
}
|
2013-04-09 16:56:07 +02:00
|
|
|
function NamedFormat(const AFormat: String; AParams: array of const; AFormatSettings: TFormatSettings): String; overload;
|
|
|
|
function NamedFormat(const AFormat: String; AParams: array of const): String; overload;
|
|
|
|
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
2013-12-06 14:09:39 +01:00
|
|
|
Windows,
|
|
|
|
|
|
|
|
X2UtDelphiCompatibility;
|
2013-04-09 16:56:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
{$I X2UtCompilerVersion.inc}
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
TProtectedMemoryStream = class(TMemoryStream);
|
|
|
|
|
|
|
|
|
|
|
|
const
|
2008-03-17 17:01:34 +01:00
|
|
|
SpecifierChar = '%';
|
|
|
|
SpecifierNameStart = '<';
|
|
|
|
SpecifierNameEnd = '>';
|
|
|
|
ValidNameChars = ['A'..'Z', 'a'..'z', '0'..'9', '_'];
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
procedure StreamWriteChar(const AStream: TStream; const AValue: Char);
|
|
|
|
begin
|
|
|
|
AStream.WriteBuffer(AValue, SizeOf(Char));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure StreamWriteString(const AStream: TStream; const AValue: String);
|
|
|
|
begin
|
2013-12-30 10:31:32 +01:00
|
|
|
AStream.WriteBuffer(PChar(AValue)^, Length(AValue) * SizeOf(Char));
|
2008-03-17 07:10:11 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function FindNameEnd(const APosition: PChar; const AEnd: PChar): PChar;
|
|
|
|
var
|
|
|
|
position: PChar;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
position := APosition;
|
|
|
|
|
|
|
|
while position < AEnd do
|
|
|
|
begin
|
2008-03-17 17:01:34 +01:00
|
|
|
if position^ = SpecifierNameEnd then
|
2008-03-17 07:10:11 +01:00
|
|
|
begin
|
|
|
|
Result := position;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
|
2013-12-05 11:49:28 +01:00
|
|
|
if not CharInSet(position^, ValidNameChars) then
|
2008-03-17 07:10:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
Inc(position);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2013-04-09 16:56:07 +02:00
|
|
|
function NamedFormat(const AFormat: String; AParams: array of const): String; overload;
|
|
|
|
var
|
|
|
|
formatSettings: TFormatSettings;
|
|
|
|
|
|
|
|
begin
|
|
|
|
{$IFDEF DXE2PLUS}
|
|
|
|
formatSettings := TFormatSettings.Create;
|
|
|
|
{$ELSE}
|
|
|
|
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);
|
|
|
|
{$ENDIF}
|
|
|
|
Result := NamedFormat(AFormat, AParams, formatSettings);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2013-12-30 10:31:32 +01:00
|
|
|
function NamedFormat(const AFormat: string; AParams: array of const; AFormatSettings: TFormatSettings): String;
|
2008-03-17 07:10:11 +01:00
|
|
|
var
|
|
|
|
currentPos: PChar;
|
|
|
|
formatEnd: PChar;
|
|
|
|
formatStream: TMemoryStream;
|
|
|
|
formatString: String;
|
|
|
|
name: String;
|
|
|
|
nameEnd: PChar;
|
|
|
|
nameStart: PChar;
|
|
|
|
param: TVarRec;
|
|
|
|
paramIndex: Integer;
|
|
|
|
paramNames: TStringList;
|
|
|
|
paramValues: array of TVarRec;
|
|
|
|
specifierIndex: Integer;
|
2008-03-21 16:07:40 +01:00
|
|
|
errorMsg: String;
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
begin
|
|
|
|
if Length(AParams) mod 2 = 1 then
|
|
|
|
raise Exception.Create('AParams must contains a multiple of 2 number of items');
|
|
|
|
|
|
|
|
currentPos := PChar(AFormat);
|
|
|
|
SetLength(paramValues, 0);
|
|
|
|
|
|
|
|
formatEnd := currentPos;
|
|
|
|
Inc(formatEnd, Length(AFormat));
|
|
|
|
|
|
|
|
paramNames := TStringList.Create();
|
|
|
|
try
|
|
|
|
paramNames.CaseSensitive := False;
|
|
|
|
|
|
|
|
formatStream := TMemoryStream.Create();
|
|
|
|
try
|
|
|
|
{ Most likely scenario; the names are longer than the replacement
|
|
|
|
indexes. }
|
2013-12-30 10:31:32 +01:00
|
|
|
TProtectedMemoryStream(formatStream).Capacity := Length(AFormat) * SizeOf(Char);
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
while currentPos < formatEnd do
|
|
|
|
begin
|
|
|
|
{ Search for % }
|
|
|
|
if currentPos^ = SpecifierChar then
|
|
|
|
begin
|
|
|
|
StreamWriteChar(formatStream, currentPos^);
|
|
|
|
Inc(currentPos);
|
|
|
|
|
2008-03-17 17:01:34 +01:00
|
|
|
{ Check if this is indeed a named specifier }
|
|
|
|
if (currentPos < formatEnd) and (currentPos^ = SpecifierNameStart) then
|
2008-03-17 07:10:11 +01:00
|
|
|
begin
|
2008-03-17 17:01:34 +01:00
|
|
|
Inc(currentPos);
|
|
|
|
|
2008-03-17 07:10:11 +01:00
|
|
|
nameStart := currentPos;
|
|
|
|
nameEnd := FindNameEnd(currentPos, formatEnd);
|
|
|
|
|
|
|
|
if Assigned(nameEnd) then
|
|
|
|
begin
|
|
|
|
SetString(name, nameStart, nameEnd - nameStart);
|
|
|
|
|
|
|
|
specifierIndex := paramNames.IndexOf(name);
|
|
|
|
if specifierIndex = -1 then
|
|
|
|
specifierIndex := paramNames.Add(name);
|
|
|
|
|
|
|
|
StreamWriteString(formatStream, IntToStr(specifierIndex));
|
2008-03-17 17:01:34 +01:00
|
|
|
|
2008-03-17 07:10:11 +01:00
|
|
|
currentPos := nameEnd;
|
|
|
|
end;
|
2013-04-10 10:25:39 +02:00
|
|
|
end else
|
|
|
|
StreamWriteChar(formatStream, currentPos^);
|
2008-03-17 17:01:34 +01:00
|
|
|
end else
|
|
|
|
StreamWriteChar(formatStream, currentPos^);
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
Inc(currentPos);
|
|
|
|
end;
|
|
|
|
|
2013-12-30 10:31:32 +01:00
|
|
|
SetString(formatString, PChar(formatStream.Memory), formatStream.Size div SizeOf(Char));
|
2008-03-17 07:10:11 +01:00
|
|
|
finally
|
|
|
|
FreeAndNil(formatStream);
|
|
|
|
end;
|
|
|
|
|
|
|
|
SetLength(paramValues, paramNames.Count);
|
|
|
|
paramIndex := 0;
|
|
|
|
|
|
|
|
while paramIndex < High(AParams) do
|
|
|
|
begin
|
|
|
|
param := AParams[paramIndex];
|
2008-03-21 16:07:40 +01:00
|
|
|
|
2008-03-17 07:10:11 +01:00
|
|
|
case param.VType of
|
2013-12-30 10:31:32 +01:00
|
|
|
vtChar: name := string(param.VChar);
|
|
|
|
vtString: name := string(param.VString^);
|
|
|
|
vtPChar: name := string(param.VPChar);
|
|
|
|
vtAnsiString: name := string(PChar(param.VAnsiString));
|
|
|
|
vtWideChar: name := string(param.VWideChar);
|
|
|
|
vtWideString: name := string(WideString(param.VWideString));
|
2014-02-27 10:26:55 +01:00
|
|
|
{$IF CompilerVersion >= 23}
|
2013-12-30 10:31:32 +01:00
|
|
|
vtUnicodeString: name := string(UnicodeString(param.VUnicodeString));
|
2014-02-27 10:26:55 +01:00
|
|
|
{$IFEND}
|
2008-03-17 07:10:11 +01:00
|
|
|
else
|
|
|
|
raise Exception.CreateFmt('Parameter name at index %d is not a string value',
|
|
|
|
[paramIndex div 2]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Inc(paramIndex);
|
|
|
|
|
|
|
|
specifierIndex := paramNames.IndexOf(name);
|
2008-03-17 17:01:34 +01:00
|
|
|
if specifierIndex > -1 then
|
|
|
|
paramValues[specifierIndex] := AParams[paramIndex];
|
2008-03-17 07:10:11 +01:00
|
|
|
|
|
|
|
Inc(paramIndex);
|
|
|
|
end;
|
2008-03-21 16:07:40 +01:00
|
|
|
|
|
|
|
try
|
2013-04-09 16:56:07 +02:00
|
|
|
|
|
|
|
Result := Format(formatString, paramValues, AFormatSettings);
|
2008-03-21 16:07:40 +01:00
|
|
|
except
|
|
|
|
on E:EConvertError do
|
|
|
|
begin
|
|
|
|
errorMsg := E.Message;
|
|
|
|
|
|
|
|
{ Translate specifiers in error messages back to names }
|
|
|
|
for paramIndex := 0 to Pred(paramNames.Count) do
|
|
|
|
errorMsg := StringReplace(errorMsg, SpecifierChar + IntToStr(paramIndex) + ':',
|
|
|
|
SpecifierChar + SpecifierNameStart +
|
|
|
|
paramNames[paramIndex] + SpecifierNameEnd + ':',
|
|
|
|
[rfReplaceAll]);
|
|
|
|
|
|
|
|
raise EConvertError.Create(errorMsg);
|
|
|
|
end;
|
|
|
|
end;
|
2008-03-17 07:10:11 +01:00
|
|
|
finally
|
|
|
|
FreeAndNil(paramNames);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-03-17 17:01:34 +01:00
|
|
|
{ TNamedFormatStringList }
|
|
|
|
procedure TNamedFormatStringList.AddLn;
|
|
|
|
begin
|
|
|
|
Add('');
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TNamedFormatStringList.Format(AParams: array of const): String;
|
|
|
|
begin
|
|
|
|
Result := NamedFormat(Text, AParams);
|
|
|
|
end;
|
|
|
|
|
2008-03-17 07:10:11 +01:00
|
|
|
end.
|