Handling strings with identical values?

140 Views Asked by At

I am using Delphi 10.3 to extend some working code. I am having a problem with strings.

I'm filling a record. Only the first 2 fields, name and org_name, are giving me problems. When name and org_name have values different from each other there is no problem. But when name and org_name have identical values, name is read correctly but org_name yields only the first character of its string.

  TMYSQL_FIELD51 = record
    name:             PAnsiChar;   // Name of column
    org_name:         PAnsiChar;   // Original column name, if an alias
  ...
  end;

The values are retrieved from a JSON object and written to the record:

fFieldInfo: TMYSQL_FIELD51;
lInput: TJSONValue;
FieldOffsets: TMYSQL_FIELDOFFSETS; //a record containing offsets into TMYSQL_FIELD51

PPAnsiChar(NativeInt(@fFieldInfo) + FieldOffsets.name)^ := PAnsiChar(AnsiString(lInput.GetValue<String>('label'))); 
PPAnsiChar(NativeInt(@fFieldInfo) + FieldOffsets.org_name)^ := PAnsiChar(AnsiString(lInput.GetValue<String>('name')));

Note: filling the fields in a more usual way does not change the outcome:

fFieldInfo.name := PAnsiChar(AnsiString(lInput.GetValue<String>('label')));
fFieldInfo.org_name := PAnsiChar(AnsiString(lInput.GetValue<String>('name')));

Later, name and org_name are extracted like this:

PMYSQL_FIELD51 = ^TMYSQL_FIELD51;
MYSQL_FIELD: PMYSQL_FIELD51;
FieldOffsets: TMYSQL_FIELDOFFSETS; //a record containing offsets into TMYSQL_FIELD51

Result.ColumnLabel := ValueToString(PPAnsichar(NativeUInt(MYSQL_FIELD)+FieldOffsets.name)^, StrLen(PPAnsichar(NativeUInt(MYSQL_FIELD)+FieldOffsets.name)^));
Result.ColumnName := ValueToString(PPAnsichar(NativeUInt(MYSQL_FIELD)+NativeUInt(FieldOffsets.org_name))^, StrLen(PPAnsichar(NativeUInt(MYSQL_FIELD)+NativeUInt(FieldOffsets.org_name))^));

When name and org_name have identical values the first parameter in the call to ValueToString evaluates correctly for name but evaluates to only the first character of the correct string for org_name.

The record structures and extraction code are in production. I am assuming the problem is with the way I am writing the string pointers to the record. What am I doing wrong?

Thanks

David

0

There are 0 best solutions below