Changeset 199
- Timestamp:
- Jun 7, 2007, 8:53:31 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Library/CharUtilsUnit.pas
r173 r199 34 34 // --------------- 35 35 36 36 37 // Allocates enough memory for a copy of aString as a PChar 38 // and copies aString to it. 39 Function NewPCharAsCopyOfStr(const aString: String) : PChar; 40 37 41 // Converts a PChar into a String like StrPas 38 42 // but conversts at least the first aLength chars … … 41 45 // Returns the difference of the pointers 42 46 Function PCharPointerDiff(const aMinuend: PChar; const aSubtrahend : PChar) : Longword; 47 48 // Concatentate AddText to Text. Reallocate and expand 49 // Text if necessary. This is a size-safe StrCat 50 Procedure AddAndResize(var aText: PChar; const aTextToAdd: PChar); 51 43 52 44 53 Implementation … … 63 72 64 73 65 // Converts a PChar into a String like StrPas66 // but conversts at least the first aLength chars67 74 Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String; 68 75 var … … 85 92 86 93 94 Procedure CheckPCharSize(Var aText: PChar; const aNeededSize: longword); 95 var 96 tmpPChar: PChar; 97 tmpNewBufferSize: longword; 98 begin 99 if (aNeededSize + 1) // + 1 to allow for null terminator 100 > StrBufSize(aText) then 101 begin 102 // allocate new buffer, double the size... 103 tmpNewBufferSize := StrBufSize(aText) * 2; 104 // or if that's not enough... 105 if tmpNewBufferSize < (aNeededSize + 1) then 106 begin 107 // double what we are going to need 108 tmpNewBufferSize:= aNeededSize * 2; 109 end; 110 111 tmpPChar := StrAlloc(tmpNewBufferSize); 112 113 // copy string to new buffer 114 StrCopy(tmpPChar, aText); 115 StrDispose(aText); 116 aText:= tmpPChar; 117 end; 118 end; 119 120 121 Procedure AddAndResize(var aText: PChar; const aTextToAdd: PChar); 122 begin 123 CheckPCharSize(aText, strlen(aText) + strlen(aTextToAdd)); 124 StrCat(aText, aTextToAdd); 125 end; 126 127 Function NewPCharAsCopyOfStr(const aString: String) : PChar; 128 begin 129 Result := StrAlloc(length(aString) + 1); 130 StrPCopy(Result, aString); 131 end; 132 87 133 END.
Note:
See TracChangeset
for help on using the changeset viewer.