Jump to content

Alexander Brazda

  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral
  1. I agree, use TStringBuilder for string to buffer copy and prevent adding coma cheking before add a new line if buffer is empty. You can reduce memory reallocations if StringBuilder buffer is set to a initial capacity by calling EnsureCapacity or set a value to Capacity. Perheaps you can do some estimation based on max or average record size or just set a value like 4 MB as start point. For csv files with ansi or utf-8 encoding and with ram limitations perheaps you can use a buffered file stream with string encoding Attached file FileBuffer.pas is a Buffered file with string encoding and in memory concatenation, support multiple code pages for input and output. It's optimized for the same input and output codepage. Encoding preamble is writed at start of file, comment if you don't need this. UTF-8 is default encoding Usage sample Buffer:=TFileBuffer.Create('test.txt',CP_UTF8); Buffer.WriteLn('FieldName'#9'Value'); Buffer .Write('Name') .Write(#9) .Write('Alex'); Buffer.Free; FileBuffer.pas
  2. There is no diference between var AText:String and const AText:String and return the value as a function result AText is reference counted so assign a value to this variable inside the función is the same as assign the result of a función to a variable, SetLength would allocate a new string and assign this to AText, so reference counting would decrease inside the funcion instead outside. String functions should work when ZEROBASEDSTRINGS compiler flag is enabled or not, to be safe for any platform. Use StringHelper funcions from System.SysUtils instead base 1 string functions Some optimizations are: declare DeleteLastComma as inline , change "const ALastChar:String" to "ALastChar:Char" function DeleteLastChar(const AText:String;AChar:Char):String; begin if AText.IsEmpty then exit(AText); if AText.Chars[AText.Length-1]=AChar then exit(AText.Substring(0,AText.Length-1); Result:=AText; end; DeleteLastComma method can be a inline function. procedure DeleteLastComma(var AText:String); inline; begin AText:=DeleteLastChar(AText,','); end;