Object TPCharView

Unit

Declaration

type TPCharView = object(specialize TPointerView<char>)

Description

A string view representing a subsequence of a string or pchar or char array.

See TPointerView for the general concept. TPCharView extends TPointerView with methods for string inputs.

For example, if you have a string '(123)', and want to convert the number between the parentheses to an integer, you can do:

var
  v: TPCharView;
  number: integer;
  ok: boolean;
begin
  v := '(123)'.pcharView;
  ok := v.rightOfFind('(');
  ok := ok and v.leftOfFind(')');
  ok := ok and v.toIntDecimalTry(number);

The integer is returned in the variable number and the variable ok returns whether the parentheses exist and the conversion was successful.

This is fast (zero allocations) and safe (no out-of-bound access or overflow is possible).
(although you need to make sure the string is not destroyed while using the TPcharView. For truly safe operations, use TStringView)

Hierarchy

Overview

Methods

Public procedure init(const buffer: string); overload;
Public procedure init(const buffer: TBytes); overload;
Public function ToString: string;
Public function length: SizeInt; reintroduce;
Public function contains(const s: string): boolean; inline;
Public function beginsWith(const s: string): boolean; inline;
Public function endsWith(const expectedEnd: string): boolean; inline;
Public function find(searched: pchar; searchedLength: SizeInt): pchar;
Public function find(const s: string): pchar;
Public function findLast(searched: pchar; searchedLength: SizeInt): pchar;
Public function findLast(const s: string): pchar;
Public function rightWithFind(const s: string): boolean;
Public function rightOfFind(const s: string): boolean;
Public function rightWithFindLast(const s: string): boolean;
Public function rightOfFindLast(const s: string): boolean;
Public function findLineBreak: pchar;
Public function rightWithLineBreak: boolean;
Public function rightOfLineBreak: boolean;
Public function leftOfFind(const s: string): boolean;
Public function leftWithFind(const s: string): boolean;
Public function leftOfFindLast(const s: string): boolean;
Public function leftWithFindLast(const s: string): boolean;
Public procedure trim(const trimCharacters: TCharSet = [#0..' ']);
Public procedure trimLeft(const trimCharacters: TCharSet = [#0..' ']);
Public procedure trimRight(const trimCharacters: TCharSet = [#0..' ']);
Public procedure trim(trimChar: char);
Public procedure trimLeft(trimChar: char);
Public procedure trimRight(trimChar: char);
Public function toIntDecimalTry(out v: Int64): boolean;
Public function toIntDecimalTry(out v: Int32): boolean;
Public function toUIntDecimalTry(out v: UInt64): boolean;
Public function toUIntDecimalTry(out v: UInt32): boolean;
Public function viewLeftWith(newLast: pchar): TPCharView; reintroduce;
Public function viewLeftOf(newEnd: pchar): TPCharView; reintroduce;
Public function viewRightWith(newStart: pchar): TPCharView; reintroduce;
Public function viewRightOf(newStartSkip: pchar): TPCharView; reintroduce;
Public function splitAt(out before: TPCharView; element: PChar; out behind: TPCharView): boolean;
Public function splitLeftOf(element: PChar; out behind: TPCharView): boolean;
Public function splitRightOf(out before: TPCharView; element: PChar): boolean;
Public function splitAtFind(out before: TPCharView; searched: pchar; searchedLength: SizeInt; out behind: TPCharView): boolean;
Public function splitLeftOfFind(searched: pchar; searchedLength: SizeInt; out behind: TPCharView): boolean;
Public function splitRightOfFind(out before: TPCharView; searched: pchar; searchedLength: SizeInt): boolean;
Public function splitAtFind(out before: TPCharView; const searched: string; out behind: TPCharView): boolean;
Public function splitLeftOfFind(const searched: string; out behind: TPCharView): boolean;
Public function splitRightOfFind(out before: TPCharView; const searched: string): boolean;

Description

Methods

Public procedure init(const buffer: string); overload;

Creates a view for a string.

Public procedure init(const buffer: TBytes); overload;

Creates a view for a byte array.

Public function ToString: string;

Converts the view to a string.

Public function length: SizeInt; reintroduce;

Length of the view in bytes.

Public function contains(const s: string): boolean; inline;

Tests whether the view contains a string.

Public function beginsWith(const s: string): boolean; inline;

Tests whether the view starts with a string.

Public function endsWith(const expectedEnd: string): boolean; inline;

Tests whether the view ends with a string.

Public function find(searched: pchar; searchedLength: SizeInt): pchar;
 
Public function find(const s: string): pchar;
 
Public function findLast(searched: pchar; searchedLength: SizeInt): pchar;
 
Public function findLast(const s: string): pchar;
 
Public function rightWithFind(const s: string): boolean;

Removes all characters before the first occurrence of a string s (keeps s itself). Keeps the view unchanged if it does not contain s.

Public function rightOfFind(const s: string): boolean;

Removes all characters before the first occurrence of a string s (removes s, too). Keeps the view unchanged if it does not contain s.

Public function rightWithFindLast(const s: string): boolean;

Removes all characters before the last occurrence of a string s (keeps s itself). Keeps the view unchanged if it does not contain s.

Public function rightOfFindLast(const s: string): boolean;

Removes all characters before the last occurrence of a string s (removes s, too). Keeps the view unchanged if it does not contain s.

Public function findLineBreak: pchar;

finds #13 or #10 (implicit #13#10)

Public function rightWithLineBreak: boolean;

Remves everything before the first line break (exclusive).

Public function rightOfLineBreak: boolean;

Remves everything before the first line break (inclusive).

Public function leftOfFind(const s: string): boolean;

Removes all characters after the first occurrence of a string (removes s, too). Keeps the view unchanged if it does not contain s.

Public function leftWithFind(const s: string): boolean;

Removes all characters after the first occurrence of a string (keeps s itself). Keeps the view unchanged if it does not contain s.

Public function leftOfFindLast(const s: string): boolean;

Removes all characters after the last occurrence of a string (removes s, too). Keeps the view unchanged if it does not contain s.

Public function leftWithFindLast(const s: string): boolean;

Removes all characters after the last occurrence of a string (keeps s itself). Keeps the view unchanged if it does not contain s.

Public procedure trim(const trimCharacters: TCharSet = [#0..' ']);

Removes all whitespace characters from the left and right side.

Public procedure trimLeft(const trimCharacters: TCharSet = [#0..' ']);

Removes all whitespace characters from the left side.

Public procedure trimRight(const trimCharacters: TCharSet = [#0..' ']);

Removes all whitespace characters from the right side.

Public procedure trim(trimChar: char);

Removes all whitespace characters trimChar from the left and right side.

Public procedure trimLeft(trimChar: char);

Removes all whitespace characters trimChar from the left side.

Public procedure trimRight(trimChar: char);

Removes all whitespace characters trimChar from the right side.

Public function toIntDecimalTry(out v: Int64): boolean;

Converts the view to a signed integer. Returns true if it matches -?[0-9]+ and does not overflow.

Public function toIntDecimalTry(out v: Int32): boolean;

Converts the view to a signed integer. Returns true if it matches -?[0-9]+ and does not overflow.

Public function toUIntDecimalTry(out v: UInt64): boolean;

Converts the view to an unsigned integer. Returns true if it matches [0-9]+ and does not overflow.

Public function toUIntDecimalTry(out v: UInt32): boolean;

Converts the view to an unsigned integer. Returns true if it matches [0-9]+ and does not overflow.

Public function viewLeftWith(newLast: pchar): TPCharView; reintroduce;

copy and leftWith.

Public function viewLeftOf(newEnd: pchar): TPCharView; reintroduce;

copy and leftOf.

Public function viewRightWith(newStart: pchar): TPCharView; reintroduce;

copy and rightWith.

Public function viewRightOf(newStartSkip: pchar): TPCharView; reintroduce;

copy and rightOf.

Public function splitAt(out before: TPCharView; element: PChar; out behind: TPCharView): boolean;

Splits the view at element. Everything before element is returned in before, everything behind it in behind. self is unchanged.

Public function splitLeftOf(element: PChar; out behind: TPCharView): boolean;

Splits the view at element. Everything before element is returned in self, everything behind it in behind.

Public function splitRightOf(out before: TPCharView; element: PChar): boolean;

Splits the view at element. Everything before element is returned in before, everything behind it in self.

Public function splitAtFind(out before: TPCharView; searched: pchar; searchedLength: SizeInt; out behind: TPCharView): boolean;

Splits the view at the first occurrence of searched. Everything before searched is returned in before, everything behind (searched+searchedLength) in behind. Self is unchanged.

Public function splitLeftOfFind(searched: pchar; searchedLength: SizeInt; out behind: TPCharView): boolean;

Splits the view at the first occurrence of searched. Everything before searched is returned in self, everything behind (searched+searchedLength) in behind.

Public function splitRightOfFind(out before: TPCharView; searched: pchar; searchedLength: SizeInt): boolean;

Splits the view at the first occurrence of searched. Everything before searched is returned in before, everything behind (searched+searchedLength) in self.

Public function splitAtFind(out before: TPCharView; const searched: string; out behind: TPCharView): boolean;

Splits the view at the first occurrence of searched. Everything before searched is returned in before, everything behind (searched+searchedLength) in behind. Self is unchanged.

Public function splitLeftOfFind(const searched: string; out behind: TPCharView): boolean;

Splits the view at the first occurrence of searched. Everything before searched is returned in self, everything behind searched in behind.

Public function splitRightOfFind(out before: TPCharView; const searched: string): boolean;

Splits the view at the first occurrence of searched. Everything before searched is returned in before, everything behind searched in self.


Generated by PasDoc 0.16.0.