OSDN Git Service

・正規表現メタキャラクタを排除する RegExpEncode を追加。
[gikonavigoeson/gikonavi.git] / YofUtils.pas
1 unit YofUtils;
2
3 {
4         HttpApp \82Ì\83N\83\8d\81[\83\93\82â\82»\82Ì\91¼\8eG\97p\8aÖ\90\94
5 }
6 interface
7
8 //==================================================
9 uses
10 //==================================================
11
12         Classes, SysUtils;
13
14 procedure ExtractHttpFields(
15         const chrSep : TSysCharSet;
16         const chrWhite : TSysCharSet;
17         const strValue : string;
18         var strResult : TStringList;
19         unknownFlag : boolean = false
20 );
21
22 function HtmlEncode(
23         const strValue : string
24 ) : string;
25
26 function HtmlDecode(
27         const strValue : string
28 ) : string;
29
30 function HttpEncode(
31         const strValue : string
32 ) : string;
33
34 function MatchesMask(
35         const filename, mask : string
36 ) : boolean;
37
38 // \83\81\83^\83L\83\83\83\89\83N\83^\82ð\90³\8bK\95\\8c»\88µ\82¢\82É\82È\82ç\82È\82¢\82æ\82¤\82É\92u\8a·
39 function RegExpEncode(
40         const text : string
41 ) : string;
42
43 //==================================================
44 const
45 //==================================================
46         kYofKanji : TSysCharSet = [#$80..#$A0, #$E0..#$ff];
47
48 //==================================================
49 implementation
50 //==================================================
51
52 // \82Æ\82è\82 \82¦\82¸\82Ì\91ã\97p\95i\82È\82Ì\82Å chrWhite \82ð\8dl\97\82µ\82Ä\82¢\82È\82¢\82±\82Æ\82É\92\8d\88Ó\81I\81I\81I
53 procedure ExtractHttpFields(
54         const chrSep : TSysCharSet;
55         const chrWhite : TSysCharSet;
56         const strValue : string;
57         var strResult : TStringList;
58         unknownFlag : boolean = false
59 );
60 var
61         last, p, strLen : Integer;
62 begin
63
64         strLen := Length( strValue );
65         p := 1;
66         last := 1;
67
68         while p <= strLen do
69         begin
70
71                 if strValue[ p ] in chrSep then
72                 begin
73                         strResult.Add( Copy( strValue, last, p - last ) );
74                         last := p + 1;
75                 end;
76
77                 p := p + 1;
78
79         end;
80
81         if last <> p then
82                 strResult.Add( Copy( strValue, last, strLen - last + 1 ) );
83
84 end;
85
86 function HtmlEncode(
87         const strValue : string
88 ) : string;
89 var
90         i : Integer;
91         strLen : Integer;
92         strResult : string;
93 begin
94
95         strLen := Length( strValue );
96         i := 1;
97
98         while i <= strLen do
99         begin
100
101                 case strValue[ i ] of
102                 '&':
103                         begin
104                                 strResult := strResult + '&amp;';
105                         end;
106                 '<':
107                         begin
108                                 strResult := strResult + '&lt;';
109                         end;
110                 '>':
111                         begin
112                                 strResult := strResult + '&gt;';
113                         end;
114                 '"':
115                         begin
116                                 strResult := strResult + '&quot;';
117                         end;
118                 else
119                         begin
120                                 if strValue[ i ] in kYofKanji then
121                                 begin
122                                         strResult := strResult + strValue[ i ];
123                                         Inc( i );
124                                 end;
125                                 strResult := strResult + strValue[ i ];
126                         end;
127                 end;
128
129                 i := i + 1;
130
131         end;
132
133         Result := strResult;
134
135 end;
136
137 function HtmlDecode(
138         const strValue : string
139 ) : string;
140 var
141         strResult : string;
142 begin
143
144         strResult := StringReplace( strValue, '&lt;', '<', [rfReplaceAll] );
145         strResult := StringReplace( strResult, '&gt;', '>', [rfReplaceAll] );
146         strResult := StringReplace( strResult, '&quot;', '"', [rfReplaceAll] );
147         strResult := StringReplace( strResult, '&amp;', '&', [rfReplaceAll] );
148
149         Result := strResult;
150
151 end;
152
153 function HttpEncode(
154         const strValue : string
155         ) : string;
156 var
157         i : Integer;
158         strLen : Integer;
159         strResult : string;
160         b : Integer;
161 const
162         kHexCode : array [0..15] of char = (
163                                 '0', '1', '2', '3', '4', '5', '6', '7',
164                                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' );
165 begin
166
167         strLen := Length( strValue );
168         i := 1;
169
170         while i <= strLen do
171         begin
172
173                 case strValue[ i ] of
174                 '0' .. '9', 'a' .. 'z', 'A' .. 'Z', '*', '-', '.', '@', '_':
175                         begin
176                                 strResult := strResult + strValue[ i ];
177                         end;
178                 else
179                         begin
180                                 b := Integer( strValue[ i ] );
181                                 strResult := strResult + '%'
182                                                                 + kHexCode[ b div $10 ]
183                                                                 + kHexCode[ b mod $10 ];
184                         end;
185                 end;
186
187                 i := i + 1;
188
189         end;
190
191         Result := strResult;
192
193 end;
194
195 // \82Æ\82è\82 \82¦\82¸\82Ì\91ã\97p\95i\82È\82Ì\82Å [] \82ð\8eg\82Á\82½\90³\8bK\95\\8c»\82ð\8dl\97\82µ\82Ä\82¢\82È\82¢\82±\82Æ\82É\92\8d\88Ó\81I\81I\81I
196 function MatchesMask(
197         const filename, mask : string
198         ) : boolean;
199 var
200         pName, pMask : Integer;
201         nameLen, maskLen : Integer;
202         chrUpMask : char;
203 begin
204
205         nameLen := Length( filename );
206         maskLen := Length( mask );
207         pName := 0;
208         pMask := 0;
209
210         while (pMask < maskLen) and (pName < nameLen) do
211         begin
212
213                 case mask[ pMask ] of
214                 '?':
215                         begin
216                                 // \82±\82Ì 1 \8e\9a\82Í\89½\82à\82µ\82È\82¢
217                         end;
218                 '*':
219                         begin
220                                 pMask := pMask + 1;
221                                 // mask \82ð\91\96\8d¸\82µ\90Ø\82Á\82½\82ç\8fI\97¹
222                                 if pMask >= maskLen then
223                                 begin
224                                         Result := true;
225                                         exit;
226                                 end;
227
228                                 // * \82Ì\8e\9f\82Ì\95\8e\9a\82ª\97\88\82é\82Ü\82Å\94ò\82Î\82·
229                                 chrUpMask := upcase( mask[ pMask ] );
230                                 while chrUpMask <> UpCase( filename[ pName ] ) do
231                                 begin
232                                         pName := pName + 1;
233                                         if pName >= nameLen then
234                                         begin
235                                                 Result := true;
236                                                 exit;
237                                         end;
238                                 end;
239
240                                 // * \82Ì\8e\9f\82Ì\95\8e\9a\82ª\8c©\82Â\82©\82ç\82È\82©\82Á\82½\82ç\8fI\97¹
241                                 if chrUpMask <> UpCase( filename[ pName ] ) then
242                                 begin
243                                         Result := false;
244                                         exit;
245                                 end;
246
247                                 pName := pName + 1;
248                                 pMask := pMask + 1;
249                         end;
250                 else
251                         begin
252                                 // \82±\82Ì 1 \95\8e\9a\82ª\88á\82Á\82½\82ç\8fI\97¹
253                                 if UpCase( mask[ pMask ] ) <> UpCase( filename[ pName ] ) then
254                                 begin
255                                         Result := false;
256                                         exit;
257                                 end;
258
259                         end;
260                 end;
261
262                 // \8e\9f\82Ì\95\8e\9a\82Ö
263                 pName := pName + 1;
264                 pMask := pMask + 1;
265
266         end;
267
268         if (pMask >= maskLen) and (pName >= nameLen) then
269                 Result := true
270         else
271                 Result := false;
272
273 end;
274
275
276 // \83\81\83^\83L\83\83\83\89\83N\83^\82ð\90³\8bK\95\\8c»\88µ\82¢\82É\82È\82ç\82È\82¢\82æ\82¤\82É\92u\8a·
277 function RegExpEncode(
278         const text : string
279 ) : string;
280 var
281         strResult : string;
282 begin
283
284         strResult := StringReplace( text, '\', '\\', [rfReplaceAll] );
285         strResult := StringReplace( strResult, '[', '\[', [rfReplaceAll] );
286         strResult := StringReplace( strResult, ']', '\]', [rfReplaceAll] );
287         strResult := StringReplace( strResult, '(', '\(', [rfReplaceAll] );
288         strResult := StringReplace( strResult, ')', '\)', [rfReplaceAll] );
289         strResult := StringReplace( strResult, '[', '\[', [rfReplaceAll] );
290         strResult := StringReplace( strResult, ']', '\]', [rfReplaceAll] );
291         strResult := StringReplace( strResult, '*', '\*', [rfReplaceAll] );
292         strResult := StringReplace( strResult, '?', '\?', [rfReplaceAll] );
293         strResult := StringReplace( strResult, '.', '\.', [rfReplaceAll] );
294         strResult := StringReplace( strResult, '+', '\+', [rfReplaceAll] );
295         strResult := StringReplace( strResult, '|', '\|', [rfReplaceAll] );
296         strResult := StringReplace( strResult, '^', '\^', [rfReplaceAll] );
297         strResult := StringReplace( strResult, '$', '\$', [rfReplaceAll] );
298
299         Result := strResult;
300
301 end;
302
303 end.