syssr.inc 2.8 KB

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