Develop and Download Open Source Software

Browse CVS Repository

Diff of /gikonavigoeson/gikonavi/MojuUtils.pas

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4.2.2 by yoffy, Thu Sep 9 16:20:33 2004 UTC revision 1.25 by h677, Sat Mar 29 01:38:48 2008 UTC
# Line 18  uses Line 18  uses
18    
19          function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;          function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
20          function AnsiStrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;          function AnsiStrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
21          function ReplaceString(const S, OldPattern, NewPattern: string): string;          function ReplaceString(const S: String; const OldPattern: String; const NewPattern: string): String;
22          function IgnoCaseReplaceString(const S, OldPattern, NewPattern: string): string;          function IgnoCaseReplaceString(const S: String; const OldPattern:String; const NewPattern: string): String;
23    
24          function CustomStringReplace(S , OldPattern: String;const  NewPattern: string): String; overload;          function CustomStringReplace(const S: String; const OldPattern: String; const  NewPattern: string; IgnoreCase : Boolean = False): String; overload;
25          function CustomStringReplace(S , OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean): String; overload;          procedure CustomStringReplace(var S : TStringList;const OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean = False); overload;
         procedure CustomStringReplace(var S : TStringList; OldPattern: String;const  NewPattern: string);overload;  
         procedure CustomStringReplace(var S : TStringList; OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean);overload;  
26    
27          function ZenToHan(const s: string): string;          function ZenToHan(const s: string): string;
28          function VaguePos(const Substr, S: string): Integer;          function VaguePos(const Substr: String; const S: string): Integer;
29    
30            function ReplaseNoValidateChar( inVal : String): String;
31            function IsNoValidID( inID :String): Boolean;
32            //<font>タグを全て削除する
33            function DeleteFontTag( inSource : string) : string;
34            function RemoveToken(var s: string;const delimiter: string): string;
35            // 無害化(& -> &amp; " -> &auot; に変換する)
36            function Sanitize(const s: String): String;
37            // 無害化解除(&amp; -> & &auot; -> " に変換する)
38            function UnSanitize(const s: String): String;
39    
40  implementation  implementation
41  // ポインター&アセンブラによる高速ポス  // ポインター&アセンブラによる高速ポス
42  function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;  function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
43  asm  asm
44          PUSH    EBX                  PUSH    EBX
45                  PUSH    ESI                  PUSH    ESI
46          PUSH    EDI                  PUSH    EDI
47    
48          MOV    ESI,ECX        { Point ESI to substr                  }                  MOV    ESI,ECX        { Point ESI to substr                  }
49          MOV    EDI,EAX        { Point EDI to s                        }                  MOV    EDI,EAX        { Point EDI to s                        }
50    
51          MOV    ECX,EDX        { ECX = search length                  }                  MOV    ECX,EDX        { ECX = search length                  }
52          SUB    ECX,EAX          SUB    ECX,EAX
53    
54          MOV    EDX,SubstrEnd          MOV    EDX,SubstrEnd
# Line 95  begin Line 103  begin
103      Result := StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd);      Result := StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd);
104    
105      while (Result <> nil) and (StrEnd - Result >= L2) do begin      while (Result <> nil) and (StrEnd - Result >= L2) do begin
106          ByteType := StrByteType(StrStart, Integer(Result-StrStart));                  ByteType := StrByteType(StrStart, Integer(Result-StrStart));
107          if (ByteType <> mbTrailByte) and                  if (ByteType <> mbTrailByte) and
108                  (CompareString(LOCALE_USER_DEFAULT, SORT_STRINGSORT, Result, L2, SubstrStart, L2) = 2)                          (CompareString(LOCALE_USER_DEFAULT, SORT_STRINGSORT, Result, L2, SubstrStart, L2) = 2)
109          then Exit;                  then Exit;
110          if (ByteType = mbLeadByte) then Inc(Result);          if (ByteType = mbLeadByte) then Inc(Result);
111          Inc(Result);          Inc(Result);
112          Result := StrPosEx(Result, StrEnd, SubStrStart, SubStrEnd);          Result := StrPosEx(Result, StrEnd, SubStrStart, SubStrEnd);
# Line 106  begin Line 114  begin
114      Result := nil;      Result := nil;
115  end;  end;
116    
 {$R-}  
117  //高速文字列置換関数(大文字小文字の違いを無視しない)  //高速文字列置換関数(大文字小文字の違いを無視しない)
118  function ReplaceString(const S, OldPattern, NewPattern: string): string;  function ReplaceString(const S: String; const OldPattern: String; const NewPattern: string): String;
119  var  var
120      ReplaceCount: Integer;          ReplaceCount: Integer;
121      DestIndex: Integer;          DestIndex: Integer;
122      i, l: Integer;          i, l: Integer;
123      p, e, ps, pe: PChar;          p, e, ps, pe: PChar;
124      Count: Integer;          Count: Integer;
125  begin          olen: Integer;
126      Result := S;  begin
127      if OldPattern = '' then Exit;          Result := S;
128      p := PChar(S);          olen := Length(OldPattern);
129      e := p + Length(S);          if olen = 0 then Exit;
130      ps := PChar(OldPattern);          p := PChar(S);
131      pe := ps + Length(OldPattern);          e := p + Length(S);
132      ReplaceCount := 0;          ps := PChar(OldPattern);
133      while p < e do begin          pe := ps + olen;
134          p := AnsiStrPosEx(p, e, ps, pe);          ReplaceCount := 0;
135          if p = nil then Break;          while p < e do begin
136          Inc(ReplaceCount);                  p := AnsiStrPosEx(p, e, ps, pe);
137          Inc(p, Length(OldPattern));                  if p = nil then Break;
138      end;                  Inc(ReplaceCount);
139      if ReplaceCount = 0 then Exit;                  Inc(p, olen);
140      SetString(Result, nil, Length(S) +          end;
141      (Length(NewPattern) - Length(OldPattern)) * ReplaceCount);          if ReplaceCount = 0 then Exit;
142      p := PChar(S);          SetString(Result, nil, Length(S) +
143      DestIndex := 1;          (Length(NewPattern) - olen) * ReplaceCount);
144      l := Length( NewPattern );          p := PChar(S);
145      for i := 0 to ReplaceCount - 1 do begin          DestIndex := 1;
146          Count := AnsiStrPosEx(p, e, ps, pe) - p;          l := Length( NewPattern );
147          Move(p^, Result[DestIndex], Count);          for i := 0 to ReplaceCount - 1 do begin
148          Inc(p, Count);//p := pp;                  Count := AnsiStrPosEx(p, e, ps, pe) - p;
149          Inc(DestIndex, Count);                  Move(p^, Result[DestIndex], Count);
150          Move(NewPattern[1], Result[DestIndex], l);                  Inc(p, Count);//p := pp;
151          Inc(p, Length(OldPattern));                  Inc(DestIndex, Count);
152          Inc(DestIndex, l);                  Move(NewPattern[1], Result[DestIndex], l);
153      end;                  Inc(p, olen);
154      Move(p^, Result[DestIndex], e - p);                  Inc(DestIndex, l);
155            end;
156            Move(p^, Result[DestIndex], e - p);
157  end;  end;
158  //高速文字列置換関数(大文字小文字の違いを無視する)  //高速文字列置換関数(大文字小文字の違いを無視する)
159  function IgnoCaseReplaceString(const S, OldPattern, NewPattern: string): string;  function IgnoCaseReplaceString(const S: String;const OldPattern:String;const NewPattern: string): String;
160  var  var
161          ReplaceCount: Integer;          ReplaceCount: Integer;
162          DestIndex: Integer;          DestIndex: Integer;
# Line 198  begin Line 207  begin
207          end;          end;
208          Move(p^, Result[DestIndex], e - p);          Move(p^, Result[DestIndex], e - p);
209  end;  end;
 {$IFDEF DEBUG}  
 {$R+}  
 {$ENDIF}  
   
 //高速文字列置換関数(プリミティブ)  
 function CustomStringReplace(  
         S ,OldPattern: String;  
         const NewPattern: string  
 ): String;  
   
 begin  
         Result := ReplaceString(S,OldPattern,NewPattern);  
 end;  
   
210  //高速文字列置換関数(汎用版1)  //高速文字列置換関数(汎用版1)
211  function CustomStringReplace(  function CustomStringReplace(
212          S , OldPattern: String;          const S :String;
213            const OldPattern: String;
214          const  NewPattern: string;          const  NewPattern: string;
215          IgnoreCase : Boolean          IgnoreCase : Boolean
216  ): String;  ): String;
217  begin  begin
         Result := '';  
218          if not IgnoreCase then begin          if not IgnoreCase then begin
219                  Result := ReplaceString(S,OldPattern,NewPattern);                  Result := ReplaceString(S,OldPattern,NewPattern);
220          end else begin          end else begin
# Line 230  end; Line 225  end;
225  //高速文字列置換関数(汎用版2)  //高速文字列置換関数(汎用版2)
226  procedure CustomStringReplace(  procedure CustomStringReplace(
227          var S : TStringList;          var S : TStringList;
228          OldPattern: String;          const OldPattern: String;
229          const  NewPattern: string;          const  NewPattern: string;
230          IgnoreCase : Boolean          IgnoreCase : Boolean
231  );  );
# Line 250  begin Line 245  begin
245          S.EndUpdate;          S.EndUpdate;
246  end;  end;
247    
 //高速文字列置換関数(汎用版3)  
 procedure CustomStringReplace(  
         var S : TStringList;  
         OldPattern: String;  
         const  NewPattern: string  
 );  
 var  
         i : Integer;  
 begin  
         S.BeginUpdate;  
         for i := 0 to S.Count - 1 do begin  
                 S.Strings[i] := ReplaceString(S.Strings[i], OldPattern,NewPattern);;  
         end;  
         S.EndUpdate;  
 end;  
   
248  (*************************************************************************  (*************************************************************************
249   * 全角→半角   * 全角→半角
250   * from HotZonu   * from HotZonu
251   *************************************************************************)   *************************************************************************)
252  function ZenToHan(const s: string): string;  function ZenToHan(const s: string): string;
253  var  var
         //Chr: array [0..1024] of char;  
         Chr: string;  
254          ChrLen  : Integer;          ChrLen  : Integer;
255  begin  begin
256          SetLength(Chr, Length(s));          SetLength(Result, Length(s));
257          ChrLen := Windows.LCMapString(          ChrLen := Windows.LCMapString(
258                   GetUserDefaultLCID(),                   GetUserDefaultLCID(),
259  //               LCMAP_HALFWIDTH,  //               LCMAP_HALFWIDTH,
260                   LCMAP_HALFWIDTH or LCMAP_KATAKANA or LCMAP_LOWERCASE,                   LCMAP_HALFWIDTH or LCMAP_KATAKANA or LCMAP_LOWERCASE,
261                   PChar(s),                   PChar(s),
262                   Length(s),                   Length(s),
263                   PChar(Chr),                   PChar(Result),
264                   Length(Chr)                   Length(Result)
265                   );                   );
266          Result := Copy(Chr, 1, ChrLen);          SetLength(Result, ChrLen);
267  end;  end;
268    
269  (*************************************************************************  (*************************************************************************
270   * 全角半角ひらがなかたかなを区別しない凄いPos   * 全角半角ひらがなかたかなを区別しない凄いPos
271   *************************************************************************)   *************************************************************************)
272  function VaguePos(const Substr, S: string): Integer;  function VaguePos(const Substr:String; const S: string): Integer;
273  begin  begin
274          Result := AnsiPos(ZenToHan(Substr), ZenToHan(S));          Result := AnsiPos(ZenToHan(Substr), ZenToHan(S));
275  end;  end;
276    (*************************************************************************
277     * FAT/NTFSのファイル名に許されない文字(\,/,:,.,;,*,>,<,|)を全角に置換する
278     *************************************************************************)
279    function ReplaseNoValidateChar( inVal : String): String;
280    begin
281            Result := CustomStringReplace(inVal, '\', '¥');
282            Result := CustomStringReplace(Result, '/', '/');
283            Result := CustomStringReplace(Result, ':', ':');
284            Result := CustomStringReplace(Result, '.', '.');
285        Result := CustomStringReplace(Result, ';', ';');
286            Result := CustomStringReplace(Result, '*', '*');
287            Result := CustomStringReplace(Result, '>', '>');
288            Result := CustomStringReplace(Result, '<', '<');
289            Result := CustomStringReplace(Result, '|', '|');
290    end;
291    (*************************************************************************
292     * 無効なIDかのチェック(無効例:ID:??? , ID:???X)
293     *************************************************************************)
294    function IsNoValidID( inID :String): Boolean;
295    var
296        bTail : Boolean;
297    begin
298        Result := True;
299            inID := Trim(inID);
300            if (Length(inID) > 0) then begin
301                    inID := Copy(inID, AnsiPos(':', inID) + 1, Length(inID) );
302            bTail := False;
303            // 末尾が?以外か
304            if Length(inID) > 0 then begin
305                bTail := (inID[Length(inID)] <> '?');
306            end;
307                    inID := CustomStringReplace(inID, '?', '');
308                    if (Length(inID) > 0) and (not
309                ((Length(inID) = 1) and (bTail))) then begin
310                        Result := False;
311            end;
312            end;
313    end;
314    
315    // *************************************************************************
316    // HTML中の<font>タグを削除する
317    // *************************************************************************
318    function        DeleteFontTag(
319             inSource : string    //タグを削除する文字列
320    ) : string;             //タグ削除語の文字列
321    var
322            pos : Integer;
323    begin
324            Result := '';
325    
326            //</font>を削除
327            inSource := CustomStringReplace( inSource, '</font>', '', True);
328            //<font を全て小文字に変換する
329            inSource := CustomStringReplace( inSource, '<font', '<font', True);
330            //<font 〜 を削除する
331            pos := AnsiPos('<font', inSource);
332            while (pos > 0) do begin
333                    Result := Result + Copy(inSource, 1, pos - 1);
334                    Delete(inSource, 1, pos);
335                    //タグを閉じる'>'までを削除
336            pos := AnsiPos('>', inSource);
337                    Delete(inSource, 1, pos);
338                    pos := AnsiPos('<font', inSource);
339            end;
340    
341            Result := Result + inSource;
342    
343    
344    end;
345    // *************************************************************************
346    
347    
348    (*************************************************************************
349     *
350     *どこかのサイトからのパクリ
351     *************************************************************************)
352    function RemoveToken(var s: string;const delimiter: string): string;
353    var
354            p: Integer;
355            pos : PChar;
356            pds, pde : PChar;
357            pss, pse : PChar;
358    begin
359            pss := PChar(s);
360            pse := pss + Length(s);
361            pds := PChar(delimiter);
362            pde := pds + Length(delimiter);
363    
364            pos := StrPosEx(pss, pse, pds, pde);
365            if pos <> nil then begin
366                    p := pos - pss;
367                    SetString(Result, pss, p);
368                    Delete(s, 1, p + Length(delimiter));
369            if (Length(Result) > 0) then begin
370                    if (StrByteType(PChar(Result), Length(Result)-1) = mbLeadByte) then begin
371                            SetLength(Result, Length(Result) - 1);
372                        end;
373            end;
374            end else begin
375                    Result := s;
376                    s := '';
377            end;
378    end;
379    
380    //! 無害化(& -> &amp; " -> &quot; に変換する)
381    function Sanitize(const s: String): String;
382    begin
383        // 余分にサニタイズされないようにいったん元に戻す
384        Result := UnSanitize(s);
385            Result := CustomStringReplace(Result, '&', '&amp;');
386            Result := CustomStringReplace(Result, '"', '&quot;');
387    end;
388    //! 無害化解除(&amp; -> & &quot; -> " に変換する)
389    function UnSanitize(const s: String): String;
390    begin
391            Result := CustomStringReplace(s, '&quot;', '"');
392            Result := CustomStringReplace(Result, '&amp;', '&');
393    end;
394    
395  end.  end.

Legend:
Removed from v.1.4.2.2  
changed lines
  Added in v.1.25

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26