builtin: s.trim_prefix/1 -> s.trim_string_left/1, s.trim_suffix/1 -> s.trim_string_right/1

pull/13038/head
Delyan Angelov 2022-01-05 12:49:22 +02:00
parent 57fa9768d5
commit d3489d4246
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
9 changed files with 82 additions and 50 deletions

View File

@ -309,24 +309,40 @@ pub fn (s string) trim_left(cutset string) string {
return s[pos..]
}
// trim_prefix strips `str` from the start of the string.
// Example: assert 'WorldHello V'.trim_prefix('World') == 'Hello V'
pub fn (s string) trim_prefix(str string) string {
// trim_string_left strips `str` from the start of the string.
// Example: assert 'WorldHello V'.trim_string_left('World') == 'Hello V'
pub fn (s string) trim_string_left(str string) string {
if s.starts_with(str) {
return s[str.len..]
}
return s.clone()
}
// trim_suffix strips `str` from the end of the string.
// Example: assert 'Hello VWorld'.trim_suffix('World') == 'Hello V'
pub fn (s string) trim_suffix(str string) string {
// trim_string_right strips `str` from the end of the string.
// Example: assert 'Hello VWorld'.trim_string_right('World') == 'Hello V'
pub fn (s string) trim_string_right(str string) string {
if s.ends_with(str) {
return s[..s.len - str.len]
}
return s.clone()
}
// trim_prefix strips `str` from the start of the string.
// Example: assert 'WorldHello V'.trim_prefix('World') == 'Hello V'
[deprecated: 'use s.trim_string_left(x) instead']
[deprecated_after: '2022-01-19']
pub fn (s string) trim_prefix(str string) string {
return s.trim_string_left(str)
}
// trim_suffix strips `str` from the end of the string.
// Example: assert 'Hello VWorld'.trim_suffix('World') == 'Hello V'
[deprecated: 'use s.trim_string_right(x) instead']
[deprecated_after: '2022-01-19']
pub fn (s string) trim_suffix(str string) string {
return s.trim_string_right(str)
}
// compare_strings returns `-1` if `a < b`, `1` if `a > b` else `0`.
pub fn compare_strings(a &string, b &string) int {
if a < b {

View File

@ -677,34 +677,34 @@ fn test_starts_with_capital() {
assert ' No'.starts_with_capital() == false
}
fn test_trim_prefix() {
fn test_trim_string_left() {
s := 'V Programming Language'
assert s.trim_prefix('V ') == 'Programming Language'
assert s.trim_prefix('V Programming ') == 'Language'
assert s.trim_prefix('Language') == s
assert s.trim_string_left('V ') == 'Programming Language'
assert s.trim_string_left('V Programming ') == 'Language'
assert s.trim_string_left('Language') == s
s2 := 'TestTestTest'
assert s2.trim_prefix('Test') == 'TestTest'
assert s2.trim_prefix('TestTest') == 'Test'
assert s2.trim_string_left('Test') == 'TestTest'
assert s2.trim_string_left('TestTest') == 'Test'
s3 := '123Test123Test'
assert s3.trim_prefix('123') == 'Test123Test'
assert s3.trim_prefix('123Test') == '123Test'
assert s3.trim_string_left('123') == 'Test123Test'
assert s3.trim_string_left('123Test') == '123Test'
}
fn test_trim_suffix() {
fn test_trim_string_right() {
s := 'V Programming Language'
assert s.trim_suffix(' Language') == 'V Programming'
assert s.trim_suffix(' Programming Language') == 'V'
assert s.trim_suffix('V') == s
assert s.trim_string_right(' Language') == 'V Programming'
assert s.trim_string_right(' Programming Language') == 'V'
assert s.trim_string_right('V') == s
s2 := 'TestTestTest'
assert s2.trim_suffix('Test') == 'TestTest'
assert s2.trim_suffix('TestTest') == 'Test'
assert s2.trim_string_right('Test') == 'TestTest'
assert s2.trim_string_right('TestTest') == 'Test'
s3 := '123Test123Test'
assert s3.trim_suffix('123') == s3
assert s3.trim_suffix('123Test') == '123Test'
assert s3.trim_string_right('123') == s3
assert s3.trim_string_right('123Test') == '123Test'
}
fn test_raw() {

View File

@ -1340,24 +1340,40 @@ pub fn (s string) trim_right(cutset string) string {
return s[..pos + 1]
}
// trim_prefix strips `str` from the start of the string.
// Example: assert 'WorldHello V'.trim_prefix('World') == 'Hello V'
pub fn (s string) trim_prefix(str string) string {
// trim_string_left strips `str` from the start of the string.
// Example: assert 'WorldHello V'.trim_string_left('World') == 'Hello V'
pub fn (s string) trim_string_left(str string) string {
if s.starts_with(str) {
return s[str.len..]
}
return s.clone()
}
// trim_suffix strips `str` from the end of the string.
// Example: assert 'Hello VWorld'.trim_suffix('World') == 'Hello V'
pub fn (s string) trim_suffix(str string) string {
// trim_string_right strips `str` from the end of the string.
// Example: assert 'Hello VWorld'.trim_string_right('World') == 'Hello V'
pub fn (s string) trim_string_right(str string) string {
if s.ends_with(str) {
return s[..s.len - str.len]
}
return s.clone()
}
// trim_prefix strips `str` from the start of the string.
// Example: assert 'WorldHello V'.trim_prefix('World') == 'Hello V'
[deprecated: 'use s.trim_string_left(x) instead']
[deprecated_after: '2022-01-19']
pub fn (s string) trim_prefix(str string) string {
return s.trim_string_left(str)
}
// trim_suffix strips `str` from the end of the string.
// Example: assert 'Hello VWorld'.trim_suffix('World') == 'Hello V'
[deprecated: 'use s.trim_string_right(x) instead']
[deprecated_after: '2022-01-19']
pub fn (s string) trim_suffix(str string) string {
return s.trim_string_right(str)
}
// compare_strings returns `-1` if `a < b`, `1` if `a > b` else `0`.
pub fn compare_strings(a &string, b &string) int {
if a < b {

View File

@ -705,34 +705,34 @@ fn test_starts_with_capital() {
assert ' No'.starts_with_capital() == false
}
fn test_trim_prefix() {
fn test_trim_string_left() {
s := 'V Programming Language'
assert s.trim_prefix('V ') == 'Programming Language'
assert s.trim_prefix('V Programming ') == 'Language'
assert s.trim_prefix('Language') == s
assert s.trim_string_left('V ') == 'Programming Language'
assert s.trim_string_left('V Programming ') == 'Language'
assert s.trim_string_left('Language') == s
s2 := 'TestTestTest'
assert s2.trim_prefix('Test') == 'TestTest'
assert s2.trim_prefix('TestTest') == 'Test'
assert s2.trim_string_left('Test') == 'TestTest'
assert s2.trim_string_left('TestTest') == 'Test'
s3 := '123Test123Test'
assert s3.trim_prefix('123') == 'Test123Test'
assert s3.trim_prefix('123Test') == '123Test'
assert s3.trim_string_left('123') == 'Test123Test'
assert s3.trim_string_left('123Test') == '123Test'
}
fn test_trim_suffix() {
fn test_trim_string_right() {
s := 'V Programming Language'
assert s.trim_suffix(' Language') == 'V Programming'
assert s.trim_suffix(' Programming Language') == 'V'
assert s.trim_suffix('V') == s
assert s.trim_string_right(' Language') == 'V Programming'
assert s.trim_string_right(' Programming Language') == 'V'
assert s.trim_string_right('V') == s
s2 := 'TestTestTest'
assert s2.trim_suffix('Test') == 'TestTest'
assert s2.trim_suffix('TestTest') == 'Test'
assert s2.trim_string_right('Test') == 'TestTest'
assert s2.trim_string_right('TestTest') == 'Test'
s3 := '123Test123Test'
assert s3.trim_suffix('123') == s3
assert s3.trim_suffix('123Test') == '123Test'
assert s3.trim_string_right('123') == s3
assert s3.trim_string_right('123Test') == '123Test'
}
fn test_raw() {

View File

@ -583,7 +583,7 @@ pub fn (node Stmt) str() string {
}
fn field_to_string(f ConstField) string {
x := f.name.trim_prefix(f.mod + '.')
x := f.name.trim_string_left(f.mod + '.')
return '$x = $f.expr'
}

View File

@ -749,7 +749,7 @@ pub fn (mut t Table) register_type_symbol(sym TypeSymbol) int {
}
}
if sym.mod == 'main' {
existing_idx = t.type_idxs[sym.name.trim_prefix('main.')]
existing_idx = t.type_idxs[sym.name.trim_string_left('main.')]
if existing_idx > 0 {
idx = t.rewrite_already_registered_symbol(sym, existing_idx)
if idx != -2 {

View File

@ -475,7 +475,7 @@ pub fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) {
} else if sym.kind == .struct_ && sym.language == .js {
c.error('sum type cannot hold an JS struct', variant.pos)
}
if sym.name.trim_prefix(sym.mod + '.') == node.name {
if sym.name.trim_string_left(sym.mod + '.') == node.name {
c.error('sum type cannot hold itself', variant.pos)
}
names_used << sym.name

View File

@ -184,7 +184,7 @@ fn (f Fmt) get_modname_prefix(mname string) (string, string) {
after_rbc := mname.all_after_last(']')
after_ref := mname.all_after_last('&')
modname := if after_rbc.len < after_ref.len { after_rbc } else { after_ref }
return modname, mname.trim_suffix(modname)
return modname, mname.trim_string_right(modname)
}
fn (mut f Fmt) is_external_name(name string) bool {
@ -210,7 +210,7 @@ pub fn (mut f Fmt) short_module(name string) string {
return f.mod2alias[name]
}
if name.ends_with('>') {
generic_levels := name.trim_suffix('>').split('<')
generic_levels := name.trim_string_right('>').split('<')
mut res := '${f.short_module(generic_levels[0])}'
for i in 1 .. generic_levels.len {
genshorts := generic_levels[i].split(', ').map(f.short_module(it)).join(', ')

View File

@ -1,3 +1,3 @@
println('d Hello V developer'.trim_left(' d'))
println(' Hello V d'.trim_right(' d'))
println('WorldHello V'.trim_prefix('World'))
println('WorldHello V'.trim_string_left('World'))