mPegFilter.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. unit mPegFilter;
  2. interface
  3. uses
  4. Classes, SysUtils;
  5. type
  6. TPegFilter = class
  7. private
  8. FBeginPeg: string;
  9. FBeginPegNum: Double;
  10. FEndPeg: string;
  11. FEndPegNum: Double;
  12. function PegFormat(const AStr: string): string;
  13. function FilterPeg(const AStr: string; var APeg: string; var ANum: Double; var ABegin, AEnd: Integer): Boolean;
  14. public
  15. function Filter(const AStr: string): Boolean;
  16. property BeginPeg: string read FBeginPeg;
  17. property BeginPegNum: Double read FBeginPegNum;
  18. property EndPeg: string read FEndPeg;
  19. property EndPegNum: Double read FEndPegNum;
  20. end;
  21. function PegFilter: TPegFilter;
  22. implementation
  23. uses Math;
  24. var
  25. FPegFilter: TPegFilter;
  26. function PegFilter: TPegFilter;
  27. begin
  28. if FPegFilter = nil then
  29. FPegFilter := TPegFilter.Create;
  30. Result := FPegFilter;
  31. end;
  32. { TPegFilter }
  33. function TPegFilter.Filter(const AStr: string): Boolean;
  34. var
  35. sPeg: string;
  36. iPos, iBegin, iEnd: Integer;
  37. begin
  38. sPeg := PegFormat(AStr);
  39. Result := FilterPeg(sPeg, FBeginPeg, FBeginPegNum, iBegin, iEnd);
  40. if Result and (sPeg[iEnd+1] = '~') then
  41. begin
  42. sPeg := StringReplace(sPeg, FBeginPeg, '', []);
  43. FilterPeg(sPeg, FEndPeg, FEndPegNum, iBegin, iEnd);
  44. if FEndPegNum = 0 then
  45. begin
  46. FEndPeg := FBeginPeg;
  47. FEndPegNum := FBeginPegNum;
  48. end
  49. else
  50. Result := FEndPegNum >= FBeginPegNum;
  51. end
  52. else
  53. begin
  54. FEndPeg := '';
  55. FEndPegNum := 0;
  56. end;
  57. end;
  58. function TPegFilter.FilterPeg(const AStr: string; var APeg: string;
  59. var ANum: Double; var ABegin, AEnd: Integer): Boolean;
  60. function FilterInt(ABegin: Integer; var AEnd: Integer; var AIntStr: string; var AValue: Integer): Boolean;
  61. var
  62. iPos, iLength: Integer;
  63. sInt: string;
  64. begin
  65. AIntStr := '';
  66. AEnd := ABegin;
  67. iPos := ABegin;
  68. iLength := Length(AStr);
  69. while (iPos <= iLength) do
  70. begin
  71. if AStr[iPos] in ['0'..'9'] then
  72. begin
  73. AIntStr := AIntStr + AStr[iPos];
  74. AEnd := iPos;
  75. end
  76. else Break;
  77. Inc(iPos);
  78. end;
  79. Result := AIntStr <> '';
  80. AValue := StrToIntDef(AIntStr, 0);
  81. end;
  82. // ¹ýÂËÒ»¸öСÓÚ1000µÄFloat
  83. function FilterFloat(ABegin: Integer; var AEnd: Integer; var AFloatStr: string; var AValue: Double): Boolean;
  84. var
  85. iPos, iLength, iIntPartLength: Integer;
  86. bHasPoint, bAllZero: Boolean;
  87. begin
  88. AFloatStr := '';
  89. AEnd := ABegin;
  90. iPos := ABegin;
  91. iLength := Length(AStr);
  92. bHasPoint := False;
  93. bAllZero := True;
  94. iIntPartLength := 0;
  95. while (iPos <= iLength) do
  96. begin
  97. if AStr[iPos] in ['0'..'9'] then
  98. begin
  99. AFloatStr := AFloatStr + AStr[iPos];
  100. AEnd := iPos;
  101. if not bHasPoint then
  102. Inc(iIntPartLength);
  103. bAllZero := bAllZero and (AStr[iPos] = '0');
  104. end
  105. else if (AStr[iPos] = '.') and (not bHasPoint) then
  106. begin
  107. AFloatStr := AFloatStr + AStr[iPos];
  108. AEnd := iPos;
  109. bHasPoint := True;
  110. end
  111. else Break;
  112. Inc(iPos);
  113. end;
  114. if iIntPartLength > 3 then
  115. begin
  116. AEnd := ABegin + 3 - 1;
  117. AFloatStr := Copy(AFloatStr, 1, 3);
  118. end;
  119. AValue := StrToFloatDef(AFloatStr, 0);
  120. Result := (AValue <> 0) or (bAllZero and (AEnd > ABegin));
  121. end;
  122. var
  123. iPos, iLength: Integer;
  124. iBeginNum, iEndNum, iPosPuls, iBeginNum2, iEndNum2: Integer;
  125. sNum, sNum2: string;
  126. iNum: Integer;
  127. fNum: Double;
  128. begin
  129. Result := False;
  130. APeg := '';
  131. ANum := 0;
  132. ABegin := -1;
  133. AEnd := -1;
  134. iPos := 1;
  135. iLength := Length(AStr);
  136. while (iPos <= iLength) and (not Result) do
  137. begin
  138. if AStr[iPos] = 'k' then
  139. begin
  140. iBeginNum := iPos + 1;
  141. if FilterInt(iBeginNum, iEndNum, sNum, iNum) then
  142. begin
  143. iPosPuls := iEndNum + 1;
  144. if AStr[iPosPuls] = '+' then
  145. begin
  146. iBeginNum2 := iPosPuls + 1;
  147. if FilterFloat(iBeginNum2, iEndNum2, sNum2, fNum) then
  148. begin
  149. Result := True;
  150. APeg := Copy(AStr, iPos, iEndNum2-iPos+1);
  151. ANum := iNum * 1000 + fNum;
  152. ABegin := iPos;
  153. AEnd := iEndNum2;
  154. end;
  155. end;
  156. end
  157. end;
  158. inc(iPos);
  159. end;
  160. end;
  161. function TPegFilter.PegFormat(const AStr: string): string;
  162. begin
  163. Result := AStr;
  164. Result := StringReplace(Result, 'K', 'k', [rfReplaceAll]);
  165. Result := StringReplace(Result, '£«', '+', [rfReplaceAll]);
  166. Result := StringReplace(Result, '¡«', '~', [rfReplaceAll]);
  167. Result := StringReplace(Result, ' ', '', [rfReplaceAll]);
  168. end;
  169. initialization
  170. FPegFilter := nil;
  171. finalization
  172. FPegFilter.Free;
  173. end.