Substring.Explicit Operator

Overload List

#SignatureDescription
1class operator Explicit(const Value: Char): Substring;
2class operator Explicit(const [ref] Value: Substring): PChar;Conversion to PChar: the pointer ToPChar returns, nothing copied.
3class operator Explicit(const Value: Substring): string;
4class operator Explicit(const Value: string): Substring;Explicit conversions from a string or a Char, and to a string; each copies the text.

Overload 1: class operator Explicit(const Value: Char): Substring;

#NameTypeDescription
1ValueChar

Returns: Substring

Overload 2: class operator Explicit(const [ref] Value: Substring): PChar;

Conversion to PChar: the pointer ToPChar returns, nothing copied.

#NameTypeDescription
1ValueSubstring

Returns: PChar

Remarks:

The text is terminated by #0 and the pointer is valid until the string next changes. A #0 already after the text is used as it is; where it cannot be placed without overwriting a character or taking memory, as after a window inside a longer text, ESubstringInPlaceError is raised, and MakeUnique first makes the conversion work. The conversion is explicit only, as string(s) is, so a Substring never becomes a string unseen.

Examples

A Substring passed as a C string.

uses Substr;

procedure Example;
var s: Substring;
    P: PChar;
begin
    s := Substring('abc');
    P := PChar(s);
    Assert((P = s.Data) and (P[3] = #0));
end;
See Also: Substring.ToPChar, Substring.MakeUnique

Overload 3: class operator Explicit(const Value: Substring): string;

#NameTypeDescription
1ValueSubstring

Returns: string

Examples

A Substring passed as a C string.

uses Substr;

procedure Example;
var s: Substring;
    P: PChar;
begin
    s := Substring('abc');
    P := PChar(s);
    Assert((P = s.Data) and (P[3] = #0));
end;

Overload 4: class operator Explicit(const Value: string): Substring;

Explicit conversions from a string or a Char, and to a string; each copies the text.

#NameTypeDescription
1Valuestring

Returns: Substring

Remarks:

A string or a character is copied into a new value, inline when it fits. string(s) copies the window into a new string. Methods and operators that take a string read it in place without a conversion; Adopt and Subrange view a string without copying it.

Examples

Copying in and out.

uses Substr;

procedure Example;
var s: Substring;
    t: string;
begin
    s := Substring('hello');
    t := string(s);
    Assert(t = 'hello');
    s := Substring(Char('x'));
    Assert(s.Length = 1);
end;
See Also: Substring.ToString, Substring.Adopt