lstrbuffer.pp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: pansichar;
  25. Pos: pansichar;
  26. end;
  27. function InitStringBuffer(InitialSize: integer): TStringBuffer;
  28. // Assumes UTF8
  29. procedure AppendString(var ABuffer: TStringBuffer; const ASource: unicodestring); overload;
  30. procedure AppendString(var ABuffer: TStringBuffer; const ASource: ansistring); overload;
  31. procedure AppendString(var ABuffer: TStringBuffer; const ASource: shortstring); overload;
  32. procedure AppendString(var ABuffer: TStringBuffer; ASource: pointer; ALength: PtrUInt); overload;
  33. procedure AppendString(var ABuffer: TStringBuffer; ASource: pansichar); overload;
  34. procedure AppendChar(var ABuffer: TStringBuffer; AChar: Ansichar);
  35. implementation
  36. function InitStringBuffer(InitialSize: integer): TStringBuffer;
  37. begin
  38. Result.Memory := GetMem(InitialSize);
  39. Result.Pos := Result.Memory;
  40. end;
  41. procedure AppendString(var ABuffer: TStringBuffer; ASource: pointer; ALength: PtrUInt);
  42. var
  43. lPos, lSize: PtrUInt;
  44. begin
  45. if ALength = 0 then exit;
  46. lPos := PtrUInt(ABuffer.Pos - ABuffer.Memory);
  47. lSize := PtrUInt(MemSize(ABuffer.Memory));
  48. { reserve 2 extra spaces }
  49. if lPos + ALength + 2 >= lSize then
  50. begin
  51. ReallocMem(ABuffer.Memory, lPos + ALength + lSize);
  52. ABuffer.Pos := ABuffer.Memory + lPos;
  53. end;
  54. Move(ASource^, ABuffer.Pos^, ALength);
  55. Inc(ABuffer.Pos, ALength);
  56. end;
  57. procedure AppendString(var ABuffer: TStringBuffer; ASource: pansichar);
  58. begin
  59. if ASource = nil then exit;
  60. AppendString(ABuffer, ASource, StrLen(ASource));
  61. end;
  62. procedure AppendString(var ABuffer: TStringBuffer; const ASource: shortstring);
  63. begin
  64. AppendString(ABuffer, @ASource[1], Length(ASource));
  65. end;
  66. procedure AppendString(var ABuffer: TStringBuffer; const ASource: ansistring);
  67. begin
  68. AppendString(ABuffer, PAnsiChar(ASource), Length(ASource));
  69. end;
  70. procedure AppendChar(var ABuffer: TStringBuffer; AChar: ansichar);
  71. begin
  72. ABuffer.Pos^ := AChar;
  73. Inc(ABuffer.Pos);
  74. end;
  75. procedure AppendString(var ABuffer: TStringBuffer; const ASource: unicodestring);
  76. Var
  77. S : UTF8String;
  78. begin
  79. S:=UTF8Encode(aSource);
  80. AppendString(aBuffer,S);
  81. end;
  82. end.