Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi in strings suchen (https://www.delphipraxis.net/55198-strings-suchen.html)

geisi 17. Okt 2005 22:17


in strings suchen
 
was gibt pos() zurück, wenn der substring nicht gefunden wird?
in der delphihilfe steht null(ist das nil, oder 0)

sniper_w 17. Okt 2005 22:22

Re: in strings suchen
 
null ist in Englisch 0. Und Pos ist so deklariert:
function Pos(...):Integer;

geisi 17. Okt 2005 22:28

Re: in strings suchen
 
Delphi-Quellcode:
...
begin
  text := 'hallo Test!';
  suche_nach := 'hallo';
  i := pos(suche_nach,text);
end;
müsste pos hier nicht auch 0 zurückgeben? hallo beginnt nämlich bei index 0 oder bin ich da falsch?

Luckie 17. Okt 2005 22:37

Re: in strings suchen
 
Das erste Zeichen eines String steht an der Stelle [1].

himitsu 17. Okt 2005 22:57

Re: in strings suchen
 
Die Strings sind ja in gewisser Weise auch nur Arrays of Char (vorallem die LongStrings ^^), aber da bei den ersten Strings, dem ShortString, als erstes das LängenByte vorhanden war (was jetzt allerdings nicht mehr indieser Art existiert), befand/befindet sich das 1. Zeichen eben auf Position 1 - eben alles um ein Feld verschoben.


ich hoffe dat war jetzt verständlich erklärt und möglichst fehlerfrei ._.

Christian Seehase 17. Okt 2005 23:04

Re: in strings suchen
 
Moin Himitsu,

Zitat:

Zitat von himitsu
als erstes das LängenByte vorhanden war (was jetzt allerdings nicht mehr indieser Art existiert)

Kommt drauf an, was Du als "nicht mehr in dieser Art" ansiehst.
Jetzt ist der Zähler ein Integer, der auf den Postionen String bis String-3 liegt, in negativer Richtung gefolgt von einem, ebenfalls 32 Bit grossen, Referenzzähler.

ripper8472 17. Okt 2005 23:30

Re: in strings suchen
 
die "neuen" strings sind auch mit einem \0 terminiert, damit man sie problemlos nach pchar casten kann :P

himitsu 18. Okt 2005 00:13

Re: in strings suchen
 
Zitat:

Zitat von Christian Seehase
Kommt drauf an, was Du als "nicht mehr in dieser Art" ansiehst.
Jetzt ist der Zähler ein Integer, der auf den Postionen String bis String-3 liegt, in negativer Richtung gefolgt von einem, ebenfalls 32 Bit grossen, Referenzzähler.

Genau das meinte ich ja :)

Delphi-Quellcode:
Const SDynArrayInfoR = 2 * SizeOf(LongInt);
  SDynArrayInfo = SizeOf(LongInt) + SDynArrayInfoR;

Type TShortStringInfo = packed Record
    Length: Byte;
    Chars: packed Array[1..255{or 1..Length}] of Char;
  End;

  TDynArrayInfo = packed Record
    MemoryInfo:  LongInt;
    RefCount:    LongInt;
    ElementCount: LongInt;
    Data:        packed Array[0..0{High(LongInt) - SDynArrayInfo - 1}] of Byte;
  End;

  TAnsiStringInfo = packed Record
    MemoryInfo:  LongInt;
    RefCount:    LongInt;
    ElementCount: LongInt;
    Data:        packed Array[1..High((LongInt) - SDynArrayInfo] of AnsiChar;
  End;

  TWideStringInfo = packed Record
    MemoryInfo:  LongInt;
    RefCount:    LongInt;
    ElementCount: LongInt;
    Data:        packed Array[1..(High((LongInt) - SDynArrayInfo) div 2] of WideChar;
  End;
Der Unterschied ist halt, daß es das LängenByte(an sich) ja nicht mehr gibt,
außerdem zeigt jetzt der Pointer of Typ auf das 1. Zeichen und nicht mehr auf das LängenByte (wie beim ShortString).

@S beim ShortString zeigt auf TShortStringInfo.Length,
wärend bei den anderen Typen Pointer(S) auf Typ.Data verweist.



Komplett sieht das Ganze dann so bei mir aus:
Delphi-Quellcode:
(***************************************************************************************)
(*** Compiler Intern Data-Types ********************************************************)
(***************************************************************************************)

Type TShortStringInfo = packed Record    // PShortStringInfo := @Var
    Length: Byte;
    Chars: packed Array[1..255{or 1..Length}] of Char;
  End;
  PShortStringInfo = ^TShortStringInfo;

(*************************************************************************** )
(                                                                            )
(  P___Info[C] := Pointer(var) - SDynArrayInfo                             )
(  Pointer(var) := T___Info[C].___Addr                                      )
(                                                                            )
(  A String/AnsiString is an Array of Char and just the same as near the    )
(  Array of Char an additional #0 is attached also there still behind.      )
(  (for the PChar compatibility)                                            )
(                                                                            )
(  The WideString and the Array of WideChar are also identical              )
(  and there #0#0 hide themselves behind that.                              )
(                                                                            )
(*************************************************************************** )
(                                                                            )
(  T___Info / P___Info:                                                     )
(  ********************                                                      )
(                                                                            )
(  Pointer(var)      = nil       > nil {in EXE}        > nil {in RAM}      )
(                                                                            )
(  MemoryInfo        -            -                     <> 0                )
(  RefCount          -            = -1                  >= 1                )
(  ElementCount      -            = Length(var)        = Length(var)      )
(  Data              -            = var                = var              )
(                                                                            )
(*************************************************************************** )
(                                                                            )
(  T___InfoC / P___InfoC:                                                   )
(  **********************                                                    )
(                                                                            )
(  Pointer(var)      = nil      > nil {in EXE}         > nil {in RAM}      )
(                                                                            )
(  MemoryInfo        = 0         = 0                    <> 0                )
(  RefCount          = 0         = -1                   >= 1                )
(  ElementCount      = 0         >= 1 = Length(var)    >= 1 = Length(var) )
(  Data              -           = var                 = var              )
(                                                                            )
(****************************************************************************)

{ No Type }
{}Const SDynArrayInfoR = 2 * SizeOf(LongInt);
{}  SDynArrayInfo = SizeOf(LongInt) + SDynArrayInfoR;
{}Type TDynArrayInfo = packed Record
{}    MemoryInfo:  LongInt;
{}    RefCount:    LongInt;
{}    ElementCount: LongInt;
{}    Data:        packed Array[0..0{High(Integer) - SDynArrayInfo - 1}] of Byte;
{}  End;

Type PDynArrayInfo = ^TDynArrayInfo;
  TDynArrayInfoC = packed Record
    MemoryInfo:  LongInt;
    RefCount:    LongInt;
    ElementCount: LongInt;
    DataAddr:    Pointer;
    isVar:       LongBool;
  End;

{ No Types }
{}Type TAnsiStringInfo = packed Record
{}    MemoryInfo:  LongInt;
{}    RefCount:    LongInt;
{}    ElementCount: LongInt;
{}    Data:        packed Array[1..High(Integer) - SDynArrayInfo] of AnsiChar;
{}  End;
{}  TWideStringInfo = packed Record
{}    MemoryInfo:  LongInt;
{}    RefCount:    LongInt;
{}    ElementCount: LongInt;
{}    Data:        packed Array[1..(High(Integer) - SDynArrayInfo) div 2] of WideChar;
{}  End;

Type PAnsiStringInfo = ^TAnsiStringInfo;
  TAnsiStringInfoC = packed Record
    MemoryInfo:  LongInt;
    RefCount:    LongInt;
    ElementCount: LongInt;
    DataAddr:    PAnsiChar;
    isVar:       LongBool;
  End;

  PWideStringInfo = ^TWideStringInfo;
  TWideStringInfoC = packed Record
    MemoryInfo:  LongInt;
    RefCount:    LongInt;
    ElementCount: LongInt;
    DataAddr:    PWideChar;
    isVar:       LongBool;
  End;

Function DynArrayInfo (Const A): PDynArrayInfo;
Function DynArrayInfoC(Const A): TDynArrayInfoC;

Function StringInfo (Const S: AnsiString): PAnsiStringInfo; Overload;
Function StringInfoC(Const S: AnsiString): TAnsiStringInfoC; Overload;

{$IFNDEF UseOLEWideString}
  Function StringInfo (Const S: WideString): PWideStringInfo; Overload;
  Function StringInfoC(Const S: WideString): TWideStringInfoC; Overload;
{$ENDIF}

Achtung: Die Typen für WideStrings bitte nicht ganz so ernst nehmen, denn aus unerfindlichen Gründen ist die aktuell implementierte Version der WideStrings nicht von Borland definiert worden - es wird einfach ein OLEWideString verwendet, welcher etwas anders aufgebaut ist.
Dieses ist auch der Hauptgrund, weßhalb WideStrings nicht über eine Referenzzählung verfügen. :cry:


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:06 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