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