OSDN Git Service

1.52.1.658
[gikonavigoeson/gikonavi.git] / MojuUtils.pas
index 5387f52..fe4aa58 100644 (file)
@@ -14,25 +14,28 @@ unit MojuUtils;
 interface
 
 uses
-       Windows, Classes, SysUtils, GikoSystem;
+       Windows, Classes, SysUtils;
 
        function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
        function AnsiStrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
-       function ReplaceString(const S, OldPattern, NewPattern: string): string;
-       function IgnoCaseReplaceString(const S, OldPattern, NewPattern: string): string;
+       function ReplaceString(const S: String; const OldPattern: String; const NewPattern: string): String;
+       function IgnoCaseReplaceString(const S: String; const OldPattern:String; const NewPattern: string): String;
 
-       function CustomStringReplace(S , OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean = False): String; overload;
-       procedure CustomStringReplace(var S : TStringList; OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean = False); overload;
+       function CustomStringReplace(const S: String; const OldPattern: String; const  NewPattern: string; IgnoreCase : Boolean = False): String; overload;
+       procedure CustomStringReplace(var S : TStringList;const OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean = False); overload;
 
        function ZenToHan(const s: string): string;
-       function VaguePos(const Substr, S: string): Integer;
+       function VaguePos(const Substr: String; const S: string): Integer;
 
        function ReplaseNoValidateChar( inVal : String): String;
        function IsNoValidID( inID :String): Boolean;
        //<font>\83^\83O\82ð\91S\82Ä\8dí\8f\9c\82·\82é
-       function        DeleteFontTag( inSource : string) : string;
-       function DivideStrLine(Line: string): TResRec;
+       function DeleteFontTag( inSource : string) : string;
        function RemoveToken(var s: string;const delimiter: string): string;
+       // \96³\8aQ\89»(& -> &amp; " -> &auot; \82É\95Ï\8a·\82·\82é)
+       function Sanitize(const s: String): String;
+       // \96³\8aQ\89»\89ð\8f\9c(&amp; -> & &auot; -> " \82É\95Ï\8a·\82·\82é)
+       function UnSanitize(const s: String): String;
 
 implementation
 // \83|\83C\83\93\83^\81[\81\95\83A\83Z\83\93\83u\83\89\82É\82æ\82é\8d\82\91¬\83|\83X
@@ -100,10 +103,10 @@ begin
     Result := StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd);
 
     while (Result <> nil) and (StrEnd - Result >= L2) do begin
-       ByteType := StrByteType(StrStart, Integer(Result-StrStart));
-       if (ByteType <> mbTrailByte) and
-               (CompareString(LOCALE_USER_DEFAULT, SORT_STRINGSORT, Result, L2, SubstrStart, L2) = 2)
-       then Exit;
+               ByteType := StrByteType(StrStart, Integer(Result-StrStart));
+               if (ByteType <> mbTrailByte) and
+                       (CompareString(LOCALE_USER_DEFAULT, SORT_STRINGSORT, Result, L2, SubstrStart, L2) = 2)
+               then Exit;
        if (ByteType = mbLeadByte) then Inc(Result);
        Inc(Result);
        Result := StrPosEx(Result, StrEnd, SubStrStart, SubStrEnd);
@@ -111,48 +114,49 @@ begin
     Result := nil;
 end;
 
-{$R-}
 //\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
-function ReplaceString(const S, OldPattern, NewPattern: string): string;
+function ReplaceString(const S: String; const OldPattern: String; const NewPattern: string): String;
 var
-    ReplaceCount: Integer;
-    DestIndex: Integer;
-    i, l: Integer;
-    p, e, ps, pe: PChar;
-    Count: Integer;
+       ReplaceCount: Integer;
+       DestIndex: Integer;
+       i, l: Integer;
+       p, e, ps, pe: PChar;
+       Count: Integer;
+       olen: Integer;
 begin
-    Result := S;
-    if OldPattern = '' then Exit;
+       Result := S;
+       olen := Length(OldPattern);
+       if olen = 0 then Exit;
        p := PChar(S);
        e := p + Length(S);
        ps := PChar(OldPattern);
-       pe := ps + Length(OldPattern);
+       pe := ps + olen;
        ReplaceCount := 0;
        while p < e do begin
                p := AnsiStrPosEx(p, e, ps, pe);
                if p = nil then Break;
                Inc(ReplaceCount);
-               Inc(p, Length(OldPattern));
+               Inc(p, olen);
        end;
        if ReplaceCount = 0 then Exit;
        SetString(Result, nil, Length(S) +
-    (Length(NewPattern) - Length(OldPattern)) * ReplaceCount);
-    p := PChar(S);
-    DestIndex := 1;
-    l := Length( NewPattern );
-    for i := 0 to ReplaceCount - 1 do begin
-        Count := AnsiStrPosEx(p, e, ps, pe) - p;
-        Move(p^, Result[DestIndex], Count);
-        Inc(p, Count);//p := pp;
-        Inc(DestIndex, Count);
-        Move(NewPattern[1], Result[DestIndex], l);
-        Inc(p, Length(OldPattern));
-        Inc(DestIndex, l);
-    end;
-    Move(p^, Result[DestIndex], e - p);
+       (Length(NewPattern) - olen) * ReplaceCount);
+       p := PChar(S);
+       DestIndex := 1;
+       l := Length( NewPattern );
+       for i := 0 to ReplaceCount - 1 do begin
+               Count := AnsiStrPosEx(p, e, ps, pe) - p;
+               Move(p^, Result[DestIndex], Count);
+               Inc(p, Count);//p := pp;
+               Inc(DestIndex, Count);
+               Move(NewPattern[1], Result[DestIndex], l);
+               Inc(p, olen);
+               Inc(DestIndex, l);
+       end;
+       Move(p^, Result[DestIndex], e - p);
 end;
 //\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
-function IgnoCaseReplaceString(const S, OldPattern, NewPattern: string): string;
+function IgnoCaseReplaceString(const S: String;const OldPattern:String;const NewPattern: string): String;
 var
        ReplaceCount: Integer;
        DestIndex: Integer;
@@ -203,18 +207,14 @@ begin
        end;
        Move(p^, Result[DestIndex], e - p);
 end;
-{$IFDEF DEBUG}
-{$R+}
-{$ENDIF}
-
 //\8d\82\91¬\95\8e\9a\97ñ\92u\8a·\8aÖ\90\94\81i\94Ä\97p\94Å\82P\81j
 function CustomStringReplace(
-       S , OldPattern: String;
+       const S :String;
+       const OldPattern: String;
        const  NewPattern: string;
        IgnoreCase : Boolean
 ): String;
 begin
-       Result := '';
        if not IgnoreCase then begin
                Result := ReplaceString(S,OldPattern,NewPattern);
        end else begin
@@ -225,7 +225,7 @@ end;
 //\8d\82\91¬\95\8e\9a\97ñ\92u\8a·\8aÖ\90\94\81i\94Ä\97p\94Å\82Q\81j
 procedure CustomStringReplace(
        var S : TStringList;
-       OldPattern: String;
+       const OldPattern: String;
        const  NewPattern: string;
        IgnoreCase : Boolean
 );
@@ -251,27 +251,25 @@ end;
  *************************************************************************)
 function ZenToHan(const s: string): string;
 var
-       //Chr: array [0..1024] of char;
-       Chr: string;
        ChrLen  : Integer;
 begin
-       SetLength(Chr, Length(s));
+       SetLength(Result, Length(s));
        ChrLen := Windows.LCMapString(
                 GetUserDefaultLCID(),
 //              LCMAP_HALFWIDTH,
                 LCMAP_HALFWIDTH or LCMAP_KATAKANA or LCMAP_LOWERCASE,
                 PChar(s),
                 Length(s),
-                PChar(Chr),
-                Length(Chr)
+                PChar(Result),
+                Length(Result)
                 );
-       Result := Copy(Chr, 1, ChrLen);
+       SetLength(Result, ChrLen);
 end;
 
 (*************************************************************************
  * \91S\8ap\94¼\8ap\82Ð\82ç\82ª\82È\82©\82½\82©\82È\82ð\8bæ\95Ê\82µ\82È\82¢\90¦\82¢Pos
  *************************************************************************)
-function VaguePos(const Substr, S: string): Integer;
+function VaguePos(const Substr:String; const S: string): Integer;
 begin
        Result := AnsiPos(ZenToHan(Substr), ZenToHan(S));
 end;
@@ -337,37 +335,6 @@ begin
 end;
 // *************************************************************************
 
-(*************************************************************************
- * dat\83t\83@\83C\83\8b\82Ì\88ê\83\89\83C\83\93\82ð\95ª\89ð
- *************************************************************************)
-function DivideStrLine(Line: string): TResRec;
-var
-       Delim: string;
-begin
-       if AnsiPos('<>', Line) = 0 then begin
-               //Delim := ',';
-               //Result.FType := glt2chOld;
-               Line := CustomStringReplace(Line, '<>', '&lt;&gt;');
-               Line := CustomStringReplace(Line, ',', '<>');
-               Line := CustomStringReplace(Line, '\81\97\81M', ',');
-       end;
-       Delim := '<>';
-       Result.FType := glt2chNew;
-       //Trim\82µ\82Ä\82Í\82¢\82¯\82È\82¢\8bC\82ª\82·\82é\81@by\82à\82\82ã
-       Result.FName := RemoveToken(Line, Delim);
-       Result.FMailTo := RemoveToken(Line, Delim);
-       Result.FDateTime := RemoveToken(Line, Delim);
-       Result.FBody := RemoveToken(Line, Delim);
-       //\82Q\82¿\82á\82ñ\82Ë\82é\82Æ\82©\82¾\82Æ\81A\96{\95\82Ì\90æ\93ª\82É\82P\82Â\94¼\8ap\8bó\94\92\82ª\93ü\82Á\82Ä\82¢\82é\82Ì\82Å\8dí\8f\9c\82·\82é
-       //\91¼\82Ì\8cf\8e¦\94Â\82Å\81A\83\8c\83X\8e©\91Ì\82Ì\8bó\94\92\82©\82à\82µ\82ê\82È\82¢\82¯\82Ç\82»\82ê\82Í\92ú\82ß\82é
-       Result.FBody := TrimLeft(Result.FBody);
-       //\8bó\82¾\82Æ\96â\91è\82ª\8bN\82«\82é\82©\82ç\81A\8bó\94\92\82ð\90Ý\92è\82·\82é
-       if Result.FBody = '' then
-               Result.FBody := '&nbsp;';
-
-       Result.FTitle := RemoveToken(Line, Delim);
-
-end;
 
 (*************************************************************************
  *
@@ -376,14 +343,41 @@ end;
 function RemoveToken(var s: string;const delimiter: string): string;
 var
        p: Integer;
+       pos : PChar;
+       pds, pde : PChar;
+       pss, pse : PChar;
 begin
-       p := AnsiPos(delimiter, s);
-       if p = 0 then
-               Result := s
-       else
-               Result := Copy(s, 1, p - 1);
-       Delete(s, 1, Length(Result) + Length(delimiter));
+       pss := PChar(s);
+       pse := pss + Length(s);
+       pds := PChar(delimiter);
+       pde := pds + Length(delimiter);
+
+       pos := StrPosEx(pss, pse, pds, pde);
+       if pos <> nil then begin
+               p := pos - pss;
+               SetString(Result, pss, p);
+               Delete(s, 1, p + Length(delimiter));
+
+               if (StrByteType(PChar(Result), Length(Result)-1) = mbLeadByte) then begin
+                       SetLength(Result, Length(Result) - 1);
+               end;
+       end else begin
+               Result := s;
+               s := '';
+       end;
 end;
 
+//! \96³\8aQ\89»(& -> &amp; " -> &auot; \82É\95Ï\8a·\82·\82é)
+function Sanitize(const s: String): String;
+begin
+       Result := CustomStringReplace(s, '&', '&amp;');
+       Result := CustomStringReplace(Result, '"', '&quot;');
+end;
+//! \96³\8aQ\89»\89ð\8f\9c(&amp; -> & &auot; -> " \82É\95Ï\8a·\82·\82é)
+function UnSanitize(const s: String): String;
+begin
+       Result := CustomStringReplace(s, '&quot;', '"');
+       Result := CustomStringReplace(Result, '&amp;', '&');
+end;
 
 end.