2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 20:20:28 +02:00
|
|
|
module builtin
|
|
|
|
|
2019-10-31 22:28:52 +01:00
|
|
|
import strconv
|
2020-12-03 16:02:48 +01:00
|
|
|
|
2019-09-28 12:54:30 +02:00
|
|
|
/*
|
2019-10-04 14:48:09 +02:00
|
|
|
NB: A V string should be/is immutable from the point of view of
|
|
|
|
V user programs after it is first created. A V string is
|
|
|
|
also slightly larger than the equivalent C string because
|
2019-09-28 12:54:30 +02:00
|
|
|
the V string also has an integer length attached.
|
2019-10-04 14:48:09 +02:00
|
|
|
|
2019-09-28 12:54:30 +02:00
|
|
|
This tradeoff is made, since V strings are created just *once*,
|
|
|
|
but potentially used *many times* over their lifetime.
|
2019-10-04 14:48:09 +02:00
|
|
|
|
2019-09-28 12:54:30 +02:00
|
|
|
The V string implementation uses a struct, that has a .str field,
|
|
|
|
which points to a C style 0 terminated memory block. Although not
|
2019-10-04 14:48:09 +02:00
|
|
|
strictly necessary from the V point of view, that additional 0
|
2019-09-28 12:54:30 +02:00
|
|
|
is *very useful for C interoperability*.
|
2019-10-04 14:48:09 +02:00
|
|
|
|
|
|
|
The V string implementation also has an integer .len field,
|
|
|
|
containing the length of the .str field, excluding the
|
2019-09-28 12:54:30 +02:00
|
|
|
terminating 0 (just like the C's strlen(s) would do).
|
2019-10-04 14:48:09 +02:00
|
|
|
|
2019-09-28 12:54:30 +02:00
|
|
|
The 0 ending of .str, and the .len field, mean that in practice:
|
|
|
|
a) a V string s can be used very easily, wherever a
|
|
|
|
C string is needed, just by passing s.str,
|
|
|
|
without a need for further conversion/copying.
|
2019-10-04 14:48:09 +02:00
|
|
|
|
|
|
|
b) where strlen(s) is needed, you can just pass s.len,
|
|
|
|
without having to constantly recompute the length of s
|
2019-09-28 12:54:30 +02:00
|
|
|
*over and over again* like some C programs do. This is because
|
|
|
|
V strings are immutable and so their length does not change.
|
|
|
|
|
2019-10-04 14:48:09 +02:00
|
|
|
Ordinary V code *does not need* to be concerned with the
|
|
|
|
additional 0 in the .str field. The 0 *must* be put there by the
|
2019-09-28 12:54:30 +02:00
|
|
|
low level string creating functions inside this module.
|
2019-10-04 14:48:09 +02:00
|
|
|
|
|
|
|
Failing to do this will lead to programs that work most of the
|
|
|
|
time, when used with pure V functions, but fail in strange ways,
|
2019-09-28 12:54:30 +02:00
|
|
|
when used with modules using C functions (for example os and so on).
|
|
|
|
*/
|
2019-10-24 11:47:21 +02:00
|
|
|
pub struct string {
|
2019-06-22 20:20:28 +02:00
|
|
|
pub:
|
2021-04-04 19:14:51 +02:00
|
|
|
str &byte = 0 // points to a C style 0 terminated string of bytes.
|
2021-04-04 16:43:32 +02:00
|
|
|
len int // the length of the .str field, excluding the ending 0 byte. It is always equal to strlen(.str).
|
2021-07-03 19:16:49 +02:00
|
|
|
// NB string.is_lit is an enumeration of the following:
|
|
|
|
// .is_lit == 0 => a fresh string, should be freed by autofree
|
|
|
|
// .is_lit == 1 => a literal string from .rodata, should NOT be freed
|
|
|
|
// .is_lit == -98761234 => already freed string, protects against double frees.
|
|
|
|
// ---------> ^^^^^^^^^ calling free on these is a bug.
|
|
|
|
// Any other value means that the string has been corrupted.
|
2020-07-10 23:59:19 +02:00
|
|
|
mut:
|
2020-06-30 17:28:28 +02:00
|
|
|
is_lit int
|
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
|
2021-10-29 14:49:30 +02:00
|
|
|
// runes returns an array of all the utf runes in the string `s`
|
|
|
|
// which is useful if you want random access to them
|
|
|
|
[direct_array_access]
|
2021-06-30 08:17:38 +02:00
|
|
|
pub fn (s string) runes() []rune {
|
|
|
|
mut runes := []rune{cap: s.len}
|
|
|
|
for i := 0; i < s.len; i++ {
|
|
|
|
char_len := utf8_char_len(unsafe { s.str[i] })
|
|
|
|
if char_len > 1 {
|
2021-06-30 21:30:28 +02:00
|
|
|
end := if s.len - 1 >= i + char_len { i + char_len } else { s.len }
|
|
|
|
mut r := unsafe { s[i..end] }
|
2021-06-30 08:17:38 +02:00
|
|
|
runes << r.utf32_code()
|
|
|
|
i += char_len - 1
|
|
|
|
} else {
|
|
|
|
runes << unsafe { s.str[i] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runes
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// tos converts a C string to a V string.
|
2019-08-17 21:19:37 +02:00
|
|
|
// String data is reused, not copied.
|
2021-02-15 16:15:52 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn tos(s &byte, len int) string {
|
2019-06-22 20:20:28 +02:00
|
|
|
// This should never happen.
|
2019-10-04 14:48:09 +02:00
|
|
|
if s == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
panic('tos(): nil string')
|
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
return string{
|
2021-05-07 14:58:48 +02:00
|
|
|
str: unsafe { s }
|
2019-06-22 20:20:28 +02:00
|
|
|
len: len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// tos_clone returns a copy of `s`.
|
2021-02-15 16:15:52 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn tos_clone(s &byte) string {
|
2021-02-15 16:15:52 +01:00
|
|
|
return unsafe { tos2(s) }.clone()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// tos2 does the same as `tos`, but also calculates the length. Called by `string(bytes)` casts.
|
2019-08-17 21:19:37 +02:00
|
|
|
// Used only internally.
|
2021-02-15 16:15:52 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn tos2(s &byte) string {
|
2019-10-04 14:48:09 +02:00
|
|
|
if s == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
panic('tos2: nil string')
|
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
return string{
|
2021-05-07 14:58:48 +02:00
|
|
|
str: unsafe { s }
|
2021-02-14 19:31:42 +01:00
|
|
|
len: unsafe { vstrlen(s) }
|
2019-10-04 14:48:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// tos3 does the same as `tos2`, but for char*, to avoid warnings.
|
2021-02-15 16:15:52 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn tos3(s &char) string {
|
2019-10-04 14:48:09 +02:00
|
|
|
if s == 0 {
|
|
|
|
panic('tos3: nil string')
|
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
return string{
|
2021-04-04 16:43:32 +02:00
|
|
|
str: &byte(s)
|
2021-08-12 20:46:38 +02:00
|
|
|
len: unsafe { vstrlen_char(s) }
|
2019-10-04 14:48:09 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// tos4 does the same as `tos2`, but returns an empty string on nil ptr.
|
2021-02-15 16:15:52 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn tos4(s &byte) string {
|
2020-12-09 20:10:41 +01:00
|
|
|
if s == 0 {
|
2020-12-12 11:10:29 +01:00
|
|
|
return ''
|
2020-12-09 20:10:41 +01:00
|
|
|
}
|
2021-02-15 16:15:52 +01:00
|
|
|
return unsafe { tos2(s) }
|
2020-12-09 20:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// tos5 does the same as `tos4`, but for char*, to avoid warnings.
|
2021-02-15 16:15:52 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn tos5(s &char) string {
|
2020-12-09 20:10:41 +01:00
|
|
|
if s == 0 {
|
2020-12-12 11:10:29 +01:00
|
|
|
return ''
|
2020-12-09 20:10:41 +01:00
|
|
|
}
|
2021-02-15 16:15:52 +01:00
|
|
|
return unsafe { tos3(s) }
|
2020-12-09 20:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// vstring converts a C style string to a V string. NB: the string data is reused, NOT copied.
|
2021-03-23 21:10:11 +01:00
|
|
|
// strings returned from this function will be normal V strings beside that (i.e. they would be
|
|
|
|
// freed by V's -autofree mechanism, when they are no longer used).
|
2020-08-10 18:05:26 +02:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (bp &byte) vstring() string {
|
2020-08-10 18:05:26 +02:00
|
|
|
return string{
|
2021-05-07 14:58:48 +02:00
|
|
|
str: unsafe { bp }
|
2021-08-12 20:46:38 +02:00
|
|
|
len: unsafe { vstrlen(bp) }
|
2020-08-12 05:54:51 +02:00
|
|
|
}
|
2020-08-10 18:05:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 21:10:11 +01:00
|
|
|
// vstring_with_len converts a C style string to a V string.
|
|
|
|
// NB: the string data is reused, NOT copied.
|
2020-08-10 18:05:26 +02:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (bp &byte) vstring_with_len(len int) string {
|
2020-08-10 18:05:26 +02:00
|
|
|
return string{
|
2021-05-07 14:58:48 +02:00
|
|
|
str: unsafe { bp }
|
2020-08-10 18:05:26 +02:00
|
|
|
len: len
|
2021-03-23 21:10:11 +01:00
|
|
|
is_lit: 0
|
2020-08-12 05:54:51 +02:00
|
|
|
}
|
2020-08-10 18:05:26 +02:00
|
|
|
}
|
2020-05-07 18:05:54 +02:00
|
|
|
|
2021-03-23 21:10:11 +01:00
|
|
|
// vstring converts C char* to V string.
|
|
|
|
// NB: the string data is reused, NOT copied.
|
2020-11-14 18:43:42 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (cp &char) vstring() string {
|
2020-11-14 18:43:42 +01:00
|
|
|
return string{
|
2021-04-04 16:43:32 +02:00
|
|
|
str: &byte(cp)
|
2021-08-12 20:46:38 +02:00
|
|
|
len: unsafe { vstrlen_char(cp) }
|
2021-03-23 21:10:11 +01:00
|
|
|
is_lit: 0
|
2020-11-14 18:43:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:10:11 +01:00
|
|
|
// vstring_with_len converts C char* to V string.
|
|
|
|
// NB: the string data is reused, NOT copied.
|
2020-11-14 18:43:42 +01:00
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (cp &char) vstring_with_len(len int) string {
|
2020-11-14 18:43:42 +01:00
|
|
|
return string{
|
2021-04-04 16:43:32 +02:00
|
|
|
str: &byte(cp)
|
2020-11-14 18:43:42 +01:00
|
|
|
len: len
|
2021-03-23 21:10:11 +01:00
|
|
|
is_lit: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vstring_literal converts a C style string to a V string.
|
|
|
|
// NB: the string data is reused, NOT copied.
|
|
|
|
// NB2: unlike vstring, vstring_literal will mark the string
|
|
|
|
// as a literal, so it will not be freed by autofree.
|
2021-03-26 09:42:40 +01:00
|
|
|
// This is suitable for readonly strings, C string literals etc,
|
2021-03-23 21:10:11 +01:00
|
|
|
// that can be read by the V program, but that should not be
|
|
|
|
// managed by it, for example `os.args` is implemented using it.
|
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (bp &byte) vstring_literal() string {
|
2021-03-23 21:10:11 +01:00
|
|
|
return string{
|
2021-05-07 14:58:48 +02:00
|
|
|
str: unsafe { bp }
|
2021-08-12 20:46:38 +02:00
|
|
|
len: unsafe { vstrlen(bp) }
|
2021-03-23 21:10:11 +01:00
|
|
|
is_lit: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vstring_with_len converts a C style string to a V string.
|
|
|
|
// NB: the string data is reused, NOT copied.
|
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (bp &byte) vstring_literal_with_len(len int) string {
|
2021-03-23 21:10:11 +01:00
|
|
|
return string{
|
2021-05-07 14:58:48 +02:00
|
|
|
str: unsafe { bp }
|
2021-03-23 21:10:11 +01:00
|
|
|
len: len
|
|
|
|
is_lit: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vstring_literal converts C char* to V string.
|
|
|
|
// See also vstring_literal defined on byteptr for more details.
|
|
|
|
// NB: the string data is reused, NOT copied.
|
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (cp &char) vstring_literal() string {
|
2021-03-23 21:10:11 +01:00
|
|
|
return string{
|
2021-04-04 16:43:32 +02:00
|
|
|
str: &byte(cp)
|
2021-08-12 20:46:38 +02:00
|
|
|
len: unsafe { vstrlen_char(cp) }
|
2021-03-23 21:10:11 +01:00
|
|
|
is_lit: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:42:40 +01:00
|
|
|
// vstring_literal_with_len converts C char* to V string.
|
2021-03-23 21:10:11 +01:00
|
|
|
// See also vstring_literal_with_len defined on byteptr.
|
|
|
|
// NB: the string data is reused, NOT copied.
|
|
|
|
[unsafe]
|
2021-04-04 16:43:32 +02:00
|
|
|
pub fn (cp &char) vstring_literal_with_len(len int) string {
|
2021-03-23 21:10:11 +01:00
|
|
|
return string{
|
2021-04-04 16:43:32 +02:00
|
|
|
str: &byte(cp)
|
2021-03-23 21:10:11 +01:00
|
|
|
len: len
|
|
|
|
is_lit: 1
|
2020-11-14 18:43:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// clone_static returns an independent copy of a given array.
|
2020-05-06 18:03:44 +02:00
|
|
|
// It should be used only in -autofree generated code.
|
|
|
|
fn (a string) clone_static() string {
|
|
|
|
return a.clone()
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// clone returns a copy of the V string `a`.
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (a string) clone() string {
|
2021-05-11 12:59:44 +02:00
|
|
|
if a.len == 0 {
|
2020-12-11 05:03:25 +01:00
|
|
|
return ''
|
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
mut b := string{
|
2021-06-12 10:27:08 +02:00
|
|
|
str: unsafe { malloc_noscan(a.len + 1) }
|
2020-05-18 21:38:06 +02:00
|
|
|
len: a.len
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2021-08-12 20:46:38 +02:00
|
|
|
vmemcpy(b.str, a.str, a.len)
|
2021-04-13 10:29:33 +02:00
|
|
|
b.str[a.len] = 0
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
|
2020-08-09 11:22:11 +02:00
|
|
|
[unsafe]
|
2021-04-05 07:02:37 +02:00
|
|
|
pub fn cstring_to_vstring(cstr &char) string {
|
|
|
|
return unsafe { tos_clone(&byte(cstr)) }
|
2019-11-28 09:46:52 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// replace_once replaces the first occurence of `rep` with the string passed in `with`.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (s string) replace_once(rep string, with string) string {
|
2021-01-15 02:26:06 +01:00
|
|
|
idx := s.index_(rep)
|
|
|
|
if idx == -1 {
|
|
|
|
return s.clone()
|
|
|
|
}
|
|
|
|
return s.substr(0, idx) + with + s.substr(idx + rep.len, s.len)
|
2019-11-11 16:43:22 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// replace replaces all occurences of `rep` with the string passed in `with`.
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (s string) replace(rep string, with string) string {
|
2021-05-01 20:27:49 +02:00
|
|
|
if s.len == 0 || rep.len == 0 || rep.len > s.len {
|
2020-05-25 08:17:36 +02:00
|
|
|
return s.clone()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2021-05-22 13:35:33 +02:00
|
|
|
if !s.contains(rep) {
|
|
|
|
return s.clone()
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO PERF Allocating ints is expensive. Should be a stack array
|
|
|
|
// Get locations of all reps within this string
|
2021-05-01 20:27:49 +02:00
|
|
|
mut idxs := []int{cap: s.len / rep.len}
|
2020-12-03 16:02:48 +01:00
|
|
|
defer {
|
2021-01-05 18:59:51 +01:00
|
|
|
unsafe { idxs.free() }
|
2020-10-21 19:44:31 +02:00
|
|
|
}
|
2019-12-10 16:50:21 +01:00
|
|
|
mut idx := 0
|
2019-06-29 17:29:29 +02:00
|
|
|
for {
|
2019-12-10 16:50:21 +01:00
|
|
|
idx = s.index_after(rep, idx)
|
|
|
|
if idx == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
idxs << idx
|
2019-12-27 05:20:06 +01:00
|
|
|
idx += rep.len
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Dont change the string if there's nothing to replace
|
|
|
|
if idxs.len == 0 {
|
2020-05-25 08:17:36 +02:00
|
|
|
return s.clone()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Now we know the number of replacements we need to do and we can calc the len of the new string
|
|
|
|
new_len := s.len + idxs.len * (with.len - rep.len)
|
2021-06-12 10:27:08 +02:00
|
|
|
mut b := unsafe { malloc_noscan(new_len + 1) } // add space for the null byte at the end
|
2019-06-22 20:20:28 +02:00
|
|
|
// Fill the new string
|
2019-06-28 14:19:46 +02:00
|
|
|
mut b_i := 0
|
2021-05-02 18:30:07 +02:00
|
|
|
mut s_idx := 0
|
|
|
|
for _, rep_pos in idxs {
|
|
|
|
for i in s_idx .. rep_pos { // copy everything up to piece being replaced
|
|
|
|
unsafe {
|
|
|
|
b[b_i] = s[i]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2021-05-02 18:30:07 +02:00
|
|
|
b_i++
|
|
|
|
}
|
|
|
|
s_idx = rep_pos + rep.len // move string index past replacement
|
|
|
|
for i in 0 .. with.len { // copy replacement piece
|
|
|
|
unsafe {
|
|
|
|
b[b_i] = with[i]
|
2019-12-10 12:32:12 +01:00
|
|
|
}
|
2021-05-02 18:30:07 +02:00
|
|
|
b_i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s_idx < s.len { // if any original after last replacement, copy it
|
|
|
|
for i in s_idx .. s.len {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
b[b_i] = s[i]
|
|
|
|
}
|
2019-12-10 12:32:12 +01:00
|
|
|
b_i++
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2021-04-13 10:29:33 +02:00
|
|
|
b[new_len] = 0
|
2020-07-15 21:56:50 +02:00
|
|
|
return tos(b, new_len)
|
|
|
|
}
|
2019-12-10 12:32:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RepIndex {
|
2019-12-19 21:52:45 +01:00
|
|
|
idx int
|
2019-12-10 12:32:12 +01:00
|
|
|
val_idx int
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// replace_each replaces all occurences of the string pairs given in `vals`.
|
|
|
|
// Example: assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC'
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2019-12-10 12:32:12 +01:00
|
|
|
pub fn (s string) replace_each(vals []string) string {
|
|
|
|
if s.len == 0 || vals.len == 0 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-12-10 12:32:12 +01:00
|
|
|
}
|
|
|
|
if vals.len % 2 != 0 {
|
2021-04-17 11:30:45 +02:00
|
|
|
eprintln('string.replace_each(): odd number of strings')
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-12-10 12:32:12 +01:00
|
|
|
}
|
|
|
|
// `rep` - string to replace
|
|
|
|
// `with` - string to replace with
|
|
|
|
// Remember positions of all rep strings, and calculate the length
|
|
|
|
// of the new string to do just one allocation.
|
|
|
|
mut new_len := s.len
|
2021-10-29 14:49:30 +02:00
|
|
|
mut idxs := []RepIndex{cap: 6}
|
2019-12-10 12:32:12 +01:00
|
|
|
mut idx := 0
|
2021-03-22 09:46:45 +01:00
|
|
|
s_ := s.clone()
|
2019-12-19 21:52:45 +01:00
|
|
|
for rep_i := 0; rep_i < vals.len; rep_i += 2 {
|
2019-12-10 12:32:12 +01:00
|
|
|
// vals: ['rep1, 'with1', 'rep2', 'with2']
|
|
|
|
rep := vals[rep_i]
|
2019-12-19 21:52:45 +01:00
|
|
|
with := vals[rep_i + 1]
|
2021-08-26 14:20:54 +02:00
|
|
|
|
2019-12-10 12:32:12 +01:00
|
|
|
for {
|
2021-03-22 09:46:45 +01:00
|
|
|
idx = s_.index_after(rep, idx)
|
2019-12-10 12:32:12 +01:00
|
|
|
if idx == -1 {
|
|
|
|
break
|
|
|
|
}
|
2021-03-22 09:46:45 +01:00
|
|
|
// The string already found is set to `/del`, to avoid duplicate searches.
|
|
|
|
for i in 0 .. rep.len {
|
|
|
|
unsafe {
|
|
|
|
s_.str[idx + i] = 127
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 12:32:12 +01:00
|
|
|
// We need to remember both the position in the string,
|
|
|
|
// and which rep/with pair it refers to.
|
2021-08-26 14:20:54 +02:00
|
|
|
|
2020-12-03 16:02:48 +01:00
|
|
|
idxs << RepIndex{
|
|
|
|
idx: idx
|
|
|
|
val_idx: rep_i
|
2020-03-16 15:46:09 +01:00
|
|
|
}
|
2021-08-25 13:40:53 +02:00
|
|
|
|
2020-09-12 02:31:06 +02:00
|
|
|
idx += rep.len
|
2019-12-10 12:32:12 +01:00
|
|
|
new_len += with.len - rep.len
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 14:20:54 +02:00
|
|
|
|
2019-12-10 12:32:12 +01:00
|
|
|
// Dont change the string if there's nothing to replace
|
|
|
|
if idxs.len == 0 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-12-10 12:32:12 +01:00
|
|
|
}
|
2021-08-09 14:42:31 +02:00
|
|
|
idxs.sort(a.idx < b.idx)
|
2021-06-12 10:27:08 +02:00
|
|
|
mut b := unsafe { malloc_noscan(new_len + 1) } // add space for 0 terminator
|
2019-12-10 12:32:12 +01:00
|
|
|
// Fill the new string
|
|
|
|
mut idx_pos := 0
|
|
|
|
mut cur_idx := idxs[idx_pos]
|
|
|
|
mut b_i := 0
|
|
|
|
for i := 0; i < s.len; i++ {
|
|
|
|
if i == cur_idx.idx {
|
2020-04-07 18:51:39 +02:00
|
|
|
// Reached the location of rep, replace it with "with"
|
2019-12-10 12:32:12 +01:00
|
|
|
rep := vals[cur_idx.val_idx]
|
2019-12-19 21:52:45 +01:00
|
|
|
with := vals[cur_idx.val_idx + 1]
|
2020-12-03 16:02:48 +01:00
|
|
|
for j in 0 .. with.len {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
b[b_i] = with[j]
|
|
|
|
}
|
2019-12-10 12:32:12 +01:00
|
|
|
b_i++
|
|
|
|
}
|
|
|
|
// Skip the length of rep, since we just replaced it with "with"
|
|
|
|
i += rep.len - 1
|
|
|
|
// Go to the next index
|
|
|
|
idx_pos++
|
|
|
|
if idx_pos < idxs.len {
|
2019-06-22 20:20:28 +02:00
|
|
|
cur_idx = idxs[idx_pos]
|
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
} else {
|
2020-04-07 18:51:39 +02:00
|
|
|
// Rep doesnt start here, just copy
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
b[b_i] = s.str[i]
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
b_i++
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2021-04-13 10:29:33 +02:00
|
|
|
b[new_len] = 0
|
2020-07-15 21:56:50 +02:00
|
|
|
return tos(b, new_len)
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// bool returns `true` if the string equals the word "true" it will return `false` otherwise.
|
2019-12-09 15:10:44 +01:00
|
|
|
pub fn (s string) bool() bool {
|
|
|
|
return s == 'true' || s == 't' // TODO t for pg, remove
|
|
|
|
}
|
2019-10-25 22:41:18 +02:00
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// int returns the value of the string as an integer `'1'.int() == 1`.
|
2019-10-25 22:41:18 +02:00
|
|
|
pub fn (s string) int() int {
|
2021-07-02 09:39:57 +02:00
|
|
|
return int(strconv.common_parse_int(s, 0, 32, false, false) or { 0 })
|
2019-10-25 22:41:18 +02:00
|
|
|
}
|
2019-06-25 10:04:02 +02:00
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// i64 returns the value of the string as i64 `'1'.i64() == i64(1)`.
|
2019-07-10 16:05:39 +02:00
|
|
|
pub fn (s string) i64() i64 {
|
2021-07-02 09:39:57 +02:00
|
|
|
return strconv.common_parse_int(s, 0, 64, false, false) or { 0 }
|
2019-07-10 16:05:39 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// i8 returns the value of the string as i8 `'1'.i8() == i8(1)`.
|
2020-01-14 18:05:38 +01:00
|
|
|
pub fn (s string) i8() i8 {
|
2021-07-02 09:39:57 +02:00
|
|
|
return i8(strconv.common_parse_int(s, 0, 8, false, false) or { 0 })
|
2020-01-14 18:05:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// i16 returns the value of the string as i16 `'1'.i16() == i16(1)`.
|
2020-01-14 18:05:38 +01:00
|
|
|
pub fn (s string) i16() i16 {
|
2021-07-02 09:39:57 +02:00
|
|
|
return i16(strconv.common_parse_int(s, 0, 16, false, false) or { 0 })
|
2020-01-14 18:05:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`.
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (s string) f32() f32 {
|
2019-12-16 23:07:13 +01:00
|
|
|
return f32(strconv.atof64(s))
|
2019-06-25 10:04:02 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// f64 returns the value of the string as f64 `'1.0'.f64() == f64(1)`.
|
2019-07-17 20:11:14 +02:00
|
|
|
pub fn (s string) f64() f64 {
|
2019-12-16 23:07:13 +01:00
|
|
|
return strconv.atof64(s)
|
2019-07-17 20:11:14 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 09:08:10 +02:00
|
|
|
// u8 returns the value of the string as u8 `'1'.u8() == u8(1)`.
|
|
|
|
pub fn (s string) byte() u8 {
|
|
|
|
return byte(strconv.common_parse_uint(s, 0, 8, false, false) or { 0 })
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// u16 returns the value of the string as u16 `'1'.u16() == u16(1)`.
|
2020-01-14 18:05:38 +01:00
|
|
|
pub fn (s string) u16() u16 {
|
2021-07-02 09:39:57 +02:00
|
|
|
return u16(strconv.common_parse_uint(s, 0, 16, false, false) or { 0 })
|
2020-01-14 18:05:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// u32 returns the value of the string as u32 `'1'.u32() == u32(1)`.
|
2019-07-17 20:11:14 +02:00
|
|
|
pub fn (s string) u32() u32 {
|
2021-07-02 09:39:57 +02:00
|
|
|
return u32(strconv.common_parse_uint(s, 0, 32, false, false) or { 0 })
|
2019-07-17 20:11:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// u64 returns the value of the string as u64 `'1'.u64() == u64(1)`.
|
2019-07-17 20:11:14 +02:00
|
|
|
pub fn (s string) u64() u64 {
|
2021-07-02 09:39:57 +02:00
|
|
|
return strconv.common_parse_uint(s, 0, 64, false, false) or { 0 }
|
2019-07-17 20:11:14 +02:00
|
|
|
}
|
|
|
|
|
2021-05-23 16:22:57 +02:00
|
|
|
[direct_array_access]
|
2021-05-24 13:05:29 +02:00
|
|
|
fn (s string) == (a string) bool {
|
2020-04-27 07:13:36 +02:00
|
|
|
if s.str == 0 {
|
2019-12-19 21:52:45 +01:00
|
|
|
// should never happen
|
2019-06-22 20:20:28 +02:00
|
|
|
panic('string.eq(): nil string')
|
|
|
|
}
|
|
|
|
if s.len != a.len {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-23 16:22:57 +02:00
|
|
|
if s.len > 0 {
|
|
|
|
last_idx := s.len - 1
|
|
|
|
if s[last_idx] != a[last_idx] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 19:06:41 +02:00
|
|
|
unsafe {
|
2021-08-12 20:46:38 +02:00
|
|
|
return vmemcmp(s.str, a.str, a.len) == 0
|
2020-07-20 19:06:41 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:09:41 +02:00
|
|
|
// compare returns -1 if `s` < `a`, 0 if `s` == `a`, and 1 if `s` > `a`
|
|
|
|
[direct_array_access]
|
|
|
|
pub fn (s string) compare(a string) int {
|
|
|
|
min_len := if s.len < a.len { s.len } else { a.len }
|
|
|
|
for i in 0 .. min_len {
|
|
|
|
if s[i] < a[i] {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if s[i] > a[i] {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.len < a.len {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if s.len > a.len {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
[direct_array_access]
|
2021-05-24 13:05:29 +02:00
|
|
|
fn (s string) < (a string) bool {
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. s.len {
|
2019-06-23 20:25:50 +02:00
|
|
|
if i >= a.len || s[i] > a[i] {
|
2019-06-22 20:20:28 +02:00
|
|
|
return false
|
2020-12-03 16:02:48 +01:00
|
|
|
} else if s[i] < a[i] {
|
2019-06-22 20:20:28 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 20:25:50 +02:00
|
|
|
if s.len < a.len {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:09:41 +02:00
|
|
|
[direct_array_access]
|
2021-05-24 13:05:29 +02:00
|
|
|
fn (s string) + (a string) string {
|
2019-06-22 20:20:28 +02:00
|
|
|
new_len := a.len + s.len
|
2019-12-19 21:52:45 +01:00
|
|
|
mut res := string{
|
2021-06-12 10:27:08 +02:00
|
|
|
str: unsafe { malloc_noscan(new_len + 1) }
|
2020-05-18 21:38:06 +02:00
|
|
|
len: new_len
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
for j in 0 .. s.len {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
res.str[j] = s.str[j]
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
for j in 0 .. a.len {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
res.str[s.len + j] = a.str[j]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
2021-04-13 10:29:33 +02:00
|
|
|
res.str[new_len] = 0 // V strings are not null terminated, but just in case
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// split splits the string to an array by `delim`.
|
|
|
|
// Example: assert 'A B C'.split(' ') == ['A','B','C']
|
|
|
|
// If `delim` is empty the string is split by it's characters.
|
|
|
|
// Example: assert 'DEF'.split('') == ['D','E','F']
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) split(delim string) []string {
|
2019-12-01 14:10:13 +01:00
|
|
|
return s.split_nth(delim, 0)
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// split_nth splits the string based on the passed `delim` substring.
|
|
|
|
// It returns the first Nth parts. When N=0, return all the splits.
|
|
|
|
// The last returned element has the remainder of the string, even if
|
|
|
|
// the remainder contains more `delim` substrings.
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2019-12-01 14:10:13 +01:00
|
|
|
pub fn (s string) split_nth(delim string, nth int) []string {
|
2020-04-26 09:17:13 +02:00
|
|
|
mut res := []string{}
|
2019-12-01 14:10:13 +01:00
|
|
|
mut i := 0
|
2021-03-16 19:30:27 +01:00
|
|
|
|
|
|
|
match delim.len {
|
|
|
|
0 {
|
|
|
|
i = 1
|
|
|
|
for ch in s {
|
|
|
|
if nth > 0 && i >= nth {
|
|
|
|
res << s[i..]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res << ch.ascii_str()
|
|
|
|
i++
|
2019-12-01 14:10:13 +01:00
|
|
|
}
|
2021-03-16 19:30:27 +01:00
|
|
|
return res
|
2019-11-10 17:37:36 +01:00
|
|
|
}
|
2021-03-16 19:30:27 +01:00
|
|
|
1 {
|
|
|
|
mut start := 0
|
|
|
|
delim_byte := delim[0]
|
|
|
|
|
|
|
|
for i < s.len {
|
|
|
|
if s[i] == delim_byte {
|
|
|
|
was_last := nth > 0 && res.len == nth - 1
|
|
|
|
if was_last {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
val := s.substr(start, i)
|
|
|
|
res << val
|
|
|
|
start = i + delim.len
|
|
|
|
i = start
|
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
2020-01-24 20:12:36 +01:00
|
|
|
}
|
2021-03-16 19:30:27 +01:00
|
|
|
|
|
|
|
// Then the remaining right part of the string
|
|
|
|
if nth < 1 || res.len < nth {
|
|
|
|
res << s[start..]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mut start := 0
|
|
|
|
// Take the left part for each delimiter occurence
|
|
|
|
for i <= s.len {
|
|
|
|
is_delim := i + delim.len <= s.len && s.substr(i, i + delim.len) == delim
|
|
|
|
if is_delim {
|
|
|
|
was_last := nth > 0 && res.len == nth - 1
|
|
|
|
if was_last {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
val := s.substr(start, i)
|
|
|
|
res << val
|
|
|
|
start = i + delim.len
|
|
|
|
i = start
|
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Then the remaining right part of the string
|
|
|
|
if nth < 1 || res.len < nth {
|
|
|
|
res << s[start..]
|
|
|
|
}
|
|
|
|
return res
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// split_into_lines splits the string by newline characters.
|
2021-05-11 17:57:32 +02:00
|
|
|
// newlines are stripped.
|
|
|
|
// Both `\n` and `\r\n` newline endings are supported.
|
|
|
|
[direct_array_access]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) split_into_lines() []string {
|
2020-04-26 09:17:13 +02:00
|
|
|
mut res := []string{}
|
2019-06-22 20:20:28 +02:00
|
|
|
if s.len == 0 {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
mut start := 0
|
2021-05-11 17:57:32 +02:00
|
|
|
mut end := 0
|
2019-06-22 20:20:28 +02:00
|
|
|
for i := 0; i < s.len; i++ {
|
2021-05-11 17:57:32 +02:00
|
|
|
if s[i] == 10 {
|
|
|
|
end = if i > 0 && s[i - 1] == 13 { i - 1 } else { i }
|
|
|
|
res << if start == end { '' } else { s[start..end] }
|
2019-06-22 20:20:28 +02:00
|
|
|
start = i + 1
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 17:57:32 +02:00
|
|
|
if start < s.len {
|
|
|
|
res << s[start..]
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-10-27 07:36:04 +01:00
|
|
|
// used internally for [2..4]
|
2020-10-15 12:32:28 +02:00
|
|
|
fn (s string) substr2(start int, _end int, end_max bool) string {
|
2019-10-27 07:36:04 +01:00
|
|
|
end := if end_max { s.len } else { _end }
|
|
|
|
return s.substr(start, end)
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// substr returns the string between index positions `start` and `end`.
|
|
|
|
// Example: assert 'ABCD'.substr(1,3) == 'BC'
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (s string) substr(start int, end int) string {
|
2020-12-03 16:02:48 +01:00
|
|
|
$if !no_bounds_checking ? {
|
2021-03-15 21:16:23 +01:00
|
|
|
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
|
|
|
|
panic('substr($start, $end) out of bounds (len=$s.len)')
|
2020-02-16 16:13:45 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
len := end - start
|
2020-12-11 05:12:18 +01:00
|
|
|
if len == s.len {
|
|
|
|
return s.clone()
|
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
mut res := string{
|
2021-06-12 10:27:08 +02:00
|
|
|
str: unsafe { malloc_noscan(len + 1) }
|
2020-05-18 21:38:06 +02:00
|
|
|
len: len
|
2019-07-22 16:51:33 +02:00
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. len {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
res.str[i] = s.str[start + i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
2021-04-13 10:29:33 +02:00
|
|
|
res.str[len] = 0
|
2019-07-22 16:51:33 +02:00
|
|
|
}
|
2019-08-06 05:54:47 +02:00
|
|
|
return res
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 02:26:06 +01:00
|
|
|
// index returns the position of the first character of the input string.
|
|
|
|
// It will return `-1` if the input string can't be found.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2021-01-15 02:26:06 +01:00
|
|
|
fn (s string) index_(p string) int {
|
2020-02-20 20:14:21 +01:00
|
|
|
if p.len > s.len || p.len == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
return -1
|
|
|
|
}
|
2021-01-15 02:26:06 +01:00
|
|
|
if p.len > 2 {
|
|
|
|
return s.index_kmp(p)
|
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
mut i := 0
|
|
|
|
for i < s.len {
|
|
|
|
mut j := 0
|
2021-01-05 18:59:51 +01:00
|
|
|
for j < p.len && unsafe { s.str[i + j] == p.str[j] } {
|
2019-06-22 20:20:28 +02:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
2019-09-23 22:34:42 +02:00
|
|
|
return i
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
i++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// index returns the position of the first character of the input string.
|
|
|
|
// It will return `none` if the input string can't be found.
|
2019-11-30 11:09:05 +01:00
|
|
|
pub fn (s string) index(p string) ?int {
|
2021-01-15 02:26:06 +01:00
|
|
|
idx := s.index_(p)
|
|
|
|
if idx == -1 {
|
2019-11-30 11:09:05 +01:00
|
|
|
return none
|
|
|
|
}
|
2021-01-15 02:26:06 +01:00
|
|
|
return idx
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// index_kmp does KMP search.
|
2021-05-06 10:44:48 +02:00
|
|
|
[direct_array_access; manualfree]
|
2019-12-12 19:44:52 +01:00
|
|
|
fn (s string) index_kmp(p string) int {
|
2019-12-19 21:52:45 +01:00
|
|
|
if p.len > s.len {
|
|
|
|
return -1
|
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
mut prefix := []int{len: p.len}
|
2021-03-23 20:48:08 +01:00
|
|
|
defer {
|
|
|
|
unsafe { prefix.free() }
|
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
mut j := 0
|
|
|
|
for i := 1; i < p.len; i++ {
|
2021-01-05 18:59:51 +01:00
|
|
|
for unsafe { p.str[j] != p.str[i] } && j > 0 {
|
2019-12-19 21:52:45 +01:00
|
|
|
j = prefix[j - 1]
|
|
|
|
}
|
2021-01-05 18:59:51 +01:00
|
|
|
if unsafe { p.str[j] == p.str[i] } {
|
2019-12-19 21:52:45 +01:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
prefix[i] = j
|
|
|
|
}
|
|
|
|
j = 0
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. s.len {
|
2021-01-05 18:59:51 +01:00
|
|
|
for unsafe { p.str[j] != s.str[i] } && j > 0 {
|
2019-12-19 21:52:45 +01:00
|
|
|
j = prefix[j - 1]
|
|
|
|
}
|
2021-01-05 18:59:51 +01:00
|
|
|
if unsafe { p.str[j] == s.str[i] } {
|
2019-12-19 21:52:45 +01:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
|
|
|
return i - p.len + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// index_any returns the position of any of the characters in the input string - if found.
|
2019-08-01 15:01:03 +02:00
|
|
|
pub fn (s string) index_any(chars string) int {
|
2021-09-06 14:06:37 +02:00
|
|
|
for i, ss in s {
|
|
|
|
for c in chars {
|
|
|
|
if c == ss {
|
|
|
|
return i
|
|
|
|
}
|
2021-01-15 02:26:06 +01:00
|
|
|
}
|
2019-08-01 15:01:03 +02:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// last_index returns the position of the last occurence of the input string.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2021-01-15 02:26:06 +01:00
|
|
|
fn (s string) last_index_(p string) int {
|
2020-02-20 20:14:21 +01:00
|
|
|
if p.len > s.len || p.len == 0 {
|
2021-01-15 02:26:06 +01:00
|
|
|
return -1
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
mut i := s.len - p.len
|
|
|
|
for i >= 0 {
|
|
|
|
mut j := 0
|
2021-01-05 18:59:51 +01:00
|
|
|
for j < p.len && unsafe { s.str[i + j] == p.str[j] } {
|
2019-06-22 20:20:28 +02:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
}
|
2021-01-15 02:26:06 +01:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// last_index returns the position of the last occurence of the input string.
|
|
|
|
pub fn (s string) last_index(p string) ?int {
|
|
|
|
idx := s.last_index_(p)
|
|
|
|
if idx == -1 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return idx
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// index_after returns the position of the input string, starting search from `start` position.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) index_after(p string, start int) int {
|
|
|
|
if p.len > s.len {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
mut strt := start
|
|
|
|
if start < 0 {
|
|
|
|
strt = 0
|
|
|
|
}
|
|
|
|
if start >= s.len {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
mut i := strt
|
|
|
|
for i < s.len {
|
|
|
|
mut j := 0
|
|
|
|
mut ii := i
|
2021-01-05 18:59:51 +01:00
|
|
|
for j < p.len && unsafe { s.str[ii] == p.str[j] } {
|
2019-06-22 20:20:28 +02:00
|
|
|
j++
|
|
|
|
ii++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// index_byte returns the index of byte `c` if found in the string.
|
|
|
|
// index_byte returns -1 if the byte can not be found.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-10-10 19:04:11 +02:00
|
|
|
pub fn (s string) index_byte(c byte) int {
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. s.len {
|
2021-01-05 18:59:51 +01:00
|
|
|
if unsafe { s.str[i] } == c {
|
2019-10-10 19:04:11 +02:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// last_index_byte returns the index of the last occurence of byte `c` if found in the string.
|
|
|
|
// last_index_byte returns -1 if the byte is not found.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-10-10 19:04:11 +02:00
|
|
|
pub fn (s string) last_index_byte(c byte) int {
|
2019-12-19 21:52:45 +01:00
|
|
|
for i := s.len - 1; i >= 0; i-- {
|
2021-01-05 18:59:51 +01:00
|
|
|
if unsafe { s.str[i] == c } {
|
2019-10-10 19:04:11 +02:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// count returns the number of occurrences of `substr` in the string.
|
|
|
|
// count returns -1 if no `substr` could be found.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-08-02 23:30:22 +02:00
|
|
|
pub fn (s string) count(substr string) int {
|
|
|
|
if s.len == 0 || substr.len == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2019-08-26 12:32:53 +02:00
|
|
|
if substr.len > s.len {
|
|
|
|
return 0
|
|
|
|
}
|
2021-03-16 23:19:48 +01:00
|
|
|
|
2019-08-02 23:30:22 +02:00
|
|
|
mut n := 0
|
2021-03-16 23:19:48 +01:00
|
|
|
|
|
|
|
if substr.len == 1 {
|
|
|
|
target := substr[0]
|
|
|
|
|
|
|
|
for letter in s {
|
|
|
|
if letter == target {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2019-08-03 00:18:19 +02:00
|
|
|
mut i := 0
|
2019-08-02 23:30:22 +02:00
|
|
|
for {
|
2019-08-03 22:24:03 +02:00
|
|
|
i = s.index_after(substr, i)
|
2019-08-02 23:30:22 +02:00
|
|
|
if i == -1 {
|
|
|
|
return n
|
|
|
|
}
|
2019-08-03 00:18:19 +02:00
|
|
|
i += substr.len
|
2019-08-02 23:30:22 +02:00
|
|
|
n++
|
|
|
|
}
|
2019-08-08 09:49:56 +02:00
|
|
|
return 0 // TODO can never get here - v doesn't know that
|
2019-08-02 23:30:22 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// contains returns `true` if the string contains `substr`.
|
2020-07-29 21:48:50 +02:00
|
|
|
pub fn (s string) contains(substr string) bool {
|
|
|
|
if substr.len == 0 {
|
2020-06-14 11:24:15 +02:00
|
|
|
return true
|
|
|
|
}
|
2021-01-15 02:26:06 +01:00
|
|
|
if s.index_(substr) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-30 11:09:05 +01:00
|
|
|
return true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// contains_any returns `true` if the string contains any chars in `chars`.
|
2020-07-29 21:48:50 +02:00
|
|
|
pub fn (s string) contains_any(chars string) bool {
|
|
|
|
for c in chars {
|
2021-03-23 09:38:56 +01:00
|
|
|
if s.contains(c.ascii_str()) {
|
2020-07-29 21:48:50 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// contains_any_substr returns `true` if the string contains any of the strings in `substrs`.
|
2020-07-29 21:48:50 +02:00
|
|
|
pub fn (s string) contains_any_substr(substrs []string) bool {
|
|
|
|
if substrs.len == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for sub in substrs {
|
|
|
|
if s.contains(sub) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// starts_with returns `true` if the string starts with `p`.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) starts_with(p string) bool {
|
2020-02-19 15:18:09 +01:00
|
|
|
if p.len > s.len {
|
2019-12-19 21:52:45 +01:00
|
|
|
return false
|
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. p.len {
|
2021-01-05 18:59:51 +01:00
|
|
|
if unsafe { s.str[i] != p.str[i] } {
|
2020-02-19 15:18:09 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// ends_with returns `true` if the string ends with `p`.
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) ends_with(p string) bool {
|
|
|
|
if p.len > s.len {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. p.len {
|
2021-05-03 13:14:32 +02:00
|
|
|
if unsafe { p.str[i] != s.str[s.len - p.len + i] } {
|
2020-02-19 15:18:09 +01:00
|
|
|
return false
|
|
|
|
}
|
2019-12-12 19:44:52 +01:00
|
|
|
}
|
2020-02-19 15:18:09 +01:00
|
|
|
return true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// to_lower returns the string in all lowercase characters.
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO only works with ASCII
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) to_lower() string {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2021-06-12 10:27:08 +02:00
|
|
|
mut b := malloc_noscan(s.len + 1)
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. s.len {
|
2021-04-14 07:50:50 +02:00
|
|
|
if s.str[i] >= `A` && s.str[i] <= `Z` {
|
|
|
|
b[i] = s.str[i] + 32
|
|
|
|
} else {
|
|
|
|
b[i] = s.str[i]
|
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2021-03-14 17:21:45 +01:00
|
|
|
b[s.len] = 0
|
2020-07-15 21:56:50 +02:00
|
|
|
return tos(b, s.len)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// is_lower returns `true` if all characters in the string is lowercase.
|
|
|
|
// Example: assert 'hello developer'.is_lower() == true
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2020-04-12 12:09:05 +02:00
|
|
|
pub fn (s string) is_lower() bool {
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. s.len {
|
2020-04-12 12:09:05 +02:00
|
|
|
if s[i] >= `A` && s[i] <= `Z` {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// to_upper returns the string in all uppercase characters.
|
|
|
|
// Example: assert 'Hello V'.to_upper() == 'HELLO V'
|
2021-10-29 14:49:30 +02:00
|
|
|
[direct_array_access]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) to_upper() string {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2021-06-12 10:27:08 +02:00
|
|
|
mut b := malloc_noscan(s.len + 1)
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. s.len {
|
2021-04-14 07:50:50 +02:00
|
|
|
if s.str[i] >= `a` && s.str[i] <= `z` {
|
|
|
|
b[i] = s.str[i] - 32
|
|
|
|
} else {
|
|
|
|
b[i] = s.str[i]
|
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2021-03-14 17:21:45 +01:00
|
|
|
b[s.len] = 0
|
2020-07-15 21:56:50 +02:00
|
|
|
return tos(b, s.len)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// is_upper returns `true` if all characters in the string is uppercase.
|
|
|
|
// Example: assert 'HELLO V'.is_upper() == true
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2020-04-12 12:09:05 +02:00
|
|
|
pub fn (s string) is_upper() bool {
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 0 .. s.len {
|
2020-04-12 12:09:05 +02:00
|
|
|
if s[i] >= `a` && s[i] <= `z` {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// capitalize returns the string with the first character capitalized.
|
|
|
|
// Example: assert 'hello'.capitalize() == 'Hello'
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2019-08-26 12:32:53 +02:00
|
|
|
pub fn (s string) capitalize() string {
|
2020-02-20 11:33:38 +01:00
|
|
|
if s.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2021-03-22 15:45:29 +01:00
|
|
|
s0 := s[0]
|
|
|
|
letter := s0.ascii_str()
|
|
|
|
uletter := letter.to_upper()
|
|
|
|
if s.len == 1 {
|
|
|
|
return uletter
|
|
|
|
}
|
|
|
|
srest := s[1..]
|
|
|
|
res := uletter + srest
|
|
|
|
return res
|
2019-08-26 12:32:53 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 12:30:40 +01:00
|
|
|
// is_capital returns `true`, if the first character in the string `s`,
|
|
|
|
// is a capital letter, and the rest are NOT.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert 'Hello'.is_capital() == true
|
2021-11-07 12:30:40 +01:00
|
|
|
// Example: assert 'HelloWorld'.is_capital() == false
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2020-04-12 12:09:05 +02:00
|
|
|
pub fn (s string) is_capital() bool {
|
|
|
|
if s.len == 0 || !(s[0] >= `A` && s[0] <= `Z`) {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
for i in 1 .. s.len {
|
2020-04-12 12:09:05 +02:00
|
|
|
if s[i] >= `A` && s[i] <= `Z` {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-11-07 12:30:40 +01:00
|
|
|
// starts_with_capital returns `true`, if the first character in the string `s`,
|
|
|
|
// is a capital letter, even if the rest are not.
|
|
|
|
// Example: assert 'Hello'.starts_with_capital() == true
|
|
|
|
// Example: assert 'Hello. World.'.starts_with_capital() == true
|
|
|
|
[direct_array_access]
|
|
|
|
pub fn (s string) starts_with_capital() bool {
|
|
|
|
if s.len == 0 || !(s[0] >= `A` && s[0] <= `Z`) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// title returns the string with each word capitalized.
|
|
|
|
// Example: assert 'hello v developer'.title() == 'Hello V Developer'
|
2019-08-26 12:32:53 +02:00
|
|
|
pub fn (s string) title() string {
|
2019-12-19 21:52:45 +01:00
|
|
|
words := s.split(' ')
|
2020-04-26 09:17:13 +02:00
|
|
|
mut tit := []string{}
|
2019-08-26 12:32:53 +02:00
|
|
|
for word in words {
|
|
|
|
tit << word.capitalize()
|
|
|
|
}
|
|
|
|
title := tit.join(' ')
|
2019-11-28 07:46:10 +01:00
|
|
|
return title
|
2019-08-26 12:32:53 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 12:30:40 +01:00
|
|
|
// is_title returns true if all words of the string are capitalized.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert 'Hello V Developer'.is_title() == true
|
2020-04-12 12:09:05 +02:00
|
|
|
pub fn (s string) is_title() bool {
|
|
|
|
words := s.split(' ')
|
|
|
|
for word in words {
|
|
|
|
if !word.is_capital() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// find_between returns the string found between `start` string and `end` string.
|
|
|
|
// Example: assert 'hey [man] how you doin'.find_between('[', ']') == 'man'
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (s string) find_between(start string, end string) string {
|
2021-01-15 02:26:06 +01:00
|
|
|
start_pos := s.index_(start)
|
|
|
|
if start_pos == -1 {
|
|
|
|
return ''
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// First get everything to the right of 'start'
|
2021-01-22 10:26:07 +01:00
|
|
|
val := s[start_pos + start.len..]
|
2021-01-15 02:26:06 +01:00
|
|
|
end_pos := val.index_(end)
|
|
|
|
if end_pos == -1 {
|
|
|
|
return val
|
|
|
|
}
|
2021-01-22 10:26:07 +01:00
|
|
|
return val[..end_pos]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// trim_space strips any of ` `, `\n`, `\t`, `\v`, `\f`, `\r` from the start and end of the string.
|
|
|
|
// Example: assert ' Hello V '.trim_space() == 'Hello V'
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) trim_space() string {
|
2019-09-06 12:22:37 +02:00
|
|
|
return s.trim(' \n\t\v\f\r')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// trim strips any of the characters given in `cutset` from the start and end of the string.
|
|
|
|
// Example: assert ' ffHello V ffff'.trim(' f') == 'Hello V'
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2019-08-27 06:53:56 +02:00
|
|
|
pub fn (s string) trim(cutset string) string {
|
2019-09-06 12:22:37 +02:00
|
|
|
if s.len < 1 || cutset.len < 1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-27 06:53:56 +02:00
|
|
|
mut pos_left := 0
|
|
|
|
mut pos_right := s.len - 1
|
|
|
|
mut cs_match := true
|
|
|
|
for pos_left <= s.len && pos_right >= -1 && cs_match {
|
|
|
|
cs_match = false
|
2021-03-18 18:52:33 +01:00
|
|
|
for cs in cutset {
|
|
|
|
if s[pos_left] == cs {
|
|
|
|
pos_left++
|
|
|
|
cs_match = true
|
|
|
|
break
|
|
|
|
}
|
2019-08-27 06:53:56 +02:00
|
|
|
}
|
2021-03-18 18:52:33 +01:00
|
|
|
for cs in cutset {
|
|
|
|
if s[pos_right] == cs {
|
|
|
|
pos_right--
|
|
|
|
cs_match = true
|
|
|
|
break
|
|
|
|
}
|
2019-08-27 06:53:56 +02:00
|
|
|
}
|
|
|
|
if pos_left > pos_right {
|
|
|
|
return ''
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
return s.substr(pos_left, pos_right + 1)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// trim_left strips any of the characters given in `cutset` from the left of the string.
|
|
|
|
// Example: assert 'd Hello V developer'.trim_left(' d') == 'Hello V developer'
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (s string) trim_left(cutset string) string {
|
2019-09-06 12:22:37 +02:00
|
|
|
if s.len < 1 || cutset.len < 1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-27 06:53:56 +02:00
|
|
|
mut pos := 0
|
2021-03-18 18:52:33 +01:00
|
|
|
for pos < s.len {
|
|
|
|
mut found := false
|
|
|
|
for cs in cutset {
|
|
|
|
if s[pos] == cs {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
break
|
|
|
|
}
|
2019-08-26 13:18:58 +02:00
|
|
|
pos++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2021-01-22 10:26:07 +01:00
|
|
|
return s[pos..]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// trim_right strips any of the characters given in `cutset` from the right of the string.
|
|
|
|
// Example: assert ' Hello V d'.trim_right(' d') == ' Hello V'
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (s string) trim_right(cutset string) string {
|
2019-09-06 12:22:37 +02:00
|
|
|
if s.len < 1 || cutset.len < 1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-08-27 06:53:56 +02:00
|
|
|
mut pos := s.len - 1
|
2021-03-18 18:52:33 +01:00
|
|
|
for pos >= 0 {
|
|
|
|
mut found := false
|
|
|
|
for cs in cutset {
|
|
|
|
if s[pos] == cs {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
break
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
pos--
|
|
|
|
}
|
2021-03-22 15:45:29 +01:00
|
|
|
if pos < 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return s[..pos + 1]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// trim_prefix strips `str` from the start of the string.
|
|
|
|
// Example: assert 'WorldHello V'.trim_prefix('World') == 'Hello V'
|
2020-05-15 19:37:14 +02:00
|
|
|
pub fn (s string) trim_prefix(str string) string {
|
|
|
|
if s.starts_with(str) {
|
2020-06-30 15:44:53 +02:00
|
|
|
return s[str.len..]
|
2020-05-15 19:37:14 +02:00
|
|
|
}
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2020-05-15 19:37:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// trim_suffix strips `str` from the end of the string.
|
|
|
|
// Example: assert 'Hello VWorld'.trim_suffix('World') == 'Hello V'
|
2020-05-15 19:37:14 +02:00
|
|
|
pub fn (s string) trim_suffix(str string) string {
|
|
|
|
if s.ends_with(str) {
|
2020-12-03 16:02:48 +01:00
|
|
|
return s[..s.len - str.len]
|
2020-05-15 19:37:14 +02:00
|
|
|
}
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2020-05-15 19:37:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// compare_strings returns `-1` if `a < b`, `1` if `a > b` else `0`.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn compare_strings(a &string, b &string) int {
|
2021-05-24 13:05:29 +02:00
|
|
|
if a < b {
|
2019-06-22 20:20:28 +02:00
|
|
|
return -1
|
|
|
|
}
|
2021-05-24 13:05:29 +02:00
|
|
|
if a > b {
|
2019-06-22 20:20:28 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// compare_strings_by_len returns `-1` if `a.len < b.len`, `1` if `a.len > b.len` else `0`.
|
2020-10-15 12:32:28 +02:00
|
|
|
fn compare_strings_by_len(a &string, b &string) int {
|
2019-06-22 20:20:28 +02:00
|
|
|
if a.len < b.len {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if a.len > b.len {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// compare_lower_strings returns the same as compare_strings but converts `a` and `b` to lower case before comparing.
|
2020-10-15 12:32:28 +02:00
|
|
|
fn compare_lower_strings(a &string, b &string) int {
|
2019-06-22 20:20:28 +02:00
|
|
|
aa := a.to_lower()
|
2019-06-25 06:29:02 +02:00
|
|
|
bb := b.to_lower()
|
2021-03-26 09:42:40 +01:00
|
|
|
return compare_strings(&aa, &bb)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// sort_ignore_case sorts the string array using case insesitive comparing.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut s []string) sort_ignore_case() {
|
2019-06-22 20:20:28 +02:00
|
|
|
s.sort_with_compare(compare_lower_strings)
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// sort_by_len sorts the the string array by each string's `.len` length.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut s []string) sort_by_len() {
|
2019-06-22 20:20:28 +02:00
|
|
|
s.sort_with_compare(compare_strings_by_len)
|
|
|
|
}
|
|
|
|
|
2021-03-18 21:22:43 +01:00
|
|
|
// str returns a copy of the string
|
2020-04-29 13:51:42 +02:00
|
|
|
pub fn (s string) str() string {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2020-04-29 13:51:42 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// at returns the byte at index `idx`.
|
|
|
|
// Example: assert 'ABC'.at(1) == byte(`B`)
|
2019-06-22 20:20:28 +02:00
|
|
|
fn (s string) at(idx int) byte {
|
2020-12-03 16:02:48 +01:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 16:13:45 +01:00
|
|
|
if idx < 0 || idx >= s.len {
|
|
|
|
panic('string index out of range: $idx / $s.len')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
return s.str[idx]
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-07-05 20:00:30 +02:00
|
|
|
// version of `at()` that is used in `a[i] or {`
|
|
|
|
// return an error when the index is out of range
|
|
|
|
fn (s string) at_with_check(idx int) ?byte {
|
|
|
|
if idx < 0 || idx >= s.len {
|
|
|
|
return error('string index out of range')
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
return s.str[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:50:55 +02:00
|
|
|
// is_space returns `true` if the byte is a white space character.
|
|
|
|
// The following list is considered white space characters: ` `, `\t`, `\n`, `\v`, `\f`, `\r`, 0x85, 0xa0
|
|
|
|
// Example: assert byte(` `).is_space() == true
|
|
|
|
[inline]
|
|
|
|
pub fn (c byte) is_space() bool {
|
|
|
|
// 0x85 is NEXT LINE (NEL)
|
|
|
|
// 0xa0 is NO-BREAK SPACE
|
|
|
|
return c == 32 || (c > 8 && c < 14) || (c == 0x85) || (c == 0xa0)
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// is_digit returns `true` if the byte is in range 0-9 and `false` otherwise.
|
|
|
|
// Example: assert byte(`9`) == true
|
2021-06-29 13:50:55 +02:00
|
|
|
[inline]
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (c byte) is_digit() bool {
|
2019-06-22 20:20:28 +02:00
|
|
|
return c >= `0` && c <= `9`
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// is_hex_digit returns `true` if the byte is either in range 0-9, a-f or A-F and `false` otherwise.
|
|
|
|
// Example: assert byte(`F`) == true
|
2021-06-29 13:50:55 +02:00
|
|
|
[inline]
|
2019-07-24 00:06:48 +02:00
|
|
|
pub fn (c byte) is_hex_digit() bool {
|
2021-11-07 12:30:40 +01:00
|
|
|
return (c >= `0` && c <= `9`) || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
2019-07-24 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
|
|
|
// Example: assert byte(`7`) == true
|
2021-06-29 13:50:55 +02:00
|
|
|
[inline]
|
2019-07-24 00:06:48 +02:00
|
|
|
pub fn (c byte) is_oct_digit() bool {
|
|
|
|
return c >= `0` && c <= `7`
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// is_bin_digit returns `true` if the byte is a binary digit (0 or 1) and `false` otherwise.
|
|
|
|
// Example: assert byte(`0`) == true
|
2021-06-29 13:50:55 +02:00
|
|
|
[inline]
|
2020-01-23 03:28:25 +01:00
|
|
|
pub fn (c byte) is_bin_digit() bool {
|
|
|
|
return c == `0` || c == `1`
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// is_letter returns `true` if the byte is in range a-z or A-Z and `false` otherwise.
|
|
|
|
// Example: assert byte(`V`) == true
|
2021-06-29 13:50:55 +02:00
|
|
|
[inline]
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (c byte) is_letter() bool {
|
2019-06-22 20:20:28 +02:00
|
|
|
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)
|
|
|
|
}
|
|
|
|
|
2021-09-19 15:22:28 +02:00
|
|
|
// is_alnum returns `true` if the byte is in range a-z, A-Z, 0-9 and `false` otherwise.
|
|
|
|
// Example: assert byte(`V`) == true
|
|
|
|
[inline]
|
|
|
|
pub fn (c byte) is_alnum() bool {
|
2021-11-07 12:30:40 +01:00
|
|
|
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`)
|
2021-09-19 15:22:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// free allows for manually freeing the memory occupied by the string
|
2021-05-23 11:37:23 +02:00
|
|
|
[manualfree; unsafe]
|
2020-05-06 18:03:44 +02:00
|
|
|
pub fn (s &string) free() {
|
2020-07-11 13:22:16 +02:00
|
|
|
$if prealloc {
|
|
|
|
return
|
|
|
|
}
|
2020-06-30 17:28:28 +02:00
|
|
|
if s.is_lit == -98761234 {
|
2021-06-23 19:17:58 +02:00
|
|
|
double_free_msg := unsafe { &byte(c'double string.free() detected\n') }
|
2021-06-23 13:29:38 +02:00
|
|
|
double_free_msg_len := unsafe { vstrlen(double_free_msg) }
|
2021-04-14 07:50:50 +02:00
|
|
|
$if freestanding {
|
2021-06-23 13:29:38 +02:00
|
|
|
bare_eprint(double_free_msg, u64(double_free_msg_len))
|
2021-04-14 07:50:50 +02:00
|
|
|
} $else {
|
2021-06-23 13:29:38 +02:00
|
|
|
_write_buf_to_fd(1, double_free_msg, double_free_msg_len)
|
2021-04-14 07:50:50 +02:00
|
|
|
}
|
2020-06-30 17:28:28 +02:00
|
|
|
return
|
|
|
|
}
|
2021-06-10 14:21:57 +02:00
|
|
|
if s.is_lit == 1 || s.str == 0 {
|
2020-06-12 19:20:51 +02:00
|
|
|
return
|
|
|
|
}
|
2021-02-14 19:31:42 +01:00
|
|
|
unsafe {
|
|
|
|
free(s.str)
|
|
|
|
}
|
2020-06-30 17:28:28 +02:00
|
|
|
s.is_lit = -98761234
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:37 +02:00
|
|
|
// before returns the contents before `sub` in the string.
|
|
|
|
// If the substring is not found, it returns the full input string.
|
|
|
|
// Example: assert '23:34:45.234'.before('.') == '23:34:45'
|
|
|
|
// Example: assert 'abcd'.before('.') == 'abcd'
|
|
|
|
// TODO: deprecate and remove either .before or .all_before
|
|
|
|
pub fn (s string) before(sub string) string {
|
|
|
|
pos := s.index_(sub)
|
2021-02-07 04:48:54 +01:00
|
|
|
if pos == -1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2021-02-07 04:48:54 +01:00
|
|
|
}
|
|
|
|
return s[..pos]
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:37 +02:00
|
|
|
// all_before returns the contents before `sub` in the string.
|
|
|
|
// If the substring is not found, it returns the full input string.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert '23:34:45.234'.all_before('.') == '23:34:45'
|
2021-06-29 13:40:37 +02:00
|
|
|
// Example: assert 'abcd'.all_before('.') == 'abcd'
|
|
|
|
pub fn (s string) all_before(sub string) string {
|
2021-02-07 04:48:54 +01:00
|
|
|
// TODO remove dup method
|
2021-06-29 13:40:37 +02:00
|
|
|
pos := s.index_(sub)
|
2021-01-15 02:26:06 +01:00
|
|
|
if pos == -1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2021-01-15 02:26:06 +01:00
|
|
|
}
|
2021-01-22 10:26:07 +01:00
|
|
|
return s[..pos]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:37 +02:00
|
|
|
// all_before_last returns the contents before the last occurence of `sub` in the string.
|
|
|
|
// If the substring is not found, it returns the full input string.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert '23:34:45.234'.all_before_last(':') == '23:34'
|
2021-06-29 13:40:37 +02:00
|
|
|
// Example: assert 'abcd'.all_before_last('.') == 'abcd'
|
|
|
|
pub fn (s string) all_before_last(sub string) string {
|
|
|
|
pos := s.last_index_(sub)
|
2021-01-15 02:26:06 +01:00
|
|
|
if pos == -1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2021-01-15 02:26:06 +01:00
|
|
|
}
|
2021-01-22 10:26:07 +01:00
|
|
|
return s[..pos]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:37 +02:00
|
|
|
// all_after returns the contents after `sub` in the string.
|
|
|
|
// If the substring is not found, it returns the full input string.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert '23:34:45.234'.all_after('.') == '234'
|
2021-06-29 13:50:55 +02:00
|
|
|
// Example: assert 'abcd'.all_after('z') == 'abcd'
|
2021-06-29 13:40:37 +02:00
|
|
|
pub fn (s string) all_after(sub string) string {
|
|
|
|
pos := s.index_(sub)
|
2021-01-15 02:26:06 +01:00
|
|
|
if pos == -1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2021-01-15 02:26:06 +01:00
|
|
|
}
|
2021-06-29 13:40:37 +02:00
|
|
|
return s[pos + sub.len..]
|
2020-05-20 11:04:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:37 +02:00
|
|
|
// all_after_last returns the contents after the last occurence of `sub` in the string.
|
2021-06-29 13:50:55 +02:00
|
|
|
// If the substring is not found, it returns the full input string.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert '23:34:45.234'.all_after_last(':') == '45.234'
|
2021-06-29 13:40:37 +02:00
|
|
|
// Example: assert 'abcd'.all_after_last('z') == 'abcd'
|
|
|
|
pub fn (s string) all_after_last(sub string) string {
|
|
|
|
pos := s.last_index_(sub)
|
2021-01-15 02:26:06 +01:00
|
|
|
if pos == -1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2021-01-15 02:26:06 +01:00
|
|
|
}
|
2021-06-29 13:40:37 +02:00
|
|
|
return s[pos + sub.len..]
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:40:37 +02:00
|
|
|
// after returns the contents after the last occurence of `sub` in the string.
|
2021-06-29 13:50:55 +02:00
|
|
|
// If the substring is not found, it returns the full input string.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert '23:34:45.234'.after(':') == '45.234'
|
2021-06-29 13:40:37 +02:00
|
|
|
// Example: assert 'abcd'.after('z') == 'abcd'
|
|
|
|
// TODO: deprecate either .all_after_last or .after
|
|
|
|
pub fn (s string) after(sub string) string {
|
|
|
|
return s.all_after_last(sub)
|
2020-05-20 11:04:28 +02:00
|
|
|
}
|
2020-02-18 20:20:15 +01:00
|
|
|
|
2021-06-29 13:40:37 +02:00
|
|
|
// after_char returns the contents after the first occurence of `sub` character in the string.
|
2021-06-29 13:50:55 +02:00
|
|
|
// If the substring is not found, it returns the full input string.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert '23:34:45.234'.after_char(`:`) == '34:45.234'
|
2021-06-29 13:50:55 +02:00
|
|
|
// Example: assert 'abcd'.after_char(`:`) == 'abcd'
|
2021-06-29 13:40:37 +02:00
|
|
|
pub fn (s string) after_char(sub byte) string {
|
|
|
|
mut pos := -1
|
2020-05-18 05:10:56 +02:00
|
|
|
for i, c in s {
|
2021-06-29 13:40:37 +02:00
|
|
|
if c == sub {
|
2020-05-18 05:10:56 +02:00
|
|
|
pos = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 13:40:37 +02:00
|
|
|
if pos == -1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2020-05-18 05:10:56 +02:00
|
|
|
}
|
2021-01-22 10:26:07 +01:00
|
|
|
return s[pos + 1..]
|
2020-05-18 05:10:56 +02:00
|
|
|
}
|
|
|
|
|
2021-05-02 18:31:47 +02:00
|
|
|
// join joins a string array into a string using `sep` separator.
|
2020-12-12 11:10:29 +01:00
|
|
|
// Example: assert ['Hello','V'].join(' ') == 'Hello V'
|
2021-05-02 18:31:47 +02:00
|
|
|
pub fn (a []string) join(sep string) string {
|
2019-06-22 20:20:28 +02:00
|
|
|
if a.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
mut len := 0
|
2020-04-27 15:16:31 +02:00
|
|
|
for val in a {
|
2021-05-02 18:31:47 +02:00
|
|
|
len += val.len + sep.len
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2021-05-02 18:31:47 +02:00
|
|
|
len -= sep.len
|
2019-06-22 20:20:28 +02:00
|
|
|
// Allocate enough memory
|
2021-04-04 16:43:32 +02:00
|
|
|
mut res := string{
|
2021-06-12 10:27:08 +02:00
|
|
|
str: unsafe { malloc_noscan(len + 1) }
|
2021-04-07 14:25:45 +02:00
|
|
|
len: len
|
2021-04-04 16:43:32 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
mut idx := 0
|
|
|
|
for i, val in a {
|
2021-04-13 10:29:33 +02:00
|
|
|
unsafe {
|
2021-08-12 20:46:38 +02:00
|
|
|
vmemcpy(res.str + idx, val.str, val.len)
|
2021-04-13 11:01:23 +02:00
|
|
|
idx += val.len
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2021-05-02 18:31:47 +02:00
|
|
|
// Add sep if it's not last
|
2021-04-13 11:01:23 +02:00
|
|
|
if i != a.len - 1 {
|
2021-04-13 10:29:33 +02:00
|
|
|
unsafe {
|
2021-08-12 20:46:38 +02:00
|
|
|
vmemcpy(res.str + idx, sep.str, sep.len)
|
2021-05-02 18:31:47 +02:00
|
|
|
idx += sep.len
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2021-04-13 10:29:33 +02:00
|
|
|
res.str[res.len] = 0
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// join joins a string array into a string using a `\n` newline delimiter.
|
2019-06-30 13:06:46 +02:00
|
|
|
pub fn (s []string) join_lines() string {
|
2019-06-22 20:20:28 +02:00
|
|
|
return s.join('\n')
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// reverse returns a reversed string.
|
|
|
|
// Example: assert 'Hello V'.reverse() == 'V olleH'
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (s string) reverse() string {
|
2020-02-20 20:14:21 +01:00
|
|
|
if s.len == 0 || s.len == 1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-12-16 19:29:32 +01:00
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
mut res := string{
|
2021-06-12 10:27:08 +02:00
|
|
|
str: unsafe { malloc_noscan(s.len + 1) }
|
2020-05-18 21:38:06 +02:00
|
|
|
len: s.len
|
2019-06-27 02:03:19 +02:00
|
|
|
}
|
|
|
|
for i := s.len - 1; i >= 0; i-- {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
res.str[s.len - i - 1] = s[i]
|
|
|
|
}
|
2019-06-27 02:03:19 +02:00
|
|
|
}
|
2021-05-29 15:45:26 +02:00
|
|
|
unsafe {
|
|
|
|
res.str[res.len] = 0
|
|
|
|
}
|
2019-06-27 02:03:19 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-10-27 20:29:55 +01:00
|
|
|
// limit returns a portion of the string, starting at `0` and extending for a given number of characters afterward.
|
2019-06-22 20:20:28 +02:00
|
|
|
// 'hello'.limit(2) => 'he'
|
|
|
|
// 'hi'.limit(10) => 'hi'
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (s string) limit(max int) string {
|
2021-07-03 19:14:09 +02:00
|
|
|
u := s.runes()
|
2019-06-22 20:20:28 +02:00
|
|
|
if u.len <= max {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2021-07-03 19:14:09 +02:00
|
|
|
return u[0..max].string()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// hash returns an integer hash of the string.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn (s string) hash() int {
|
2020-07-11 00:18:52 +02:00
|
|
|
mut h := u32(0)
|
2019-08-17 21:19:37 +02:00
|
|
|
if h == 0 && s.len > 0 {
|
|
|
|
for c in s {
|
2020-07-11 00:18:52 +02:00
|
|
|
h = h * 31 + u32(c)
|
2019-07-10 09:48:10 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2020-07-11 00:18:52 +02:00
|
|
|
return int(h)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// bytes returns the string converted to a byte array.
|
2019-07-15 17:49:01 +02:00
|
|
|
pub fn (s string) bytes() []byte {
|
|
|
|
if s.len == 0 {
|
2019-11-14 07:18:07 +01:00
|
|
|
return []
|
2019-07-15 17:49:01 +02:00
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
mut buf := []byte{len: s.len}
|
2021-08-12 20:46:38 +02:00
|
|
|
unsafe { vmemcpy(buf.data, s.str, s.len) }
|
2019-07-15 17:49:01 +02:00
|
|
|
return buf
|
|
|
|
}
|
2019-09-26 21:54:53 +02:00
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// repeat returns a new string with `count` number of copies of the string it was called on.
|
2019-09-26 21:54:53 +02:00
|
|
|
pub fn (s string) repeat(count int) string {
|
2020-02-29 15:25:31 +01:00
|
|
|
if count < 0 {
|
|
|
|
panic('string.repeat: count is negative: $count')
|
|
|
|
} else if count == 0 {
|
|
|
|
return ''
|
|
|
|
} else if count == 1 {
|
2021-03-18 21:22:43 +01:00
|
|
|
return s.clone()
|
2019-09-26 21:54:53 +02:00
|
|
|
}
|
2021-06-12 10:27:08 +02:00
|
|
|
mut ret := unsafe { malloc_noscan(s.len * count + 1) }
|
2019-12-19 21:52:45 +01:00
|
|
|
for i in 0 .. count {
|
|
|
|
for j in 0 .. s.len {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
ret[i * s.len + j] = s[j]
|
|
|
|
}
|
2019-09-28 01:51:42 +02:00
|
|
|
}
|
2019-09-26 21:54:53 +02:00
|
|
|
}
|
2021-05-29 15:45:26 +02:00
|
|
|
new_len := s.len * count
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2020-08-10 18:05:26 +02:00
|
|
|
ret[new_len] = 0
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2021-05-29 15:45:26 +02:00
|
|
|
return unsafe { ret.vstring_with_len(new_len) }
|
2019-09-26 21:54:53 +02:00
|
|
|
}
|
2020-03-15 05:46:12 +01:00
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// fields returns a string array of the string split by `\t` and ` `
|
2021-05-11 12:59:44 +02:00
|
|
|
// Example: assert '\t\tv = v'.fields() == ['v', '=', 'v']
|
|
|
|
// Example: assert ' sss ssss'.fields() == ['sss', 'ssss']
|
2020-06-08 13:10:47 +02:00
|
|
|
pub fn (s string) fields() []string {
|
2021-03-16 06:29:14 +01:00
|
|
|
mut res := []string{}
|
|
|
|
mut word_start := 0
|
2021-03-16 18:45:27 +01:00
|
|
|
mut word_len := 0
|
2021-03-16 06:29:14 +01:00
|
|
|
mut is_in_word := false
|
|
|
|
mut is_space := false
|
|
|
|
for i, c in s {
|
2021-05-05 23:31:25 +02:00
|
|
|
is_space = c in [32, 9, 10]
|
2021-03-16 18:45:27 +01:00
|
|
|
if !is_space {
|
|
|
|
word_len++
|
|
|
|
}
|
2021-03-16 06:29:14 +01:00
|
|
|
if !is_in_word && !is_space {
|
|
|
|
word_start = i
|
|
|
|
is_in_word = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if is_space && is_in_word {
|
2021-03-16 18:45:27 +01:00
|
|
|
res << s[word_start..word_start + word_len]
|
2021-03-16 06:29:14 +01:00
|
|
|
is_in_word = false
|
2021-03-16 18:45:27 +01:00
|
|
|
word_len = 0
|
2021-03-16 06:29:14 +01:00
|
|
|
word_start = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 18:45:27 +01:00
|
|
|
if is_in_word && word_len > 0 {
|
2021-03-16 06:29:14 +01:00
|
|
|
// collect the remainder word at the end
|
|
|
|
res << s[word_start..s.len]
|
|
|
|
}
|
|
|
|
return res
|
2020-06-08 13:10:47 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// strip_margin allows multi-line strings to be formatted in a way that removes white-space
|
2020-03-15 05:46:12 +01:00
|
|
|
// before a delimeter. by default `|` is used.
|
|
|
|
// Note: the delimiter has to be a byte at this time. That means surrounding
|
|
|
|
// the value in ``.
|
2020-03-16 15:46:09 +01:00
|
|
|
//
|
2020-03-15 05:46:12 +01:00
|
|
|
// Example:
|
|
|
|
// st := 'Hello there,
|
2020-12-03 16:02:48 +01:00
|
|
|
// |this is a string,
|
|
|
|
// | Everything before the first | is removed'.strip_margin()
|
2020-03-15 05:46:12 +01:00
|
|
|
// Returns:
|
|
|
|
// Hello there,
|
|
|
|
// this is a string,
|
2020-12-03 16:02:48 +01:00
|
|
|
// Everything before the first | is removed
|
2020-04-11 10:06:03 +02:00
|
|
|
pub fn (s string) strip_margin() string {
|
2020-12-03 16:02:48 +01:00
|
|
|
return s.strip_margin_custom(`|`)
|
2020-04-11 10:06:03 +02:00
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
|
2020-12-12 11:10:29 +01:00
|
|
|
// strip_margin_custom does the same as `strip_margin` but will use `del` as delimiter instead of `|`
|
2021-05-05 23:31:25 +02:00
|
|
|
[direct_array_access]
|
2020-04-11 10:06:03 +02:00
|
|
|
pub fn (s string) strip_margin_custom(del byte) string {
|
|
|
|
mut sep := del
|
|
|
|
if sep.is_space() {
|
2020-12-03 16:02:48 +01:00
|
|
|
eprintln('Warning: `strip_margin` cannot use white-space as a delimiter')
|
|
|
|
eprintln(' Defaulting to `|`')
|
2020-04-11 10:06:03 +02:00
|
|
|
sep = `|`
|
2020-03-15 05:46:12 +01:00
|
|
|
}
|
|
|
|
// don't know how much space the resulting string will be, but the max it
|
|
|
|
// can be is this big
|
2021-06-12 10:27:08 +02:00
|
|
|
mut ret := unsafe { malloc_noscan(s.len + 1) }
|
2020-03-15 05:46:12 +01:00
|
|
|
mut count := 0
|
|
|
|
for i := 0; i < s.len; i++ {
|
2021-05-05 23:31:25 +02:00
|
|
|
if s[i] in [10, 13] {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
ret[count] = s[i]
|
|
|
|
}
|
2020-03-16 15:46:09 +01:00
|
|
|
count++
|
|
|
|
// CRLF
|
2021-05-05 23:31:25 +02:00
|
|
|
if s[i] == 13 && i < s.len - 1 && s[i + 1] == 10 {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
2020-12-03 16:02:48 +01:00
|
|
|
ret[count] = s[i + 1]
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2020-03-15 05:46:12 +01:00
|
|
|
count++
|
2020-03-16 15:46:09 +01:00
|
|
|
i++
|
2020-03-15 05:46:12 +01:00
|
|
|
}
|
|
|
|
for s[i] != sep {
|
|
|
|
i++
|
|
|
|
if i >= s.len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
ret[count] = s[i]
|
|
|
|
}
|
2020-03-15 05:46:12 +01:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
ret[count] = 0
|
2020-08-10 18:05:26 +02:00
|
|
|
return ret.vstring_with_len(count)
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2020-03-15 05:46:12 +01:00
|
|
|
}
|