smarthaert / txquery

Automatically exported from code.google.com/p/txquery
Other
0 stars 0 forks source link

format string #16

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.use format function with stringfield some like
SELEC format(" %s ",[strfld]) from table 
and the result is wrong values.

Original issue reported on code.google.com by Nenukes...@gmail.com on 1 Aug 2011 at 8:23

GoogleCodeExporter commented 9 years ago
The problem is solved.

IN Unit Qbaseexpr;

at function GetAsString
the variable ss must be: Array[0..MAXARGS] Of shortString; 
as shown below.

Function TFormatExpr.GetAsString: String;
Const
  MAXARGS = 20; {maximum number of arguments allowed (increase if needed)}
Var
  cnt, n: integer;
  ss: Array[0..MAXARGS] Of shortString;  ///iris    ss: Array[0..MAXARGS] Of String;

after this it works fine.

Original comment by Nenukes...@gmail.com on 2 Aug 2011 at 5:28

Attachments:

GoogleCodeExporter commented 9 years ago
Seems a good patch but this can cause problems when working with Unicode 
strings and doesn't work ok with delphi 2009 or new.

This is better approach:

You will have two change Two functions: 
1) Function TFormatExpr.GetMaxAsString and 
2) Function TFormatExpr.GetAsString

Here is the code below:
--------------------------------------------------------------------------------
--
{ TFormatExpr - class implementation
  Format('%d %s ...', 1234, 'ABC', ..., etc) }
Function TFormatExpr.GetMaxString: String;
Const
  MAXARGS = 20; {maximum number of arguments allowed (increase if needed)}
Var
  cnt, n: integer;
  ss: Array[0..MAXARGS] Of String;
  ea: Array[0..MAXARGS] Of Extended;
  Vars: Array[0..MAXARGS] Of TVarRec;
  //Args: Array[0..1] Of TVarRec = ('hello');
Begin
  n := imin( ParameterCount - 1, MAXARGS );
  { first parameter is the format string and the rest are the args}
  For cnt := 1 To n Do
  Begin
    Case Param[cnt].ExprType Of
      ttString:
        Begin
          Vars[cnt - 1].VType := {$if RtlVersion <= 18.5} vtString {$else} vtPWideChar {$ifend} ; {patched by fduenas for Unicode enabling}
          ss[cnt - 1] := Param[cnt].GetMaxString; {patched by fduenas changed frpm AsString to GetMaxString to prevent data truncation}
          {$if RtlVersion <= 18.5}
           Vars[cnt - 1].VString := @ss[cnt - 1]; {patched by fduenas for Unicode enabling}
          {$else}
           Vars[cnt - 1].VPWideChar := PWideChar( ss[cnt - 1] ); {patched by fduenas for Unicode enabling}
          {$ifend}
        End;
...
...
--------------------------------------------------------------------------------
-
Function TFormatExpr.GetAsString: String;
Const
  MAXARGS = 20; {maximum number of arguments allowed (increase if needed)}
Var
  cnt, n: integer;
  ss: Array[0..MAXARGS] Of String;
  ea: Array[0..MAXARGS] Of Extended;
  Vars: Array[0..MAXARGS] Of TVarRec;
  SFS: xqmiscel.TSaveFormatSettings;
Begin
  n := imin( ParameterCount - 1, MAXARGS );
  { first parameter is the format string and the rest are the args}
  For cnt := 1 To n Do
  Begin
    Case Param[cnt].ExprType Of
      ttString:
        Begin
          Vars[cnt - 1].VType := {$if RtlVersion <= 18.5} vtString {$else} vtPWideChar {$ifend} ; {patched by fduenas for Unicode enabling}
          ss[cnt - 1] := Param[cnt].AsString; {patched by fduenas changed frpm AsString to GetMaxString to prevent data truncation}
          {$if RtlVersion <= 18.5}
           Vars[cnt - 1].VString := @ss[cnt - 1]; {patched by fduenas for Unicode enabling}
          {$else}
           Vars[cnt - 1].VPWideChar := PWideChar( ss[cnt - 1] ); {patched by fduenas for Unicode enabling}
          {$ifend}
        End;

---------------------------------------------------------------------
Anyway when doing some fixes I found some issue with Cast(field as Char(n)), so 
I made some fixes also. I will post newest sources in next issue #17

Original comment by fdue...@gmail.com on 10 Aug 2011 at 12:28