If you need a function that takes a Delphi string literal as input, and returns the string, e.g.
'This is a test! '#9728#9729
would be evaluated to
This is a test! ☀☁
then this will do it:
function ParseStr(const Str: string): string;
var
InLiteral, InOrdinal: boolean;
ActualHigh: integer;
i: Integer;
ordinal: string;
const
STRING_TERMINATOR = '''';
CHAR_SYMBOL = '#';
NUMBERS = ['0' .. '9'];
WHITESPACE = [#$20, #$A0, #$09];
begin
SetLength(result, length(Str));
ActualHigh := 1;
InLiteral := false;
InOrdinal := false;
i := 1;
if length(Str) = 0 then Exit;
repeat
if InLiteral then
begin
if (Str[i] = STRING_TERMINATOR) and
(i < length(Str)) and (Str[i + 1] = STRING_TERMINATOR) then
begin
result[ActualHigh] := STRING_TERMINATOR;
inc(ActualHigh);
inc(i, 2);
Continue;
end
else if (Str[i] = STRING_TERMINATOR) then
begin
InLiteral := false;
inc(i);
Continue;
end;
result[ActualHigh] := Str[i];
inc(ActualHigh);
inc(i);
end
else if InOrdinal then
begin
if Str[i] in NUMBERS then
begin
ordinal := ordinal + Str[i];
if i = length(Str) then
begin
result[ActualHigh] := char(StrToInt(ordinal));
inc(ActualHigh);
end;
inc(i);
end
else if Str[i] = STRING_TERMINATOR then
begin
result[ActualHigh] := char(StrToInt(ordinal));
inc(ActualHigh);
InLiteral := true;
InOrdinal := false;
inc(i);
end
else if Str[i] = CHAR_SYMBOL then
begin
result[ActualHigh] := char(StrToInt(ordinal));
inc(ActualHigh);
ordinal := '';
inc(i);
end
else if Str[i] in WHITESPACE then
inc(i)
else
raise EConvertError.CreateFmt('Invalid string constant: "%s"', [Str]);
end
else
begin
if Str[i] = STRING_TERMINATOR then
begin
InLiteral := true;
inc(i);
end
else if Str[i] = CHAR_SYMBOL then
begin
InOrdinal := true;
inc(i);
ordinal := '';
end
else if Str[i] in WHITESPACE then
inc(i)
else
raise EConvertError.CreateFmt('Invalid string constant: "%s"', [Str]);
end;
until i > length(Str);
SetLength(result, ActualHigh - 1);
end;