Delphi custom enumerator gives strange errors

When I create a custom enumerator for parsing strings, I see some strange error messages. When using the record, the following error appears:

E2010 Incompatible types: "TSplitStringEnumerator" and "Pointer"

When using classes (add a few .Create calls in the code) instead of entries, I get some internal errors from time to time:

Does anyone know how to save an enumeration work with a record data type instead of classes?

type
    TSplitStringEnumerator = record
        StringToParse:          string;
        Separator:              Char;
        S:                      Integer;
        E:                      Integer;
        L:                      Integer;
        function    GetCurrent  (): string; inline;
        function    MoveNext    (): Boolean; inline;
        property    Current:    string read GetCurrent;
    end;

    TSplitStringGenerator = record
        Enum:                   TSplitStringEnumerator;
        function GetEnumerator: TSplitStringEnumerator; inline;
    end;

function SplitString( const StringToParse: string; Separator: Char ): TSplitStringGenerator; //inline;
begin
    Result.Enum.StringToParse := StringToParse;
    Result.Enum.Separator     := Separator;
    Result.Enum.S             := 0;
    Result.Enum.E             := 0;
    Result.Enum.L             := Length( StringToParse );
end;

procedure Test();
var
    S: string;
begin
    for S in SplitString( 'A;B;C', ';' ) do begin
        OutputDebugString( PChar( S ) );
    end;
end;

{ TSplitStringGenerator }

function TSplitStringGenerator.GetEnumerator(): TSplitStringEnumerator;
begin
    Result := Enum;
end;

{ TSplitStringEnumerator }

function TSplitStringEnumerator.GetCurrent(): string;
begin
    Result := Copy( StringToParse, S, E - S );
end;

function TSplitStringEnumerator.MoveNext(): Boolean;
begin
    S := E + 1;
    Result := S <= L;
    E := S;
    while ( E <= L ) and ( StringToParse[ E ] <> Separator ) do Inc( E );
end;
+3
source share
2 answers

# 72213 QC. ​​ Delphi 2010 (. ).

+4

Delphi 2010. :

Debug Output: A Process Project4.exe (4656)
Debug Output: B Process Project4.exe (4656)
Debug Output: C Process Project4.exe (4656)

, Delphi 2007?

+2

Source: https://habr.com/ru/post/1788701/


All Articles