How to quickly parse an ANSI string?

I have a large ansi text file. A file contains many records (from millions to billions). Each entry has 4 lines:

@Instrument:6:73:941:1973#0/1
other stuff2
other stuff3
other stuff4

I'm interested in the first line. From the first line I need to extract its contents (numbers and strings). I use StringReplaceto replace :and space with #13, then split the string into a record like this:

   TYPE
      RBlock= record                                     // @Instrument:6:73:941:1973#0/1
       Instrument: String;                               // Instrument
       Lane: Integer;                                    // 6  
       TileNo: Integer;                                  // 73
       X: integer;                                       // 941
       Y: Integer;                                       // 1973
       Pair: Byte;                                       // could be 1 or 2 
       MultiplexID: AnsiString;                          // #0  <----  I need it as AnsiString
      end;

Using StrToIntoto convert text to numbers can be slow because it first converts a string AnsiStringto a string.

Any ideas on how I could read this faster would be appreciated.

Update: the line may also have an alternative format: @Instrument:136:FC6:2:2104:15343:197393 1:Y:18:TACA

-2
source share
2 answers

, . - ( ):

procedure ParseLine(const aLine: RawByteString; var aInstrument: string; var
    aLane, aTileNo, aX, aY: Integer; var aMultiplexID: Ansistring; var aPair:
    Byte);
var
  arrayIndex: Integer;
  index: Integer;
  lineLength: Integer;
  NumList: array[0..3] of Integer;
  I: Integer;
  multiEnd: Integer;
begin
  lineLength := Length(aLine);
  // Get the aInstrument
  index := Pos(':', aLine);
  SetLength(aInstrument, index - 2);
  for I := 2 to index - 1 do
    aInstrument[I-1] := Char(aLine[I]);
  // Get the integers
  arrayIndex := 0;
  FillMemory(@NumList, SizeOf(NumList), 0);
  while (index < lineLength) and (arrayIndex < 4) do
  begin
    Inc(index);
    if (aLine[index] = ':') or (aLine[index] = '#') then
      Inc(arrayIndex)
    else
      NumList[arrayIndex] := NumList[arrayIndex] * 10 + Ord(aLine[index]) - Ord('0');
  end;
  aLane := NumList[0];
  aTileNo := NumList[1];
  aX := NumList[2];
  aY := NumList[3];
  // Get the Multiplex
  multiEnd := Pos('/', aLine, index);
  SetLength(aMultiplexID, multiEnd - index - 1);
  Inc(index);
  for I := index to multiEnd - 1 do
    aMultiplexID[I-index+1] := aLine[I];
  // Get the aPair
  if (multiEnd+1 < lineLength) then
    aPair := Ord(aLine[multiEnd+1]) - Ord('0')
  else
    aPair := 0;
end;

, . , . , , , . . , , , , , . . , .

+2

C/++ sscanf() .

Delphi. Google, , , freaking library, .

.

, , , .

(: , , .)

-1

Source: https://habr.com/ru/post/1544751/


All Articles