Changeset 391
- Timestamp:
- Apr 30, 2016, 7:53:55 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Library/CharUtilsUnit.pas
r388 r391 2 2 3 3 // 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) 5 6 // This software is released under the GNU Public License - see readme.txt 6 7 … … 34 35 // --------------- 35 36 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 37 42 // Converts a PChar into a String like StrPas 38 43 // but conversts at least the first aLength chars … … 41 46 // Returns the difference of the pointers 42 47 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 43 53 44 54 Implementation … … 63 73 64 74 65 // Converts a PChar into a String like StrPas66 // but conversts at least the first aLength chars67 75 Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String; 68 76 var … … 85 93 86 94 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 87 134 END. -
trunk/unittests/CharUtilsUnitTests.pas
r384 r391 1 1 Unit 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 2 8 3 9 Interface
Note:
See TracChangeset
for help on using the changeset viewer.