![]() |
AW: Wie kann man diesen Code optimieren
Zitat:
Delphi-Quellcode:
Keine Gewähr! Den WINANSI_CHAR_MAP habe ich von Copilot nach folgenden Regeln erstellen lassen:
function CleanUpWinAnsiText(const pmcOriginalText: String; var pmvNormalizedText: String): Integer;
const UNWANTED_CHARS: set of AnsiChar = [ ' ', '-', Chr(151), '_', ':', ';', ',', '.', '!', '''']; WINANSI_CHAR_MAP: array[Byte] of AnsiChar = ( #0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21, #22, #23, #24, #25, #26, #27, #28, #29, #30, #31, ' ', '!', '"', '#', '$', '%', '&', '''', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', // Zeile 5: Großbuchstaben nach Kleinbuchstaben (64-79) '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', // Zeile 6: Großbuchstaben nach Kleinbuchstaben (80-95) 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', #127, #128, #129, #130, #131, #132, #133, #134, #135, #136, #137, #138, #139, #140, #141, #142, #143, #144, #145, #146, #147, #148, #149, #150, #151, #152, #153, #154, #155, #156, #157, #158, #159, // Zeile 11: Umlaute (160-175) 'a', 'u', 'a', 'o', 'a', 'u', 'o', 'a', 'o', 'a', 'o', 'a', 'a', 'a', 'a', 'a', // Zeile 12: Umlaute (176-191) 'c', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'a', 'a', 'a', 'a', 'e', 'e', 'e', 'e', // Zeile 13: Sonderzeichen und Umlaute (192-207) 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'y', 'y', 's', 'd', 'd', 'd', #208, #209, #210, #211, #212, #213, #214, 'o', #216, #217, #218, #219, 'u', #221, #222, #223, 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'y', 'y', #252, #253, #254, #255, 'a', 'a', 'o', 'o' ); var p: PChar; idx: Integer; begin if pmcOriginalText = '' then begin pmvNormalizedText := ''; Exit(0); end; Result := Length(pmcOriginalText); SetString(pmvNormalizedText, PChar(Pointer(pmcOriginalText)), Result); p := Pointer(pmvNormalizedText); for idx := 1 to Result do begin if not (pmcOriginalText[idx] in UNWANTED_CHARS) then begin PByte(p)^ := Byte(WINANSI_CHAR_MAP[Ord(pmcOriginalText[idx])]); PByte(p + 1)^ := 0; Inc(p); end; end; Result := p - Pointer(pmvNormalizedText); SetLength(pmvNormalizedText, Result); end;
Thomas |
AW: Wie kann man diesen Code optimieren
Zitat:
Delphi-Quellcode:
Testprozedur:
FUNCTION RemoveChars(const S:String):String;
var Len:Integer; P:PChar; begin SetLength(Result,Length(S)); P:=PChar(S); Len:=0; while P^<>#0 do begin if not (P^ in Removals) then begin Inc(Len); Result[Len]:=P^; end; Inc(P); end; SetLength(Result,Len); end;
Delphi-Quellcode:
Message
PROCEDURE TMain.Test;
const Removals : set of Char = [' ', '-', Chr(151), '_', ':', ';', ',', '.', '!', '''']; Text='Mit Delphi kann man tolle Sacchen machen, zum Beispiel '+ 'Sonderzeichen wie ''_'', '':'', '';'', ''.'', ''!'' entfernen'; function CleanUpTextUsingBuilder(const S: string): string; var i : Integer; sb : TStringBuilder; begin sb := TStringBuilder.Create(Length(S)); try for i := 1 to Length(S) do begin if not (S[i] in Removals) then sb.Append(S[i]); end; Result := sb.ToString; finally sb.Free; end; end; FUNCTION RemoveChars(const S:String):String; var Len:Integer; P:PChar; begin SetLength(Result,Length(S)); P:=PChar(S); Len:=0; while P^<>#0 do begin if not (P^ in Removals) then begin Inc(Len); Result[Len]:=P^; end; Inc(P); end; SetLength(Result,Len); end; FUNCTION TimeStamp:Int64; asm rdtsc end; var T1,T2,T3:Int64; S1,S2:String; begin T1:=TimeStamp; S1:=CleanUpTextUsingBuilder(Text); T2:=TimeStamp; S2:=RemoveChars(Text); T3:=TimeStamp; ShowMessage(S1+#13+S2+#13+IntToStr(T2-T1)+#13+IntToStr(T3-T2)); end; MitDelphikannmantolleSacchenmachenzumBeispielSonde rzeichenwieentfernen MitDelphikannmantolleSacchenmachenzumBeispielSonde rzeichenwieentfernen 67064 8032 |
AW: Wie kann man diesen Code optimieren
@AmateurProfi: es war zu erwarten, dass dein Code die anderen um Längen schlägt, weil diese den "Fehler" machten immer wieder die Länge des Result string zu ändern. Nicht gut, da immer wieder umkopiert wird, wenn ich mich richtig an die Speicherverwaltung von strings denke.
Du kürzt das nur einmal am Ende falls es etwas zu entfernen gab. 8-) |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:12 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz