Tuesday, February 9, 2010

Simple String Buffer

 
unit StrBuf;
 
{$mode objfpc}{$H+}
 
{ Simple string buffer
 
  No setup/teardown required, memory mngmt, initialization, and finalization
  is done by FPC itself.
 
  Copyright (C) 2010 bflm, contact: befelemepeseveze at Google's free mail
 
  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:
 
  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.
 
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.
 
  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
 
interface
 
uses
  SysUtils;
 
type
 
  { TStrBuf }
 
  TStrBuf = object
  private
    FLen: Integer;
    FS: String;
    function GetS: String;
  public
    procedure W(const S: String);
    procedure W(const Fmt: String; Args: array of const);
    property S: String read GetS;
  end;
 
operator +(var Buf: TStrBuf; const S: String): TStrBuf;
operator :=(const S: String): TStrBuf;
operator :=(var Buf: TStrBuf): String;
 
implementation
 
{ TStrBuf }
 
function TStrBuf.GetS: String;
begin
  if FS = '' then
    FLen := 0;
  Result := LeftStr(FS, FLen);
end;
 
procedure TStrBuf.W(const S: String);
var Len, Len1: Integer;
begin
  Len := Length(S);
  if Len = 0 then
    Exit;
  if FS = '' then
    FLen := 0;
  Len1 := FLen + Len;
  if Len1 > Length(FS) then
    SetLength(FS, 2 * Len1);
  Move(S[1], FS[FLen + 1], Len);
  FLen += Len;
end;
 
procedure TStrBuf.W(const Fmt: String; Args: array of const);
begin
  W(Format(Fmt, Args));
end;
 
operator +(var Buf: TStrBuf; const S: String): TStrBuf;
begin
  Result.FS := Buf.FS;
  Result.FLen := Buf.FLen;
  Result.W(S);
end;
 
operator :=(const S: String): TStrBuf;
begin
  Result.FLen := Length(S);
  Result.FS := S;
end;
 
operator :=(var Buf: TStrBuf): String;
begin
  Result := Buf.S;
end;
 
end.
 

 
program project1;
 
{$mode objfpc}{$H+}
 
// Simple string buffer example
 
uses
  heaptrc, StrBuf;
 
function F(N: Integer): String;
var
  Buf: TStrBuf;
  I: Integer;
begin
  for I := 1 to N do
    Buf.W('line %d%s', [I, sLineBreak]);
  Result := Buf.S;
end;
 
var
  Buf, Buf2: TStrBuf;
 
begin
  Buf.W('abc');
  Buf.W('%d', [123]);
  Buf += 'xyz';
  Writeln(Buf.S);
  Writeln(String(Buf));
  Buf := '456';
  Writeln(Buf.S);
  Buf2 := Buf + ' bar';
  Buf += ' foo';
  Writeln(Buf.S);
  Writeln(Buf2.S);
  Write(F(10));
end.

$ fpc -B project1.pas && ./project1 
Free Pascal Compiler version 2.5.1 [2010/02/07] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling project1.pas
Compiling strbuf.pas
Linking project1
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
148 lines compiled, 0.2 sec 
abc123xyz
abc123xyz
456
456 foo
456 bar
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
Heap dump by heaptrc unit
71 memory blocks allocated : 2926/3200
71 memory blocks freed     : 2926/3200
0 unfreed memory blocks : 0
True heap size : 360448
True free heap : 360448
$

No comments:

Post a Comment