public struct Array<T> {
public const init()
public init(elements: Collection<T>)
public init(size: Int64, item!: T)
public init(size: Int64, initElement: (Int64) -> T)
} 功能:仓颉数组类型,用来表示单一类型的元素构成的有序序列。
T 表示数组的元素类型,T 可以是任意类型。
public init(elements: Collection<T>)
功能:根据 Collection 实例创建数组,把 Collection 实例中所有元素存入数组。
参数:
public init(size: Int64, initElement: (Int64) -> T)
功能:创建指定长度的数组,其中元素根据初始化函数计算获取。
即:将 [0, size) 范围内的值分别传入初始化函数 initElement,执行得到数组对应下标的元素。
参数:
异常:
public init(size: Int64, item!: T)
功能:构造一个指定长度的数组,其中元素都用指定初始值进行初始化。
该构造函数不会拷贝 item, 如果 item 是一个引用类型,构造后数组的每一个元素都将指向相同的引用。
参数:
异常:
public func clone(range: Range<Int64>) : Array<T>
功能:克隆数组的指定区间。
参数:
返回值:
异常:
示例:
main() {
let arr = [0, 1, 2, 3, 4, 5]
let new = arr.clone(1..4)
println(new)
} 运行结果:
[1, 2, 3]
public func concat(other: Array<T>): Array<T>
功能:该函数将创建一个新的数组,数组内容是当前数组后面串联 other 指向的数组。
参数:
返回值:
示例:
main() {
let arr = [0, 1, 2, 3, 4, 5]
let new = arr.concat([6, 7, 8, 9, 10])
println(new)
} 运行结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
public func copyTo(dst: Array<T>, srcStart: Int64, dstStart: Int64, copyLen: Int64): Unit
功能:将当前数组中的一段数据拷贝到目标数组中。
参数:
异常:
示例:
main() {
let arr = [0, 1, 2, 3, 4, 5]
let new = [0, 0, 0, 0, 0, 0]
arr.copyTo(new, 2, 2, 4)
println(new)
} 运行结果:
[0, 0, 2, 3, 4, 5]
public func get(index: Int64): Option<T>
功能:获取数组中下标 index 对应的元素。
该函数结果将用 Option 封装,如果 index 越界,将返回 None。
也可以通过 [] 操作符获取数组指定下标的元素,该接口将在 index 越界时抛出异常。
参数:
返回值:
public func reverse(): Unit
功能:反转数组,将数组中元素的顺序进行反转。
示例:
main() {
let arr = [0, 1, 2, 3, 4, 5]
arr.reverse()
println(arr)
} 运行结果:
[5, 4, 3, 2, 1, 0]
public func set(index: Int64, element: T): Unit
功能:修改数组中下标 index 对应的值。
也可以通过 [] 操作符完成对指定下标元素的修改,这两个函数的行为一致。
参数:
异常:
public func slice(start: Int64, len: Int64): Array<T>
功能:获取数组切片。
切片不会对数组数据进行拷贝,是对原数据特定区间的引用。
参数:
返回值:
异常:
public operator func [](index: Int64): T
功能:获取数组下标 index 对应的值。
该函数中如果 index 越界,将抛出异常。
也可以通过 get 函数获取数组指定下标的元素,get 函数将在 index 越界时返回 None。
参数:
返回值:
异常:
public operator func [](index: Int64, value!: T): Unit
功能:修改数组中下标 index 对应的值。
参数:
异常:
public operator func [](range: Range<Int64>): Array<T>
功能:根据给定区间获取数组切片。
参数:
返回值:
异常:
示例:
main() {
let arr = [0, 1, 2, 3, 4, 5]
let slice = arr[1..4]
arr[3] = 10
println(slice)
} 运行结果:
[1, 2, 10]
public operator func [](range: Range<Int64>, value!: Array<T>): Unit
功能:用指定的数组对本数组一个连续范围的元素赋值。
range 表示的区见的长度和目标数组 value 的大小需相等。
参数:
异常:
示例:
main() {
let arr = [0, 1, 2, 3, 4, 5]
arr[1..3] = [10, 11]
println(arr)
} 运行结果:
[0, 10, 11, 3, 4, 5]
public operator func [](range: Range<Int64>, value!: T): Unit
功能:用指定的值对本数组一个连续范围的元素赋值。
参数:
异常:
示例:
main() {
let arr = [0, 1, 2, 3, 4, 5]
arr[1..3] = 10
println(arr)
} 运行结果:
[0, 10, 10, 3, 4, 5]
prop size
public prop size: Int64
功能:获取元素数量。
类型:Int64
func isEmpty()
public func isEmpty(): Bool
功能:判断数组是否为空。
返回值:
func iterator()
public func iterator(): Iterator<T>
功能:获取当前数组的迭代器,用于遍历数组。
返回值:
func toArray()
public func toArray(): Array<T>
功能:根据当前 Array 实例拷贝一个新的 Array 实例。
返回值:
extend<T> Array<T> <: Equatable<Array<T>> where T <: Equatable<T>
func contains(T)
public func contains(element: T): Bool
功能:查找当前数组是否包含指定元素。
参数:
返回值:
func indexOf(Array<T>)
public func indexOf(elements: Array<T>): Option<Int64>
功能:返回数组中子数组 elements 出现的第一个位置,如果数组中不包含此数组,返回 None。
当 T 的类型是 Int64 时,此函数的变长参数语法糖版本可能会和 public func indexOf(element: T, fromIndex: Int64): Option<Int64> 产生歧义,根据优先级,当参数数量是 2 个时,会优先调用 public func indexOf(element: T, fromIndex: Int64): Option<Int64>。
参数:
返回值:
func indexOf(Array<T>, Int64)
public func indexOf(elements: Array<T>, fromIndex: Int64): Option<Int64>
功能:返回数组中在 fromIndex之后,子数组elements 出现的第一个位置,未找到返回 None。
函数会对 fromIndex 范围进行检查,fromIndex 小于 0 时,将会从第 0 位开始搜索,当 fromIndex 大于等于本数组的大小时,结果为 None。
参数:
返回值:
func indexOf(T)
public func indexOf(element: T): Option<Int64>
功能:获取数组中 element 出现的第一个位置,如果数组中不包含此元素,返回 None。
参数:
返回值:
func indexOf(T, Int64)
public func indexOf(element: T, fromIndex: Int64): Option<Int64>
功能:返回数组中在 fromIndex之后, element 出现的第一个位置,未找到返回 None。
函数会从下标 fromIndex 开始查找,fromIndex 小于 0 时,将会从第 0 位开始搜索,当 fromIndex 大于等于本数组的大小时,结果为 None。
参数:
返回值:
func lastIndexOf(Array<T>)
public func lastIndexOf(elements: Array<T>): Option<Int64>
功能:返回数组中子数组 elements 出现的最后一个位置,如果数组中不存在此子数组,返回 None。
参数:
返回值:
func lastIndexOf(Array<T>, Int64)
public func lastIndexOf(elements: Array<T>, fromIndex: Int64): Option<Int64>
功能:从 fromIndex 开始向后搜索,返回数组中子数组 elements 出现的最后一个位置,如果数组中不存在此子数组,返回 None。
函数会对 fromIndex 范围进行检查,fromIndex 小于 0 时,将会从第 0 位开始搜索,当 fromIndex 大于等于本数组的大小时,结果为 None。
参数:
返回值:
func lastIndexOf(T)
public func lastIndexOf(element: T): Option<Int64>
功能:返回数组中 element 出现的最后一个位置,如果数组中不存在此元素,返回 None。
参数:
返回值:
func lastIndexOf(T, Int64)
public func lastIndexOf(element: T, fromIndex: Int64): Option<Int64>
功能:从 fromIndex 开始向后搜索,返回数组中 element 出现的最后一个位置,如果数组中不存在此元素,返回 None。
函数会对 fromIndex 范围进行检查,fromIndex 小于 0 时,将会从第 0 位开始搜索,当 fromIndex 大于等于本数组的大小时,结果为 None。
参数:
返回值:
func trimLeft(Array<T>)
public func trimLeft(prefix: Array<T>): Array<T>
功能:修剪当前数组,去除掉前缀为 prefix 的部分,并且返回当前数组的切片。
参数:
返回值:
func trimRight(Array<T>)
public func trimRight(suffix: Array<T>): Array<T>
功能:修剪当前数组,去除掉后缀为 suffix 的部分,并且返回当前数组的切片。
参数:
返回值:
operator func !=(Array<T>)
public const operator func !=(that: Array<T>): Bool
功能:判断当前实例与指定 Array<T> 实例是否不等。
参数:
返回值:
operator func ==(Array<T>)
public const operator func ==(that: Array<T>): Bool
功能:判断当前实例与指定 Array<T> 实例是否相等。
两个 Array<T> 相等指的是其中的每个元素都相等。
参数:
返回值:
func toString()
public func toString(): String
功能:将数组转换为可输出的字符串。
字符串形如 "[1, 2, 3, 4, 5]"
返回值:
public struct CPointerHandle<T> where T <: CType {
public let array: Array<T>
public let pointer: CPointer<T>
public init()
public init(ptr: CPointer<T>, arr: Array<T>)
} public init(ptr: CPointer<T>, arr: Array<T>)
功能:通过传入的 CPointer 和 Array 初始化一个 CPointerHandle。
参数:
public struct CPointerResource<T> <: Resource where T <: CType {
public var value: CPointer<T>
} 功能:该结构体表示 CPointer 对应的资源管理类型,其实例可以通过 CPointer 的成员函数 asResource 获取。
public struct CStringResource <: Resource {
public let value: CString
} 功能:该结构体表示 CString 对应的资源管理类型,其实例可以通过 CString 的成员函数 asResource 获取。
public struct DefaultHasher <: Hasher {
public init(res!: Int64 = 0)
} 功能:该结构体提供了默认哈希算法实现。
可以使用一系列 write 函数传入不同数据类型实例,并计算他们的组合哈希值。
public init(res!: Int64 = 0)
功能:构造函数,创建一个 DefaultHasher。
参数:
public mut func write(value: Bool): Unit
功能:通过该函数把想要哈希运算的 Bool 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Float16): Unit
功能:通过该函数把想要哈希运算的 Float16 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Float32): Unit
功能:通过该函数把想要哈希运算的 Float32 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Float64): Unit
功能:通过该函数把想要哈希运算的 Float64 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Int16): Unit
功能:通过该函数把想要哈希运算的 Int16 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Int32): Unit
功能:通过该函数把想要哈希运算的 Int32 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Int64): Unit
功能:通过该函数把想要哈希运算的 Int64 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Int8): Unit
功能:通过该函数把想要哈希运算的 Int8 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: Rune): Unit
功能:通过该函数把想要哈希运算的 Rune 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: String): Unit
功能:通过该函数把想要哈希运算的 String 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: UInt16): Unit
功能:通过该函数把想要哈希运算的 UInt16 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: UInt32): Unit
功能:通过该函数把想要哈希运算的 UInt32 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: UInt64): Unit
功能:通过该函数把想要哈希运算的 UInt64 值传入,然后进行哈希组合运算。
参数:
public mut func write(value: UInt8): Unit
功能:通过该函数把想要哈希运算的 UInt8 值传入,然后进行哈希组合运算。
参数:
public static unsafe func free<T>(p: CPointer<T>): Unit where T <: CType
功能:释放指针 p 指向的堆内存。
参数:
public static unsafe func free(cstr: CString): Unit
功能:释放 C 风格字符串。
参数:
public static unsafe func mallocCString(str: String): CString
功能:通过 String 申请与之字符内容相同的 C 风格字符串。
构造的 C 风格字符串将已 '\0' 结束。
参数:
返回值:
public static func malloc<T>(count!: Int64 = 1): CPointer<T> where T <: CType
功能:在堆中申请指定个数的 T 实例,并返回其起始指针。
参数:
返回值:
public struct Range<T> <: Iterable<T> where T <: Countable<T> & Comparable<T> & Equatable<T> {
public let end: T
public let hasEnd: Bool
public let hasStart: Bool
public let isClosed: Bool
public let start: T
public let step: Int64
public const init(start: T, end: T, step: Int64, hasStart: Bool, hasEnd: Bool, isClosed: Bool)
} 功能:该类是区间类型,用于表示一个拥有固定范围和步长的 T 的序列,要求 T 是可数的,有序的。
区间类型有对应的字面量表示,其格式为:
public const init(start: T, end: T, step: Int64, hasStart: Bool, hasEnd: Bool, isClosed: Bool)
功能:使用该构造函数创建 Range 序列。
参数:
异常:
public const func isEmpty(): Bool
功能:判断该区间是否为空。
返回值:
extend<T> Range<T> <: Equatable<Range<T>> where T <: Countable<T> & Comparable<T> & Equatable<T>
operator func !=(Range<T>)
public operator func !=(that: Range<T>): Bool
功能:判断两个 Range 是否不相等。
参数:
返回值:
operator func ==(Range<T>)
public operator func ==(that: Range<T>): Bool
功能:判断两个 Range 实例是否相等。
两个 Range 实例相等指的是它们表示同一个区间,即 start、end、step、isClosed 值相等。
参数:
返回值:
extend<T> Range<T> <: Hashable where T <: Hashable & Countable<T> & Comparable<T> & Equatable<T>
func hashCode()
public func hashCode(): Int64
功能:获取哈希值,该值为 start、end、step、isClosed 的组合哈希运算结果。
返回值:
public struct String <: Collection<Byte> & Equatable<String> & Comparable<String> & Hashable & ToString {
public static let empty: String
public const init()
public init(value: Array<Rune>)
public init(value: Collection<Rune>)
} 功能:该结构体表示仓颉字符串,提供了构造、查找、拼接等一系列字符串操作。
String 类型仅支持 UTF-8 编码。
public init(value: Array<Rune>)
功能:根据字符数组构造一个字符串,字符串内容为数组中的所有字符。
参数:
public init(value: Collection<Rune>)
功能:据字符集合构造一个字符串,字符串内容为集合中的所有字符。
参数:
public static func fromUtf8(utf8Data: Array<UInt8>): String
功能:根据 UTF-8 编码的字节数组构造一个字符串。
参数:
返回值:
异常:
public static unsafe func fromUtf8Unchecked(utf8Data: Array<UInt8>): String
功能:根据字节数组构造一个字符串。
相较于 fromUtf8 函数,它并没有针对于字节数组进行 UTF-8 相关规则的检查,所以它所构建的字符串并不一定保证是合法的,甚至出现非预期的异常,如果不是某些场景下的性能考虑,请优先使用安全的 fromUtf8 函数。
参数:
返回值:
public static func join(strArray: Array<String>, delimiter!: String = String.empty): String
功能:连接字符串列表中的所有字符串,以指定分隔符分隔。
参数:
返回值:
public func compare(str: String): Ordering
功能:按字典序比较当前字符串和参数指定的字符串。
参数:
返回值:
异常:
public func contains(str: String): Bool
功能:判断原字符串中是否包含字符串 str。
参数:
返回值:
public func count(str: String): Int64
功能:返回子字符串 str 在原字符串中出现的次数。
参数:
返回值:
public func endsWith(suffix: String): Bool
功能:判断原字符串是否以 suffix 字符串为后缀结尾。
参数:
返回值:
public unsafe func getRaw(): CPointerHandle<UInt8>
功能:获取当前 String 的原始指针,用于和C语言交互,使用完后需要 releaseRaw 函数释放该指针。
getRaw 与 releaseRaw 之间仅可包含简单的 foreign C 函数调用等逻辑,不构造例如 CString 等的仓颉对象,否则可能造成不可预知的错误。
返回值:
public func indexOf(b: Byte): Option<Int64>
功能:获取指定字节 b 第一次出现的在原字符串内的索引。
参数:
返回值:
public func indexOf(b: Byte, fromIndex: Int64): Option<Int64>
功能:从原字符串指定索引开始搜索,获取指定字节第一次出现的在原字符串内的索引。
参数:
返回值:
public func indexOf(str: String): Option<Int64>
功能:返回指定字符串 str 在原字符串中第一次出现的起始索引。
参数:
返回值:
public func indexOf(str: String, fromIndex: Int64): Option<Int64>
功能:从原字符串 fromIndex 索引开始搜索,获取指定字符串 str 第一次出现的在原字符串的起始索引。
参数:
返回值:
public func isAscii(): Bool
功能:判断字符串是否是一个 Ascii 字符串,如果字符串为空或没有 Ascii 以外的字符,则返回 true。
返回值:
public func isAsciiBlank(): Bool
功能:判断字符串是否为空或者字符串中的所有 Rune 都是 ascii 码的空白字符(包括:0x09、0x10、0x11、0x12、0x13、0x20)。
返回值:
public func iterator(): Iterator<Byte>
功能:获取字符串的 UTF-8 编码字节迭代器,可用于支持 for-in 循环。
返回值:
public func lastIndexOf(b: Byte): Option<Int64>
功能:返回指定字节 b 最后一次出现的在原字符串内的索引。
参数:
返回值:
public func lastIndexOf(b: Byte, fromIndex: Int64): Option<Int64>
功能:从原字符串 fromIndex 索引开始搜索,返回指定 UTF-8 编码字节 b 最后一次出现的在原字符串内的索引。
参数:
返回值:
public func lastIndexOf(str: String): Option<Int64>
功能:返回指定字符串 str 最后一次出现的在原字符串的起始索引。
参数:
返回值:
public func lastIndexOf(str: String, fromIndex: Int64): Option<Int64>
功能:从原字符串指定索引开始搜索,获取指定字符串 str 最后一次出现的在原字符串的起始索引。
参数:
返回值:
public func lazySplit(str: String, removeEmpty!: Bool = false): Iterator<String>
功能:对原字符串按照字符串 str 分隔符分割,该函数不立即对字符串进行分割,而是返回迭代器,使用迭代器进行遍历时再实际执行分隔操作。
当 str 未出现在原字符串中,返回大小为 1 的字符串迭代器,唯一的元素为原字符串。
参数:
返回值:
public func lazySplit(str: String, maxSplits: Int64, removeEmpty!: Bool = false): Iterator<String>
功能:对原字符串按照字符串 str 分隔符分割,该函数不立即对字符串进行分割,而是返回迭代器,使用迭代器进行遍历时再实际执行分隔操作。
参数:
返回值:
public func lines(): Iterator<String>
功能:获取字符串的行迭代器,每行都由换行符进行分隔,换行符是 \n \r \r\n 之一,结果中每行不包括换行符。
返回值:
public func padLeft(totalWidth: Int64, padding!: String = " "): String
功能:按指定长度右对齐原字符串,如果原字符串长度小于指定长度,在其左侧添加指定字符串。
当指定长度小于字符串长度时,返回字符串本身,不会发生截断;当指定长度大于字符串长度时,在左侧添加 padding 字符串,当 padding 长度大于 1 时,返回字符串的长度可能大于指定长度。
参数:
返回值:
异常:
public func padRight(totalWidth: Int64, padding!: String = " "): String
功能:按指定长度左对齐原字符串,如果原字符串长度小于指定长度,在其右侧添加指定字符串。
当指定长度小于字符串长度时,返回字符串本身,不会发生截断;当指定长度大于字符串长度时,在右侧添加 padding 字符串,当 padding 长度大于 1 时,返回字符串的长度可能大于指定长度。
参数:
返回值:
异常:
public unsafe func rawData(): Array<Byte>
功能:获取字符串的 UTF-8 编码的原始字节数组。
用户不应该对获取的数组进行修改,这将破坏字符串的不可变性。
返回值:
public unsafe func releaseRaw(cp: CPointerHandle<UInt8>): Unit
功能:释放 getRaw 函数获取的指针。
参数:
public func replace(old: String, new: String): String
功能:使用新字符串替换原字符串中旧字符串。
参数:
返回值:
异常:
public func runes(): Iterator<Rune>
功能:获取字符串的 Rune 迭代器。
返回值:
异常:
public func split(str: String, removeEmpty!: Bool = false): Array<String>
功能:对原字符串按照字符串 str 分隔符分割,指定是否删除空串。
当 str 未出现在原字符串中,返回长度为 1 的字符串数组,唯一的元素为原字符串。
参数:
返回值:
public func split(str: String, maxSplits: Int64, removeEmpty!: Bool = false): Array<String>
功能:对原字符串按照字符串 str 分隔符分割,指定最多分隔子串数,以及是否删除空串。
参数:
返回值:
public func startsWith(prefix: String): Bool
功能:判断原字符串是否以 prefix 字符串为前缀。
参数:
返回值:
public func toAsciiLower(): String
功能:将该字符串中所有 Ascii 大写字母转化为 Ascii 小写字母。
返回值:
public func toAsciiTitle(): String
功能:将该字符串标题化。
该函数只转换 Ascii 英文字符,当该英文字符是字符串中第一个字符或者该字符的前一个字符不是英文字符,则该字符大写,其他英文字符小写。
返回值:
public func toAsciiUpper(): String
功能:将该字符串中所有 Ascii 小写字母转化为 Ascii 大写字母。
返回值:
public func toRuneArray(): Array<Rune>
功能:获取字符串的 Rune 数组。如果原字符串为空字符串,则返回空数组。
返回值:
public func trimAscii(): String
功能:去除原字符串开头结尾以 whitespace 字符组成的子字符串。
whitespace 的 unicode 码点范围为 [0009, 000D] 和 [0020]。
返回值:
public func trimAsciiLeft(): String
功能:去除原字符串开头以 whitespace 字符组成的子字符串。
返回值:
public func trimAsciiRight(): String
功能:去除原字符串结尾以 whitespace 字符组成的子字符串。
返回值:
public func trimLeft(prefix: String): String
功能:去除字符串的 prefix 前缀。
参数:
返回值:
public func trimRight(suffix: String): String
功能:去除字符串的 suffix 后缀。
参数:
返回值:
public func tryGet(index: Int64): Option<Byte>
功能:返回字符串下标 index 对应的 UTF-8 编码字节值。
参数:
返回值:
public const operator func !=(right: String): Bool
功能:判断两个字符串是否不相等。
参数:
返回值:
public const operator func *(count: Int64): String
功能:原字符串重复 count 次。
参数:
返回值:
public const operator func +(right: String): String
功能:两个字符串相加,将 right 字符串拼接在原字符串的末尾。
参数:
返回值:
public const operator func <(right: String): Bool
功能:判断两个字符串大小。
参数:
返回值:
public const operator func <=(right: String): Bool
功能:判断两个字符串大小。
参数:
返回值:
public const operator func ==(right: String): Bool
功能:判断两个字符串是否相等。
参数:
返回值:
public const operator func >(right: String): Bool
功能:判断两个字符串大小。
参数:
返回值:
public const operator func >=(right: String): Bool
功能:判断两个字符串大小。
参数:
返回值:
public const operator func [](index: Int64): Byte
功能:返回指定索引 index 处的 UTF-8 编码字节。
参数:
返回值:
异常:
public const operator func [](range: Range<Int64>): String
功能:根据给定区间获取当前字符串的切片。
参数:
返回值:
异常: