1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov
2019-12-20 00:29:37 +03:00
parent b6fe2ebc0b
commit 6210984c97
54 changed files with 1757 additions and 1993 deletions

View File

@@ -1,19 +1,18 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module strings
pub struct Builder {
mut:
buf []byte
buf []byte
pub mut:
len int
initial_size int = 1
len int
initial_size int=1
}
pub fn new_builder(initial_size int) Builder {
return Builder {
return Builder{
buf: make(0, initial_size, 1)
initial_size: initial_size
}
@@ -31,30 +30,33 @@ pub fn (b mut Builder) write_b(data byte) {
pub fn (b mut Builder) write(s string) {
b.buf.push_many(s.str, s.len)
//for c in s {
//b.buf << c
//}
//b.buf << []byte(s) // TODO
// for c in s {
// b.buf << c
// }
// b.buf << []byte(s) // TODO
b.len += s.len
}
pub fn (b mut Builder) writeln(s string) {
//for c in s {
//b.buf << c
//}
// for c in s {
// b.buf << c
// }
b.buf.push_many(s.str, s.len)
//b.buf << []byte(s) // TODO
// b.buf << []byte(s) // TODO
b.buf << `\n`
b.len += s.len + 1
}
pub fn (b mut Builder) str() string {
b.buf << `\0`
return string(b.buf, b.len)
return string(b.buf,b.len)
}
pub fn (b mut Builder) free() {
unsafe{ free(b.buf.data) }
unsafe{
free(b.buf.data)
}
b.buf = make(0, b.initial_size, 1)
b.len = 0
}

View File

@@ -1,7 +1,5 @@
module strings
//#-js
// #-js
// use levenshtein distance algorithm to calculate
// the distance between between two strings (lower is closer)
pub fn levenshtein_distance(a, b string) int {
@@ -66,3 +64,4 @@ pub fn dice_coefficient(s1, s2 string) f32 {
}
return (2.0 * intersection_size) / (f32(a.len) + f32(b.len) - 2)
}

View File

@@ -6,6 +6,6 @@ pub fn repeat(c byte, n int) string {
}
mut arr := [c].repeat(n + 1)
arr[n] = `\0`
return string(arr, n)
return string(arr,n)
}