[fpc-pascal] How to split file of whitespace separated numbers?

Howard Page-Clark hdpc at talktalk.net
Fri Dec 23 12:49:57 CET 2016


On 23/12/16 08:14, Bo Berglund wrote:
> Is there a quick way to split a string of whitespace separated values
> into the separate members?
It is possible that a custom string parser (something along these lines) 
might improve your processing speed:

type
     TDoubleArray = array of Double;

function StrToDblArray(const aString: string): TDoubleArray;
var
   c: Char;
   prevNumeric: boolean = False;
   sNum: string = '';
   number: double;

   function IsNumeric: boolean; inline;
   begin
     Exit(c in ['.', '0'..'9']);
   end;

begin
   SetLength(Result, 0);
   for c in aString do begin
     case IsNumeric of
       False: if prevNumeric then begin
                 if TryStrToFloat(sNum, number) then begin
                   SetLength(Result, Length(Result) + 1);
                   Result[High(Result)]:=number;
                 end;
                 sNum:='';
                 prevNumeric:=False;
              end;
       True: begin
               sNum:=sNum + c;
               if not prevNumeric then
                 prevNumeric:=True;
             end;
     end;
   end;
   if prevNumeric and TryStrToFloat(sNum, number) then begin
     SetLength(Result, Length(Result) + 1);
     Result[High(Result)]:=number;
   end;
end;



More information about the fpc-pascal mailing list