TSubstringList.Add Method

Overload List

#SignatureDescription
1function Add(const S: string): Integer; overload;Adds S: at the end, or, in a sorted list, at its place.
2function Add(const Src: string; const SrcIdx, SrcLen: Integer): Integer; overload;Adds SrcLen characters of Src from SrcIdx (1-based) as an item.

Overload 1: function Add(const S: string): Integer; overload;

Adds S: at the end, or, in a sorted list, at its place.

#NameDescription
1S

The text, every text kind; it may be an item of this list.

Returns: Integer - The new item's index; in a sorted list under dupIgnore, the index of the equal item already there.

Remarks:

A Substring is stored as an assignment stores it: a view stays a view of the same characters (List[i] of another item included, which dangles once that item is written or deleted); a list's long item, or a Substring taken from one, is shared; any other text is copied.

Examples

Adding and reading back.

uses Substr;

procedure Example;
var List: TSubstringList;
begin
    List := TSubstringList.Create(4);
    try
        Assert(List.Add('one') = 0);
        Assert(List.Add('two') = 1);
        Assert(List[1] = 'two');
    finally
        List.Free;
    end;
end;

A full list grows by 128 rows.

uses Substr;

procedure Example;
var List: TSubstringList;
    k: Integer;
begin
    List := TSubstringList.Create;
    try
        for k := 1 to 129 do List.Add('row');
        Assert((List.Count = 129) and (List.Capacity = 256));
    finally
        List.Free;
    end;
end;

A view stays a view: the item reads the characters it was given.

uses Substr;

procedure Example;
var List: TSubstringList;
    Text: string;
    s: Substring;
begin
    Text := 'adopted';
    s.Adopt(Text);
    List := TSubstringList.Create(4);
    try
        List.Add(s);
        Assert(List[0].Data = PChar(Text));
        List.Add(List[0]);
        Assert(List[1].Data = PChar(Text));
        List.Add(Substring('own text'));
        Assert(List.Items[2].IsInline);
    finally
        List.Free;
    end;
end;
See Also: TSubstringList.AddObject, TSubstringList.Insert, TSubstringList.Put

Overload 2: function Add(const Src: string; const SrcIdx, SrcLen: Integer): Integer; overload;

Adds SrcLen characters of Src from SrcIdx (1-based) as an item.

#NameDescription
1Src

The text, every text kind but Char.

2SrcIdx

The part's first character, from 1.

3SrcLen

The part's length.

Returns: Integer - The new item's index.

Remarks:

The range must lie inside Src, else ESubstringRangeError is raised.

Examples

Adding a part of a string.

uses Substr;

procedure Example;
var List: TSubstringList;
begin
    List := TSubstringList.Create(4);
    try
        List.Add('key=value', 5, 5);
        Assert(List[0] = 'value');
    finally
        List.Free;
    end;
end;
See Also: Substring.Subrange, Substring.Subrange