⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 count.pas

📁 《Delphi技术手册源码》原书佩戴的光盘
💻 PAS
字号:
unit Count;

interface

uses Windows, SysUtils, Classes, Futures;

// Count characters, words, and lines in a text string.
type
  TCountFuture = class(TFuture)
  private
    fText: string;
  public
    constructor Create(const Text: string);
    function Compute: Variant; override;
    property Text: string read fText;
  end;

implementation

{ TCountFuture }

constructor TCountFuture.Create(const Text: string);
begin
  inherited Create;
  fText := Text;
  UniqueString(fText);
end;

function TCountFuture.Compute: Variant;
const
  WhiteSpace = [#0..' '];
  WordChars = ['a'..'z','A'..'Z','0'..'9','-'];
  Punctuation = [#0..#255] - WhiteSpace - WordChars;
var
  I: Integer;
  InWord: Boolean;
  NumChars, NumWords, NumLines: Integer;
begin
  Result := VarArrayCreate([1,3], varInteger);
  InWord := False;
  NumChars := Length(Text);
  NumWords := 0;
  NumLines := 0;
  for I := 1 to Length(Text) do
    if Text[I] in WordChars then
      InWord := True
    else if Text[I] in  WhiteSpace then
    begin
      if Terminated then
        Break;

      if InWord then
        Inc(NumWords);
      InWord := False;

      if Text[I] = #10 then
        Inc(NumLines)
      else if (Text[I] = #13) and not ((I < Length(Text)) and (Text[I+1] = #10)) then
        // Increment the line count now if Text[i] is CR but the next character
        // is NOT a linefeed. If Text[I] is a CR, and the next char is LF,
        // the test above will increment the line count.
        Inc(NumLines);
    end
    else
    begin
      if InWord then
        Inc(NumWords);
      InWord := False;
    end;

  if InWord then
    Inc(NumWords);

  Result[1] := NumChars;
  Result[2] := NumWords;
  Result[3] := NumLines;
end;

end.
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -