Changeset 391


Ignore:
Timestamp:
Apr 30, 2016, 7:53:55 PM (9 years ago)
Author:
RBRi
Message:

more char utils (merged from old 2.20)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Library/CharUtilsUnit.pas

    r388 r391  
    22
    33// NewView - a new OS/2 Help Viewer
    4 // Copyright 2006-2007 Ronald Brill (rbri at rbri dot de)
     4// Copyright 2003-2006 Aaron Lawrence
     5// Copyright 2006-2009 Ronald Brill (rbri at rbri dot de)
    56// This software is released under the GNU Public License - see readme.txt
    67
     
    3435  // ---------------
    3536
    36  
     37
     38  // Allocates enough memory for a copy of aString as a PChar
     39  // and copies aString to it.
     40  Function NewPCharAsCopyOfStr(const aString: String) : PChar;
     41
    3742  // Converts a PChar into a String like StrPas
    3843  // but conversts at least the first aLength chars
     
    4146  // Returns the difference of the pointers
    4247  Function PCharPointerDiff(const aMinuend: PChar; const aSubtrahend : PChar)  : Longword;
     48
     49  // Concatentate AddText to Text. Reallocate and expand
     50  // Text if necessary. This is a size-safe StrCat
     51  Procedure AddAndResize(var aText: PChar; const aTextToAdd: PChar);
     52
    4353
    4454Implementation
     
    6373
    6474
    65   // Converts a PChar into a String like StrPas
    66   // but conversts at least the first aLength chars
    6775  Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String;
    6876  var
     
    8593
    8694
     95  Procedure CheckPCharSize(Var aText: PChar; const aNeededSize: longword);
     96  var
     97    tmpPChar: PChar;
     98    tmpNewBufferSize: longword;
     99  begin
     100    if (aNeededSize + 1) // + 1 to allow for null terminator
     101       > StrBufSize(aText) then
     102    begin
     103      // allocate new buffer, double the size...
     104      tmpNewBufferSize := StrBufSize(aText) * 2;
     105      // or if that's not enough...
     106      if tmpNewBufferSize < (aNeededSize + 1) then
     107      begin
     108        // double what we are going to need
     109        tmpNewBufferSize:= aNeededSize * 2;
     110      end;
     111
     112      tmpPChar := StrAlloc(tmpNewBufferSize);
     113
     114      // copy string to new buffer
     115      StrCopy(tmpPChar, aText);
     116      StrDispose(aText);
     117      aText:= tmpPChar;
     118    end;
     119  end;
     120
     121
     122  Procedure AddAndResize(var aText: PChar; const aTextToAdd: PChar);
     123  begin
     124    CheckPCharSize(aText, strlen(aText) + strlen(aTextToAdd));
     125    StrCat(aText, aTextToAdd);
     126  end;
     127
     128  Function NewPCharAsCopyOfStr(const aString: String) : PChar;
     129  begin
     130    Result := StrAlloc(length(aString) + 1);
     131    StrPCopy(Result, aString);
     132  end;
     133
    87134END.
  • trunk/unittests/CharUtilsUnitTests.pas

    r384 r391  
    11Unit CharUtilsUnitTests;
     2
     3// NewView - a new OS/2 Help Viewer
     4// Copyright 2006-2009 Ronald Brill (rbri at rbri dot de)
     5// This software is released under the GNU Public License - see readme.txt
     6
     7// UnitTests for CharUtilsUnit
    28
    39Interface
Note: See TracChangeset for help on using the changeset viewer.