2 //******************************************************************************
3 //
\95¶
\8e\9a\97ñ
\92u
\8a·
\8aÖ
\90\94 CustomStringReplace
4 //
\8eg
\82¢
\95û
\82Í
\81A
5 //
\81@CustomStringReplace(
6 //
\81@
\8c³
\82Ì
\95¶
\8e\9a\97ñ
\81iString
\82à
\82µ
\82
\82ÍTStringList),
7 //
\81@
\8c\9f\8dõ
\95¶
\8e\9a\97ñ
\81iString),
8 //
\92u
\8a·
\95¶
\8e\9a\97ñ
\81iString),
9 //
\91å
\95¶
\8e\9a\8f¬
\95¶
\8e\9a\81iBoolean)True:
\8bæ
\95Ê
\82µ
\82È
\82¢
\81@false or
\8fÈ
\97ª:
\8bæ
\95Ê
\82·
\82é
11 // Delphi-ML
\82Ì
\8bL
\8e\9669334
\82É
\8dÚ
\82Á
\82Ä
\82¢
\82½
\83R
\81[
\83h
\82ð
\8aÛ
\83p
\83N
\83\8a\82µ
\82Ü
\82µ
\82½
\81B
12 //******************************************************************************
17 Windows, Classes, SysUtils;
19 function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
20 function AnsiStrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
21 function ReplaceString(const S, OldPattern, NewPattern: string): string;
22 function IgnoCaseReplaceString(const S, OldPattern, NewPattern: string): string;
24 function CustomStringReplace(S , OldPattern: String;const NewPattern: string): String; overload;
25 function CustomStringReplace(S , OldPattern: String;const NewPattern: string; IgnoreCase : Boolean): String; overload;
26 procedure CustomStringReplace(var S : TStringList; OldPattern: String;const NewPattern: string);overload;
27 procedure CustomStringReplace(var S : TStringList; OldPattern: String;const NewPattern: string; IgnoreCase : Boolean);overload;
29 function ZenToHan(const s: string): string;
30 function VaguePos(const Substr, S: string): Integer;
32 function ReplaseNoValidateChar( inVal : String): String;
35 //
\83|
\83C
\83\93\83^
\81[
\81\95\83A
\83Z
\83\93\83u
\83\89\82É
\82æ
\82é
\8d\82\91¬
\83|
\83X
36 function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
42 MOV ESI,ECX { Point ESI to substr }
43 MOV EDI,EAX { Point EDI to s }
45 MOV ECX,EDX { ECX = search length }
51 DEC EDX { EDX = Length(substr) - 1 }
52 JS @@fail { < 0 ? return 0 }
53 MOV AL,[ESI] { AL = first char of substr }
54 INC ESI { Point ESI to 2'nd char of substr }
56 SUB ECX,EDX { #positions in s to look at }
57 { = Length(s) - Length(substr) + 1 }
62 MOV EBX,ECX { save outer loop counter }
63 PUSH ESI { save outer loop substr pointer }
64 PUSH EDI { save outer loop s pointer }
68 POP EDI { restore outer loop s pointer }
69 POP ESI { restore outer loop substr pointer }
71 MOV ECX,EBX { restore outer loop counter }
79 MOV EAX,EDI { EDI points of char after match }
86 //
\81@AnsiPos
\82Ì
\8d\82\91¬
\94Å
87 function AnsiStrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
90 ByteType : TMbcsByteType;
93 if (StrStart = nil) or (StrStart^ = #0) or
94 (SubstrStart = nil) or (SubstrStart^ = #0) then Exit;
96 L2 := SubstrEnd - SubstrStart;
97 Result := StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd);
99 while (Result <> nil) and (StrEnd - Result >= L2) do begin
100 ByteType := StrByteType(StrStart, Integer(Result-StrStart));
101 if (ByteType <> mbTrailByte) and
102 (CompareString(LOCALE_USER_DEFAULT, SORT_STRINGSORT, Result, L2, SubstrStart, L2) = 2)
104 if (ByteType = mbLeadByte) then Inc(Result);
106 Result := StrPosEx(Result, StrEnd, SubStrStart, SubStrEnd);
112 //
\8d\82\91¬
\95¶
\8e\9a\97ñ
\92u
\8a·
\8aÖ
\90\94\81i
\91å
\95¶
\8e\9a\8f¬
\95¶
\8e\9a\82Ì
\88á
\82¢
\82ð
\96³
\8e\8b\82µ
\82È
\82¢
\81j
113 function ReplaceString(const S, OldPattern, NewPattern: string): string;
115 ReplaceCount: Integer;
122 if OldPattern = '' then Exit;
125 ps := PChar(OldPattern);
126 pe := ps + Length(OldPattern);
129 p := AnsiStrPosEx(p, e, ps, pe);
130 if p = nil then Break;
132 Inc(p, Length(OldPattern));
134 if ReplaceCount = 0 then Exit;
135 SetString(Result, nil, Length(S) +
136 (Length(NewPattern) - Length(OldPattern)) * ReplaceCount);
139 l := Length( NewPattern );
140 for i := 0 to ReplaceCount - 1 do begin
141 Count := AnsiStrPosEx(p, e, ps, pe) - p;
142 Move(p^, Result[DestIndex], Count);
143 Inc(p, Count);//p := pp;
144 Inc(DestIndex, Count);
145 Move(NewPattern[1], Result[DestIndex], l);
146 Inc(p, Length(OldPattern));
149 Move(p^, Result[DestIndex], e - p);
151 //
\8d\82\91¬
\95¶
\8e\9a\97ñ
\92u
\8a·
\8aÖ
\90\94\81i
\91å
\95¶
\8e\9a\8f¬
\95¶
\8e\9a\82Ì
\88á
\82¢
\82ð
\96³
\8e\8b\82·
\82é
\81j
152 function IgnoCaseReplaceString(const S, OldPattern, NewPattern: string): string;
154 ReplaceCount: Integer;
157 p, e{, ps, pe}: PChar;
158 p2, e2, ps2, pe2: PChar;
161 bufferOldPattern : String;
164 bufferS := AnsiLowerCase(S);
165 bufferOldPattern := AnsiLowerCase(OldPattern);
167 if OldPattern = '' then Exit;
169 p2 := PChar(bufferS);
171 e2 := p2 + Length(bufferS);
172 //ps := PChar(OldPattern);
173 ps2 := PChar(bufferOldPattern);
174 //pe := ps + Length(OldPattern);
175 pe2 := ps2 + Length(bufferOldPattern);
178 while p2 < e2 do begin
179 p2 := AnsiStrPosEx(p2, e2, ps2, pe2);
180 if p2 = nil then Break;
182 Inc(p2, Length(bufferOldPattern));
184 if ReplaceCount = 0 then Exit;
185 SetString(Result, nil, Length(bufferS) +
186 (Length(NewPattern) - Length(bufferOldPattern)) * ReplaceCount);
187 p2 := PChar(bufferS);
189 l := Length( NewPattern );
190 for i := 0 to ReplaceCount - 1 do begin
191 Count := AnsiStrPosEx(p2, e2, ps2, pe2) - p2;
192 Move(p^, Result[DestIndex], Count);
193 Inc(p, Count);//p := pp;
194 Inc(p2, Count);//p := pp;
195 Inc(DestIndex, Count);
196 Move(NewPattern[1], Result[DestIndex], l);
197 Inc(p, Length(OldPattern));
198 Inc(p2, Length(OldPattern));
201 Move(p^, Result[DestIndex], e - p);
207 //
\8d\82\91¬
\95¶
\8e\9a\97ñ
\92u
\8a·
\8aÖ
\90\94\81i
\83v
\83\8a\83~
\83e
\83B
\83u
\81j
208 function CustomStringReplace(
209 S ,OldPattern: String;
210 const NewPattern: string
214 Result := ReplaceString(S,OldPattern,NewPattern);
217 //
\8d\82\91¬
\95¶
\8e\9a\97ñ
\92u
\8a·
\8aÖ
\90\94\81i
\94Ä
\97p
\94Å
\82P
\81j
218 function CustomStringReplace(
219 S , OldPattern: String;
220 const NewPattern: string;
225 if not IgnoreCase then begin
226 Result := ReplaceString(S,OldPattern,NewPattern);
228 Result := IgnoCaseReplaceString(S,OldPattern,NewPattern);
232 //
\8d\82\91¬
\95¶
\8e\9a\97ñ
\92u
\8a·
\8aÖ
\90\94\81i
\94Ä
\97p
\94Å
\82Q
\81j
233 procedure CustomStringReplace(
236 const NewPattern: string;
243 if not IgnoreCase then begin
244 for i := 0 to S.Count - 1 do begin
245 S.Strings[i] := ReplaceString(S.Strings[i], OldPattern,NewPattern);
248 for i := 0 to S.Count - 1 do begin
249 S.Strings[i] := IgnoCaseReplaceString(S.Strings[i], OldPattern,NewPattern);
255 //
\8d\82\91¬
\95¶
\8e\9a\97ñ
\92u
\8a·
\8aÖ
\90\94\81i
\94Ä
\97p
\94Å
\82R
\81j
256 procedure CustomStringReplace(
259 const NewPattern: string
265 for i := 0 to S.Count - 1 do begin
266 S.Strings[i] := ReplaceString(S.Strings[i], OldPattern,NewPattern);;
271 (*************************************************************************
272 *
\91S
\8ap
\81¨
\94¼
\8ap
274 *************************************************************************)
275 function ZenToHan(const s: string): string;
277 //Chr: array [0..1024] of char;
281 SetLength(Chr, Length(s));
282 ChrLen := Windows.LCMapString(
283 GetUserDefaultLCID(),
285 LCMAP_HALFWIDTH or LCMAP_KATAKANA or LCMAP_LOWERCASE,
291 Result := Copy(Chr, 1, ChrLen);
294 (*************************************************************************
295 *
\91S
\8ap
\94¼
\8ap
\82Ð
\82ç
\82ª
\82È
\82©
\82½
\82©
\82È
\82ð
\8bæ
\95Ê
\82µ
\82È
\82¢
\90¦
\82¢Pos
296 *************************************************************************)
297 function VaguePos(const Substr, S: string): Integer;
299 Result := AnsiPos(ZenToHan(Substr), ZenToHan(S));
301 (*************************************************************************
302 * FAT/NTFS
\82Ì
\83t
\83@
\83C
\83\8b\96¼
\82É
\8b\96\82³
\82ê
\82È
\82¢
\95¶
\8e\9a\81i\,/,*,>,<,|
\81j
\82ð
\91S
\8ap
\82É
\92u
\8a·
\82·
\82é
303 *************************************************************************)
304 function ReplaseNoValidateChar( inVal : String): String;
306 Result := CustomStringReplace(inVal, '\', '
\81\8f');
307 Result := CustomStringReplace(Result, '/', '
\81^');
308 Result := CustomStringReplace(Result, '*', '
\81\96');
309 Result := CustomStringReplace(Result, '>', '
\81\84');
310 Result := CustomStringReplace(Result, '<', '
\81\83');
311 Result := CustomStringReplace(Result, '|', '
\81b');