syssr.inc 2.8 KB

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