1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: add more detailed error descriptions, add custom format parsing with time.parse_format (#18257)

This commit is contained in:
sandbankdisperser
2023-06-06 17:43:10 +02:00
committed by GitHub
parent 0bbbf1e801
commit e97aff8742
7 changed files with 438 additions and 27 deletions

View File

@ -106,10 +106,13 @@ pub fn (s string) clone() string {
return string(s.str)
}
// contains returns `true` if the string contains `substr`.
// See also: [`string.index`](#string.index)
pub fn (s string) contains(substr string) bool {
return bool(s.str.includes(substr.str))
}
// contains_any returns `true` if the string contains any chars in `chars`.
pub fn (s string) contains_any(chars string) bool {
sep := ''
res := chars.str.split(sep.str)
@ -121,6 +124,26 @@ pub fn (s string) contains_any(chars string) bool {
return false
}
// contains_only returns `true`, if the string contains only the characters in `chars`.
pub fn (s string) contains_only(chars string) bool {
if chars.len == 0 {
return false
}
for ch in s {
mut res := 0
for c in chars {
if ch == c {
res++
break
}
}
if res == 0 {
return false
}
}
return true
}
pub fn (s string) contains_any_substr(chars []string) bool {
if chars.len == 0 {
return true

View File

@ -404,6 +404,13 @@ fn test_contains_any() {
assert !''.contains_any('')
}
fn test_contains_only() {
assert '23885'.contains_only('0123456789')
assert '23gg885'.contains_only('01g23456789')
assert !'hello;'.contains_only('hello')
assert !''.contains_only('')
}
fn test_contains_any_substr() {
s := 'Some random text'
assert s.contains_any_substr(['false', 'not', 'rand'])