XU XUv0.8.8
EN

API

#bytes

import: #bytes

counts: 51 函数, 31 方法, 4 类型, 0 常量, 0 方法集

字节串工具,形状与 strings 对称。Buffer 可读写。查找同样走 Horspool。

struct Buffer

XuAPI
pub struct Buffer {
    pub data string
    pub off int
}

用途

公开结构体 `Buffer`。

参数

data string
数据
off int
偏移

buffer

XuAPI
pub fn buffer() Buffer

用途

缓冲。

参数

无

返回

Buffer — 该类型的值

buffer_from

XuAPI
pub fn buffer_from(s string) Buffer

用途

缓冲从。

参数

s string
源字符串(string)

返回

Buffer — 该类型的值

(&mut Buffer) write

XuAPI
pub fn (b &mut Buffer) write(data &string) int

用途

`&mut Buffer` 上的方法:写入数据,返回写入的字节数。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
data &string
数据(借用,调用方仍持有;类型 &string)

返回

int — 写入的字节数

(&mut Buffer) write_byte

XuAPI
pub fn (b &mut Buffer) write_byte(c int) int

用途

`&mut Buffer` 上的方法:写入一个字节。失败时 throw。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
c int
字节或字符码(int)

返回

int — 写入的字节数

(&mut Buffer) read_n

XuAPI
pub fn (b &mut Buffer) read_n(n int) string

用途

`&mut Buffer` 上的方法:最多读 n 字节;到末尾可返回更短或空串。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
n int
数量、长度或上限(int)

返回

string — 结果字符串

(&Buffer) length

XuAPI
pub fn (b &Buffer) length() int

用途

`&Buffer` 上的方法:返回当前长度。

参数

b &Buffer
接收者 &Buffer(借用)

返回

int — 该类型的值

(&Buffer) text

XuAPI
pub fn (b &Buffer) text() string

用途

`&Buffer` 上的方法:返回当前文本。

参数

b &Buffer
接收者 &Buffer(借用)

返回

string — 结果字符串

(&Buffer) bytes

XuAPI
pub fn (b &Buffer) bytes() string

用途

`&Buffer` 上的方法:返回已缓冲的字节串。

参数

b &Buffer
接收者 &Buffer(借用)

返回

string — 结果字符串

(&mut Buffer) reset

XuAPI
pub fn (b &mut Buffer) reset()

用途

`&mut Buffer` 上的方法:清空内部状态,可再写入或读取。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)

(&mut Buffer) truncate

XuAPI
pub fn (b &mut Buffer) truncate(n int) int

用途

`&mut Buffer` 上的方法:截断。失败时 throw。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
n int
数量、长度或上限(int)

返回

int — 该类型的值

view_of

XuAPI
pub fn view_of(s &string) strings.ByteView

用途

viewof。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)

返回

strings.ByteView — 该类型的值

view_slice

XuAPI
pub fn view_slice(s &string, off int, n int) strings.ByteView

用途

view切片。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
off int
偏移(int)
n int
数量、长度或上限(int)

返回

strings.ByteView — 该类型的值

equal

XuAPI
pub fn equal(a &string, b &string) bool

用途

判断两值是否相等。

参数

a &string
值(借用,调用方仍持有;类型 &string)
b &string
b(借用,调用方仍持有;类型 &string)

返回

bool — 是否成立

compare

XuAPI
pub fn compare(a &string, b &string) int

用途

字典序比较:小于 -1、相等 0、大于 1。

参数

a &string
左操作数(借用,调用方仍持有;类型 &string)
b &string
右操作数(借用,调用方仍持有;类型 &string)

返回

int — 小于 -1、相等 0、大于 1

has_prefix

XuAPI
pub fn has_prefix(s &string, prefix string) bool

用途

判断是否具有给定前缀。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
prefix string
前缀(string)

返回

bool — 是否成立

has_suffix

XuAPI
pub fn has_suffix(s &string, suffix string) bool

用途

判断是否具有给定后缀。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
suffix string
后缀(string)

返回

bool — 是否成立

index_byte

XuAPI
pub fn index_byte(s &string, c int) int

用途

返回字节 c 首次出现的下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
c int
字节或字符码(int)

返回

int — 下标;未找到为 -1

last_index_byte

XuAPI
pub fn last_index_byte(s &string, c int) int

用途

返回字节 c 最后一次出现的下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
c int
字节或字符码(int)

返回

int — 下标;未找到为 -1

last_index

XuAPI
pub fn last_index(s &string, sub string) int

用途

返回最后一次出现的下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sub string
子串(string)

返回

int — 下标;未找到为 -1

count

XuAPI
pub fn count(s &string, sub string) int

用途

统计非重叠出现次数。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sub string
子串(string)

返回

int — 该类型的值

replace

XuAPI
pub fn replace(s &string, old string, new string, n int) string

用途

替换子串,次数由 n 限制。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
old string
旧值 / 被替换片段(string)
new string
新值 / 替换成的片段(string)
n int
数量、长度或上限(int)

返回

string — 结果字符串

replace_all

XuAPI
pub fn replace_all(s &string, old string, new string) string

用途

替换全部出现的子串。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
old string
旧值 / 被替换片段(string)
new string
新值 / 替换成的片段(string)

返回

string — 结果字符串

contains

XuAPI
pub fn contains(s &string, sub string) bool

用途

判断是否包含给定片段。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sub string
子串(string)

返回

bool — 是否成立

index

XuAPI
pub fn index(s &string, sub string) int

用途

返回首次出现的下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sub string
子串(string)

返回

int — 下标;未找到为 -1

trim_left

XuAPI
pub fn trim_left(s &string, cutset string) string

用途

去掉左端出现在 cutset 中的字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
cutset string
要剥除的字节集合(string)

返回

string — 结果字符串

trim_right

XuAPI
pub fn trim_right(s &string, cutset string) string

用途

去掉右端出现在 cutset 中的字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
cutset string
要剥除的字节集合(string)

返回

string — 结果字符串

trim

XuAPI
pub fn trim(s &string, cutset string) string

用途

去掉两端出现在 cutset 中的字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
cutset string
要剥除的字节集合(string)

返回

string — 结果字符串

trim_space

XuAPI
pub fn trim_space(s &string) string

用途

去掉两端 ASCII 空白,返回新串。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)

返回

string — 结果字符串

trim_prefix

XuAPI
pub fn trim_prefix(s &string, pre string) string

用途

若有前缀则去掉该前缀,否则原样返回。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
pre string
前缀(string)

返回

string — 结果字符串

trim_suffix

XuAPI
pub fn trim_suffix(s &string, suf string) string

用途

若有后缀则去掉该后缀,否则原样返回。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
suf string
后缀(string)

返回

string — 结果字符串

join

XuAPI
pub fn join(parts []string, sep string) string

用途

用分隔符拼接各段。

参数

parts []string
各段([]string)
sep string
分隔符(string)

返回

string — 结果字符串

split

XuAPI
pub fn split(s &string, sep string) []string

用途

按分隔符切开。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sep string
分隔符(string)

返回

[]string — 切片结果

fields

XuAPI
pub fn fields(s &string) []string

用途

按空白段切开,丢掉空白本身。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)

返回

[]string — 切片结果

index_func

XuAPI
pub fn index_func(s &string, f fn(int) bool) int

用途

返回使回调为真的首个字节下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) bool
字节回调:参数为字节值 0..255,返回是否命中

返回

int — 下标;未找到为 -1

last_index_func

XuAPI
pub fn last_index_func(s &string, f fn(int) bool) int

用途

返回使回调为真的最后字节下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) bool
字节回调:参数为字节值 0..255,返回是否命中

返回

int — 下标;未找到为 -1

map_bytes

XuAPI
pub fn map_bytes(s &string, f fn(int) int) string

用途

对每个字节调用 f,用返回值重建字符串。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) int
字节回调:参数为字节值 0..255,返回是否命中

返回

string — 结果字符串

fields_func

XuAPI
pub fn fields_func(s &string, f fn(int) bool) []string

用途

按回调为真的字节切开。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) bool
字节回调:参数为字节值 0..255,返回是否命中

返回

[]string — 切片结果

clone

XuAPI
pub fn clone(s &string) string

用途

拷贝一份独立的 string。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)

返回

string — 结果字符串

contains_func

XuAPI
pub fn contains_func(s &string, f fn(int) bool) bool

用途

判断是否存在使回调为真的字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) bool
字节回调:参数为字节值 0..255,返回是否命中

返回

bool — 是否成立

index_any

XuAPI
pub fn index_any(s &string, chars string) int

用途

返回 cutset 中任一字节首次出现的下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
chars string
字节集合(cutset)(string)

返回

int — 下标;未找到为 -1

last_index_any

XuAPI
pub fn last_index_any(s &string, chars string) int

用途

返回 cutset 中字节最后一次出现的下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
chars string
字节集合(cutset)(string)

返回

int — 下标;未找到为 -1

contains_any

XuAPI
pub fn contains_any(s &string, chars string) bool

用途

判断是否包含 cutset 中任一字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
chars string
字节集合(cutset)(string)

返回

bool — 是否成立

index_rune

XuAPI
pub fn index_rune(s &string, r int) int

用途

返回 rune 首次出现的下标;未找到为 -1。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
r int
rune(Unicode 码点)(int)

返回

int — 下标;未找到为 -1

contains_rune

XuAPI
pub fn contains_rune(s &string, r int) bool

用途

判断是否包含给定 rune。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
r int
rune(Unicode 码点)(int)

返回

bool — 是否成立

split_n

XuAPI
pub fn split_n(s &string, sep string, n int) []string

用途

按分隔符切开,最多 n 段。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sep string
分隔符(string)
n int
数量、长度或上限(int)

返回

[]string — 切片结果

split_after

XuAPI
pub fn split_after(s &string, sep string) []string

用途

按分隔符切开,分隔符留在各段末尾。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sep string
分隔符(string)

返回

[]string — 切片结果

split_after_n

XuAPI
pub fn split_after_n(s &string, sep string, n int) []string

用途

按分隔符切开并保留分隔符,最多 n 段。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sep string
分隔符(string)
n int
数量、长度或上限(int)

返回

[]string — 切片结果

trim_left_func

XuAPI
pub fn trim_left_func(s &string, f fn(int) bool) string

用途

按回调去掉左端字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) bool
字节回调:参数为字节值 0..255,返回是否命中

返回

string — 结果字符串

trim_right_func

XuAPI
pub fn trim_right_func(s &string, f fn(int) bool) string

用途

按回调去掉右端字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) bool
字节回调:参数为字节值 0..255,返回是否命中

返回

string — 结果字符串

trim_func

XuAPI
pub fn trim_func(s &string, f fn(int) bool) string

用途

按回调去掉两端字节。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
f fn(int) bool
字节回调:参数为字节值 0..255,返回是否命中

返回

string — 结果字符串

to_valid_utf8

XuAPI
pub fn to_valid_utf8(s &string, replacement string) string

用途

把非法 UTF-8 序列换成 replacement。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
replacement string
非法序列的替换串(string)

返回

string — 结果字符串

repeat

XuAPI
pub fn repeat(s &string, n int) string

用途

把内容重复 n 次;n<=0 得空串。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
n int
数量、长度或上限(int)

返回

string — 结果字符串

equal_fold

XuAPI
pub fn equal_fold(a &string, b &string) bool

用途

ASCII 大小写不敏感比较。

参数

a &string
值(借用,调用方仍持有;类型 &string)
b &string
b(借用,调用方仍持有;类型 &string)

返回

bool — 是否成立

to_lower

XuAPI
pub fn to_lower(s &string) string

用途

ASCII 字母转小写。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)

返回

string — 结果字符串

to_upper

XuAPI
pub fn to_upper(s &string) string

用途

ASCII 字母转大写。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)

返回

string — 结果字符串

(&mut Buffer) grow

XuAPI
pub fn (b &mut Buffer) grow(n int)

用途

`&mut Buffer` 上的方法:保证至少还能再写入 n 字节。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
n int
数量、长度或上限(int)

(&mut Buffer) write_string

XuAPI
pub fn (b &mut Buffer) write_string(s &string) int

用途

`&mut Buffer` 上的方法:写入字符串,返回写入的字节数。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
s &string
源字符串(借用,调用方仍持有;类型 &string)

返回

int — 写入的字节数

(&mut Buffer) write_rune

XuAPI
pub fn (b &mut Buffer) write_rune(r int) int

用途

`&mut Buffer` 上的方法:写入一个 rune 的 UTF-8 编码。失败时 throw。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
r int
rune(Unicode 码点)(int)

返回

int — 写入的字节数

(&mut Buffer) read_byte

XuAPI
pub fn (b &mut Buffer) read_byte() int

用途

`&mut Buffer` 上的方法:读一个字节;失败时 throw。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)

返回

int — 该类型的值

(&mut Buffer) unread_byte

XuAPI
pub fn (b &mut Buffer) unread_byte() int

用途

`&mut Buffer` 上的方法:把最近读出的一字节退回。失败时 throw。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)

返回

int — 该类型的值

(&mut Buffer) read_rune

XuAPI
pub fn (b &mut Buffer) read_rune() RuneRead

用途

`&mut Buffer` 上的方法:读一个 UTF-8 rune。失败时 throw。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)

返回

RuneRead — 该类型的值

(&mut Buffer) unread_rune

XuAPI
pub fn (b &mut Buffer) unread_rune() int

用途

`&mut Buffer` 上的方法:把最近读出的 rune 退回。失败时 throw。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)

返回

int — 该类型的值

(&Buffer) cap

XuAPI
pub fn (b &Buffer) cap() int

用途

`&Buffer` 上的方法:返回容量。

参数

b &Buffer
接收者 &Buffer(借用)

返回

int — 该类型的值

(&mut Buffer) next

XuAPI
pub fn (b &mut Buffer) next(n int) string

用途

`&mut Buffer` 上的方法:取出下一项;没有则按本类型约定结束。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
n int
数量、长度或上限(int)

返回

string — 结果字符串

struct Cut

XuAPI
pub struct Cut {
    pub before string
    pub after string
    pub found bool
}

用途

公开结构体 `Cut`。

参数

before string
字段(string)
after string
字段(string)
found bool
字段(bool)

cut

XuAPI
pub fn cut(s &string, sep string) Cut

用途

在首个 sep 处切成前后两段。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
sep string
分隔符(string)

返回

Cut — 该类型的值

cut_prefix

XuAPI
pub fn cut_prefix(s &string, pre string) Cut

用途

若有前缀则切掉。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
pre string
前缀(string)

返回

Cut — 该类型的值

cut_suffix

XuAPI
pub fn cut_suffix(s &string, suf string) Cut

用途

若有后缀则切掉。

参数

s &string
源字符串(借用,调用方仍持有;类型 &string)
suf string
后缀(string)

返回

Cut — 该类型的值

(&mut Buffer) read_bytes

XuAPI
pub fn (b &mut Buffer) read_bytes(delim int) string

用途

`&mut Buffer` 上的方法:读到 delim(含 delim)。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
delim int
delim(int)

返回

string — 结果字符串

(&mut Reader) read_result

XuAPI
pub fn (r &mut Reader) read_result(n int) io.ReadResult

用途

`&mut Reader` 上的方法:读取结果。

参数

r &mut Reader
接收者 &mut Reader(可变借用)
n int
数量、长度或上限(int)

返回

io.ReadResult — 该类型的值

(&mut Buffer) read_result

XuAPI
pub fn (b &mut Buffer) read_result(n int) io.ReadResult

用途

`&mut Buffer` 上的方法:读取结果。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
n int
数量、长度或上限(int)

返回

io.ReadResult — 该类型的值

(&mut Buffer) write_result

XuAPI
pub fn (b &mut Buffer) write_result(data string) io.WriteResult

用途

`&mut Buffer` 上的方法:写入结果。

参数

b &mut Buffer
接收者 &mut Buffer(可变借用)
data string
数据(string)

返回

io.WriteResult — 该类型的值

struct Reader

XuAPI
pub struct Reader {

}

用途

公开结构体 `Reader`。

参数

无

struct RuneRead

XuAPI
pub struct RuneRead {
    pub ch int
    pub size int
}

用途

公开结构体 `RuneRead`。

参数

ch int
字符码
size int
字段(int)

new_reader

XuAPI
pub fn new_reader(s string) Reader

用途

构造 reader。

参数

s string
源字符串(string)

返回

Reader — 该类型的值

(&Reader) size

XuAPI
pub fn (r &Reader) size() int

用途

`&Reader` 上的方法:返回尺寸。

参数

r &Reader
接收者 &Reader(借用)

返回

int — 该类型的值

(&Reader) length

XuAPI
pub fn (r &Reader) length() int

用途

`&Reader` 上的方法:返回当前长度。

参数

r &Reader
接收者 &Reader(借用)

返回

int — 该类型的值

(&mut Reader) reset

XuAPI
pub fn (r &mut Reader) reset(s string)

用途

`&mut Reader` 上的方法:清空内部状态,可再写入或读取。

参数

r &mut Reader
接收者 &mut Reader(可变借用)
s string
源字符串(string)

(&mut Reader) read_n

XuAPI
pub fn (r &mut Reader) read_n(n int) string

用途

`&mut Reader` 上的方法:最多读 n 字节;到末尾可返回更短或空串。

参数

r &mut Reader
接收者 &mut Reader(可变借用)
n int
数量、长度或上限(int)

返回

string — 结果字符串

(&mut Reader) read_byte

XuAPI
pub fn (r &mut Reader) read_byte() int

用途

`&mut Reader` 上的方法:读一个字节;失败时 throw。

参数

r &mut Reader
接收者 &mut Reader(可变借用)

返回

int — 该类型的值

(&mut Reader) unread_byte

XuAPI
pub fn (r &mut Reader) unread_byte() int

用途

`&mut Reader` 上的方法:把最近读出的一字节退回。失败时 throw。

参数

r &mut Reader
接收者 &mut Reader(可变借用)

返回

int — 该类型的值

(&mut Reader) read_rune

XuAPI
pub fn (r &mut Reader) read_rune() RuneRead

用途

`&mut Reader` 上的方法:读一个 UTF-8 rune。失败时 throw。

参数

r &mut Reader
接收者 &mut Reader(可变借用)

返回

RuneRead — 该类型的值

(&mut Reader) unread_rune

XuAPI
pub fn (r &mut Reader) unread_rune() int

用途

`&mut Reader` 上的方法:把最近读出的 rune 退回。失败时 throw。

参数

r &mut Reader
接收者 &mut Reader(可变借用)

返回

int — 该类型的值

(&mut Reader) seek

XuAPI
pub fn (r &mut Reader) seek(offset int, whence int) int

用途

`&mut Reader` 上的方法:按 whence 把读位置移到 offset。whence 0/1/2 分别为绝对、相对、从末尾。失败时 throw。

参数

r &mut Reader
接收者 &mut Reader(可变借用)
offset int
偏移(int)
whence int
定位基准:0 起点 / 1 当前 / 2 末尾(int)

返回

int — 该类型的值

(&mut Reader) read_at

XuAPI
pub fn (r &mut Reader) read_at(n int, off int) string

用途

`&mut Reader` 上的方法:从偏移 off 起最多读 n 字节。失败时 throw。

参数

r &mut Reader
接收者 &mut Reader(可变借用)
n int
数量、长度或上限(int)
off int
偏移(int)

返回

string — 结果字符串