Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#2

Re: Aus einem TMemo doppelte Strings zählen und dann löschen

  Alt 25. Jul 2005, 17:39
Wie würdest du es denn mit Papier und Bleistift lösen? Ich würde den Text durchgehen und eine Strichliste führen und immer aufaddieren.

Stichwörter für Delphi: pos, posex, copy, delete, ...

Zufällig hatte ich gerade so was mal selber gebraucht:
Delphi-Quellcode:
type
  TWordDelimiter = set of Char;

var
  WordDelimiter: TWordDelimiter = [#1..#64, #91..#96, #123..#127];

function CountWord(const Text, Word: string): Cardinal;
var
  cnt: Cardinal;
  i: Integer;
begin
  cnt := 0;
  i := 0;
  i := NextPos(Word, Text, i);
  while i > 0 do
  begin
    // Wort wenn Zeichen nach Zeichenkette...
    if (Text[i + length(Word)] in WordDelimiter) then
      Inc(cnt);
    i := NextPos(Word, Text, i);
  end;
  result := cnt;
end;
Nachtrag: NextPos sieht so aus:
Delphi-Quellcode:
function NextPos(SubStr: AnsiString; Str: AnsiString; LastPos: DWORD
  = 0): DWORD;
type
  StrRec = packed record
    allocSiz: Longint;
    refCnt: Longint;
    length: Longint;
  end;

const
  skew = sizeof(StrRec);

asm
  // Search-String passed?
  TEST EAX,EAX
  JE @@noWork

  // Sub-String passed?
  TEST EDX,EDX
  JE @@stringEmpty

  // Save registers affected
  PUSH ECX
  PUSH EBX
  PUSH ESI
  PUSH EDI

  // Load Sub-String pointer
  MOV ESI,EAX
  // Load Search-String pointer
  MOV EDI,EDX
  // Save Last Position in EBX
  MOV EBX,ECX

  // Get Search-String Length
  MOV ECX,[EDI-skew].StrRec.length
  // subtract Start Position
  SUB ECX,EBX
  // Save Start Position of Search String to return
  PUSH EDI
  // Adjust Start Position of Search String
  ADD EDI,EBX

  // Get Sub-String Length
  MOV EDX,[ESI-skew].StrRec.length
  // Adjust
  DEC EDX
  // Failed if Sub-String Length was zero
  JS @@fail
  // Pull first character of Sub-String for SCASB function
  MOV AL,[ESI]
  // Point to second character for CMPSB function
  INC ESI

  // Load character count to be scanned
  SUB ECX,EDX
  // Failed if Sub-String was equal or longer than Search-String
  JLE @@fail
@@loop:
  // Scan for first matching character
  REPNE SCASB
  // Failed, if none are matching
  JNE @@fail
  // Save counter
  MOV EBX,ECX
  PUSH ESI
  PUSH EDI
  // load Sub-String length
  MOV ECX,EDX
  // compare all bytes until one is not equal
  REPE CMPSB
  // restore counter
  POP EDI
  POP ESI
  // all byte were equal, search is completed
  JE @@found
  // restore counter
  MOV ECX,EBX
  // continue search
  JMP @@loop
@@fail:
  // saved pointer is not needed
  POP EDX
  XOR EAX,EAX
  JMP @@exit
@@stringEmpty:
  // return zero - no match
  XOR EAX,EAX
  JMP @@noWork
@@found:
  // restore pointer to start position of Search-String
  POP EDX
  // load position of match
  MOV EAX,EDI
  // difference between position and start in memory is
  // position of Sub
  SUB EAX,EDX
@@exit:
  // restore registers
  POP EDI
  POP ESI
  POP EBX
  POP ECX
@@noWork:
end;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat