string_builder_t Derived Type

type, public :: string_builder_t

Builds a string by appending pieces in amortized constant time.


Inherited by

type~~string_builder_t~~InheritedByGraph type~string_builder_t string_builder_t type~writer_t writer_t type~writer_t->type~string_builder_t buf

Components

Type Visibility Attributes Name Initial
character(len=:), private, allocatable :: buf

storage; only buf(1:n) is in use

integer, private :: n = 0

length of the string so far


Type-Bound Procedures

procedure, public :: append => builder_append

  • private pure subroutine builder_append(self, str)

    Append str to the string, doubling the storage when it is full.

    Arguments

    Type IntentOptional Attributes Name
    class(string_builder_t), intent(inout) :: self

    the builder

    character(len=*), intent(in) :: str

    the piece to append

procedure, public :: get => builder_get

  • private pure function builder_get(self) result(str)

    The string built so far.

    Arguments

    Type IntentOptional Attributes Name
    class(string_builder_t), intent(in) :: self

    the builder

    Return Value character(len=:), allocatable

procedure, public :: length => builder_length

  • private pure function builder_length(self) result(n)

    Length of the string built so far.

    Arguments

    Type IntentOptional Attributes Name
    class(string_builder_t), intent(in) :: self

    the builder

    Return Value integer

Source Code

type, public :: string_builder_t
    !! Builds a string by appending pieces in amortized constant time.
    private
    character(len=:), allocatable :: buf    !! storage; only `buf(1:n)` is in use
    integer :: n = 0                        !! length of the string so far
contains
    procedure :: append => builder_append
    procedure :: get => builder_get
    procedure :: length => builder_length
end type string_builder_t