2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module builtin
|
|
|
|
|
2019-11-01 00:28:52 +03:00
|
|
|
import strconv
|
2020-12-03 18:02:48 +03:00
|
|
|
|
2019-09-28 13:54:30 +03:00
|
|
|
/*
|
2019-10-04 15:48:09 +03: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 13:54:30 +03:00
|
|
|
the V string also has an integer length attached.
|
2019-10-04 15:48:09 +03:00
|
|
|
|
2019-09-28 13:54:30 +03:00
|
|
|
This tradeoff is made, since V strings are created just *once*,
|
|
|
|
but potentially used *many times* over their lifetime.
|
2019-10-04 15:48:09 +03:00
|
|
|
|
2019-09-28 13:54:30 +03: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 15:48:09 +03:00
|
|
|
strictly necessary from the V point of view, that additional 0
|
2019-09-28 13:54:30 +03:00
|
|
|
is *very useful for C interoperability*.
|
2019-10-04 15:48:09 +03:00
|
|
|
|
|
|
|
The V string implementation also has an integer .len field,
|
|
|
|
containing the length of the .str field, excluding the
|
2019-09-28 13:54:30 +03:00
|
|
|
terminating 0 (just like the C's strlen(s) would do).
|
2019-10-04 15:48:09 +03:00
|
|
|
|
2019-09-28 13:54:30 +03: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 15:48:09 +03: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 13:54:30 +03: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 15:48:09 +03: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 13:54:30 +03:00
|
|
|
low level string creating functions inside this module.
|
2019-10-04 15:48:09 +03: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 13:54:30 +03:00
|
|
|
when used with modules using C functions (for example os and so on).
|
|
|
|
*/
|
2019-10-24 12:47:21 +03:00
|
|
|
pub struct string {
|
2019-06-22 21:20:28 +03:00
|
|
|
pub:
|
2021-01-15 04:26:06 +03:00
|
|
|
str byteptr // points to a C style 0 terminated string of bytes.
|
|
|
|
len int // the length of the .str field, excluding the ending 0 byte. It is always equal to strlen(.str).
|
2020-07-11 00:59:19 +03:00
|
|
|
mut:
|
2020-06-30 18:28:28 +03:00
|
|
|
is_lit int
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
|
2020-06-30 18:28:28 +03:00
|
|
|
// mut:
|
|
|
|
// hash_cache int
|
|
|
|
//
|
|
|
|
// 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.
|
2020-12-12 13:10:29 +03:00
|
|
|
// ---------> ^^^^^^^^^ calling free on these is a bug.
|
2020-06-30 18:28:28 +03:00
|
|
|
// Any other value means that the string has been corrupted.
|
2019-10-24 12:47:21 +03:00
|
|
|
pub struct ustring {
|
2020-04-27 23:53:26 +03:00
|
|
|
pub mut:
|
2019-06-22 21:20:28 +03:00
|
|
|
s string
|
|
|
|
runes []int
|
|
|
|
len int
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// vstrlen returns the V length of the C string `s` (0 terminator is not counted).
|
2020-08-09 12:22:11 +03:00
|
|
|
[unsafe]
|
2019-09-15 15:36:05 +03:00
|
|
|
pub fn vstrlen(s byteptr) int {
|
2021-01-05 20:59:51 +03:00
|
|
|
return unsafe { C.strlen(charptr(s)) }
|
2019-11-28 09:46:10 +03:00
|
|
|
}
|
2019-09-15 15:36:05 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// tos converts a C string to a V string.
|
2019-08-17 22:19:37 +03:00
|
|
|
// String data is reused, not copied.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn tos(s byteptr, len int) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
// This should never happen.
|
2019-10-04 15:48:09 +03:00
|
|
|
if s == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
panic('tos(): nil string')
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
return string{
|
2019-06-22 21:20:28 +03:00
|
|
|
str: s
|
|
|
|
len: len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// tos_clone returns a copy of `s`.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn tos_clone(s byteptr) string {
|
2021-02-15 18:15:52 +03:00
|
|
|
return unsafe { tos2(s) }.clone()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// tos2 does the same as `tos`, but also calculates the length. Called by `string(bytes)` casts.
|
2019-08-17 22:19:37 +03:00
|
|
|
// Used only internally.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2019-10-31 13:08:01 +03:00
|
|
|
pub fn tos2(s byteptr) string {
|
2019-10-04 15:48:09 +03:00
|
|
|
if s == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
panic('tos2: nil string')
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
return string{
|
2019-10-04 15:48:09 +03:00
|
|
|
str: s
|
2021-02-14 21:31:42 +03:00
|
|
|
len: unsafe { vstrlen(s) }
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// tos3 does the same as `tos2`, but for char*, to avoid warnings.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2019-12-02 11:37:35 +03:00
|
|
|
pub fn tos3(s charptr) string {
|
2019-10-04 15:48:09 +03:00
|
|
|
if s == 0 {
|
|
|
|
panic('tos3: nil string')
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
return string{
|
2019-10-04 15:48:09 +03:00
|
|
|
str: byteptr(s)
|
2021-01-05 20:59:51 +03:00
|
|
|
len: unsafe { C.strlen(s) }
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// tos4 does the same as `tos2`, but returns an empty string on nil ptr.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2020-12-09 22:10:41 +03:00
|
|
|
pub fn tos4(s byteptr) string {
|
|
|
|
if s == 0 {
|
2020-12-12 13:10:29 +03:00
|
|
|
return ''
|
2020-12-09 22:10:41 +03:00
|
|
|
}
|
2021-02-15 18:15:52 +03:00
|
|
|
return unsafe { tos2(s) }
|
2020-12-09 22:10:41 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// tos5 does the same as `tos4`, but for char*, to avoid warnings.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2020-12-09 22:10:41 +03:00
|
|
|
pub fn tos5(s charptr) string {
|
|
|
|
if s == 0 {
|
2020-12-12 13:10:29 +03:00
|
|
|
return ''
|
2020-12-09 22:10:41 +03:00
|
|
|
}
|
2021-02-15 18:15:52 +03:00
|
|
|
return unsafe { tos3(s) }
|
2020-12-09 22:10:41 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 21:34:58 +03:00
|
|
|
[deprecated]
|
2020-05-07 19:05:54 +03:00
|
|
|
pub fn tos_lit(s charptr) string {
|
2020-12-04 21:34:58 +03:00
|
|
|
eprintln('warning: `tos_lit` has been deprecated, use `_SLIT` instead')
|
2020-05-07 19:05:54 +03:00
|
|
|
return string{
|
|
|
|
str: byteptr(s)
|
2021-01-05 20:59:51 +03:00
|
|
|
len: unsafe { C.strlen(s) }
|
2020-06-30 18:28:28 +03:00
|
|
|
is_lit: 1
|
2020-05-07 19:05:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// vstring converts a C style string to a V string. NB: the string data is reused, NOT copied.
|
2020-08-10 19:05:26 +03:00
|
|
|
[unsafe]
|
|
|
|
pub fn (bp byteptr) vstring() string {
|
|
|
|
return string{
|
|
|
|
str: bp
|
2021-01-05 20:59:51 +03:00
|
|
|
len: unsafe { C.strlen(charptr(bp)) }
|
2020-08-12 06:54:51 +03:00
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// vstring_with_len converts a C style string to a V string. NB: the string data is reused, NOT copied.
|
2020-08-10 19:05:26 +03:00
|
|
|
[unsafe]
|
|
|
|
pub fn (bp byteptr) vstring_with_len(len int) string {
|
|
|
|
return string{
|
|
|
|
str: bp
|
|
|
|
len: len
|
2020-08-12 06:54:51 +03:00
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
}
|
2020-05-07 19:05:54 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// vstring converts C char* to V string. NB: the string data is reused, NOT copied.
|
2020-11-14 20:43:42 +03:00
|
|
|
[unsafe]
|
|
|
|
pub fn (cp charptr) vstring() string {
|
|
|
|
return string{
|
|
|
|
str: byteptr(cp)
|
2021-01-05 20:59:51 +03:00
|
|
|
len: unsafe { C.strlen(cp) }
|
2020-11-14 20:43:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// vstring_with_len converts C char* to V string. NB: the string data is reused, NOT copied.
|
2020-11-14 20:43:42 +03:00
|
|
|
[unsafe]
|
|
|
|
pub fn (cp charptr) vstring_with_len(len int) string {
|
|
|
|
return string{
|
|
|
|
str: byteptr(cp)
|
|
|
|
len: len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// clone_static returns an independent copy of a given array.
|
2020-05-06 19:03:44 +03:00
|
|
|
// It should be used only in -autofree generated code.
|
|
|
|
fn (a string) clone_static() string {
|
|
|
|
return a.clone()
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// clone returns a copy of the V string `a`.
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (a string) clone() string {
|
2020-12-11 07:03:25 +03:00
|
|
|
if a == '' {
|
2020-12-12 13:10:29 +03:00
|
|
|
// TODO perf? an extra check in each clone() is not nice.
|
2020-12-11 07:03:25 +03:00
|
|
|
return ''
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
mut b := string{
|
2021-01-05 20:59:51 +03:00
|
|
|
str: unsafe { malloc(a.len + 1) }
|
2020-05-18 22:38:06 +03:00
|
|
|
len: a.len
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
2020-07-20 17:44:35 +03:00
|
|
|
C.memcpy(b.str, a.str, a.len)
|
2020-07-15 22:56:50 +03:00
|
|
|
b.str[a.len] = `\0`
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
/*
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) cstr() byteptr {
|
2019-06-22 21:20:28 +03:00
|
|
|
clone := s.clone()
|
|
|
|
return clone.str
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
*/
|
2020-12-12 13:10:29 +03:00
|
|
|
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
|
2020-08-09 12:22:11 +03:00
|
|
|
[unsafe]
|
2019-11-28 11:46:52 +03:00
|
|
|
pub fn cstring_to_vstring(cstr byteptr) string {
|
2021-02-15 18:15:52 +03:00
|
|
|
return unsafe { tos_clone(cstr) }
|
2019-11-28 11:46:52 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// replace_once replaces the first occurence of `rep` with the string passed in `with`.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn (s string) replace_once(rep string, with string) string {
|
2021-01-15 04:26:06 +03: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 18:43:22 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// replace replaces all occurences of `rep` with the string passed in `with`.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn (s string) replace(rep string, with string) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
if s.len == 0 || rep.len == 0 {
|
2020-05-25 09:17:36 +03:00
|
|
|
return s.clone()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// TODO PERF Allocating ints is expensive. Should be a stack array
|
|
|
|
// Get locations of all reps within this string
|
2020-04-26 10:17:13 +03:00
|
|
|
mut idxs := []int{}
|
2020-12-03 18:02:48 +03:00
|
|
|
defer {
|
2021-01-05 20:59:51 +03:00
|
|
|
unsafe { idxs.free() }
|
2020-10-21 20:44:31 +03:00
|
|
|
}
|
2019-12-10 18:50:21 +03:00
|
|
|
mut idx := 0
|
2019-06-29 18:29:29 +03:00
|
|
|
for {
|
2019-12-10 18:50:21 +03:00
|
|
|
idx = s.index_after(rep, idx)
|
|
|
|
if idx == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
idxs << idx
|
2019-12-27 07:20:06 +03:00
|
|
|
idx += rep.len
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// Dont change the string if there's nothing to replace
|
|
|
|
if idxs.len == 0 {
|
2020-05-25 09:17:36 +03:00
|
|
|
return s.clone()
|
2019-06-22 21:20:28 +03: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-02-14 21:31:42 +03:00
|
|
|
mut b := unsafe { malloc(new_len + 1) } // add a newline just in case
|
2019-06-22 21:20:28 +03:00
|
|
|
// Fill the new string
|
|
|
|
mut idx_pos := 0
|
|
|
|
mut cur_idx := idxs[idx_pos]
|
2019-06-28 15:19:46 +03:00
|
|
|
mut b_i := 0
|
2019-06-22 21:20:28 +03:00
|
|
|
for i := 0; i < s.len; i++ {
|
|
|
|
if i == cur_idx {
|
2020-04-07 19:51:39 +03:00
|
|
|
// Reached the location of rep, replace it with "with"
|
2020-12-03 18:02:48 +03:00
|
|
|
for j in 0 .. with.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
b[b_i] = with[j]
|
|
|
|
}
|
2019-06-22 21:20:28 +03: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-12-10 14:32:12 +03:00
|
|
|
cur_idx = idxs[idx_pos]
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
} else {
|
2020-04-07 19:51:39 +03:00
|
|
|
// Rep doesnt start here, just copy
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
b[b_i] = s[i]
|
|
|
|
}
|
2019-12-10 14:32:12 +03:00
|
|
|
b_i++
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
b[new_len] = `\0`
|
|
|
|
return tos(b, new_len)
|
|
|
|
}
|
2019-12-10 14:32:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RepIndex {
|
2019-12-19 23:52:45 +03:00
|
|
|
idx int
|
2019-12-10 14:32:12 +03:00
|
|
|
val_idx int
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// compare_rep_index returns the result of comparing RepIndex `a` and `b`.
|
2020-10-15 13:32:28 +03:00
|
|
|
fn compare_rep_index(a &RepIndex, b &RepIndex) int {
|
2020-02-04 19:44:39 +03:00
|
|
|
if a.idx < b.idx {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if a.idx > b.idx {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// sort2 sorts the RepIndex array using `compare_rep_index`.
|
2020-08-12 06:54:51 +03:00
|
|
|
fn (mut a []RepIndex) sort2() {
|
2019-12-10 14:32:12 +03:00
|
|
|
a.sort_with_compare(compare_rep_index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
/*
|
|
|
|
fn (a RepIndex) < (b RepIndex) bool {
|
|
|
|
return a.idx < b.idx
|
|
|
|
}
|
|
|
|
*/
|
2020-12-12 13:10:29 +03: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'
|
2019-12-10 14:32:12 +03:00
|
|
|
pub fn (s string) replace_each(vals []string) string {
|
|
|
|
if s.len == 0 || vals.len == 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
if vals.len % 2 != 0 {
|
2020-04-26 13:40:39 +03:00
|
|
|
println('string.replace_each(): odd number of strings')
|
2019-12-10 14:32:12 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
// `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
|
2020-04-26 10:17:13 +03:00
|
|
|
mut idxs := []RepIndex{}
|
2019-12-10 14:32:12 +03:00
|
|
|
mut idx := 0
|
2019-12-19 23:52:45 +03:00
|
|
|
for rep_i := 0; rep_i < vals.len; rep_i += 2 {
|
2019-12-10 14:32:12 +03:00
|
|
|
// vals: ['rep1, 'with1', 'rep2', 'with2']
|
|
|
|
rep := vals[rep_i]
|
2019-12-19 23:52:45 +03:00
|
|
|
with := vals[rep_i + 1]
|
2019-12-10 14:32:12 +03:00
|
|
|
for {
|
|
|
|
idx = s.index_after(rep, idx)
|
|
|
|
if idx == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// We need to remember both the position in the string,
|
|
|
|
// and which rep/with pair it refers to.
|
2020-12-03 18:02:48 +03:00
|
|
|
idxs << RepIndex{
|
|
|
|
idx: idx
|
|
|
|
val_idx: rep_i
|
2020-03-16 17:46:09 +03:00
|
|
|
}
|
2020-09-12 03:31:06 +03:00
|
|
|
idx += rep.len
|
2019-12-10 14:32:12 +03:00
|
|
|
new_len += with.len - rep.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Dont change the string if there's nothing to replace
|
|
|
|
if idxs.len == 0 {
|
|
|
|
return s
|
|
|
|
}
|
2020-08-12 06:54:51 +03:00
|
|
|
idxs.sort2()
|
2021-02-14 21:31:42 +03:00
|
|
|
mut b := unsafe { malloc(new_len + 1) } // add a \0 just in case
|
2019-12-10 14:32:12 +03: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 19:51:39 +03:00
|
|
|
// Reached the location of rep, replace it with "with"
|
2019-12-10 14:32:12 +03:00
|
|
|
rep := vals[cur_idx.val_idx]
|
2019-12-19 23:52:45 +03:00
|
|
|
with := vals[cur_idx.val_idx + 1]
|
2020-12-03 18:02:48 +03:00
|
|
|
for j in 0 .. with.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
b[b_i] = with[j]
|
|
|
|
}
|
2019-12-10 14:32:12 +03: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 21:20:28 +03:00
|
|
|
cur_idx = idxs[idx_pos]
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
} else {
|
2020-04-07 19:51:39 +03:00
|
|
|
// Rep doesnt start here, just copy
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
b[b_i] = s.str[i]
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
b_i++
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
b[new_len] = `\0`
|
|
|
|
return tos(b, new_len)
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// bool returns `true` if the string equals the word "true" it will return `false` otherwise.
|
2019-12-09 17:10:44 +03:00
|
|
|
pub fn (s string) bool() bool {
|
|
|
|
return s == 'true' || s == 't' // TODO t for pg, remove
|
|
|
|
}
|
2019-10-25 23:41:18 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// int returns the value of the string as an integer `'1'.int() == 1`.
|
2019-10-25 23:41:18 +03:00
|
|
|
pub fn (s string) int() int {
|
2019-12-19 23:52:45 +03:00
|
|
|
return int(strconv.common_parse_int(s, 0, 32, false, false))
|
2019-10-25 23:41:18 +03:00
|
|
|
}
|
2019-06-25 11:04:02 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// i64 returns the value of the string as i64 `'1'.i64() == i64(1)`.
|
2019-07-10 17:05:39 +03:00
|
|
|
pub fn (s string) i64() i64 {
|
2019-11-28 09:46:10 +03:00
|
|
|
return strconv.common_parse_int(s, 0, 64, false, false)
|
2019-07-10 17:05:39 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// i8 returns the value of the string as i8 `'1'.i8() == i8(1)`.
|
2020-01-14 20:05:38 +03:00
|
|
|
pub fn (s string) i8() i8 {
|
|
|
|
return i8(strconv.common_parse_int(s, 0, 8, false, false))
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// i16 returns the value of the string as i16 `'1'.i16() == i16(1)`.
|
2020-01-14 20:05:38 +03:00
|
|
|
pub fn (s string) i16() i16 {
|
|
|
|
return i16(strconv.common_parse_int(s, 0, 16, false, false))
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`.
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) f32() f32 {
|
2019-12-19 23:52:45 +03:00
|
|
|
// return C.atof(charptr(s.str))
|
2019-12-17 01:07:13 +03:00
|
|
|
return f32(strconv.atof64(s))
|
2019-06-25 11:04:02 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// f64 returns the value of the string as f64 `'1.0'.f64() == f64(1)`.
|
2019-07-17 21:11:14 +03:00
|
|
|
pub fn (s string) f64() f64 {
|
2019-12-19 23:52:45 +03:00
|
|
|
// return C.atof(charptr(s.str))
|
2019-12-17 01:07:13 +03:00
|
|
|
return strconv.atof64(s)
|
2019-07-17 21:11:14 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// u16 returns the value of the string as u16 `'1'.u16() == u16(1)`.
|
2020-01-14 20:05:38 +03:00
|
|
|
pub fn (s string) u16() u16 {
|
|
|
|
return u16(strconv.common_parse_uint(s, 0, 16, false, false))
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// u32 returns the value of the string as u32 `'1'.u32() == u32(1)`.
|
2019-07-17 21:11:14 +03:00
|
|
|
pub fn (s string) u32() u32 {
|
2019-11-28 09:46:10 +03:00
|
|
|
return u32(strconv.common_parse_uint(s, 0, 32, false, false))
|
2019-07-17 21:11:14 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// u64 returns the value of the string as u64 `'1'.u64() == u64(1)`.
|
2019-07-17 21:11:14 +03:00
|
|
|
pub fn (s string) u64() u64 {
|
2019-11-28 09:46:10 +03:00
|
|
|
return strconv.common_parse_uint(s, 0, 64, false, false)
|
2019-07-17 21:11:14 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// eq implements the `s == a` (equal) operator.
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (s string) eq(a string) bool {
|
2020-04-27 08:13:36 +03:00
|
|
|
if s.str == 0 {
|
2019-12-19 23:52:45 +03:00
|
|
|
// should never happen
|
2019-06-22 21:20:28 +03:00
|
|
|
panic('string.eq(): nil string')
|
|
|
|
}
|
|
|
|
if s.len != a.len {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-20 20:06:41 +03:00
|
|
|
unsafe {
|
|
|
|
return C.memcmp(s.str, a.str, a.len) == 0
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// ne implements the `s != a` (not equal) operator.
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (s string) ne(a string) bool {
|
|
|
|
return !s.eq(a)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// lt implements the `s < a` (less than) operator.
|
2019-06-23 21:25:50 +03:00
|
|
|
fn (s string) lt(a string) bool {
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. s.len {
|
2019-06-23 21:25:50 +03:00
|
|
|
if i >= a.len || s[i] > a[i] {
|
2019-06-22 21:20:28 +03:00
|
|
|
return false
|
2020-12-03 18:02:48 +03:00
|
|
|
} else if s[i] < a[i] {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 21:25:50 +03:00
|
|
|
if s.len < a.len {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// le implements the `s <= a` (less than or equal to) operator.
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (s string) le(a string) bool {
|
2019-06-23 21:25:50 +03:00
|
|
|
return s.lt(a) || s.eq(a)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// gt implements the `s > a` (greater than) operator.
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (s string) gt(a string) bool {
|
2019-06-23 21:25:50 +03:00
|
|
|
return !s.le(a)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// ge implements the `s >= a` (greater than or equal to) operator.
|
2019-06-23 21:25:50 +03:00
|
|
|
fn (s string) ge(a string) bool {
|
|
|
|
return !s.lt(a)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO `fn (s string) + (a string)` ? To be consistent with operator overloading syntax.
|
2020-12-12 13:10:29 +03:00
|
|
|
// add concatenates string with the string given in `s`.
|
2019-07-20 17:43:33 +03:00
|
|
|
fn (s string) add(a string) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
new_len := a.len + s.len
|
2019-12-19 23:52:45 +03:00
|
|
|
mut res := string{
|
2021-02-14 21:31:42 +03:00
|
|
|
str: unsafe { malloc(new_len + 1) }
|
2020-05-18 22:38:06 +03:00
|
|
|
len: new_len
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
for j in 0 .. s.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
res.str[j] = s.str[j]
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
for j in 0 .. a.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
res.str[s.len + j] = a.str[j]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
res.str[new_len] = `\0` // V strings are not null terminated, but just in case
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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 21:20:28 +03:00
|
|
|
pub fn (s string) split(delim string) []string {
|
2019-12-01 16:10:13 +03:00
|
|
|
return s.split_nth(delim, 0)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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.
|
2019-12-01 16:10:13 +03:00
|
|
|
pub fn (s string) split_nth(delim string, nth int) []string {
|
2020-04-26 10:17:13 +03:00
|
|
|
mut res := []string{}
|
2019-12-01 16:10:13 +03:00
|
|
|
mut i := 0
|
2019-06-22 21:20:28 +03:00
|
|
|
if delim.len == 0 {
|
2019-12-01 16:10:13 +03:00
|
|
|
i = 1
|
2019-11-10 19:37:36 +03:00
|
|
|
for ch in s {
|
2019-12-01 16:10:13 +03:00
|
|
|
if nth > 0 && i >= nth {
|
2021-01-22 12:26:07 +03:00
|
|
|
res << s[i..]
|
2019-12-01 16:10:13 +03:00
|
|
|
break
|
|
|
|
}
|
2021-01-05 20:59:51 +03:00
|
|
|
res << ch.ascii_str()
|
2019-12-01 16:10:13 +03:00
|
|
|
i++
|
2019-11-10 19:37:36 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return res
|
|
|
|
}
|
2019-12-01 16:10:13 +03:00
|
|
|
mut start := 0
|
2020-12-08 11:51:47 +03:00
|
|
|
// Take the left part for each delimiter occurence
|
2019-12-01 16:10:13 +03:00
|
|
|
for i <= s.len {
|
2020-12-08 11:51:47 +03:00
|
|
|
is_delim := i + delim.len <= s.len && s.substr(i, i + delim.len) == delim
|
|
|
|
if is_delim {
|
|
|
|
val := s.substr(start, i)
|
|
|
|
was_last := nth > 0 && res.len == nth - 1
|
2020-01-24 22:12:36 +03:00
|
|
|
if was_last {
|
|
|
|
break
|
|
|
|
}
|
2019-12-01 16:10:13 +03:00
|
|
|
res << val
|
|
|
|
start = i + delim.len
|
2020-12-08 11:51:47 +03:00
|
|
|
i = start
|
|
|
|
} else {
|
|
|
|
i++
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-08 11:51:47 +03:00
|
|
|
// Then the remaining right part of the string
|
|
|
|
if nth < 1 || res.len < nth {
|
2021-01-22 12:26:07 +03:00
|
|
|
res << s[start..]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// split_into_lines splits the string by newline characters.
|
|
|
|
// Both `\n` and `\r\n` newline endings is supported.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn (s string) split_into_lines() []string {
|
2020-04-26 10:17:13 +03:00
|
|
|
mut res := []string{}
|
2019-06-22 21:20:28 +03:00
|
|
|
if s.len == 0 {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
mut start := 0
|
|
|
|
for i := 0; i < s.len; i++ {
|
2021-01-05 20:59:51 +03:00
|
|
|
is_lf := unsafe { s.str[i] } == `\n`
|
|
|
|
is_crlf := i != s.len - 1 && unsafe { s.str[i] == `\r` && s.str[i + 1] == `\n` }
|
2020-02-09 11:23:57 +03:00
|
|
|
is_eol := is_lf || is_crlf
|
2020-12-03 18:02:48 +03:00
|
|
|
is_last := if is_crlf { i == s.len - 2 } else { i == s.len - 1 }
|
2020-02-09 11:23:57 +03:00
|
|
|
if is_eol || is_last {
|
|
|
|
if is_last && !is_eol {
|
2019-06-22 21:20:28 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
line := s.substr(start, i)
|
|
|
|
res << line
|
2020-02-09 11:23:57 +03:00
|
|
|
if is_crlf {
|
|
|
|
i++
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
start = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-10-27 09:36:04 +03:00
|
|
|
// used internally for [2..4]
|
2020-10-15 13:32:28 +03:00
|
|
|
fn (s string) substr2(start int, _end int, end_max bool) string {
|
2019-10-27 09:36:04 +03:00
|
|
|
end := if end_max { s.len } else { _end }
|
|
|
|
return s.substr(start, end)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// substr returns the string between index positions `start` and `end`.
|
|
|
|
// Example: assert 'ABCD'.substr(1,3) == 'BC'
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn (s string) substr(start int, end int) string {
|
2020-12-03 18:02:48 +03:00
|
|
|
$if !no_bounds_checking ? {
|
2021-03-15 23:16:23 +03:00
|
|
|
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
|
|
|
|
/*
|
|
|
|
$if debug {
|
|
|
|
println('substr($start, $end) out of bounds (len=$s.len)')
|
|
|
|
println('s="$s"')
|
|
|
|
print_backtrace()
|
|
|
|
return ''
|
2021-03-15 23:12:11 +03:00
|
|
|
}
|
2021-03-15 23:16:23 +03:00
|
|
|
*/
|
|
|
|
panic('substr($start, $end) out of bounds (len=$s.len)')
|
2020-02-16 18:13:45 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
len := end - start
|
2020-12-11 07:12:18 +03:00
|
|
|
if len == s.len {
|
|
|
|
return s.clone()
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
mut res := string{
|
2021-02-14 21:31:42 +03:00
|
|
|
str: unsafe { malloc(len + 1) }
|
2020-05-18 22:38:06 +03:00
|
|
|
len: len
|
2019-07-22 17:51:33 +03:00
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
res.str[i] = s.str[start + i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
res.str[len] = `\0`
|
2019-07-22 17:51:33 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
/*
|
2019-06-22 21:20:28 +03:00
|
|
|
res := string {
|
|
|
|
str: s.str + start
|
|
|
|
len: len
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
*/
|
2019-08-06 06:54:47 +03:00
|
|
|
return res
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-01-15 04:26:06 +03: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.
|
|
|
|
fn (s string) index_(p string) int {
|
2020-02-20 22:14:21 +03:00
|
|
|
if p.len > s.len || p.len == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
return -1
|
|
|
|
}
|
2021-01-15 04:26:06 +03:00
|
|
|
if p.len > 2 {
|
|
|
|
return s.index_kmp(p)
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
mut i := 0
|
|
|
|
for i < s.len {
|
|
|
|
mut j := 0
|
2021-01-05 20:59:51 +03:00
|
|
|
for j < p.len && unsafe { s.str[i + j] == p.str[j] } {
|
2019-06-22 21:20:28 +03:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
2019-09-23 23:34:42 +03:00
|
|
|
return i
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
i++
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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 13:09:05 +03:00
|
|
|
pub fn (s string) index(p string) ?int {
|
2021-01-15 04:26:06 +03:00
|
|
|
idx := s.index_(p)
|
|
|
|
if idx == -1 {
|
2019-11-30 13:09:05 +03:00
|
|
|
return none
|
|
|
|
}
|
2021-01-15 04:26:06 +03:00
|
|
|
return idx
|
2019-11-30 13:09:05 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// index_kmp does KMP search.
|
2019-12-12 21:44:52 +03:00
|
|
|
fn (s string) index_kmp(p string) int {
|
2019-12-19 23:52:45 +03:00
|
|
|
if p.len > s.len {
|
|
|
|
return -1
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
mut prefix := []int{len: p.len}
|
2019-12-19 23:52:45 +03:00
|
|
|
mut j := 0
|
|
|
|
for i := 1; i < p.len; i++ {
|
2021-01-05 20:59:51 +03:00
|
|
|
for unsafe { p.str[j] != p.str[i] } && j > 0 {
|
2019-12-19 23:52:45 +03:00
|
|
|
j = prefix[j - 1]
|
|
|
|
}
|
2021-01-05 20:59:51 +03:00
|
|
|
if unsafe { p.str[j] == p.str[i] } {
|
2019-12-19 23:52:45 +03:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
prefix[i] = j
|
|
|
|
}
|
|
|
|
j = 0
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. s.len {
|
2021-01-05 20:59:51 +03:00
|
|
|
for unsafe { p.str[j] != s.str[i] } && j > 0 {
|
2019-12-19 23:52:45 +03:00
|
|
|
j = prefix[j - 1]
|
|
|
|
}
|
2021-01-05 20:59:51 +03:00
|
|
|
if unsafe { p.str[j] == s.str[i] } {
|
2019-12-19 23:52:45 +03:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
|
|
|
return i - p.len + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// index_any returns the position of any of the characters in the input string - if found.
|
2019-08-01 16:01:03 +03:00
|
|
|
pub fn (s string) index_any(chars string) int {
|
|
|
|
for c in chars {
|
2021-01-15 04:26:06 +03:00
|
|
|
idx := s.index_(c.ascii_str())
|
|
|
|
if idx == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return idx
|
2019-08-01 16:01:03 +03:00
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// last_index returns the position of the last occurence of the input string.
|
2021-01-15 04:26:06 +03:00
|
|
|
fn (s string) last_index_(p string) int {
|
2020-02-20 22:14:21 +03:00
|
|
|
if p.len > s.len || p.len == 0 {
|
2021-01-15 04:26:06 +03:00
|
|
|
return -1
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
mut i := s.len - p.len
|
|
|
|
for i >= 0 {
|
|
|
|
mut j := 0
|
2021-01-05 20:59:51 +03:00
|
|
|
for j < p.len && unsafe { s.str[i + j] == p.str[j] } {
|
2019-06-22 21:20:28 +03:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
}
|
2021-01-15 04:26:06 +03: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 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// index_after returns the position of the input string, starting search from `start` position.
|
2019-06-22 21:20:28 +03: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 20:59:51 +03:00
|
|
|
for j < p.len && unsafe { s.str[ii] == p.str[j] } {
|
2019-06-22 21:20:28 +03:00
|
|
|
j++
|
|
|
|
ii++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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.
|
2019-10-10 20:04:11 +03:00
|
|
|
pub fn (s string) index_byte(c byte) int {
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. s.len {
|
2021-01-05 20:59:51 +03:00
|
|
|
if unsafe { s.str[i] } == c {
|
2019-10-10 20:04:11 +03:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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.
|
2019-10-10 20:04:11 +03:00
|
|
|
pub fn (s string) last_index_byte(c byte) int {
|
2019-12-19 23:52:45 +03:00
|
|
|
for i := s.len - 1; i >= 0; i-- {
|
2021-01-05 20:59:51 +03:00
|
|
|
if unsafe { s.str[i] == c } {
|
2019-10-10 20:04:11 +03:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// count returns the number of occurrences of `substr` in the string.
|
|
|
|
// count returns -1 if no `substr` could be found.
|
2019-08-03 00:30:22 +03:00
|
|
|
pub fn (s string) count(substr string) int {
|
|
|
|
if s.len == 0 || substr.len == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2019-08-26 13:32:53 +03:00
|
|
|
if substr.len > s.len {
|
|
|
|
return 0
|
|
|
|
}
|
2019-08-03 00:30:22 +03:00
|
|
|
mut n := 0
|
2019-08-03 01:18:19 +03:00
|
|
|
mut i := 0
|
2019-08-03 00:30:22 +03:00
|
|
|
for {
|
2019-08-03 23:24:03 +03:00
|
|
|
i = s.index_after(substr, i)
|
2019-08-03 00:30:22 +03:00
|
|
|
if i == -1 {
|
|
|
|
return n
|
|
|
|
}
|
2019-08-03 01:18:19 +03:00
|
|
|
i += substr.len
|
2019-08-03 00:30:22 +03:00
|
|
|
n++
|
|
|
|
}
|
2019-08-08 10:49:56 +03:00
|
|
|
return 0 // TODO can never get here - v doesn't know that
|
2019-08-03 00:30:22 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// contains returns `true` if the string contains `substr`.
|
2020-07-29 22:48:50 +03:00
|
|
|
pub fn (s string) contains(substr string) bool {
|
|
|
|
if substr.len == 0 {
|
2020-06-14 12:24:15 +03:00
|
|
|
return true
|
|
|
|
}
|
2021-01-15 04:26:06 +03:00
|
|
|
if s.index_(substr) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-30 13:09:05 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// contains_any returns `true` if the string contains any chars in `chars`.
|
2020-07-29 22:48:50 +03:00
|
|
|
pub fn (s string) contains_any(chars string) bool {
|
|
|
|
for c in chars {
|
2021-01-05 20:59:51 +03:00
|
|
|
if c.ascii_str() in s {
|
2020-07-29 22:48:50 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// contains_any_substr returns `true` if the string contains any of the strings in `substrs`.
|
2020-07-29 22:48:50 +03: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 13:10:29 +03:00
|
|
|
// starts_with returns `true` if the string starts with `p`.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn (s string) starts_with(p string) bool {
|
2020-02-19 17:18:09 +03:00
|
|
|
if p.len > s.len {
|
2019-12-19 23:52:45 +03:00
|
|
|
return false
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. p.len {
|
2021-01-05 20:59:51 +03:00
|
|
|
if unsafe { s.str[i] != p.str[i] } {
|
2020-02-19 17:18:09 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// ends_with returns `true` if the string ends with `p`.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn (s string) ends_with(p string) bool {
|
|
|
|
if p.len > s.len {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. p.len {
|
2020-02-19 17:18:09 +03:00
|
|
|
if p[i] != s[s.len - p.len + i] {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-12 21:44:52 +03:00
|
|
|
}
|
2020-02-19 17:18:09 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// to_lower returns the string in all lowercase characters.
|
2019-06-22 21:20:28 +03:00
|
|
|
// TODO only works with ASCII
|
|
|
|
pub fn (s string) to_lower() string {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
mut b := malloc(s.len + 1)
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. s.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
b[i] = byte(C.tolower(s.str[i]))
|
|
|
|
}
|
2021-03-14 19:21:45 +03:00
|
|
|
b[s.len] = 0
|
2020-07-15 22:56:50 +03:00
|
|
|
return tos(b, s.len)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_lower returns `true` if all characters in the string is lowercase.
|
|
|
|
// Example: assert 'hello developer'.is_lower() == true
|
2020-04-12 13:09:05 +03:00
|
|
|
pub fn (s string) is_lower() bool {
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. s.len {
|
2020-04-12 13:09:05 +03:00
|
|
|
if s[i] >= `A` && s[i] <= `Z` {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// to_upper returns the string in all uppercase characters.
|
|
|
|
// Example: assert 'Hello V'.to_upper() == 'HELLO V'
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn (s string) to_upper() string {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
mut b := malloc(s.len + 1)
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. s.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
b[i] = byte(C.toupper(s.str[i]))
|
|
|
|
}
|
2021-03-14 19:21:45 +03:00
|
|
|
b[s.len] = 0
|
2020-07-15 22:56:50 +03:00
|
|
|
return tos(b, s.len)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_upper returns `true` if all characters in the string is uppercase.
|
|
|
|
// Example: assert 'HELLO V'.is_upper() == true
|
2020-04-12 13:09:05 +03:00
|
|
|
pub fn (s string) is_upper() bool {
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 0 .. s.len {
|
2020-04-12 13:09:05 +03:00
|
|
|
if s[i] >= `a` && s[i] <= `z` {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// capitalize returns the string with the first character capitalized.
|
|
|
|
// Example: assert 'hello'.capitalize() == 'Hello'
|
2019-08-26 13:32:53 +03:00
|
|
|
pub fn (s string) capitalize() string {
|
2020-02-20 13:33:38 +03:00
|
|
|
if s.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2021-01-05 20:59:51 +03:00
|
|
|
return s[0].ascii_str().to_upper() + s[1..]
|
2020-12-03 18:02:48 +03:00
|
|
|
// sl := s.to_lower()
|
2021-01-22 12:26:07 +03:00
|
|
|
// cap := sl[0].str().to_upper() + sl[1..]
|
2020-12-03 18:02:48 +03:00
|
|
|
// return cap
|
2019-08-26 13:32:53 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_capital returns `true` if the first character in the string is a capital letter.
|
|
|
|
// Example: assert 'Hello'.is_capital() == true
|
2020-04-12 13:09:05 +03:00
|
|
|
pub fn (s string) is_capital() bool {
|
|
|
|
if s.len == 0 || !(s[0] >= `A` && s[0] <= `Z`) {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
for i in 1 .. s.len {
|
2020-04-12 13:09:05 +03:00
|
|
|
if s[i] >= `A` && s[i] <= `Z` {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// title returns the string with each word capitalized.
|
|
|
|
// Example: assert 'hello v developer'.title() == 'Hello V Developer'
|
2019-08-26 13:32:53 +03:00
|
|
|
pub fn (s string) title() string {
|
2019-12-19 23:52:45 +03:00
|
|
|
words := s.split(' ')
|
2020-04-26 10:17:13 +03:00
|
|
|
mut tit := []string{}
|
2019-08-26 13:32:53 +03:00
|
|
|
for word in words {
|
|
|
|
tit << word.capitalize()
|
|
|
|
}
|
|
|
|
title := tit.join(' ')
|
2019-11-28 09:46:10 +03:00
|
|
|
return title
|
2019-08-26 13:32:53 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_title returns true if all words of the string is capitalized.
|
|
|
|
// Example: assert 'Hello V Developer'.is_title() == true
|
2020-04-12 13:09:05 +03: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 13:10:29 +03: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 13:32:28 +03:00
|
|
|
pub fn (s string) find_between(start string, end string) string {
|
2021-01-15 04:26:06 +03:00
|
|
|
start_pos := s.index_(start)
|
|
|
|
if start_pos == -1 {
|
|
|
|
return ''
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// First get everything to the right of 'start'
|
2021-01-22 12:26:07 +03:00
|
|
|
val := s[start_pos + start.len..]
|
2021-01-15 04:26:06 +03:00
|
|
|
end_pos := val.index_(end)
|
|
|
|
if end_pos == -1 {
|
|
|
|
return val
|
|
|
|
}
|
2021-01-22 12:26:07 +03:00
|
|
|
return val[..end_pos]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
/*
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn (a []string) to_c() voidptr {
|
2019-08-17 22:19:37 +03:00
|
|
|
mut res := malloc(sizeof(byteptr) * a.len)
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..a.len {
|
2019-06-22 21:20:28 +03:00
|
|
|
val := a[i]
|
2019-08-17 22:19:37 +03:00
|
|
|
res[i] = val.str
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
return res
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
*/
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_space returns `true` if the byte is a white space character.
|
|
|
|
// The following list is considered white space characters: ` `, `\n`, `\t`, `\v`, `\f`, `\r`, 0x85, 0xa0
|
|
|
|
// Example: assert byte(` `).is_space() == true
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (c byte) is_space() bool {
|
2020-02-20 22:14:54 +03:00
|
|
|
// 0x0085 is NEXT LINE (NEL)
|
|
|
|
// 0x00a0 is NO-BREAK SPACE
|
|
|
|
return c in [` `, `\n`, `\t`, `\v`, `\f`, `\r`, 0x85, 0xa0]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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 21:20:28 +03:00
|
|
|
pub fn (s string) trim_space() string {
|
2019-09-06 13:22:37 +03:00
|
|
|
return s.trim(' \n\t\v\f\r')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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'
|
2019-08-27 07:53:56 +03:00
|
|
|
pub fn (s string) trim(cutset string) string {
|
2019-09-06 13:22:37 +03:00
|
|
|
if s.len < 1 || cutset.len < 1 {
|
2019-08-27 07:53:56 +03:00
|
|
|
return s
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-27 07:53:56 +03:00
|
|
|
cs_arr := cutset.bytes()
|
|
|
|
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
|
|
|
|
if s[pos_left] in cs_arr {
|
|
|
|
pos_left++
|
|
|
|
cs_match = true
|
|
|
|
}
|
|
|
|
if s[pos_right] in cs_arr {
|
|
|
|
pos_right--
|
|
|
|
cs_match = true
|
|
|
|
}
|
|
|
|
if pos_left > pos_right {
|
|
|
|
return ''
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
return s.substr(pos_left, pos_right + 1)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) trim_left(cutset string) string {
|
2019-09-06 13:22:37 +03:00
|
|
|
if s.len < 1 || cutset.len < 1 {
|
2019-06-22 21:20:28 +03:00
|
|
|
return s
|
|
|
|
}
|
2019-08-26 14:18:58 +03:00
|
|
|
cs_arr := cutset.bytes()
|
2019-08-27 07:53:56 +03:00
|
|
|
mut pos := 0
|
2020-04-06 15:30:25 +03:00
|
|
|
for pos < s.len && s[pos] in cs_arr {
|
2019-08-26 14:18:58 +03:00
|
|
|
pos++
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2021-01-22 12:26:07 +03:00
|
|
|
return s[pos..]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) trim_right(cutset string) string {
|
2019-09-06 13:22:37 +03:00
|
|
|
if s.len < 1 || cutset.len < 1 {
|
2019-08-17 22:19:37 +03:00
|
|
|
return s
|
|
|
|
}
|
2019-08-26 14:18:58 +03:00
|
|
|
cs_arr := cutset.bytes()
|
2019-08-27 07:53:56 +03:00
|
|
|
mut pos := s.len - 1
|
2020-04-06 15:30:25 +03:00
|
|
|
for pos >= 0 && s[pos] in cs_arr {
|
2019-08-17 22:19:37 +03:00
|
|
|
pos--
|
|
|
|
}
|
2021-02-08 02:28:46 +03:00
|
|
|
return if pos < 0 { '' } else { s[..pos + 1] }
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// trim_prefix strips `str` from the start of the string.
|
|
|
|
// Example: assert 'WorldHello V'.trim_prefix('World') == 'Hello V'
|
2020-05-15 20:37:14 +03:00
|
|
|
pub fn (s string) trim_prefix(str string) string {
|
|
|
|
if s.starts_with(str) {
|
2020-06-30 16:44:53 +03:00
|
|
|
return s[str.len..]
|
2020-05-15 20:37:14 +03:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// trim_suffix strips `str` from the end of the string.
|
|
|
|
// Example: assert 'Hello VWorld'.trim_suffix('World') == 'Hello V'
|
2020-05-15 20:37:14 +03:00
|
|
|
pub fn (s string) trim_suffix(str string) string {
|
|
|
|
if s.ends_with(str) {
|
2020-12-03 18:02:48 +03:00
|
|
|
return s[..s.len - str.len]
|
2020-05-15 20:37:14 +03:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// compare_strings returns `-1` if `a < b`, `1` if `a > b` else `0`.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn compare_strings(a &string, b &string) int {
|
2019-06-23 19:00:29 +03:00
|
|
|
if a.lt(b) {
|
2019-06-22 21:20:28 +03:00
|
|
|
return -1
|
|
|
|
}
|
2019-06-23 19:00:29 +03:00
|
|
|
if a.gt(b) {
|
2019-06-22 21:20:28 +03:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// compare_strings_reverse returns `1` if `a < b`, `-1` if `a > b` else `0`.
|
2020-10-15 13:32:28 +03:00
|
|
|
fn compare_strings_reverse(a &string, b &string) int {
|
2020-10-06 21:32:32 +03:00
|
|
|
if a.lt(b) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if a.gt(b) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// compare_strings_by_len returns `-1` if `a.len < b.len`, `1` if `a.len > b.len` else `0`.
|
2020-10-15 13:32:28 +03:00
|
|
|
fn compare_strings_by_len(a &string, b &string) int {
|
2019-06-22 21:20:28 +03:00
|
|
|
if a.len < b.len {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if a.len > b.len {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// compare_lower_strings returns the same as compare_strings but converts `a` and `b` to lower case before comparing.
|
2020-10-15 13:32:28 +03:00
|
|
|
fn compare_lower_strings(a &string, b &string) int {
|
2019-06-22 21:20:28 +03:00
|
|
|
aa := a.to_lower()
|
2019-06-25 07:29:02 +03:00
|
|
|
bb := b.to_lower()
|
2019-06-22 21:20:28 +03:00
|
|
|
return compare_strings(aa, bb)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// sort sorts the string array.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut s []string) sort() {
|
2019-06-22 21:20:28 +03:00
|
|
|
s.sort_with_compare(compare_strings)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// sort_ignore_case sorts the string array using case insesitive comparing.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut s []string) sort_ignore_case() {
|
2019-06-22 21:20:28 +03:00
|
|
|
s.sort_with_compare(compare_lower_strings)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// sort_by_len sorts the the string array by each string's `.len` length.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut s []string) sort_by_len() {
|
2019-06-22 21:20:28 +03:00
|
|
|
s.sort_with_compare(compare_strings_by_len)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// str returns the string itself.
|
2020-04-29 14:51:42 +03:00
|
|
|
pub fn (s string) str() string {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// str returns the string itself.
|
2020-04-02 19:21:12 +03:00
|
|
|
pub fn (s ustring) str() string {
|
2020-12-03 18:02:48 +03:00
|
|
|
return s.s
|
2020-04-02 19:21:12 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// ustring converts the string to a unicode string.
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) ustring() ustring {
|
2019-12-19 23:52:45 +03:00
|
|
|
mut res := ustring{
|
2020-12-03 18:02:48 +03:00
|
|
|
s: s // runes will have at least s.len elements, save reallocations
|
2020-12-12 13:10:29 +03:00
|
|
|
// TODO use VLA for small strings?
|
2020-06-01 22:15:59 +03:00
|
|
|
runes: __new_array(0, s.len, int(sizeof(int)))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
for i := 0; i < s.len; i++ {
|
2021-01-05 20:59:51 +03:00
|
|
|
char_len := utf8_char_len(unsafe { s.str[i] })
|
2019-06-22 21:20:28 +03:00
|
|
|
res.runes << i
|
|
|
|
i += char_len - 1
|
|
|
|
res.len++
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// A hack that allows to create ustring without allocations.
|
|
|
|
// It's called from functions like draw_text() where we know that the string is going to be freed
|
|
|
|
// right away. Uses global buffer for storing runes []int array.
|
2020-10-03 16:41:45 +03:00
|
|
|
__global ( g_ustring_runes []int )
|
2019-12-19 23:52:45 +03:00
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) ustring_tmp() ustring {
|
2019-06-29 17:24:59 +03:00
|
|
|
if g_ustring_runes.len == 0 {
|
2020-06-01 22:15:59 +03:00
|
|
|
g_ustring_runes = __new_array(0, 128, int(sizeof(int)))
|
2019-06-29 17:24:59 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
mut res := ustring{
|
2019-06-22 21:20:28 +03:00
|
|
|
s: s
|
|
|
|
}
|
2019-06-27 20:02:47 +03:00
|
|
|
res.runes = g_ustring_runes
|
|
|
|
res.runes.len = s.len
|
2019-06-22 21:20:28 +03:00
|
|
|
mut j := 0
|
|
|
|
for i := 0; i < s.len; i++ {
|
2021-01-05 20:59:51 +03:00
|
|
|
char_len := utf8_char_len(unsafe { s.str[i] })
|
2019-06-22 21:20:28 +03:00
|
|
|
res.runes[j] = i
|
|
|
|
j++
|
|
|
|
i += char_len - 1
|
|
|
|
res.len++
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// eq implements the `u == a` (equal) operator.
|
2019-09-20 19:07:38 +03:00
|
|
|
fn (u ustring) eq(a ustring) bool {
|
|
|
|
if u.len != a.len || u.s != a.s {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// ne implements the `u != a` (not equal) operator.
|
2019-09-20 19:07:38 +03:00
|
|
|
fn (u ustring) ne(a ustring) bool {
|
|
|
|
return !u.eq(a)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// lt implements the `u < a` (less than) operator.
|
2019-09-20 19:07:38 +03:00
|
|
|
fn (u ustring) lt(a ustring) bool {
|
|
|
|
return u.s < a.s
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// le implements the `u <= a` (less than or equal to) operator.
|
2019-09-20 19:07:38 +03:00
|
|
|
fn (u ustring) le(a ustring) bool {
|
|
|
|
return u.lt(a) || u.eq(a)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// gt implements the `u > a` (greater than) operator.
|
2019-09-20 19:07:38 +03:00
|
|
|
fn (u ustring) gt(a ustring) bool {
|
|
|
|
return !u.le(a)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// ge implements the `u >= a` (greater than or equal to) operator.
|
2019-09-20 19:07:38 +03:00
|
|
|
fn (u ustring) ge(a ustring) bool {
|
|
|
|
return !u.lt(a)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// add concatenates ustring with the string given in `s`.
|
2020-04-02 19:21:12 +03:00
|
|
|
pub fn (u ustring) add(a ustring) ustring {
|
2019-12-19 23:52:45 +03:00
|
|
|
mut res := ustring{
|
2019-09-20 19:07:38 +03:00
|
|
|
s: u.s + a.s
|
2020-06-01 22:15:59 +03:00
|
|
|
runes: __new_array(0, u.s.len + a.s.len, int(sizeof(int)))
|
2019-09-20 19:07:38 +03:00
|
|
|
}
|
|
|
|
mut j := 0
|
|
|
|
for i := 0; i < u.s.len; i++ {
|
2021-01-05 20:59:51 +03:00
|
|
|
char_len := utf8_char_len(unsafe { u.s.str[i] })
|
2019-09-20 19:07:38 +03:00
|
|
|
res.runes << j
|
|
|
|
i += char_len - 1
|
|
|
|
j += char_len
|
|
|
|
res.len++
|
|
|
|
}
|
|
|
|
for i := 0; i < a.s.len; i++ {
|
2021-01-05 20:59:51 +03:00
|
|
|
char_len := utf8_char_len(unsafe { a.s.str[i] })
|
2019-09-20 19:07:38 +03:00
|
|
|
res.runes << j
|
|
|
|
i += char_len - 1
|
|
|
|
j += char_len
|
|
|
|
res.len++
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// index_after returns the position of the input string, starting search from `start` position.
|
2019-09-20 19:07:38 +03:00
|
|
|
pub fn (u ustring) index_after(p ustring, start int) int {
|
|
|
|
if p.len > u.len {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
mut strt := start
|
|
|
|
if start < 0 {
|
|
|
|
strt = 0
|
|
|
|
}
|
|
|
|
if start > u.len {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
mut i := strt
|
|
|
|
for i < u.len {
|
|
|
|
mut j := 0
|
|
|
|
mut ii := i
|
|
|
|
for j < p.len && u.at(ii) == p.at(j) {
|
|
|
|
j++
|
|
|
|
ii++
|
|
|
|
}
|
|
|
|
if j == p.len {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// count returns the number of occurrences of `substr` in the string.
|
|
|
|
// count returns -1 if no `substr` could be found.
|
2019-09-20 19:07:38 +03:00
|
|
|
pub fn (u ustring) count(substr ustring) int {
|
|
|
|
if u.len == 0 || substr.len == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if substr.len > u.len {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
mut n := 0
|
|
|
|
mut i := 0
|
|
|
|
for {
|
|
|
|
i = u.index_after(substr, i)
|
|
|
|
if i == -1 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
i += substr.len
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
return 0 // TODO can never get here - v doesn't know that
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// substr returns the string between index positions `_start` and `_end`.
|
|
|
|
// Example: assert 'ABCD'.substr(1,3) == 'BC'
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn (u ustring) substr(_start int, _end int) string {
|
2020-12-03 18:02:48 +03:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 18:13:45 +03:00
|
|
|
if _start > _end || _start > u.len || _end > u.len || _start < 0 || _end < 0 {
|
|
|
|
panic('substr($_start, $_end) out of bounds (len=$u.len)')
|
|
|
|
}
|
2019-09-20 19:07:38 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
end := if _end >= u.len { u.s.len } else { u.runes[_end] }
|
2019-09-20 19:07:38 +03:00
|
|
|
return u.s.substr(u.runes[_start], end)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// left returns the `n`th leftmost characters of the ustring.
|
|
|
|
// Example: assert 'hello'.left(2) == 'he'
|
2019-07-07 22:46:21 +03:00
|
|
|
pub fn (u ustring) left(pos int) string {
|
2019-09-20 19:07:38 +03:00
|
|
|
if pos >= u.len {
|
|
|
|
return u.s
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return u.substr(0, pos)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// right returns the `n`th rightmost characters of the ustring.
|
|
|
|
// Example: assert 'hello'.right(2) == 'lo'
|
2019-07-07 22:46:21 +03:00
|
|
|
pub fn (u ustring) right(pos int) string {
|
2019-09-20 19:07:38 +03:00
|
|
|
if pos >= u.len {
|
|
|
|
return ''
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return u.substr(pos, u.len)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// at returns the byte at index `idx`.
|
|
|
|
// Example: assert 'ABC'.at(1) == byte(`B`)
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (s string) at(idx int) byte {
|
2020-12-03 18:02:48 +03:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 18:13:45 +03:00
|
|
|
if idx < 0 || idx >= s.len {
|
|
|
|
panic('string index out of range: $idx / $s.len')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
return s.str[idx]
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// at returns the string at index `idx`.
|
|
|
|
// Example: assert 'ABC'.at(1) == 'B'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (u ustring) at(idx int) string {
|
2020-12-03 18:02:48 +03:00
|
|
|
$if !no_bounds_checking ? {
|
2020-02-16 18:13:45 +03:00
|
|
|
if idx < 0 || idx >= u.len {
|
|
|
|
panic('string index out of range: $idx / $u.runes.len')
|
|
|
|
}
|
2019-09-20 19:07:38 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return u.substr(idx, idx + 1)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// free allows for manually freeing the memory occupied by the unicode string.
|
2020-08-09 12:22:11 +03:00
|
|
|
[unsafe]
|
2020-05-06 19:03:44 +03:00
|
|
|
fn (u &ustring) free() {
|
2020-07-11 14:22:16 +03:00
|
|
|
$if prealloc {
|
|
|
|
return
|
|
|
|
}
|
2020-07-17 20:14:12 +03:00
|
|
|
unsafe {
|
|
|
|
u.runes.free()
|
2020-11-28 05:41:43 +03:00
|
|
|
u.s.free()
|
2020-07-17 20:14:12 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_digit returns `true` if the byte is in range 0-9 and `false` otherwise.
|
|
|
|
// Example: assert byte(`9`) == true
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (c byte) is_digit() bool {
|
2019-06-22 21:20:28 +03:00
|
|
|
return c >= `0` && c <= `9`
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03: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
|
2019-07-24 01:06:48 +03:00
|
|
|
pub fn (c byte) is_hex_digit() bool {
|
|
|
|
return c.is_digit() || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
|
|
|
// Example: assert byte(`7`) == true
|
2019-07-24 01:06:48 +03:00
|
|
|
pub fn (c byte) is_oct_digit() bool {
|
|
|
|
return c >= `0` && c <= `7`
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_bin_digit returns `true` if the byte is a binary digit (0 or 1) and `false` otherwise.
|
|
|
|
// Example: assert byte(`0`) == true
|
2020-01-23 05:28:25 +03:00
|
|
|
pub fn (c byte) is_bin_digit() bool {
|
|
|
|
return c == `0` || c == `1`
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// is_letter returns `true` if the byte is in range a-z or A-Z and `false` otherwise.
|
|
|
|
// Example: assert byte(`V`) == true
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (c byte) is_letter() bool {
|
2019-06-22 21:20:28 +03:00
|
|
|
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// free allows for manually freeing the memory occupied by the string
|
2021-02-14 21:31:42 +03:00
|
|
|
[unsafe]
|
2020-05-06 19:03:44 +03:00
|
|
|
pub fn (s &string) free() {
|
2020-07-11 14:22:16 +03:00
|
|
|
$if prealloc {
|
|
|
|
return
|
|
|
|
}
|
2020-06-30 18:28:28 +03:00
|
|
|
if s.is_lit == -98761234 {
|
|
|
|
C.printf('double string.free() detected\n')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.is_lit == 1 || s.len == 0 {
|
2020-06-12 20:20:51 +03:00
|
|
|
return
|
|
|
|
}
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
|
|
|
free(s.str)
|
|
|
|
}
|
2020-06-30 18:28:28 +03:00
|
|
|
s.is_lit = -98761234
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-02-07 06:48:54 +03:00
|
|
|
// before returns the contents before `dot` in the string.
|
|
|
|
// Example: assert '23:34:45.234'.all_before('.') == '23:34:45'
|
|
|
|
pub fn (s string) before(dot string) string {
|
|
|
|
pos := s.index_(dot)
|
|
|
|
if pos == -1 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[..pos]
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// all_before returns the contents before `dot` in the string.
|
|
|
|
// Example: assert '23:34:45.234'.all_before('.') == '23:34:45'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) all_before(dot string) string {
|
2021-02-07 06:48:54 +03:00
|
|
|
// TODO remove dup method
|
2021-01-15 04:26:06 +03:00
|
|
|
pos := s.index_(dot)
|
|
|
|
if pos == -1 {
|
|
|
|
return s
|
|
|
|
}
|
2021-01-22 12:26:07 +03:00
|
|
|
return s[..pos]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// all_before_last returns the contents before the last occurence of `dot` in the string.
|
|
|
|
// Example: assert '23:34:45.234'.all_before_last(':') == '23:34'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) all_before_last(dot string) string {
|
2021-01-15 04:26:06 +03:00
|
|
|
pos := s.last_index_(dot)
|
|
|
|
if pos == -1 {
|
|
|
|
return s
|
|
|
|
}
|
2021-01-22 12:26:07 +03:00
|
|
|
return s[..pos]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// all_after returns the contents after `dot` in the string.
|
|
|
|
// Example: assert '23:34:45.234'.all_after('.') == '234'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) all_after(dot string) string {
|
2021-01-15 04:26:06 +03:00
|
|
|
pos := s.index_(dot)
|
|
|
|
if pos == -1 {
|
|
|
|
return s
|
|
|
|
}
|
2021-01-22 12:26:07 +03:00
|
|
|
return s[pos + dot.len..]
|
2020-05-20 12:04:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// all_after_last returns the contents after the last occurence of `dot` in the string.
|
|
|
|
// Example: assert '23:34:45.234'.all_after_last(':') == '45.234'
|
2020-05-20 12:04:28 +03:00
|
|
|
pub fn (s string) all_after_last(dot string) string {
|
2021-01-15 04:26:06 +03:00
|
|
|
pos := s.last_index_(dot)
|
|
|
|
if pos == -1 {
|
|
|
|
return s
|
|
|
|
}
|
2021-01-22 12:26:07 +03:00
|
|
|
return s[pos + dot.len..]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// after returns the contents after the last occurence of `dot` in the string.
|
|
|
|
// Example: assert '23:34:45.234'.after(':') == '45.234'
|
2020-05-25 09:17:36 +03:00
|
|
|
pub fn (s string) after(dot string) string {
|
2020-05-20 12:04:28 +03:00
|
|
|
return s.all_after_last(dot)
|
|
|
|
}
|
2020-02-18 22:20:15 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// after_char returns the contents after the first occurence of `dot` character in the string.
|
|
|
|
// Example: assert '23:34:45.234'.after_char(`:`) == '34:45.234'
|
2020-05-18 06:10:56 +03:00
|
|
|
pub fn (s string) after_char(dot byte) string {
|
|
|
|
mut pos := 0
|
|
|
|
for i, c in s {
|
|
|
|
if c == dot {
|
|
|
|
pos = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pos == 0 {
|
|
|
|
return s
|
|
|
|
}
|
2021-01-22 12:26:07 +03:00
|
|
|
return s[pos + 1..]
|
2020-05-18 06:10:56 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// fn (s []string) substr(a, b int) string {
|
|
|
|
// return join_strings(s.slice_fast(a, b))
|
|
|
|
// }
|
2020-12-12 13:10:29 +03:00
|
|
|
// join joins a string array into a string using `del` delimiter.
|
|
|
|
// Example: assert ['Hello','V'].join(' ') == 'Hello V'
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn (a []string) join(del string) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
if a.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
mut len := 0
|
2020-04-27 16:16:31 +03:00
|
|
|
for val in a {
|
2019-06-22 21:20:28 +03:00
|
|
|
len += val.len + del.len
|
|
|
|
}
|
|
|
|
len -= del.len
|
|
|
|
// Allocate enough memory
|
|
|
|
mut res := ''
|
|
|
|
res.len = len
|
2021-02-14 21:31:42 +03:00
|
|
|
res.str = unsafe { malloc(res.len + 1) }
|
2019-06-22 21:20:28 +03:00
|
|
|
mut idx := 0
|
|
|
|
// Go thru every string and copy its every char one by one
|
|
|
|
for i, val in a {
|
2020-12-03 18:02:48 +03:00
|
|
|
for j in 0 .. val.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
res.str[idx] = val.str[j]
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
idx++
|
|
|
|
}
|
|
|
|
// Add del if it's not last
|
|
|
|
if i != a.len - 1 {
|
2020-12-03 18:02:48 +03:00
|
|
|
for k in 0 .. del.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
res.str[idx] = del.str[k]
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
idx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
res.str[res.len] = `\0`
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// join joins a string array into a string using a `\n` newline delimiter.
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn (s []string) join_lines() string {
|
2019-06-22 21:20:28 +03:00
|
|
|
return s.join('\n')
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// reverse returns a reversed string.
|
|
|
|
// Example: assert 'Hello V'.reverse() == 'V olleH'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) reverse() string {
|
2020-02-20 22:14:21 +03:00
|
|
|
if s.len == 0 || s.len == 1 {
|
|
|
|
return s
|
2019-12-16 21:29:32 +03:00
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
mut res := string{
|
2021-02-14 21:31:42 +03:00
|
|
|
str: unsafe { malloc(s.len) }
|
2020-05-18 22:38:06 +03:00
|
|
|
len: s.len
|
2019-06-27 03:03:19 +03:00
|
|
|
}
|
|
|
|
for i := s.len - 1; i >= 0; i-- {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
res.str[s.len - i - 1] = s[i]
|
|
|
|
}
|
2019-06-27 03:03:19 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-10-27 22:29:55 +03:00
|
|
|
// limit returns a portion of the string, starting at `0` and extending for a given number of characters afterward.
|
2019-06-22 21:20:28 +03:00
|
|
|
// 'hello'.limit(2) => 'he'
|
|
|
|
// 'hi'.limit(10) => 'hi'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (s string) limit(max int) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
u := s.ustring()
|
|
|
|
if u.len <= max {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return u.substr(0, max)
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// hash returns an integer hash of the string.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn (s string) hash() int {
|
2019-12-19 23:52:45 +03:00
|
|
|
// mut h := s.hash_cache
|
2020-07-11 01:18:52 +03:00
|
|
|
mut h := u32(0)
|
2019-08-17 22:19:37 +03:00
|
|
|
if h == 0 && s.len > 0 {
|
|
|
|
for c in s {
|
2020-07-11 01:18:52 +03:00
|
|
|
h = h * 31 + u32(c)
|
2019-07-10 10:48:10 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2020-07-11 01:18:52 +03:00
|
|
|
return int(h)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// bytes returns the string converted to a byte array.
|
2019-07-15 18:49:01 +03:00
|
|
|
pub fn (s string) bytes() []byte {
|
|
|
|
if s.len == 0 {
|
2019-11-14 09:18:07 +03:00
|
|
|
return []
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
mut buf := []byte{len: s.len}
|
2021-01-05 20:59:51 +03:00
|
|
|
unsafe { C.memcpy(buf.data, s.str, s.len) }
|
2019-07-15 18:49:01 +03:00
|
|
|
return buf
|
|
|
|
}
|
2019-09-26 22:54:53 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// repeat returns a new string with `count` number of copies of the string it was called on.
|
2019-09-26 22:54:53 +03:00
|
|
|
pub fn (s string) repeat(count int) string {
|
2020-02-29 17:25:31 +03:00
|
|
|
if count < 0 {
|
|
|
|
panic('string.repeat: count is negative: $count')
|
|
|
|
} else if count == 0 {
|
|
|
|
return ''
|
|
|
|
} else if count == 1 {
|
2019-09-26 22:54:53 +03:00
|
|
|
return s
|
|
|
|
}
|
2021-02-14 21:31:42 +03:00
|
|
|
mut ret := unsafe { malloc(s.len * count + 1) }
|
2019-12-19 23:52:45 +03:00
|
|
|
for i in 0 .. count {
|
|
|
|
for j in 0 .. s.len {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
ret[i * s.len + j] = s[j]
|
|
|
|
}
|
2019-09-28 02:51:42 +03:00
|
|
|
}
|
2019-09-26 22:54:53 +03:00
|
|
|
}
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
2020-08-12 06:54:51 +03:00
|
|
|
new_len := s.len * count
|
2020-08-10 19:05:26 +03:00
|
|
|
ret[new_len] = 0
|
|
|
|
return ret.vstring_with_len(new_len)
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2019-09-26 22:54:53 +03:00
|
|
|
}
|
2020-03-15 07:46:12 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// fields returns a string array of the string split by `\t` and ` `
|
|
|
|
// Example: assert '\t\tv = v'.fields() == ['', '', 'v', '=', 'v']
|
2020-06-08 14:10:47 +03:00
|
|
|
pub fn (s string) fields() []string {
|
2020-06-09 11:15:17 +03:00
|
|
|
// TODO do this in a better way
|
|
|
|
return s.replace('\t', ' ').split(' ')
|
2020-06-08 14:10:47 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// strip_margin allows multi-line strings to be formatted in a way that removes white-space
|
2020-03-15 07:46:12 +03: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 17:46:09 +03:00
|
|
|
//
|
2020-03-15 07:46:12 +03:00
|
|
|
// Example:
|
|
|
|
// st := 'Hello there,
|
2020-12-03 18:02:48 +03:00
|
|
|
// |this is a string,
|
|
|
|
// | Everything before the first | is removed'.strip_margin()
|
2020-03-15 07:46:12 +03:00
|
|
|
// Returns:
|
|
|
|
// Hello there,
|
|
|
|
// this is a string,
|
2020-12-03 18:02:48 +03:00
|
|
|
// Everything before the first | is removed
|
2020-04-11 11:06:03 +03:00
|
|
|
pub fn (s string) strip_margin() string {
|
2020-12-03 18:02:48 +03:00
|
|
|
return s.strip_margin_custom(`|`)
|
2020-04-11 11:06:03 +03:00
|
|
|
}
|
2020-12-03 18:02:48 +03:00
|
|
|
|
2020-12-12 13:10:29 +03:00
|
|
|
// strip_margin_custom does the same as `strip_margin` but will use `del` as delimiter instead of `|`
|
2020-04-11 11:06:03 +03:00
|
|
|
pub fn (s string) strip_margin_custom(del byte) string {
|
|
|
|
mut sep := del
|
|
|
|
if sep.is_space() {
|
2020-12-03 18:02:48 +03:00
|
|
|
eprintln('Warning: `strip_margin` cannot use white-space as a delimiter')
|
|
|
|
eprintln(' Defaulting to `|`')
|
2020-04-11 11:06:03 +03:00
|
|
|
sep = `|`
|
2020-03-15 07:46:12 +03:00
|
|
|
}
|
|
|
|
// don't know how much space the resulting string will be, but the max it
|
|
|
|
// can be is this big
|
2021-02-14 21:31:42 +03:00
|
|
|
mut ret := unsafe { malloc(s.len + 1) }
|
2020-03-15 07:46:12 +03:00
|
|
|
mut count := 0
|
|
|
|
for i := 0; i < s.len; i++ {
|
2020-03-27 16:57:19 +03:00
|
|
|
if s[i] in [`\n`, `\r`] {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
ret[count] = s[i]
|
|
|
|
}
|
2020-03-16 17:46:09 +03:00
|
|
|
count++
|
|
|
|
// CRLF
|
2020-12-03 18:02:48 +03:00
|
|
|
if s[i] == `\r` && i < s.len - 1 && s[i + 1] == `\n` {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
2020-12-03 18:02:48 +03:00
|
|
|
ret[count] = s[i + 1]
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2020-03-15 07:46:12 +03:00
|
|
|
count++
|
2020-03-16 17:46:09 +03:00
|
|
|
i++
|
2020-03-15 07:46:12 +03:00
|
|
|
}
|
|
|
|
for s[i] != sep {
|
|
|
|
i++
|
|
|
|
if i >= s.len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
ret[count] = s[i]
|
|
|
|
}
|
2020-03-15 07:46:12 +03:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
|
|
|
ret[count] = 0
|
2020-08-10 19:05:26 +03:00
|
|
|
return ret.vstring_with_len(count)
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2020-03-15 07:46:12 +03:00
|
|
|
}
|
2020-12-29 09:46:46 +03:00
|
|
|
|
|
|
|
// split_by_whitespace - extract only the non whitespace tokens/words from the given string `s`.
|
|
|
|
// example: ' sss ssss'.split_by_whitespace() => ['sss', 'ssss']
|
|
|
|
pub fn (s string) split_by_whitespace() []string {
|
|
|
|
mut res := []string{}
|
|
|
|
mut word_start := 0
|
|
|
|
mut word_end := 0
|
|
|
|
mut is_in_word := false
|
|
|
|
mut is_space := false
|
|
|
|
for i, c in s {
|
|
|
|
is_space = c in [` `, `\t`, `\n`]
|
|
|
|
if !is_in_word && !is_space {
|
|
|
|
word_start = i
|
|
|
|
is_in_word = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if is_space && is_in_word {
|
|
|
|
word_end = i
|
2021-01-05 20:59:51 +03:00
|
|
|
res << s[word_start..word_end]
|
2020-12-29 09:46:46 +03:00
|
|
|
is_in_word = false
|
|
|
|
word_end = 0
|
|
|
|
word_start = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_in_word && word_start > 0 {
|
|
|
|
// collect the remainder word at the end
|
2021-01-05 20:59:51 +03:00
|
|
|
res << s[word_start..s.len]
|
2020-12-29 09:46:46 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|