Einzelnen Beitrag anzeigen

EgonHugeist

Registriert seit: 17. Sep 2011
187 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#12

AW: Leerzeichen in String einfügen

  Alt 27. Jan 2016, 11:48
@mav

meine Portierung hält sich exakt an das, was der TE wollte. Ich halte es für abwägig, das LineEndings oder andere ctrl chars im TEdit landen...

Klar geht noch was:

Code:
function EH_QuadUnderScoreFillerBy4(Const Text: String): String;
type PMyQuadMover = {$IFDEF UNICODE}PUInt64{$ELSE}PLongWord{$ENDIF};
var
  Src, Dest, PEnd: PChar;
  SrcLen: Integer;
begin
  if Text = '' then
    Result :=''
  else begin
    SrcLen := PLongInt(NativeUInt(Text) - SizeOf(LongInt))^; //fast get src length
    Src := Pointer(Text);
    PEnd := Src+SrcLen-5;
    if (Pointer(Result) = nil) or //result unassigned?
       (PLongInt(NativeUInt(Result) - SizeOf(LongInt))^ <> SrcLen) {PStrRec.Len} or //length different?
       (PLongInt(NativeUInt(Result) - (SizeOf(LongInt) shl 1))^ <> 1) {PStrRec.RefCnt} then //no unique string?
      SetLength(Result,SrcLen+((SrcLen-1) shr 2));
    Dest := Pointer(Result);
    while (Src <= PEnd) do begin//making a quad processing loop
      PMyQuadMover(Dest)^ := PMyQuadMover(Src)^;
      (Dest+4)^ := '_';
      Inc(Dest, 5);
      Inc(Src, 4);
    end;
    Inc(PEnd, 4);
    while Src < PEnd do //processing final chars
    begin
      Dest^ := Src^;
      Inc(Src);
      Inc(Dest);
    end;
    Dest^ := Src^; //null term save
  end;
end;
und nu 2 quad moves in einer loop (macht nur sinn, wenn der String länger ist)
Code:
function EH_QuadUnderScoreFillerBy8(Const Text: String): String;
type PMyQuadMover = {$IFDEF UNICODE}PUInt64{$ELSE}PLongWord{$ENDIF};
var
  Src, Dest, PEnd: PChar;
  SrcLen: Integer;
begin
  if Text = '' then
    Result :=''
  else begin
    SrcLen := PLongInt(NativeUInt(Text) - SizeOf(LongInt))^; //fast get src length
    Src := Pointer(Text);
    PEnd := Src+SrcLen-9;
    if (Pointer(Result) = nil) or //result unassigned?
       (PLongInt(NativeUInt(Result) - SizeOf(LongInt))^ <> SrcLen) {PStrRec.Len} or //length different?
       (PLongInt(NativeUInt(Result) - (SizeOf(LongInt) shl 1))^ <> 1) {PStrRec.RefCnt} then //no unique string?
      SetLength(Result,SrcLen+((SrcLen-1) shr 2));
    Dest := Pointer(Result);
    while (Src <= PEnd) do begin//making a octed processing loop
      PMyQuadMover(Dest)^ := PMyQuadMover(Src)^;
      (Dest+4)^ := '_';
      PMyQuadMover(Dest+5)^ := PMyQuadMover(Src+4)^;
      (Dest+9)^ := '_';
      Inc(Dest, 10);
      Inc(Src, 8);
    end;
    Inc(PEnd, 8);
    if ((Src+4) <= PEnd) then begin//making final quad move
      PMyQuadMover(Dest)^ := PMyQuadMover(Src)^;
      (Dest+4)^ := '_';
      Inc(Dest, 5);
      Inc(Src, 4);
    end;
    while Src < PEnd do //processing final chars
    begin
      Dest^ := Src^;
      Inc(Src);
      Inc(Dest);
    end;
    Dest^ := Src^; //null term save
  end;
end;
Jup mehr drum herum, ist nicht abzustreiten. Jags mal durch nen Tick-Counter mit 'ner loop x 5.000.000 ich garantier dir der Unterschied ist riesig.

Das gilt im PurePascal erst mal zu Schlagen! Wer traut sich?

Gruß vom ZeosDevTeam

Edit: a shr vs shl war verdreht, sorry sonst funzt das "Reuse-Result" nicht

Geändert von EgonHugeist (27. Jan 2016 um 12:37 Uhr) Grund: typo
  Mit Zitat antworten Zitat