stringlist_type

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_headlist_tail 是此类型的 2 个特殊实例,分别表示字符串列表的头部和尾部。索引独立于它所使用的字符串列表(或stringlist_type),因此,一个索引可以在同一个程序中与多个字符串列表一起使用。

状态

实验性

fidx/bidx

描述

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

insert_at

描述

在索引idx处插入字符串string,以便在插入后新添加的元素出现在索引idx处。在超过length + 1的索引处插入元素会在list_tail处插入元素,同样,在非正索引处插入会在list_head处插入元素。

语法

call stringlist_type % insert_at (idx, string)

状态

实验性。

纯子例程。

参数

示例

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

get

描述

返回当前在字符串列表中的索引idx处存在的字符串。如果索引idx超出范围,则返回空字符串。

语法

res = stringlist_type % get (idx)

状态

实验性。

纯函数。

参数

结果值

结果是类型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

len

描述

返回当前存在于字符串列表中的元素数量。

语法

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

clear

描述

从字符串列表中删除所有元素。

语法

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.)

参数

结果值

结果是默认的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.)

参数

结果值

结果是默认的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(//)

参数

结果值

结果是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