syssr.inc 2.7 KB

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