2
0

syssr.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var
  2. OldPat,Srch: SRstring; // Srch and Oldp can contain uppercase versions of S,OldPattern
  3. PatLength,NewPatLength,P,Cnt,PrevP: Integer;
  4. c,d: SRPChar ;
  5. begin
  6. aCount:=0;
  7. Result:='';
  8. c:= NIL; d:=NIL;
  9. OldPat:='';
  10. Srch:='';
  11. PatLength:=Length(OldPattern);
  12. if PatLength=0 then begin
  13. Result:=S;
  14. exit;
  15. end;
  16. if rfIgnoreCase in Flags then begin
  17. Srch:=SRUpperCase(S);
  18. OldPat:=SRUpperCase(OldPattern);
  19. end else begin
  20. Srch:=S;
  21. OldPat:=OldPattern;
  22. end;
  23. PatLength:=Length(OldPat);
  24. if Length(NewPattern)=PatLength then begin
  25. //Result length will not change
  26. Result:=S;
  27. P:=1;
  28. repeat
  29. P:=Pos(OldPat,Srch,P);
  30. if P>0 then begin
  31. inc(aCount);
  32. move(NewPattern[1],Result[P],PatLength*SizeOf(SRChar));
  33. if not (rfReplaceAll in Flags) then exit;
  34. inc(P,PatLength);
  35. end;
  36. until p=0;
  37. end else begin
  38. //Different pattern length -> Result length will change
  39. //To avoid creating a lot of temporary strings, we count how many
  40. //replacements we're going to make.
  41. P:=1;
  42. repeat
  43. P:=Pos(OldPat,Srch,P);
  44. if P>0 then begin
  45. inc(P,PatLength);
  46. inc(aCount);
  47. if not (rfReplaceAll in Flags) then break;
  48. end;
  49. until p=0;
  50. if aCount=0 then begin
  51. Result:=S;
  52. exit;
  53. end;
  54. NewPatLength:=Length(NewPattern);
  55. SetLength(Result,Length(S)+aCount*(NewPatLength-PatLength));
  56. P:=1; PrevP:=0;
  57. c:=SRPChar(Result); d:=SRPChar(S);
  58. repeat
  59. P:=Pos(OldPat,Srch,P);
  60. if P>0 then begin
  61. Cnt:=P-PrevP-1;
  62. if Cnt>0 then begin
  63. Move(d^,c^,Cnt*SizeOf(SRChar));
  64. inc(c,Cnt);
  65. inc(d,Cnt);
  66. end;
  67. if NewPatLength>0 then begin
  68. Move(NewPattern[1],c^,NewPatLength*SizeOf(SRChar));
  69. inc(c,NewPatLength);
  70. end;
  71. inc(P,PatLength);
  72. inc(d,PatLength);
  73. PrevP:=P-1;
  74. if not (rfReplaceAll in Flags) then break;
  75. end;
  76. until p=0;
  77. Cnt:=Length(S)-PrevP;
  78. if Cnt>0 then Move(d^,c^,Cnt*SizeOf(SRChar));
  79. end;
  80. end;
  81. (*
  82. var
  83. Srch,OldP,RemS: SRString; // Srch and Oldp can contain uppercase versions of S,OldPattern
  84. P : Integer;
  85. begin
  86. Srch:=S;
  87. OldP:=OldPattern;
  88. if rfIgnoreCase in Flags then
  89. begin
  90. Srch:=SRUpperCase(Srch);
  91. OldP:=SRUpperCase(OldP);
  92. end;
  93. RemS:=S;
  94. Result:='';
  95. while (Length(Srch)<>0) do
  96. begin
  97. P:=AnsiPos(OldP, Srch);
  98. if P=0 then
  99. begin
  100. Result:=Result+RemS;
  101. Srch:='';
  102. end
  103. else
  104. begin
  105. Result:=Result+Copy(RemS,1,P-1)+NewPattern;
  106. P:=P+Length(OldP);
  107. RemS:=Copy(RemS,P,Length(RemS)-P+1);
  108. if not (rfReplaceAll in Flags) then
  109. begin
  110. Result:=Result+RemS;
  111. Srch:='';
  112. end
  113. else
  114. Srch:=Copy(Srch,P,Length(Srch)-P+1);
  115. end;
  116. end;
  117. end;
  118. *)