Janilabo

Janilabo | ReplaceEx2()

Aug 30th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.69 KB | None | 0 0
  1. const
  2.   TEXT = '||| || |||| |||||';
  3.   FIND_STR = '||';  
  4.   REPLACE_STR = '*';
  5.  
  6. type          
  7.   TMatchMethod = (mmAll, mmBackward, mmIgnoreCase, mmOverlap, mmWholeWords, mmStrictWW);
  8.   TMatchMethods = set of TMatchMethod;
  9.  
  10. function ReplaceEx2(Text, FindStr, ReplaceStr: string; Methods: TMatchMethods; Offset: Integer): string;
  11. var
  12.   fsL, tL, i, c, e, t: Integer;
  13.   rgx_fs, tmp: string;
  14.   r: TRegexMatchArray;
  15.   p: TIntArray;
  16. begin
  17.   Result := Text;
  18.   tL := Length(Text);
  19.   fsL := Length(FindStr);
  20.   if ((tL < 1) or (Length(FindStr) > tL) or (FindStr = '')) then
  21.     Exit;
  22.   if (Offset < 1) then
  23.     Offset := 1;  
  24.   FindStr := PregQuote(FindStr);  
  25.   if (mmWholeWords in Methods) then
  26.   begin
  27.     if (mmStrictWW in Methods) then  
  28.       rgx_fs := '/(?<!\S)' + FindStr + '(?!\S)/'
  29.     else
  30.       rgx_fs := '/(^|\b|(?<=\s))' + FindStr + '((?=\s)|\b|$)/'
  31.   end else
  32.     rgx_fs := '/' + FindStr + '/';
  33.   if (mmIgnoreCase in Methods) then
  34.     rgx_fs := (rgx_fs + 'i');
  35.   rgx_fs := (rgx_fs + 'm');  
  36.   case (mmBackward in Methods) of
  37.     True:
  38.     begin      
  39.       Result := Copy(Text, 1, Offset);    
  40.       tmp := Result;    
  41.       SetLength(p, Length(tmp));
  42.       while PregMatchEx(rgx_fs, tmp, r) do
  43.       begin
  44.         p[c] := (r[0].Offset + c);
  45.         Inc(c);
  46.         Delete(tmp, r[0].Offset, 1);
  47.         SetLength(r, 0);
  48.       end;        
  49.       SetLength(p, c);
  50.       TIAUnique(p);
  51.       c := Length(p);
  52.       InvertTIA(p);
  53.       if (c > 0) then
  54.         if not (mmAll in Methods) then
  55.         begin
  56.           Delete(Result, p[0], fsL);
  57.           Insert(ReplaceStr, Result, p[0]);
  58.         end else
  59.         case (not (mmWholeWords in Methods) and (mmOverlap in Methods)) of
  60.           True:
  61.           for i := 0 to (c - 1) do
  62.           begin      
  63.             e := fsL;
  64.             if (i > 0) then
  65.               if ((p[(i - 1)] - p[i]) <= fsL) then
  66.                 e := (p[(i - 1)] - p[i]);
  67.             Delete(Result, p[i], e);
  68.             Insert(ReplaceStr, Result, p[i]);
  69.           end;
  70.           False:  
  71.           begin  
  72.             e := 0;
  73.             if (mmWholeWords in Methods) then
  74.               e := 1;
  75.             for i := 0 to (c - 1) do
  76.             begin                                        
  77.               Inc(t);
  78.               if (i > 0) then
  79.                 if ((p[(i - t)] - p[i]) < (fsL - e)) then
  80.                   Continue;
  81.               Delete(Result, p[i], fsL);
  82.               Insert(ReplaceStr, Result, p[i]);
  83.               t := 0;
  84.             end;
  85.           end;
  86.         end;
  87.       Result := (Result + Copy(Text, (Offset + 1), (Length(Text) - Offset)));
  88.     end;  
  89.     False:
  90.     begin
  91.       Result := Copy(Text, Offset, ((tL - Offset) + 1));
  92.       case (mmAll in Methods) of
  93.         True:
  94.         begin
  95.           if (not (mmWholeWords in Methods) and (mmOverlap in Methods)) then
  96.           begin
  97.             tmp := Result;
  98.             SetLength(p, Length(tmp));
  99.             while PregMatchEx(rgx_fs, tmp, r) do
  100.             begin
  101.               p[c] := (r[0].Offset + c);
  102.               Inc(c);
  103.               Delete(tmp, r[0].Offset, 1);
  104.               SetLength(r, 0);
  105.             end;        
  106.             SetLength(p, c);
  107.             TIAUnique(p);
  108.             c := Length(p);
  109.             for i := (c - 1) downto 0 do
  110.             begin          
  111.               e := fsL;
  112.               if (i < (c - 1)) then
  113.                 if ((p[(i + 1)] - p[i]) <= fsL) then
  114.                   e := (p[(i + 1)] - p[i]);        
  115.               Delete(Result, p[i], e);
  116.               Insert(ReplaceStr, Result, p[i]);
  117.             end;
  118.           end else
  119.             Result := PregReplace(rgx_fs, ReplaceStr, Result);  
  120.         end;
  121.         False:
  122.         if PregMatchEx(rgx_fs, Result, r) then
  123.         begin
  124.           Result := (Copy(Result, 1, (r[0].Offset - 1)) + ReplaceStr + Copy(Result, (r[0].Offset + r[0].Length), (Length(Result) - (r[0].Offset + r[0].Length) + 1)));
  125.           SetLength(r, 0);  
  126.         end;
  127.       end;                                                              
  128.       Result := (Copy(Text, 1, (Offset - 1)) + Result);    
  129.     end;
  130.   end;  
  131.   SetLength(p, 0);
  132.   tmp := '';    
  133. end;
  134.  
  135. var
  136.   rTEXT: string;
  137.   l, i, s, o: Integer;
  138.   m_sets: array of TMatchMethods;
  139.   TSA: TStrArray;
  140.                
  141. procedure BuildSets(find_method: (fmBackward, fmForward));
  142. begin
  143.   case find_method of
  144.     fmForward:
  145.     begin
  146.       TSA := ['[]', '[mmIgnoreCase]', '[mmIgnoreCase, mmAll]', '[mmIgnoreCase, mmAll, mmOverlap]', '[mmIgnoreCase, mmAll, mmOverlap, mmWholeWords]', '[mmIgnoreCase, mmAll, mmOverlap, mmWholeWords, mmStrictWW]'];
  147.       l := Length(FIND_STR);
  148.       o := 1;  
  149.       s := High(TSA);
  150.       SetLength(m_sets, (s + 1));
  151.       m_sets[0] := [];
  152.       m_sets[1] := [mmIgnoreCase];
  153.       m_sets[2] := [mmIgnoreCase, mmAll];
  154.       m_sets[3] := [mmIgnoreCase, mmAll, mmOverlap];
  155.       m_sets[4] := [mmIgnoreCase, mmAll, mmOverlap, mmWholeWords];
  156.       m_sets[5] := [mmIgnoreCase, mmAll, mmOverlap, mmWholeWords, mmStrictWW];  
  157.     end;
  158.     fmBackward:
  159.     begin
  160.       TSA := ['[mmBackward]', '[mmBackward, mmIgnoreCase]', '[mmBackward, mmIgnoreCase, mmAll]', '[mmBackward, mmIgnoreCase, mmAll, mmOverlap]', '[mmBackward, mmIgnoreCase, mmAll, mmOverlap, mmWholeWords]', '[mmBackward, mmIgnoreCase, mmAll, mmOverlap, mmWholeWords, mmStrictWW]'];
  161.       l := Length(FIND_STR);
  162.       o := Length(TEXT);
  163.       s := High(TSA);
  164.       SetLength(m_sets, (s + 1));
  165.       m_sets[0] := [mmBackward];
  166.       m_sets[1] := [mmBackward, mmIgnoreCase];
  167.       m_sets[2] := [mmBackward, mmIgnoreCase, mmAll];
  168.       m_sets[3] := [mmBackward, mmIgnoreCase, mmAll, mmOverlap];
  169.       m_sets[4] := [mmBackward, mmIgnoreCase, mmAll, mmOverlap, mmWholeWords];
  170.       m_sets[5] := [mmBackward, mmIgnoreCase, mmAll, mmOverlap, mmWholeWords, mmStrictWW];  
  171.     end;    
  172.   end;
  173. end;
  174.  
  175. begin                
  176.   ClearDebug;                              
  177.   BuildSets(fmForward);      
  178.   WriteLn('FORWARD:');
  179.   for i := 0 to s do
  180.   begin
  181.     rTEXT := ReplaceEx2(TEXT, FIND_STR, REPLACE_STR, m_sets[i], o);
  182.     WriteLn('ReplaceEx2(''' + TEXT + ''', ''' + FIND_STR + ''', ''' + REPLACE_STR + ''', ' + TSA[i] + ', ' + IntToStr(o) + ')');
  183.     WriteLn('TEXT: ' + rTEXT);
  184.     if (i < s) then
  185.       WriteLn('');
  186.   end;              
  187.   BuildSets(fmBackward);
  188.   WriteLn('BACKWARD:');
  189.   for i := 0 to s do
  190.   begin
  191.     rTEXT := ReplaceEx2(TEXT, FIND_STR, REPLACE_STR, m_sets[i], o);
  192.     WriteLn('ReplaceEx2(''' + TEXT + ''', ''' + FIND_STR + ''', ''' + REPLACE_STR + ''', ' + TSA[i] + ', ' + IntToStr(o) + ')');
  193.     WriteLn('TEXT: ' + rTEXT);
  194.     if (i < s) then
  195.       WriteLn('');
  196.   end;
  197.   SetLength(m_sets, 0);
  198.   SetLength(TSA, 0);      
  199. end.
Advertisement
Add Comment
Please, Sign In to add comment