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.1 by h677, Tue Nov 25 17:50:53 2003 UTC revision 1.2 by h677, Tue Nov 25 18:22:43 2003 UTC
# Line 6  unit MojuUtils; Line 6  unit MojuUtils;
6  interface  interface
7    
8  uses  uses
9          Classes, SysUtils;          Windows, Classes, SysUtils;
10    
11        function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
12        function AnsiStrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
13        function ReplaceString(const S, OldPattern, NewPattern: string): string;
14    
15      function CustomStringReplace(S , OldPattern: String;const  NewPattern: string): String; overload;      function CustomStringReplace(S , OldPattern: String;const  NewPattern: string): String; overload;
16      function CustomStringReplace(S , OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean): String; overload;      function CustomStringReplace(S , OldPattern: String;const  NewPattern: string; IgnoreCase : Boolean): String; overload;
# Line 20  function CustomStringReplace( Line 24  function CustomStringReplace(
24          S ,OldPattern: String;          S ,OldPattern: String;
25      const NewPattern: string      const NewPattern: string
26  ): String;  ): String;
27    {
28  var  var
29          position : Integer;          position : Integer;
30      lenOld : Integer;//OldPatternの長さ      lenOld : Integer;//OldPatternの長さ
31    }
32  begin  begin
33        Result := ReplaceString(S,OldPattern,NewPattern);
34    {
35      position := 0;      position := 0;
36      lenOld := Length(OldPattern);      lenOld := Length(OldPattern);
37      Result := '';      Result := '';
# Line 37  begin Line 44  begin
44      if Length( S ) > 0 then begin      if Length( S ) > 0 then begin
45          Result := Result + S;          Result := Result + S;
46      end;      end;
47    }
48    
49  end;  end;
50  function CustomStringReplace(  function CustomStringReplace(
# Line 54  begin Line 61  begin
61      lenOld := Length(OldPattern);      lenOld := Length(OldPattern);
62      Result := '';      Result := '';
63      if not IgnoreCase then begin      if not IgnoreCase then begin
64          Result := CustomStringReplace( S, OldPattern, NewPattern );          Result := ReplaceString(S,OldPattern,NewPattern);
65      end else begin      end else begin
66          buffer := AnsiLowerCase(S);          buffer := AnsiLowerCase(S);
67          OldPattern := AnsiLowerCase(OldPattern);          OldPattern := AnsiLowerCase(OldPattern);
# Line 81  var Line 88  var
88  begin  begin
89      if not IgnoreCase then begin      if not IgnoreCase then begin
90          for i := 0 to S.Count - 1 do begin          for i := 0 to S.Count - 1 do begin
91                  S.Strings[i] := CustomStringReplace( S.Strings[i], OldPattern, NewPattern );                  S.Strings[i] := ReplaceString(S.Strings[i],OldPattern,NewPattern);
92          end;          end;
93      end else begin      end else begin
94          for i := 0 to S.Count - 1 do begin          for i := 0 to S.Count - 1 do begin
# Line 98  var Line 105  var
105      i : Integer;      i : Integer;
106  begin  begin
107          for i := 0 to S.Count - 1 do begin          for i := 0 to S.Count - 1 do begin
108          S.Strings[i] := CustomStringReplace( S.Strings[i], OldPattern, NewPattern );          S.Strings[i] := ReplaceString(S.Strings[i],OldPattern,NewPattern);
109        end;
110    end;
111    
112    function StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
113    asm
114            PUSH    EBX
115            PUSH    ESI
116            PUSH    EDI
117    
118            MOV    ESI,ECX        { Point ESI to substr                  }
119            MOV    EDI,EAX        { Point EDI to s                        }
120    
121            MOV    ECX,EDX        { ECX = search length                  }
122            SUB    ECX,EAX
123    
124            MOV    EDX,SubstrEnd
125            SUB    EDX,ESI
126    
127            DEC    EDX            { EDX = Length(substr) - 1              }
128            JS      @@fail        { < 0 ? return 0                        }
129            MOV    AL,[ESI]      { AL = first char of substr            }
130            INC    ESI            { Point ESI to 2'nd char of substr      }
131    
132            SUB    ECX,EDX        { #positions in s to look at            }
133                                  { = Length(s) - Length(substr) + 1      }
134            JLE    @@fail
135    @@loop:
136            REPNE  SCASB
137            JNE    @@fail
138            MOV    EBX,ECX        { save outer loop counter              }
139            PUSH    ESI            { save outer loop substr pointer        }
140            PUSH    EDI            { save outer loop s pointer            }
141    
142            MOV    ECX,EDX
143            REPE    CMPSB
144            POP    EDI            { restore outer loop s pointer          }
145            POP    ESI            { restore outer loop substr pointer    }
146            JE      @@found
147            MOV    ECX,EBX        { restore outer loop counter            }
148            JMP    @@loop
149    
150    @@fail:
151            XOR    EAX,EAX
152            JMP    @@exit
153    
154    @@found:
155            MOV    EAX,EDI        { EDI points of char after match        }
156            DEC    EAX
157    @@exit:
158            POP    EDI
159            POP    ESI
160            POP    EBX
161    end;
162    
163    function AnsiStrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd: PChar): PChar;
164    var
165      L2: Cardinal;
166      ByteType : TMbcsByteType;
167    begin
168      Result := nil;
169      if (StrStart = nil) or (StrStart^ = #0) or
170        (SubstrStart = nil) or (SubstrStart^ = #0) then Exit;
171      L2 := SubstrEnd - SubstrStart;
172      Result := StrPosEx(StrStart, StrEnd, SubstrStart, SubstrEnd);
173      while (Result <> nil) and (StrEnd - Result >= L2) do begin
174        ByteType := StrByteType(StrStart, Integer(Result-StrStart));
175        if (ByteType <> mbTrailByte) and
176          (CompareString(LOCALE_USER_DEFAULT, SORT_STRINGSORT, Result, L2, SubstrStart, L2) = 2)
177    then Exit;
178        if (ByteType = mbLeadByte) then Inc(Result);
179        Inc(Result);
180        Result := StrPosEx(Result, StrEnd, SubStrStart, SubStrEnd);
181      end;
182      Result := nil;
183    end;
184    
185    function ReplaceString(const S, OldPattern, NewPattern: string): string;
186    var
187      ReplaceCount: Integer;
188      DestIndex: Integer;
189      i: Integer;
190      p, e, ps, pe: PChar;
191      Count: Integer;
192    begin
193      Result := S;
194      if OldPattern = '' then Exit;
195        p := PChar(S);
196        e := p + Length(S);
197        ps := PChar(OldPattern);
198        pe := ps + Length(OldPattern);
199        ReplaceCount := 0;
200        while p < e do begin
201          p := AnsiStrPosEx(p, e, ps, pe);
202          if p = nil then Break;
203          Inc(ReplaceCount);
204          Inc(p, Length(OldPattern));
205      end;      end;
206        if ReplaceCount = 0 then Exit;
207        SetString(Result, nil, Length(S) +
208          (Length(NewPattern) - Length(OldPattern)) * ReplaceCount);
209        p := PChar(S);
210        DestIndex := 1;
211        for i := 0 to ReplaceCount - 1 do begin
212          Count := AnsiStrPosEx(p, e, ps, pe) - p;
213          Move(p^, Result[DestIndex], Count);
214          Inc(p, Count);//p := pp;
215          Inc(DestIndex, Count);
216          Move(NewPattern[1], Result[DestIndex], Length(NewPattern));
217          Inc(p, Length(OldPattern));
218          Inc(DestIndex, Length(NewPattern));
219        end;
220        Move(p^, Result[DestIndex], e - p);
221  end;  end;
222    
223  end.  end.

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

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