X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=MojuUtils.pas;h=1bf4a86df343a245a3f653a56223d06d87cdaea4;hb=99f2be3a17dfec20c0edcaaa09e20c39a74d3af9;hp=2c949c69dc8e622d424f796e43f25a83fc154a60;hpb=515bd2b6618d2d1d6bfbdfc4974290cc64e79b24;p=gikonavigoeson%2Fgikonavi.git diff --git a/MojuUtils.pas b/MojuUtils.pas index 2c949c6..1bf4a86 100644 --- a/MojuUtils.pas +++ b/MojuUtils.pas @@ -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; //ƒ^ƒO‚ð‘S‚č폜‚·‚é - function DeleteFontTag( inSource : string) : string; - function DivideStrLine(Line: string): TResRec; + function DeleteFontTag( inSource : string) : string; function RemoveToken(var s: string;const delimiter: string): string; + // –³ŠQ‰»(& -> & " -> &auot; ‚É•ÏŠ·‚·‚é) + function Sanitize(const s: String): String; + // –³ŠQ‰»‰ðœ(& -> & &auot; -> " ‚É•ÏŠ·‚·‚é) + function UnSanitize(const s: String): String; implementation // ƒ|ƒCƒ“ƒ^[•ƒAƒZƒ“ƒuƒ‰‚É‚æ‚鍂‘¬ƒ|ƒX @@ -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-} //‚‘¬•¶Žš—ñ’uŠ·ŠÖ”i‘啶Žš¬•¶Žš‚̈Ⴂ‚𖳎‹‚µ‚È‚¢j -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; //‚‘¬•¶Žš—ñ’uŠ·ŠÖ”i‘啶Žš¬•¶Žš‚̈Ⴂ‚𖳎‹‚·‚éj -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} - //‚‘¬•¶Žš—ñ’uŠ·ŠÖ”i”Ä—p”Å‚Pj 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; //‚‘¬•¶Žš—ñ’uŠ·ŠÖ”i”Ä—p”Å‚Qj 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; (************************************************************************* * ‘SŠp”¼Šp‚Ђ炪‚È‚©‚½‚©‚È‚ð‹æ•Ê‚µ‚È‚¢¦‚¢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; @@ -289,19 +287,29 @@ begin Result := CustomStringReplace(Result, '>', '„'); Result := CustomStringReplace(Result, '<', 'ƒ'); Result := CustomStringReplace(Result, '|', 'b'); + Result := CustomStringReplace(Result, #9, ''); end; (************************************************************************* - * –³Œø‚ÈID‚©‚̃`ƒFƒbƒNi–³Œø—áFID:??? , ID:???0) + * –³Œø‚ÈID‚©‚̃`ƒFƒbƒNi–³Œø—áFID:??? , ID:???X) *************************************************************************) function IsNoValidID( inID :String): Boolean; +var + bTail : Boolean; begin + Result := True; inID := Trim(inID); - if inID = '' then Result := True - else begin + if (Length(inID) > 0) then begin inID := Copy(inID, AnsiPos(':', inID) + 1, Length(inID) ); + bTail := False; + // ––”ö‚ª?ˆÈŠO‚© + if Length(inID) > 0 then begin + bTail := (inID[Length(inID)] <> '?'); + end; inID := CustomStringReplace(inID, '?', ''); - if (inID = '') or (inID = '0') then Result := True - else Result := False; + if (Length(inID) > 0) and (not + ((Length(inID) = 1) and (bTail))) then begin + Result := False; + end; end; end; @@ -337,47 +345,6 @@ begin end; // ************************************************************************* -(************************************************************************* - * datƒtƒ@ƒCƒ‹‚̈ꃉƒCƒ“‚𕪉ð - *************************************************************************) -function DivideStrLine(Line: string): TResRec; -const - delimiter = '<>'; -var -// Delim: string; - pds, pde : PChar; - pss, pse : PChar; -begin - pss := PChar(Line); - pse := pss + Length(Line); - pds := PChar(delimiter); - pde := pds + Length(delimiter); - - if AnsiStrPosEx(pss, pse, pds, pde) = nil then begin - //if AnsiPos('<>', Line) = 0 then begin - //Delim := ','; - //Result.FType := glt2chOld; - Line := CustomStringReplace(Line, '<>', '<>'); - Line := CustomStringReplace(Line, ',', '<>'); - Line := CustomStringReplace(Line, '—M', ','); - end; - //Delim := '<>'; - Result.FType := glt2chNew; - //Trim‚µ‚Ä‚Í‚¢‚¯‚È‚¢‹C‚ª‚·‚é@by‚à‚¶‚ã - Result.FName := RemoveToken(Line, delimiter); - Result.FMailTo := RemoveToken(Line, delimiter); - Result.FDateTime := RemoveToken(Line, delimiter); - Result.FBody := RemoveToken(Line, delimiter); - //‚Q‚¿‚á‚ñ‚Ë‚é‚Æ‚©‚¾‚ƁA–{•¶‚̐擪‚É‚P‚”¼Šp‹ó”’‚ª“ü‚Á‚Ä‚¢‚é‚̂ō폜‚·‚é - //‘¼‚ÌŒfŽ¦”‚ŁAƒŒƒXŽ©‘Ì‚Ì‹ó”’‚©‚à‚µ‚ê‚È‚¢‚¯‚Ç‚»‚ê‚Í’ú‚ß‚é - Result.FBody := TrimLeft(Result.FBody); - //‹ó‚¾‚Æ–â‘肪‹N‚«‚é‚©‚çA‹ó”’‚ðÝ’è‚·‚é - if Result.FBody = '' then - Result.FBody := ' '; - - Result.FTitle := RemoveToken(Line, delimiter); - -end; (************************************************************************* * @@ -395,16 +362,35 @@ begin pds := PChar(delimiter); pde := pds + Length(delimiter); - pos := AnsiStrPosEx(pss, pse, pds, pde); + 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 (Length(Result) > 0) then begin + if (StrByteType(PChar(Result), Length(Result)-1) = mbLeadByte) then begin + SetLength(Result, Length(Result) - 1); + end; + end; end else begin Result := s; s := ''; end; end; +//! –³ŠQ‰»(& -> & " -> " ‚É•ÏŠ·‚·‚é) +function Sanitize(const s: String): String; +begin + // —]•ª‚ɃTƒjƒ^ƒCƒY‚³‚ê‚È‚¢‚悤‚É‚¢‚Á‚½‚ñŒ³‚É–ß‚· + Result := UnSanitize(s); + Result := CustomStringReplace(Result, '&', '&'); + Result := CustomStringReplace(Result, '"', '"'); +end; +//! –³ŠQ‰»‰ðœ(& -> & " -> " ‚É•ÏŠ·‚·‚é) +function UnSanitize(const s: String): String; +begin + Result := CustomStringReplace(s, '"', '"'); + Result := CustomStringReplace(Result, '&', '&'); +end; end.