API
#bytes
Xu 0.8.8 API
import: #bytes
counts: 51 fns, 31 methods, 4 types, 0 consts, 0 ifaces
Byte-string helpers, parallel to strings. Buffer is readable and writable. Search uses Horspool.
struct Buffer
pub struct Buffer {
pub data string
pub off int
}
Purpose
Owning byte helpers aligned with `bytes` (Buffer, Equal, Compare, IndexByte, Count, Replace, HasPrefix). Unread views are copied strings, never a shared []byte header. Do not wrap Buffer in Result. Text cutsets, callbacks and simple case mappings share `#strings` Unicode semantics. IndexByte and MapBytes retain their explicit byte contracts.
Parameters
data string- data
off int- offset
buffer
Purpose
buffer; returns Buffer.
Parameters
none
Returns
Buffer — a value of this type
buffer_from
pub fn buffer_from(s string) Buffer
Purpose
Buffer from.
Parameters
s string- source string (string)
Returns
Buffer — a value of this type
(&mut Buffer) write
pub fn (b &mut Buffer) write(data &string) int
Purpose
Method on `&mut Buffer`: write data and return the byte count.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
data &string- data (borrowed; caller keeps it; type &string)
Returns
int — bytes written
(&mut Buffer) write_byte
pub fn (b &mut Buffer) write_byte(c int) int
Purpose
Method on `&mut Buffer`: write one byte. Throws on failure.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
c int- byte or character code (int)
Returns
int — bytes written
(&mut Buffer) read_n
pub fn (b &mut Buffer) read_n(n int) string
Purpose
Method on `&mut Buffer`: read at most n bytes; may return shorter or empty at end.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
n int- count, length, or limit (int)
Returns
string — result string
(&Buffer) length
pub fn (b &Buffer) length() int
Purpose
Method on `&Buffer`: return the current length.
Parameters
b &Buffer- receiver &Buffer (borrow)
Returns
int — a value of this type
(&Buffer) text
pub fn (b &Buffer) text() string
Purpose
Method on `&Buffer`: return the current text.
Parameters
b &Buffer- receiver &Buffer (borrow)
Returns
string — result string
(&Buffer) bytes
pub fn (b &Buffer) bytes() string
Purpose
Method on `&Buffer`: return the buffered bytes.
Parameters
b &Buffer- receiver &Buffer (borrow)
Returns
string — result string
(&mut Buffer) reset
pub fn (b &mut Buffer) reset()
Purpose
Method on `&mut Buffer`: clear internal state so the value can be reused.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
(&mut Buffer) truncate
pub fn (b &mut Buffer) truncate(n int) int
Purpose
Call `truncate` on `&mut Buffer`. Throws on failure.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
n int- count, length, or limit (int)
Returns
int — a value of this type
view_of
pub fn view_of(s &string) strings.ByteView
Purpose
View of.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
Returns
strings.ByteView — a value of this type
view_slice
pub fn view_slice(s &string, off int, n int) strings.ByteView
Purpose
View slice.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
off int- offset (int)
n int- count, length, or limit (int)
Returns
strings.ByteView — a value of this type
equal
pub fn equal(a &string, b &string) bool
Purpose
Report whether the two values are equal.
Parameters
a &string- value (borrowed; caller keeps it; type &string)
b &string- b (borrowed; caller keeps it; type &string)
Returns
bool — true or false
compare
pub fn compare(a &string, b &string) int
Purpose
Lexicographic compare: -1, 0, or 1.
Parameters
a &string- left operand (borrowed; caller keeps it; type &string)
b &string- right operand (borrowed; caller keeps it; type &string)
Returns
int — -1 if less, 0 if equal, 1 if greater
has_prefix
pub fn has_prefix(s &string, prefix string) bool
Purpose
Report whether the value has the given prefix.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
prefix string- prefix (string)
Returns
bool — true or false
has_suffix
pub fn has_suffix(s &string, suffix string) bool
Purpose
Report whether the value has the given suffix.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
suffix string- suffix (string)
Returns
bool — true or false
index_byte
pub fn index_byte(s &string, c int) int
Purpose
Index of the first byte c; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
c int- byte or character code (int)
Returns
int — index; -1 if missing
last_index_byte
pub fn last_index_byte(s &string, c int) int
Purpose
Index of the last byte c; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
c int- byte or character code (int)
Returns
int — index; -1 if missing
last_index
pub fn last_index(s &string, sub string) int
Purpose
Index of the last occurrence; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sub string- substring (string)
Returns
int — index; -1 if missing
count
pub fn count(s &string, sub string) int
Purpose
Count non-overlapping occurrences.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sub string- substring (string)
Returns
int — a value of this type
replace
pub fn replace(s &string, old string, new string, n int) string
Purpose
Replace a substring, limited by n.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
old string- old value / piece to replace (string)
new string- new value / replacement (string)
n int- count, length, or limit (int)
Returns
string — result string
replace_all
pub fn replace_all(s &string, old string, new string) string
Purpose
Replace every occurrence of the substring.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
old string- old value / piece to replace (string)
new string- new value / replacement (string)
Returns
string — result string
contains
pub fn contains(s &string, sub string) bool
Purpose
Report whether the input contains the given piece.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sub string- substring (string)
Returns
bool — true or false
index
pub fn index(s &string, sub string) int
Purpose
Index of the first occurrence; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sub string- substring (string)
Returns
int — index; -1 if missing
trim_left
pub fn trim_left(s &string, cutset string) string
Purpose
Strip leading bytes that appear in the cutset.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
cutset string- bytes to trim (string)
Returns
string — result string
trim_right
pub fn trim_right(s &string, cutset string) string
Purpose
Strip trailing bytes that appear in the cutset.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
cutset string- bytes to trim (string)
Returns
string — result string
trim
pub fn trim(s &string, cutset string) string
Purpose
Strip leading and trailing bytes that appear in the cutset.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
cutset string- bytes to trim (string)
Returns
string — result string
trim_space
pub fn trim_space(s &string) string
Purpose
Strip leading and trailing ASCII whitespace.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
Returns
string — result string
trim_prefix
pub fn trim_prefix(s &string, pre string) string
Purpose
Remove prefix if present; otherwise return the input.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
pre string- prefix (string)
Returns
string — result string
trim_suffix
pub fn trim_suffix(s &string, suf string) string
Purpose
Remove suffix if present; otherwise return the input.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
suf string- suffix (string)
Returns
string — result string
join
pub fn join(parts []string, sep string) string
Purpose
Join pieces with a separator.
Parameters
parts []string- parts ([]string)
sep string- separator (string)
Returns
string — result string
split
pub fn split(s &string, sep string) []string
Purpose
Split on the separator.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sep string- separator (string)
Returns
[]string — slice result
fields
pub fn fields(s &string) []string
Purpose
Split on whitespace runs and drop the whitespace.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
Returns
[]string — slice result
index_func
pub fn index_func(s &string, f fn(int) bool) int
Purpose
Text callbacks receive runes, aligned with strings. MapBytes remains byte-based.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) bool- byte callback: argument is 0..255, return whether it matches
Returns
int — index; -1 if missing
last_index_func
pub fn last_index_func(s &string, f fn(int) bool) int
Purpose
Last index where the callback is true; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) bool- byte callback: argument is 0..255, return whether it matches
Returns
int — index; -1 if missing
map_bytes
pub fn map_bytes(s &string, f fn(int) int) string
Purpose
Map each byte through f and rebuild the string.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) int- byte callback: argument is 0..255, return whether it matches
Returns
string — result string
fields_func
pub fn fields_func(s &string, f fn(int) bool) []string
Purpose
Split on bytes for which the callback is true.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) bool- byte callback: argument is 0..255, return whether it matches
Returns
[]string — slice result
clone
pub fn clone(s &string) string
Purpose
Copy into a new independent string.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
Returns
string — result string
contains_func
pub fn contains_func(s &string, f fn(int) bool) bool
Purpose
Report whether any byte satisfies the callback.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) bool- byte callback: argument is 0..255, return whether it matches
Returns
bool — true or false
index_any
pub fn index_any(s &string, chars string) int
Purpose
Index of the first byte that appears in the cutset; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
chars string- byte cutset (string)
Returns
int — index; -1 if missing
last_index_any
pub fn last_index_any(s &string, chars string) int
Purpose
Last index of any cutset byte; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
chars string- byte cutset (string)
Returns
int — index; -1 if missing
contains_any
pub fn contains_any(s &string, chars string) bool
Purpose
Report whether any byte from the cutset occurs.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
chars string- byte cutset (string)
Returns
bool — true or false
index_rune
pub fn index_rune(s &string, r int) int
Purpose
Index of the first rune; -1 if missing.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
r int- rune (Unicode code point) (int)
Returns
int — index; -1 if missing
contains_rune
pub fn contains_rune(s &string, r int) bool
Purpose
Report whether the rune occurs.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
r int- rune (Unicode code point) (int)
Returns
bool — true or false
split_n
pub fn split_n(s &string, sep string, n int) []string
Purpose
Split on the separator, at most n parts.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sep string- separator (string)
n int- count, length, or limit (int)
Returns
[]string — slice result
split_after
pub fn split_after(s &string, sep string) []string
Purpose
Split after the separator, keeping it on each piece.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sep string- separator (string)
Returns
[]string — slice result
split_after_n
pub fn split_after_n(s &string, sep string, n int) []string
Purpose
Split after the separator, at most n parts.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sep string- separator (string)
n int- count, length, or limit (int)
Returns
[]string — slice result
trim_left_func
pub fn trim_left_func(s &string, f fn(int) bool) string
Purpose
Trim leading bytes using the callback.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) bool- byte callback: argument is 0..255, return whether it matches
Returns
string — result string
trim_right_func
pub fn trim_right_func(s &string, f fn(int) bool) string
Purpose
Trim trailing bytes using the callback.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) bool- byte callback: argument is 0..255, return whether it matches
Returns
string — result string
trim_func
pub fn trim_func(s &string, f fn(int) bool) string
Purpose
Trim bytes on both ends using the callback.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
f fn(int) bool- byte callback: argument is 0..255, return whether it matches
Returns
string — result string
to_valid_utf8
pub fn to_valid_utf8(s &string, replacement string) string
Purpose
Replace invalid UTF-8 sequences with replacement.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
replacement string- replacement for invalid sequences (string)
Returns
string — result string
repeat
pub fn repeat(s &string, n int) string
Purpose
Repeat the content n times; n<=0 yields empty.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
n int- count, length, or limit (int)
Returns
string — result string
equal_fold
pub fn equal_fold(a &string, b &string) bool
Purpose
ASCII case-insensitive equality.
Parameters
a &string- value (borrowed; caller keeps it; type &string)
b &string- b (borrowed; caller keeps it; type &string)
Returns
bool — true or false
to_lower
pub fn to_lower(s &string) string
Purpose
Map ASCII letters to lowercase.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
Returns
string — result string
to_upper
pub fn to_upper(s &string) string
Purpose
Map ASCII letters to uppercase.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
Returns
string — result string
(&mut Buffer) grow
pub fn (b &mut Buffer) grow(n int)
Purpose
Method on `&mut Buffer`: ensure room for at least n more bytes.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
n int- count, length, or limit (int)
(&mut Buffer) write_string
pub fn (b &mut Buffer) write_string(s &string) int
Purpose
Method on `&mut Buffer`: write a string and return the byte count.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
s &string- source string (borrowed; caller keeps it; type &string)
Returns
int — bytes written
(&mut Buffer) write_rune
pub fn (b &mut Buffer) write_rune(r int) int
Purpose
Method on `&mut Buffer`: write a rune as UTF-8. Throws on failure.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
r int- rune (Unicode code point) (int)
Returns
int — bytes written
(&mut Buffer) read_byte
pub fn (b &mut Buffer) read_byte() int
Purpose
Method on `&mut Buffer`: read one byte; throws on failure. Throws on failure.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
Returns
int — a value of this type
(&mut Buffer) unread_byte
pub fn (b &mut Buffer) unread_byte() int
Purpose
Method on `&mut Buffer`: unread the last byte. Throws on failure.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
Returns
int — a value of this type
(&mut Buffer) read_rune
pub fn (b &mut Buffer) read_rune() RuneRead
Purpose
Method on `&mut Buffer`: read one UTF-8 rune. Throws on failure.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
Returns
RuneRead — a value of this type
(&mut Buffer) unread_rune
pub fn (b &mut Buffer) unread_rune() int
Purpose
Method on `&mut Buffer`: unread the last rune. Throws on failure.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
Returns
int — a value of this type
(&Buffer) cap
pub fn (b &Buffer) cap() int
Purpose
Method on `&Buffer`: return the capacity.
Parameters
b &Buffer- receiver &Buffer (borrow)
Returns
int — a value of this type
(&mut Buffer) next
pub fn (b &mut Buffer) next(n int) string
Purpose
Method on `&mut Buffer`: take the next item, or end according to this type.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
n int- count, length, or limit (int)
Returns
string — result string
struct Cut
pub struct Cut {
pub before string
pub after string
pub found bool
}
Purpose
Public struct `Cut`.
Parameters
before string- field (string)
after string- field (string)
found bool- field (bool)
cut
pub fn cut(s &string, sep string) Cut
Purpose
Cut into before/after at the first sep.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
sep string- separator (string)
Returns
Cut — a value of this type
cut_prefix
pub fn cut_prefix(s &string, pre string) Cut
Purpose
Cut off a prefix when present.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
pre string- prefix (string)
Returns
Cut — a value of this type
cut_suffix
pub fn cut_suffix(s &string, suf string) Cut
Purpose
Cut off a suffix when present.
Parameters
s &string- source string (borrowed; caller keeps it; type &string)
suf string- suffix (string)
Returns
Cut — a value of this type
(&mut Buffer) read_bytes
pub fn (b &mut Buffer) read_bytes(delim int) string
Purpose
Method on `&mut Buffer`: read through delim (inclusive).
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
delim int- delim (int)
Returns
string — result string
(&mut Reader) read_result
pub fn (r &mut Reader) read_result(n int) io.ReadResult
Purpose
Method on `&mut Reader`: read result.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
n int- count, length, or limit (int)
Returns
io.ReadResult — a value of this type
(&mut Buffer) read_result
pub fn (b &mut Buffer) read_result(n int) io.ReadResult
Purpose
Method on `&mut Buffer`: read result.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
n int- count, length, or limit (int)
Returns
io.ReadResult — a value of this type
(&mut Buffer) write_result
pub fn (b &mut Buffer) write_result(data string) io.WriteResult
Purpose
Method on `&mut Buffer`: write result.
Parameters
b &mut Buffer- receiver &mut Buffer (exclusive borrow)
data string- data (string)
Returns
io.WriteResult — a value of this type
struct Reader
Purpose
`bytes.NewReader` subset. Same shape as `#strings` Reader: owning string payload (no shared []byte). read_n is empty at EOF; read_byte read_rune throw "EOF". seek whence 0/1/2.
Parameters
none
struct RuneRead
pub struct RuneRead {
pub ch int
pub size int
}
Purpose
Public struct `RuneRead`.
Parameters
ch int- character code
size int- field (int)
new_reader
pub fn new_reader(s string) Reader
Purpose
Construct reader.
Parameters
s string- source string (string)
Returns
Reader — a value of this type
(&Reader) size
pub fn (r &Reader) size() int
Purpose
Method on `&Reader`: return the size.
Parameters
r &Reader- receiver &Reader (borrow)
Returns
int — a value of this type
(&Reader) length
pub fn (r &Reader) length() int
Purpose
Method on `&Reader`: return the current length.
Parameters
r &Reader- receiver &Reader (borrow)
Returns
int — a value of this type
(&mut Reader) reset
pub fn (r &mut Reader) reset(s string)
Purpose
Method on `&mut Reader`: clear internal state so the value can be reused.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
s string- source string (string)
(&mut Reader) read_n
pub fn (r &mut Reader) read_n(n int) string
Purpose
Method on `&mut Reader`: read at most n bytes; may return shorter or empty at end.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
n int- count, length, or limit (int)
Returns
string — result string
(&mut Reader) read_byte
pub fn (r &mut Reader) read_byte() int
Purpose
Method on `&mut Reader`: read one byte; throws on failure. Throws on failure.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
Returns
int — a value of this type
(&mut Reader) unread_byte
pub fn (r &mut Reader) unread_byte() int
Purpose
Method on `&mut Reader`: unread the last byte. Throws on failure.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
Returns
int — a value of this type
(&mut Reader) read_rune
pub fn (r &mut Reader) read_rune() RuneRead
Purpose
Method on `&mut Reader`: read one UTF-8 rune. Throws on failure.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
Returns
RuneRead — a value of this type
(&mut Reader) unread_rune
pub fn (r &mut Reader) unread_rune() int
Purpose
Method on `&mut Reader`: unread the last rune. Throws on failure.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
Returns
int — a value of this type
(&mut Reader) seek
pub fn (r &mut Reader) seek(offset int, whence int) int
Purpose
Method on `&mut Reader`: move the read position. whence 0/1/2 = absolute / relative / from end. Throws on failure.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
offset int- offset (int)
whence int- seek base: 0 start / 1 current / 2 end (int)
Returns
int — a value of this type
(&mut Reader) read_at
pub fn (r &mut Reader) read_at(n int, off int) string
Purpose
Method on `&mut Reader`: read at most n bytes starting at offset off. Throws on failure.
Parameters
r &mut Reader- receiver &mut Reader (exclusive borrow)
n int- count, length, or limit (int)
off int- offset (int)
Returns
string — result string