[112] | 1 | Unit CharUtilsUnit;
|
---|
| 2 |
|
---|
| 3 | // NewView - a new OS/2 Help Viewer
|
---|
| 4 | // Copyright 2006-2007 Ronald Brill (rbri at rbri dot de)
|
---|
| 5 | // This software is released under the GNU Public License - see readme.txt
|
---|
| 6 |
|
---|
| 7 | // Helper functions to work with characters
|
---|
| 8 |
|
---|
| 9 | Interface
|
---|
| 10 |
|
---|
| 11 | uses
|
---|
| 12 | Classes;
|
---|
| 13 |
|
---|
| 14 | const
|
---|
| 15 | CharTAB = chr(9);
|
---|
| 16 | CharCR = chr(13);
|
---|
| 17 | CharLF = chr(10);
|
---|
[139] | 18 | CharSingleQuote = '''';
|
---|
| 19 | CharDoubleQuote = '"';
|
---|
[112] | 20 |
|
---|
| 21 |
|
---|
| 22 | TYPE
|
---|
| 23 | TSetOfChars = set of char;
|
---|
| 24 |
|
---|
| 25 | // Returns true if aChar is a digit 0..9
|
---|
| 26 | Function CharIsDigit(const aChar : char) : boolean;
|
---|
| 27 |
|
---|
| 28 | // Returns true if aChar is an alphabetic character a..z A..Z
|
---|
| 29 | Function CharIsAlpha(const aChar : char) : boolean;
|
---|
| 30 |
|
---|
[117] | 31 |
|
---|
| 32 | // ---------------
|
---|
| 33 | // ---- PChar ----
|
---|
| 34 | // ---------------
|
---|
| 35 |
|
---|
[199] | 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 |
|
---|
[117] | 41 | // Converts a PChar into a String like StrPas
|
---|
| 42 | // but conversts at least the first aLength chars
|
---|
| 43 | Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String;
|
---|
| 44 |
|
---|
| 45 | // Returns the difference of the pointers
|
---|
[121] | 46 | Function PCharPointerDiff(const aMinuend: PChar; const aSubtrahend : PChar) : Longword;
|
---|
[117] | 47 |
|
---|
[199] | 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 |
|
---|
| 52 |
|
---|
[112] | 53 | Implementation
|
---|
| 54 |
|
---|
| 55 | uses
|
---|
| 56 | SysUtils;
|
---|
| 57 |
|
---|
| 58 | Function CharIsDigit(const aChar : char) : boolean;
|
---|
| 59 | begin
|
---|
| 60 | Result := (aChar >= '0') and (aChar <= '9');
|
---|
| 61 | end;
|
---|
| 62 |
|
---|
| 63 | Function CharIsAlpha(const aChar : char) : boolean;
|
---|
| 64 | begin
|
---|
| 65 | Result := ( (aChar >= 'A') and (aChar <= 'Z') ) or ((aChar >= 'a') and (aChar <= 'z'));
|
---|
| 66 | end;
|
---|
| 67 |
|
---|
| 68 |
|
---|
[117] | 69 | // ---------------
|
---|
| 70 | // ---- PChar ----
|
---|
| 71 | // ---------------
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String;
|
---|
| 75 | var
|
---|
| 76 | i: integer;
|
---|
| 77 | begin
|
---|
| 78 | Result := '';
|
---|
| 79 | i := 0;
|
---|
| 80 | while (aPChar[i] <> #0) and (i < aLength) do
|
---|
| 81 | begin
|
---|
| 82 | Result := Result + aPChar[i];
|
---|
| 83 | inc( i );
|
---|
| 84 | end;
|
---|
| 85 | end;
|
---|
| 86 |
|
---|
| 87 |
|
---|
[121] | 88 | Function PCharPointerDiff(const aMinuend: PChar; const aSubtrahend : PChar) : Longword;
|
---|
[117] | 89 | begin
|
---|
| 90 | Result := Longword(aMinuend) - Longword(aSubtrahend);
|
---|
| 91 | end;
|
---|
| 92 |
|
---|
| 93 |
|
---|
[199] | 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 |
|
---|
[112] | 133 | END.
|
---|