fn dice_coefficient(s1 string, s2 string) f32
implementation of Sørensen–Dice coefficient. find the similarity between two strings. returns coefficient between 0.0 (not similar) and 1.0 (exact match).
fn levenshtein_distance(a string, b string) int
#-js use levenshtein distance algorithm to calculate the distance between between two strings (lower is closer)
fn levenshtein_distance_percentage(a string, b string) f32
use levenshtein distance algorithm to calculate how similar two strings are as a percentage (higher is closer)
fn new_builder(initial_size int) Builder
new_builder returns a new string builder, with an initial capacity of initial_size
fn repeat(c byte, n int) string
strings.repeat - fill a string with n
repetitions of the character c
fn repeat_string(s string, n int) string
strings.repeat_string - gives you n
repetitions of the substring s
NB: strings.repeat, that repeats a single byte, is between 2x and 24x faster than strings.repeat_string called for a 1 char string.
struct Builder {
pub mut:
buf []byte
len int
initial_size int = 1
}
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.
fn (mut b Builder) write_bytes(bytes &byte, len int)
write_bytes appends bytes
to the accumulated buffer
fn (mut b Builder) write_ptr(ptr &byte, len int)
write_ptr writes len
bytes provided byteptr to the accumulated buffer
fn (mut b Builder) write_b(data byte)
write_b appends a single data
byte to the accumulated buffer
fn (mut b Builder) write(data []byte) ?int
write implements the Writer interface
fn (mut b Builder) write_string(s string)
write appends the string s
to the buffer
fn (mut b Builder) go_back(n int)
go_back discards the last n
bytes from the buffer
fn (mut b Builder) cut_last(n int) string
cut_last cuts the last n
bytes from the buffer and returns them
fn (mut b Builder) go_back_to(pos int)
go_back_to resets the buffer to the given position pos
NB: pos should be < than the existing buffer length.
fn (mut b Builder) writeln(s string)
writeln appends the string s
, and then a newline character.
fn (b &Builder) last_n(n int) string
buf == 'hello world' last_n(5) returns 'world'
fn (b &Builder) after(n int) string
buf == 'hello world' after(6) returns 'world'
fn (mut b Builder) str() string
str returns a copy of all of the accumulated buffer content. NB: after a call to b.str(), the builder b should not be used again, you need to call b.free() first, or just leave it to be freed by -autofree when it goes out of scope. The returned string owns its own separate copy of the accumulated data that was in the string builder, before the .str() call.
fn (mut b Builder) free()
free - manually free the contents of the buffer