From d3489d4246d883b1812cd96199793c5dd80cb631 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 5 Jan 2022 12:49:22 +0200 Subject: [PATCH] builtin: s.trim_prefix/1 -> s.trim_string_left/1, s.trim_suffix/1 -> s.trim_string_right/1 --- vlib/builtin/js/string.js.v | 28 ++++++++++++---- vlib/builtin/js/string_test.js.v | 32 +++++++++---------- vlib/builtin/string.v | 28 ++++++++++++---- vlib/builtin/string_test.v | 32 +++++++++---------- vlib/v/ast/str.v | 2 +- vlib/v/ast/table.v | 2 +- vlib/v/checker/checker.v | 2 +- vlib/v/fmt/fmt.v | 4 +-- vlib/v/gen/js/tests/testdata/string_methods.v | 2 +- 9 files changed, 82 insertions(+), 50 deletions(-) diff --git a/vlib/builtin/js/string.js.v b/vlib/builtin/js/string.js.v index 33ec080dce..09b740cffa 100644 --- a/vlib/builtin/js/string.js.v +++ b/vlib/builtin/js/string.js.v @@ -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 { diff --git a/vlib/builtin/js/string_test.js.v b/vlib/builtin/js/string_test.js.v index 89dd83c56c..aacf411ef5 100644 --- a/vlib/builtin/js/string_test.js.v +++ b/vlib/builtin/js/string_test.js.v @@ -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() { diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index f4015bda93..23efe60de3 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -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 { diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index cc8b65b7f7..88af1bc2a1 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -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() { diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 9e127ae2f8..9e56c3b32e 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -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' } diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 1fd9b152d6..ba0cc99931 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -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 { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7877b3e119..fe863ac103 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index ce231b262c..d046116644 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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(', ') diff --git a/vlib/v/gen/js/tests/testdata/string_methods.v b/vlib/v/gen/js/tests/testdata/string_methods.v index 1594b4882a..5fd4d1cc3b 100644 --- a/vlib/v/gen/js/tests/testdata/string_methods.v +++ b/vlib/v/gen/js/tests/testdata/string_methods.v @@ -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'))