Overview
strings
provides utilities for efficiently processing large strings.
If you got here looking for methods available on the string
struct, those
methods are found in the builtin
module.
Aliases
reuse_as_plain_u8_array allows using the Builder instance as a plain []u8 return value.
It is useful, when you have accumulated data in the builder, that you want to pass/access as []u8 later, without copying or freeing the buffer.
NB: you should NOT use the string builder instance after calling this method.
Use only the return value after calling this method.
write_ptr writes len
bytes provided byteptr to the accumulated buffer
write_rune appends a single rune to the accumulated buffer
write_runes appends all the given runes to the accumulated buffer
write_u8 appends a single data
byte to the accumulated buffer
fn (mut b &Builder) write_byte(data byte)
write_byte appends a single data
byte to the accumulated buffer
write implements the Writer interface
drain_builder writes all of the other
builder content, then re-initialises other
, so that the other
strings builder is ready to receive new content.
byte_at returns a byte, located at a given index i
.
Note: it can panic, if there are not enough bytes in the strings builder yet.
write appends the string s
to the buffer
go_back discards the last n
bytes from the buffer
cut_last cuts the last n
bytes from the buffer and returns them
cut_to cuts the string after pos
and returns it.
if pos
is superior to builder length, returns an empty string and cancel further operations
go_back_to resets the buffer to the given position pos
Note: pos should be < than the existing buffer length.
writeln appends the string s
, and then a newline character.
last_n(5) returns 'world' buf == 'hello world'
after(6) returns 'world' buf == 'hello world'
str returns a copy of all of the accumulated buffer content.
Note: after a call to b.str(), the builder b will be empty, and could be used again.
The returned string owns its own separate copy of the accumulated data that was in the string builder, before the .str() call.
ensure_cap ensures that the buffer has enough space for at least n
bytes by growing the buffer if necessary
Constants
This section is empty.
Sum types
This section is empty.
Functions
implementation of Sørensen–Dice coefficient.
find the similarity between two strings.
returns coefficient between 0.0 (not similar) and 1.0 (exact match).
find_between_pair_rune returns the string found between the pair of marks defined by start
and end
.
As opposed to the find_between
, all_after*
, all_before*
methods defined on the string
type, this function can extract content between nested marks in input
.
If start
and end
marks are nested in input
, the characters between the outermost mark pair is returned. It is expected that start
and end
marks are balanced, meaning that the amount of start
marks equal the amount of end
marks in the input
. An empty string is returned otherwise.
Using two identical marks as start
and end
results in undefined output behavior.
find_between_pair_rune is inbetween the fastest and slowest in the find_between_pair_* family of functions.
Example:
assert strings.find_between_pair_rune('(V) (NOT V)',`(`,`)`) == 'V'
Example:
assert strings.find_between_pair_rune('s {X{Y}} s',`{`,`}`) == 'X{Y}'
find_between_pair_string returns the string found between the pair of marks defined by start
and end
.
As opposed to the find_between
, all_after*
, all_before*
methods defined on the string
type, this function can extract content between nested marks in input
.
If start
and end
marks are nested in input
, the characters between the outermost mark pair is returned. It is expected that start
and end
marks are balanced, meaning that the amount of start
marks equal the amount of end
marks in the input
. An empty string is returned otherwise.
Using two identical marks as start
and end
results in undefined output behavior.
find_between_pair_string is the slowest in the find_between_pair_* function family.
Example:
assert strings.find_between_pair_string('/*V*/ /*NOT V*/','/*','*/') == 'V'
Example:
assert strings.find_between_pair_string('s {{X{{Y}}}} s','{{','}}') == 'X{{Y}}'
find_between_pair_byte returns the string found between the pair of marks defined by start
and end
.
As opposed to the find_between
, all_after*
, all_before*
methods defined on the string
type, this function can extract content between nested marks in input
.
If start
and end
marks are nested in input
, the characters between the outermost mark pair is returned. It is expected that start
and end
marks are balanced, meaning that the amount of start
marks equal the amount of end
marks in the input
. An empty string is returned otherwise.
Using two identical marks as start
and end
results in undefined output behavior.
find_between_pair_byte is the fastest in the find_between_pair_* family of functions.
Example:
assert strings.find_between_pair_u8('(V) (NOT V)',`(`,`)`) == 'V'
Example:
assert strings.find_between_pair_u8('s {X{Y}} s',`{`,`}`) == 'X{Y}'
#-js
use levenshtein distance algorithm to calculate the distance between between two strings (lower is closer)
use levenshtein distance algorithm to calculate how similar two strings are as a percentage (higher is closer)
new_builder returns a new string builder, with an initial capacity of initial_size
strings.repeat - fill a string with n
repetitions of the character c
strings.repeat_string - gives you n
repetitions of the substring s
Note: strings.repeat, that repeats a single byte, is between 2x and 24x faster than strings.repeat_string called for a 1 char string.
split_capital returns an array containing the contents of s
split by capital letters.
Example:
assert strings.split_capital('XYZ') == ['X', 'Y', 'Z']
Example:
assert strings.split_capital('XYStar') == ['X', 'Y', 'Star']
Structs
This section is empty.
Interfaces
This section is empty.
Enums
This section is empty.
strings.Builder is used to efficiently append many strings to a large dynamically growing buffer, then use the resulting large string. Using a string builder is much better for performance/memory usage than doing constantly string concatenation.