1 |
|
---|
2 | {*****************************************************************************}
|
---|
3 | { }
|
---|
4 | { Tnt Delphi Unicode Controls }
|
---|
5 | { http://www.tntware.com/delphicontrols/unicode/ }
|
---|
6 | { Version: 2.3.0 }
|
---|
7 | { }
|
---|
8 | { Copyright (c) 2002-2007, Troy Wolbrink (troy.wolbrink@tntware.com) }
|
---|
9 | { }
|
---|
10 | {*****************************************************************************}
|
---|
11 |
|
---|
12 | unit TntWideStrUtils;
|
---|
13 |
|
---|
14 | {$INCLUDE TntCompilers.inc}
|
---|
15 |
|
---|
16 | interface
|
---|
17 |
|
---|
18 | { Wide string manipulation functions }
|
---|
19 |
|
---|
20 | {$IFNDEF COMPILER_9_UP}
|
---|
21 | function WStrAlloc(Size: Cardinal): PWideChar;
|
---|
22 | function WStrBufSize(const Str: PWideChar): Cardinal;
|
---|
23 | {$ENDIF}
|
---|
24 | {$IFNDEF COMPILER_10_UP}
|
---|
25 | function WStrMove(Dest: PWideChar; const Source: PWideChar; Count: Cardinal): PWideChar;
|
---|
26 | {$ENDIF}
|
---|
27 | {$IFNDEF COMPILER_9_UP}
|
---|
28 | function WStrNew(const Str: PWideChar): PWideChar;
|
---|
29 | procedure WStrDispose(Str: PWideChar);
|
---|
30 | {$ENDIF}
|
---|
31 | //---------------------------------------------------------------------------------------------
|
---|
32 | {$IFNDEF COMPILER_9_UP}
|
---|
33 | function WStrLen(Str: PWideChar): Cardinal;
|
---|
34 | function WStrEnd(Str: PWideChar): PWideChar;
|
---|
35 | {$ENDIF}
|
---|
36 | {$IFNDEF COMPILER_10_UP}
|
---|
37 | function WStrCat(Dest: PWideChar; const Source: PWideChar): PWideChar;
|
---|
38 | {$ENDIF}
|
---|
39 | {$IFNDEF COMPILER_9_UP}
|
---|
40 | function WStrCopy(Dest, Source: PWideChar): PWideChar;
|
---|
41 | function WStrLCopy(Dest, Source: PWideChar; MaxLen: Cardinal): PWideChar;
|
---|
42 | function WStrPCopy(Dest: PWideChar; const Source: WideString): PWideChar;
|
---|
43 | function WStrPLCopy(Dest: PWideChar; const Source: WideString; MaxLen: Cardinal): PWideChar;
|
---|
44 | {$ENDIF}
|
---|
45 | {$IFNDEF COMPILER_10_UP}
|
---|
46 | function WStrScan(const Str: PWideChar; Chr: WideChar): PWideChar;
|
---|
47 | // WStrComp and WStrPos were introduced as broken in Delphi 2006, but fixed in Delphi 2006 Update 2
|
---|
48 | function WStrComp(Str1, Str2: PWideChar): Integer;
|
---|
49 | function WStrPos(Str, SubStr: PWideChar): PWideChar;
|
---|
50 | {$ENDIF}
|
---|
51 | function Tnt_WStrComp(Str1, Str2: PWideChar): Integer; deprecated;
|
---|
52 | function Tnt_WStrPos(Str, SubStr: PWideChar): PWideChar; deprecated;
|
---|
53 |
|
---|
54 | { ------------ introduced --------------- }
|
---|
55 | function WStrECopy(Dest, Source: PWideChar): PWideChar;
|
---|
56 | function WStrLComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
|
---|
57 | function WStrLIComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
|
---|
58 | function WStrIComp(Str1, Str2: PWideChar): Integer;
|
---|
59 | function WStrLower(Str: PWideChar): PWideChar;
|
---|
60 | function WStrUpper(Str: PWideChar): PWideChar;
|
---|
61 | function WStrRScan(const Str: PWideChar; Chr: WideChar): PWideChar;
|
---|
62 | function WStrLCat(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
|
---|
63 | function WStrPas(const Str: PWideChar): WideString;
|
---|
64 |
|
---|
65 | { SysUtils.pas } //-------------------------------------------------------------------------
|
---|
66 |
|
---|
67 | {$IFNDEF COMPILER_10_UP}
|
---|
68 | function WideLastChar(const S: WideString): PWideChar;
|
---|
69 | function WideQuotedStr(const S: WideString; Quote: WideChar): WideString;
|
---|
70 | {$ENDIF}
|
---|
71 | {$IFNDEF COMPILER_9_UP}
|
---|
72 | function WideExtractQuotedStr(var Src: PWideChar; Quote: WideChar): Widestring;
|
---|
73 | {$ENDIF}
|
---|
74 | {$IFNDEF COMPILER_10_UP}
|
---|
75 | function WideDequotedStr(const S: WideString; AQuote: WideChar): WideString;
|
---|
76 | {$ENDIF}
|
---|
77 |
|
---|
78 | implementation
|
---|
79 |
|
---|
80 | uses
|
---|
81 | {$IFDEF COMPILER_9_UP} WideStrUtils, {$ENDIF} Math, Windows, TntWindows;
|
---|
82 |
|
---|
83 | {$IFNDEF COMPILER_9_UP}
|
---|
84 | function WStrAlloc(Size: Cardinal): PWideChar;
|
---|
85 | begin
|
---|
86 | Size := SizeOf(Cardinal) + (Size * SizeOf(WideChar));
|
---|
87 | GetMem(Result, Size);
|
---|
88 | PCardinal(Result)^ := Size;
|
---|
89 | Inc(PAnsiChar(Result), SizeOf(Cardinal));
|
---|
90 | end;
|
---|
91 |
|
---|
92 | function WStrBufSize(const Str: PWideChar): Cardinal;
|
---|
93 | var
|
---|
94 | P: PWideChar;
|
---|
95 | begin
|
---|
96 | P := Str;
|
---|
97 | Dec(PAnsiChar(P), SizeOf(Cardinal));
|
---|
98 | Result := PCardinal(P)^ - SizeOf(Cardinal);
|
---|
99 | Result := Result div SizeOf(WideChar);
|
---|
100 | end;
|
---|
101 | {$ENDIF}
|
---|
102 |
|
---|
103 | {$IFNDEF COMPILER_10_UP}
|
---|
104 | function WStrMove(Dest: PWideChar; const Source: PWideChar; Count: Cardinal): PWideChar;
|
---|
105 | var
|
---|
106 | Length: Integer;
|
---|
107 | begin
|
---|
108 | Result := Dest;
|
---|
109 | Length := Count * SizeOf(WideChar);
|
---|
110 | Move(Source^, Dest^, Length);
|
---|
111 | end;
|
---|
112 | {$ENDIF}
|
---|
113 |
|
---|
114 | {$IFNDEF COMPILER_9_UP}
|
---|
115 | function WStrNew(const Str: PWideChar): PWideChar;
|
---|
116 | var
|
---|
117 | Size: Cardinal;
|
---|
118 | begin
|
---|
119 | if Str = nil then Result := nil else
|
---|
120 | begin
|
---|
121 | Size := WStrLen(Str) + 1;
|
---|
122 | Result := WStrMove(WStrAlloc(Size), Str, Size);
|
---|
123 | end;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure WStrDispose(Str: PWideChar);
|
---|
127 | begin
|
---|
128 | if Str <> nil then
|
---|
129 | begin
|
---|
130 | Dec(PAnsiChar(Str), SizeOf(Cardinal));
|
---|
131 | FreeMem(Str, Cardinal(Pointer(Str)^));
|
---|
132 | end;
|
---|
133 | end;
|
---|
134 | {$ENDIF}
|
---|
135 |
|
---|
136 | //---------------------------------------------------------------------------------------------
|
---|
137 |
|
---|
138 | {$IFNDEF COMPILER_9_UP}
|
---|
139 | function WStrLen(Str: PWideChar): Cardinal;
|
---|
140 | begin
|
---|
141 | Result := WStrEnd(Str) - Str;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | function WStrEnd(Str: PWideChar): PWideChar;
|
---|
145 | begin
|
---|
146 | // returns a pointer to the end of a null terminated string
|
---|
147 | Result := Str;
|
---|
148 | While Result^ <> #0 do
|
---|
149 | Inc(Result);
|
---|
150 | end;
|
---|
151 | {$ENDIF}
|
---|
152 |
|
---|
153 | {$IFNDEF COMPILER_10_UP}
|
---|
154 | function WStrCat(Dest: PWideChar; const Source: PWideChar): PWideChar;
|
---|
155 | begin
|
---|
156 | Result := Dest;
|
---|
157 | WStrCopy(WStrEnd(Dest), Source);
|
---|
158 | end;
|
---|
159 | {$ENDIF}
|
---|
160 |
|
---|
161 | {$IFNDEF COMPILER_9_UP}
|
---|
162 | function WStrCopy(Dest, Source: PWideChar): PWideChar;
|
---|
163 | begin
|
---|
164 | Result := WStrLCopy(Dest, Source, MaxInt);
|
---|
165 | end;
|
---|
166 |
|
---|
167 | function WStrLCopy(Dest, Source: PWideChar; MaxLen: Cardinal): PWideChar;
|
---|
168 | var
|
---|
169 | Count: Cardinal;
|
---|
170 | begin
|
---|
171 | // copies a specified maximum number of characters from Source to Dest
|
---|
172 | Result := Dest;
|
---|
173 | Count := 0;
|
---|
174 | While (Count < MaxLen) and (Source^ <> #0) do begin
|
---|
175 | Dest^ := Source^;
|
---|
176 | Inc(Source);
|
---|
177 | Inc(Dest);
|
---|
178 | Inc(Count);
|
---|
179 | end;
|
---|
180 | Dest^ := #0;
|
---|
181 | end;
|
---|
182 |
|
---|
183 | function WStrPCopy(Dest: PWideChar; const Source: WideString): PWideChar;
|
---|
184 | begin
|
---|
185 | Result := WStrLCopy(Dest, PWideChar(Source), Length(Source));
|
---|
186 | end;
|
---|
187 |
|
---|
188 | function WStrPLCopy(Dest: PWideChar; const Source: WideString; MaxLen: Cardinal): PWideChar;
|
---|
189 | begin
|
---|
190 | Result := WStrLCopy(Dest, PWideChar(Source), MaxLen);
|
---|
191 | end;
|
---|
192 | {$ENDIF}
|
---|
193 |
|
---|
194 | {$IFNDEF COMPILER_10_UP}
|
---|
195 | function WStrScan(const Str: PWideChar; Chr: WideChar): PWideChar;
|
---|
196 | begin
|
---|
197 | Result := Str;
|
---|
198 | while Result^ <> Chr do
|
---|
199 | begin
|
---|
200 | if Result^ = #0 then
|
---|
201 | begin
|
---|
202 | Result := nil;
|
---|
203 | Exit;
|
---|
204 | end;
|
---|
205 | Inc(Result);
|
---|
206 | end;
|
---|
207 | end;
|
---|
208 |
|
---|
209 | function WStrComp(Str1, Str2: PWideChar): Integer;
|
---|
210 | begin
|
---|
211 | Result := WStrLComp(Str1, Str2, MaxInt);
|
---|
212 | end;
|
---|
213 |
|
---|
214 | function WStrPos(Str, SubStr: PWideChar): PWideChar;
|
---|
215 | var
|
---|
216 | PSave: PWideChar;
|
---|
217 | P: PWideChar;
|
---|
218 | PSub: PWideChar;
|
---|
219 | begin
|
---|
220 | // returns a pointer to the first occurance of SubStr in Str
|
---|
221 | Result := nil;
|
---|
222 | if (Str <> nil) and (Str^ <> #0) and (SubStr <> nil) and (SubStr^ <> #0) then begin
|
---|
223 | P := Str;
|
---|
224 | While P^ <> #0 do begin
|
---|
225 | if P^ = SubStr^ then begin
|
---|
226 | // investigate possibility here
|
---|
227 | PSave := P;
|
---|
228 | PSub := SubStr;
|
---|
229 | While (P^ = PSub^) do begin
|
---|
230 | Inc(P);
|
---|
231 | Inc(PSub);
|
---|
232 | if (PSub^ = #0) then begin
|
---|
233 | Result := PSave;
|
---|
234 | exit; // found a match
|
---|
235 | end;
|
---|
236 | if (P^ = #0) then
|
---|
237 | exit; // no match, hit end of string
|
---|
238 | end;
|
---|
239 | P := PSave;
|
---|
240 | end;
|
---|
241 | Inc(P);
|
---|
242 | end;
|
---|
243 | end;
|
---|
244 | end;
|
---|
245 | {$ENDIF}
|
---|
246 |
|
---|
247 | function Tnt_WStrComp(Str1, Str2: PWideChar): Integer; deprecated;
|
---|
248 | begin
|
---|
249 | Result := WStrComp(Str1, Str2);
|
---|
250 | end;
|
---|
251 |
|
---|
252 | function Tnt_WStrPos(Str, SubStr: PWideChar): PWideChar; deprecated;
|
---|
253 | begin
|
---|
254 | Result := WStrPos(Str, SubStr);
|
---|
255 | end;
|
---|
256 |
|
---|
257 | //------------------------------------------------------------------------------
|
---|
258 |
|
---|
259 | function WStrECopy(Dest, Source: PWideChar): PWideChar;
|
---|
260 | begin
|
---|
261 | Result := WStrEnd(WStrCopy(Dest, Source));
|
---|
262 | end;
|
---|
263 |
|
---|
264 | function WStrComp_EX(Str1, Str2: PWideChar; MaxLen: Cardinal; dwCmpFlags: Cardinal): Integer;
|
---|
265 | var
|
---|
266 | Len1, Len2: Integer;
|
---|
267 | begin
|
---|
268 | if MaxLen = Cardinal(MaxInt) then begin
|
---|
269 | Len1 := -1;
|
---|
270 | Len2 := -1;
|
---|
271 | end else begin
|
---|
272 | Len1 := Min(WStrLen(Str1), MaxLen);
|
---|
273 | Len2 := Min(WStrLen(Str2), MaxLen);
|
---|
274 | end;
|
---|
275 | Result := Tnt_CompareStringW(GetThreadLocale, dwCmpFlags, Str1, Len1, Str2, Len2) - 2;
|
---|
276 | end;
|
---|
277 |
|
---|
278 | function WStrLComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
|
---|
279 | begin
|
---|
280 | Result := WStrComp_EX(Str1, Str2, MaxLen, 0);
|
---|
281 | end;
|
---|
282 |
|
---|
283 | function WStrLIComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
|
---|
284 | begin
|
---|
285 | Result := WStrComp_EX(Str1, Str2, MaxLen, NORM_IGNORECASE);
|
---|
286 | end;
|
---|
287 |
|
---|
288 | function WStrIComp(Str1, Str2: PWideChar): Integer;
|
---|
289 | begin
|
---|
290 | Result := WStrLIComp(Str1, Str2, MaxInt);
|
---|
291 | end;
|
---|
292 |
|
---|
293 | function WStrLower(Str: PWideChar): PWideChar;
|
---|
294 | begin
|
---|
295 | Result := Str;
|
---|
296 | Tnt_CharLowerBuffW(Str, WStrLen(Str))
|
---|
297 | end;
|
---|
298 |
|
---|
299 | function WStrUpper(Str: PWideChar): PWideChar;
|
---|
300 | begin
|
---|
301 | Result := Str;
|
---|
302 | Tnt_CharUpperBuffW(Str, WStrLen(Str))
|
---|
303 | end;
|
---|
304 |
|
---|
305 | function WStrRScan(const Str: PWideChar; Chr: WideChar): PWideChar;
|
---|
306 | var
|
---|
307 | MostRecentFound: PWideChar;
|
---|
308 | begin
|
---|
309 | if Chr = #0 then
|
---|
310 | Result := WStrEnd(Str)
|
---|
311 | else
|
---|
312 | begin
|
---|
313 | Result := nil;
|
---|
314 | MostRecentFound := Str;
|
---|
315 | while True do
|
---|
316 | begin
|
---|
317 | while MostRecentFound^ <> Chr do
|
---|
318 | begin
|
---|
319 | if MostRecentFound^ = #0 then
|
---|
320 | Exit;
|
---|
321 | Inc(MostRecentFound);
|
---|
322 | end;
|
---|
323 | Result := MostRecentFound;
|
---|
324 | Inc(MostRecentFound);
|
---|
325 | end;
|
---|
326 | end;
|
---|
327 | end;
|
---|
328 |
|
---|
329 | function WStrLCat(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
|
---|
330 | begin
|
---|
331 | Result := Dest;
|
---|
332 | WStrLCopy(WStrEnd(Dest), Source, MaxLen - WStrLen(Dest));
|
---|
333 | end;
|
---|
334 |
|
---|
335 | function WStrPas(const Str: PWideChar): WideString;
|
---|
336 | begin
|
---|
337 | Result := Str;
|
---|
338 | end;
|
---|
339 |
|
---|
340 | //---------------------------------------------------------------------------------------------
|
---|
341 |
|
---|
342 | {$IFNDEF COMPILER_10_UP}
|
---|
343 | function WideLastChar(const S: WideString): PWideChar;
|
---|
344 | begin
|
---|
345 | if S = '' then
|
---|
346 | Result := nil
|
---|
347 | else
|
---|
348 | Result := @S[Length(S)];
|
---|
349 | end;
|
---|
350 |
|
---|
351 | function WideQuotedStr(const S: WideString; Quote: WideChar): WideString;
|
---|
352 | var
|
---|
353 | P, Src,
|
---|
354 | Dest: PWideChar;
|
---|
355 | AddCount: Integer;
|
---|
356 | begin
|
---|
357 | AddCount := 0;
|
---|
358 | P := WStrScan(PWideChar(S), Quote);
|
---|
359 | while (P <> nil) do
|
---|
360 | begin
|
---|
361 | Inc(P);
|
---|
362 | Inc(AddCount);
|
---|
363 | P := WStrScan(P, Quote);
|
---|
364 | end;
|
---|
365 |
|
---|
366 | if AddCount = 0 then
|
---|
367 | Result := Quote + S + Quote
|
---|
368 | else
|
---|
369 | begin
|
---|
370 | SetLength(Result, Length(S) + AddCount + 2);
|
---|
371 | Dest := PWideChar(Result);
|
---|
372 | Dest^ := Quote;
|
---|
373 | Inc(Dest);
|
---|
374 | Src := PWideChar(S);
|
---|
375 | P := WStrScan(Src, Quote);
|
---|
376 | repeat
|
---|
377 | Inc(P);
|
---|
378 | Move(Src^, Dest^, 2 * (P - Src));
|
---|
379 | Inc(Dest, P - Src);
|
---|
380 | Dest^ := Quote;
|
---|
381 | Inc(Dest);
|
---|
382 | Src := P;
|
---|
383 | P := WStrScan(Src, Quote);
|
---|
384 | until P = nil;
|
---|
385 | P := WStrEnd(Src);
|
---|
386 | Move(Src^, Dest^, 2 * (P - Src));
|
---|
387 | Inc(Dest, P - Src);
|
---|
388 | Dest^ := Quote;
|
---|
389 | end;
|
---|
390 | end;
|
---|
391 | {$ENDIF}
|
---|
392 |
|
---|
393 | {$IFNDEF COMPILER_9_UP}
|
---|
394 | function WideExtractQuotedStr(var Src: PWideChar; Quote: WideChar): Widestring;
|
---|
395 | var
|
---|
396 | P, Dest: PWideChar;
|
---|
397 | DropCount: Integer;
|
---|
398 | begin
|
---|
399 | Result := '';
|
---|
400 | if (Src = nil) or (Src^ <> Quote) then Exit;
|
---|
401 | Inc(Src);
|
---|
402 | DropCount := 1;
|
---|
403 | P := Src;
|
---|
404 | Src := WStrScan(Src, Quote);
|
---|
405 | while Src <> nil do // count adjacent pairs of quote chars
|
---|
406 | begin
|
---|
407 | Inc(Src);
|
---|
408 | if Src^ <> Quote then Break;
|
---|
409 | Inc(Src);
|
---|
410 | Inc(DropCount);
|
---|
411 | Src := WStrScan(Src, Quote);
|
---|
412 | end;
|
---|
413 | if Src = nil then Src := WStrEnd(P);
|
---|
414 | if ((Src - P) <= 1) then Exit;
|
---|
415 | if DropCount = 1 then
|
---|
416 | SetString(Result, P, Src - P - 1)
|
---|
417 | else
|
---|
418 | begin
|
---|
419 | SetLength(Result, Src - P - DropCount);
|
---|
420 | Dest := PWideChar(Result);
|
---|
421 | Src := WStrScan(P, Quote);
|
---|
422 | while Src <> nil do
|
---|
423 | begin
|
---|
424 | Inc(Src);
|
---|
425 | if Src^ <> Quote then Break;
|
---|
426 | Move(P^, Dest^, (Src - P) * SizeOf(WideChar));
|
---|
427 | Inc(Dest, Src - P);
|
---|
428 | Inc(Src);
|
---|
429 | P := Src;
|
---|
430 | Src := WStrScan(Src, Quote);
|
---|
431 | end;
|
---|
432 | if Src = nil then Src := WStrEnd(P);
|
---|
433 | Move(P^, Dest^, (Src - P - 1) * SizeOf(WideChar));
|
---|
434 | end;
|
---|
435 | end;
|
---|
436 | {$ENDIF}
|
---|
437 |
|
---|
438 | {$IFNDEF COMPILER_10_UP}
|
---|
439 | function WideDequotedStr(const S: WideString; AQuote: WideChar): WideString;
|
---|
440 | var
|
---|
441 | LText : PWideChar;
|
---|
442 | begin
|
---|
443 | LText := PWideChar(S);
|
---|
444 | Result := WideExtractQuotedStr(LText, AQuote);
|
---|
445 | if Result = '' then
|
---|
446 | Result := S;
|
---|
447 | end;
|
---|
448 | {$ENDIF}
|
---|
449 |
|
---|
450 |
|
---|
451 | end.
|
---|