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

string: add trim_prefix and trim_suffix

This commit is contained in:
Swastik Baranwal 2020-05-15 23:07:14 +05:30 committed by GitHub
parent 5d0cc0944e
commit 04744a5390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

2
.gitignore vendored
View File

@ -15,6 +15,7 @@
*.lib
*.bak
a.out
.noprefix.vrepl_temp
# ignore v build files
/v.c
@ -22,6 +23,7 @@ a.out
/v.c.out
.vrepl_temp.v
fns.txt
.noprefix.vrepl_temp.v
# ignore temp directories
/temp

View File

@ -926,6 +926,20 @@ pub fn (s string) trim_right(cutset string) string {
return if pos < 0 { '' } else { s.left(pos + 1) }
}
pub fn (s string) trim_prefix(str string) string {
if s.starts_with(str) {
return s.replace(str, "")
}
return s
}
pub fn (s string) trim_suffix(str string) string {
if s.ends_with(str) {
return s.replace(str, "")
}
return s
}
// fn print_cur_thread() {
// //C.printf("tid = %08x \n", pthread_self());
// }

View File

@ -25,6 +25,12 @@ fn test_add() {
fn test_ends_with() {
a := 'browser.v'
assert a.ends_with('.v')
s := 'V Programming Language'
assert s.ends_with('guage') == true
assert s.ends_with('Language') == true
assert s.ends_with('Programming Language') == true
assert s.ends_with('V') == false
}
fn test_between() {
@ -646,6 +652,27 @@ fn test_repeat() {
// TODO Add test for negative values
}
fn test_starts_with() {
s := 'V Programming Language'
assert s.starts_with('V') == true
assert s.starts_with('V Programming') == true
assert s.starts_with('Language') == false
}
fn test_trim_prefix() {
s := 'V Programming Language'
assert s.trim_prefix('V ') == 'Programming Language'
assert s.trim_prefix('V Programming ') == 'Language'
assert s.trim_prefix('Language') == s
}
fn test_trim_suffix() {
s := 'V Programming Language'
assert s.trim_suffix(' Language') == 'V Programming'
assert s.trim_suffix(' Programming Language') == 'V'
assert s.trim_suffix('V') == s
}
fn test_raw() {
raw := r'raw\nstring'
lines := raw.split('\n')