stdlib_stringlist_type
模块(一维字符串列表)stdlib_stringlist_type
模块提供 2 种派生类型来处理字符串列表。stringlist_type
派生类型表示一维可变长度字符串列表(也称为一维字符串列表),它与 Fortran 内置字符兼容,stringlist_index_type
派生类型表示一个索引,用于访问、修改字符串列表的元素,将元素插入字符串列表等等。
stringlist_type
派生类型stringlist_type
派生类型表示一维字符串列表(也称为一维字符串列表)。
列表的内部表示依赖于实现,对模块用户不可见。
注意:字符串列表是一个抽象概念,通过派生类型stringlist_type
来表达。
实验性
stringlist_index_type
派生类型派生类型stringlist_index_type
的实例表示前向索引或后向索引。内部表示依赖于实现,对模块用户不可见。
list_head
和 list_tail
是此类型的 2 个特殊实例,分别表示字符串列表的头部和尾部。索引独立于它所使用的字符串列表(或stringlist_type
),因此,一个索引可以在同一个程序中与多个字符串列表一起使用。
实验性
fidx
:返回一个表示正向索引idx
的实例。
bidx
:返回一个表示反向索引idx
的实例。
对于 fidx:res =
fidx (idx)
对于 bidx:res =
bidx (idx)
实验性。
纯函数。
idx
:应为integer
类型。此参数为intent(in)
。结果类型为stringlist_index_type
。
program example_fidx_bidx
use stdlib_stringlist_type, only: stringlist_index_type, fidx, bidx
implicit none
type(stringlist_index_type) :: index
index = fidx(1)
! forward index 1
index = bidx(3)
! backward index 3
end program example_fidx_bidx
stringlist_type
(或字符串列表)的构造函数未给出参数:初始化一个空字符串列表(一个字符串列表,其中不包含任何元素)。
带有参数:初始化一个等效于输入数组array
的字符串列表,即一个包含输入数组array
中所有元素的字符串列表,顺序相同。
res =
stringlist_type ([array])
实验性
纯函数。
array
:应为character
标量数组或string_type数组。此参数为intent(in)
且optional
。结果是类型stringlist_type
的实例。
program example_constructor
use stdlib_stringlist_type, only: stringlist_type
use stdlib_string_type, only: string_type
implicit none
type(stringlist_type) :: stringlist
stringlist = stringlist_type()
! stringlist <-- { } (empty stringlist)
stringlist = stringlist_type(["#1", "#2", "#3"])
! stringlist <-- {"#1", "#2", "#3"}
stringlist = stringlist_type([string_type("#1"), string_type("#2")])
! stringlist <-- {"#1", "#2"}
end program example_constructor
在索引idx
处插入字符串string
,以便在插入后新添加的元素出现在索引idx
处。在超过length + 1
的索引处插入元素会在list_tail
处插入元素,同样,在非正索引处插入会在list_head
处插入元素。
call
stringlist_type %
insert_at (idx, string)
实验性。
纯子例程。
idx
:stringlist_index_type。此参数为intent(in)
。
string
:字符标量或string_type。此参数为intent(in)
。
program example_insert_at
use stdlib_stringlist_type, only: stringlist_type, stringlist_index_type, fidx, bidx
use stdlib_string_type, only: string_type
implicit none
type(stringlist_type) :: stringlist
type(stringlist_index_type) :: index
index = fidx(1)
call stringlist%insert_at(index, "Element No. one")
! stringlist <-- {"Element No. one"}
index = bidx(1)
call stringlist%insert_at(index, string_type("Element No. two"))
! stringlist <-- {"Element No. one", "Element No. two"}
call stringlist%insert_at(fidx(2), string_type("Element No. three"))
! stringlist <-- {"Element No. one", "Element No. three", "Element No. two"}
call stringlist%insert_at(bidx(1), "Element No. four")
! stringlist <-- {"Element No. one", "Element No. three", "Element No. two", "Element No. four"}
end program example_insert_at
返回当前在字符串列表中的索引idx
处存在的字符串。如果索引idx
超出范围,则返回空字符串。
res =
stringlist_type %
get (idx)
实验性。
纯函数。
idx
:stringlist_index_type。此参数为intent(in)
。结果是类型string_type
的字符串。
program example_get
use stdlib_stringlist_type, only: stringlist_type, fidx, bidx
use stdlib_string_type, only: string_type
implicit none
type(stringlist_type) :: stringlist
type(string_type) :: output
!> inserting 4 elements to the stringlist
call stringlist%insert_at(fidx(1), "Element No. one")
call stringlist%insert_at(fidx(1), "Element No. two")
call stringlist%insert_at(fidx(1), "Element No. three")
call stringlist%insert_at(fidx(1), "Element No. four")
! stringlist <-- {"Element No. four", "Element No. three", "Element No. two", "Element No. one"}
output = stringlist%get(fidx(1))
! output <-- "Element No. four"
output = stringlist%get(bidx(1))
! output <-- "Element No. one"
!> accessing out of bounds index
output = stringlist%get(bidx(5))
! output <-- ""
output = stringlist%get(fidx(0))
! output <-- ""
end program example_get
返回当前存在于字符串列表中的元素数量。
res =
stringlist_type %
len ()
实验性。
纯函数。
无参数。
结果类型为integer
。
program example_len
use stdlib_stringlist_type, only: stringlist_type, bidx
implicit none
type(stringlist_type) :: stringlist
integer :: output
output = stringlist%len()
! output <-- 0
!> inserting 2 elements to the stringlist
call stringlist%insert_at(bidx(1), "Element No. one")
call stringlist%insert_at(bidx(1), "Element No. two")
! stringlist <-- {"Element No. one", "Element No. two"}
print'(a)', stringlist%len()
! 2
end program example_len
从字符串列表中删除所有元素。
call
stringlist_type %
clear ()
实验性。
纯子例程。
无参数。
program example_clear
use stdlib_stringlist_type, only: stringlist_type, fidx
implicit none
type(stringlist_type) :: stringlist
!> inserting 2 elements to the stringlist
call stringlist%insert_at(fidx(1), "Element No. one")
call stringlist%insert_at(fidx(1), "Element No. two")
! stringlist <-- {"Element No. two", "Element No. one"}
call stringlist%clear()
! stringlist <-- { } (empty stringlist)
!> inserting 1 element to the stringlist
call stringlist%insert_at(fidx(1), "Element No. one")
! stringlist <-- {"Element No. one"}
end program example_clear
将左侧(lhs)与右侧(rhs)进行相等比较。
res = lhs == rhs
res = lhs .eq. rhs
实验性。
纯函数,operator(==)
和 operator(.eq.)
。
lhs
:应为character
标量数组或string_type数组,或stringlist_type。此参数为intent(in)
。
rhs
:应为character
标量数组或string_type数组,或stringlist_type。此参数为intent(in)
。
结果是默认的logical
标量值。
program example_equality_operator
use stdlib_stringlist_type, only: stringlist_type, fidx, list_head, operator(==)
use stdlib_string_type, only: string_type
implicit none
type(stringlist_type) :: stringlist
type(string_type), allocatable :: stringarray(:)
logical :: res
!> inserting 4 elements to the stringlist
call stringlist%insert_at(fidx(1), "#1")
call stringlist%insert_at(list_head, "#2")
call stringlist%insert_at(fidx(1), "#3")
call stringlist%insert_at(list_head, "#4")
! stringlist <-- {"#4", "#3", "#2", "#1"}
!> creating an array of 4 string_type elements
stringarray = [string_type("#4"), string_type("#3"), string_type("#2"), string_type("#1")]
res = (stringarray == stringlist)
! res <-- .true.
res = (stringlist == ["#4", "#3", "#2", "#1"])
! res <-- .true.
print'(a)', stringlist == ["#4", "#3", "#1"]
! .false.
end program example_equality_operator
将左侧(lhs)与右侧(rhs)进行不相等比较。
res = lhs /= rhs
res = lhs .ne. rhs
实验性。
纯函数,operator(/=)
和 operator(.ne.)
。
lhs
:应为character
标量数组或string_type数组,或stringlist_type。此参数为intent(in)
。
rhs
:应为character
标量数组或string_type数组,或stringlist_type。此参数为intent(in)
。
结果是默认的logical
标量值。
program example_inequality_operator
use stdlib_stringlist_type, only: stringlist_type, bidx, list_tail, operator(/=)
use stdlib_string_type, only: string_type
implicit none
type(stringlist_type) :: stringlist
type(string_type), allocatable :: stringarray(:)
logical :: res
!> inserting 4 elements to the stringlist
call stringlist%insert_at(bidx(1), "#1")
call stringlist%insert_at(list_tail, "#2")
call stringlist%insert_at(bidx(1), "#3")
call stringlist%insert_at(list_tail, "#4")
! stringlist <-- {"#1", "#2", "#3", "#4"}
!> creating an array of 4 string_type elements
stringarray = [string_type("#1"), string_type("#2"), string_type("#3"), string_type("#4")]
res = (stringarray /= stringlist)
! res <-- .false.
res = (stringlist /= ["#111", "#222", "#333", "#444"])
! res <-- .true.
print'(a)', stringlist /= ["#4", "#3", "#1"]
! .true.
end program example_inequality_operator
返回左侧(lhs)和右侧(rhs)的连接输出。
res = lhs // rhs
实验性。
纯函数,operator(//)
。
lhs
:应为character
标量或string_type,或character
标量数组或string_type数组,或stringlist_type。此参数为intent(in)
。
rhs
:应为character
标量或string_type,或character
标量数组或string_type数组,或stringlist_type。此参数为intent(in)
。
结果是stringlist_type的实例。
program example_concatenate_operator
use stdlib_stringlist_type, only: stringlist_type, operator(//)
use stdlib_string_type, only: string_type
implicit none
type(stringlist_type) :: first_stringlist, second_stringlist
type(string_type), allocatable :: stringarray(:)
first_stringlist = first_stringlist//"Element No. one"
! first_stringlist <-- {"Element No. one"}
second_stringlist = string_type("Element No. two")//first_stringlist
! second_stringlist <-- {Element No. two, "Element No. one"}
!> Creating an array of 2 string_type elements
stringarray = [string_type("Element No. three"), string_type("Element No. four")]
second_stringlist = first_stringlist//stringarray
! second_stringlist <-- {"Element No. one", "Element No. three", "Element No. four"}
second_stringlist = ["#1", "#2"]//second_stringlist
! second_stringlist <-- {"#1", "#2", "Element No. one", "Element No. three", "Element No. four"}
first_stringlist = first_stringlist//second_stringlist
! first_stringlist <-- {"Element No. one", "#1", "#2", "Element No. one", "Element No. three", "Element No. four"}
end program example_concatenate_operator