views:

105

answers:

2

Above string equals "3test line3"

When I enter above line into an editbox, and say; StrVar := Edit1.text; StrVar is "chr(33)+'test line'+chr(33)"

How can i get the result 3test line3 as a variant;

To achieve this, i use pascal scripting engine from rem-objects. Might be a simpler way to do this..

Any ideas are welcomed...

A: 

A script engine is probably the best you're going to get. Some languages have a built-in way to evaluate expressions written in their own code, but only scripting languages that have an interpreter (or sometimes a compiler) built into the runtime. As a pure compiled language, Delphi doesn't offer that, so you need to provide your own scripting language, such as PascalScript.

Mason Wheeler
+2  A: 

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;
Andreas Rejbrand