lstrbuffer.pp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. { Efficient string buffer helper
  2. Copyright (C) 2006-2008 Micha Nelissen
  3. This library is Free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. This license has been modified. See file LICENSE.ADDON for more information.
  15. Should you find these sources without a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit lStrBuffer;
  19. {$mode objfpc}{$h+}
  20. interface
  21. type
  22. PStringBuffer = ^TStringBuffer;
  23. TStringBuffer = record
  24. Memory: pchar;
  25. Pos: pchar;
  26. end;
  27. function InitStringBuffer(InitialSize: integer): TStringBuffer;
  28. procedure AppendString(var ABuffer: TStringBuffer; const ASource: string); overload;
  29. procedure AppendString(var ABuffer: TStringBuffer; const ASource: shortstring); overload;
  30. procedure AppendString(var ABuffer: TStringBuffer; ASource: pointer; ALength: PtrUInt); overload;
  31. procedure AppendString(var ABuffer: TStringBuffer; ASource: pchar); overload;
  32. procedure AppendChar(var ABuffer: TStringBuffer; AChar: char);
  33. implementation
  34. function InitStringBuffer(InitialSize: integer): TStringBuffer;
  35. begin
  36. Result.Memory := GetMem(InitialSize);
  37. Result.Pos := Result.Memory;
  38. end;
  39. procedure AppendString(var ABuffer: TStringBuffer; ASource: pointer; ALength: PtrUInt);
  40. var
  41. lPos, lSize: PtrUInt;
  42. begin
  43. if ALength = 0 then exit;
  44. lPos := PtrUInt(ABuffer.Pos - ABuffer.Memory);
  45. lSize := PtrUInt(MemSize(ABuffer.Memory));
  46. { reserve 2 extra spaces }
  47. if lPos + ALength + 2 >= lSize then
  48. begin
  49. ReallocMem(ABuffer.Memory, lPos + ALength + lSize);
  50. ABuffer.Pos := ABuffer.Memory + lPos;
  51. end;
  52. Move(ASource^, ABuffer.Pos^, ALength);
  53. Inc(ABuffer.Pos, ALength);
  54. end;
  55. procedure AppendString(var ABuffer: TStringBuffer; ASource: pchar);
  56. begin
  57. if ASource = nil then exit;
  58. AppendString(ABuffer, ASource, StrLen(ASource));
  59. end;
  60. procedure AppendString(var ABuffer: TStringBuffer; const ASource: shortstring);
  61. begin
  62. AppendString(ABuffer, @ASource[1], Length(ASource));
  63. end;
  64. procedure AppendString(var ABuffer: TStringBuffer; const ASource: string);
  65. begin
  66. AppendString(ABuffer, PChar(ASource), Length(ASource));
  67. end;
  68. procedure AppendChar(var ABuffer: TStringBuffer; AChar: char);
  69. begin
  70. ABuffer.Pos^ := AChar;
  71. Inc(ABuffer.Pos);
  72. end;
  73. end.