builtin: use `return s.clone()` instead of `return s` consistently, to make manual free easier
parent
00651c8deb
commit
8fa7e67000
|
@ -316,11 +316,11 @@ fn (a RepIndex) < (b RepIndex) bool {
|
||||||
// Example: assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC'
|
// Example: assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC'
|
||||||
pub fn (s string) replace_each(vals []string) string {
|
pub fn (s string) replace_each(vals []string) string {
|
||||||
if s.len == 0 || vals.len == 0 {
|
if s.len == 0 || vals.len == 0 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
if vals.len % 2 != 0 {
|
if vals.len % 2 != 0 {
|
||||||
println('string.replace_each(): odd number of strings')
|
println('string.replace_each(): odd number of strings')
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
// `rep` - string to replace
|
// `rep` - string to replace
|
||||||
// `with` - string to replace with
|
// `with` - string to replace with
|
||||||
|
@ -350,7 +350,7 @@ pub fn (s string) replace_each(vals []string) string {
|
||||||
}
|
}
|
||||||
// Dont change the string if there's nothing to replace
|
// Dont change the string if there's nothing to replace
|
||||||
if idxs.len == 0 {
|
if idxs.len == 0 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
idxs.sort2()
|
idxs.sort2()
|
||||||
mut b := unsafe { malloc(new_len + 1) } // add a \0 just in case
|
mut b := unsafe { malloc(new_len + 1) } // add a \0 just in case
|
||||||
|
@ -1064,7 +1064,7 @@ pub fn (s string) trim_space() string {
|
||||||
// Example: assert ' ffHello V ffff'.trim(' f') == 'Hello V'
|
// Example: assert ' ffHello V ffff'.trim(' f') == 'Hello V'
|
||||||
pub fn (s string) trim(cutset string) string {
|
pub fn (s string) trim(cutset string) string {
|
||||||
if s.len < 1 || cutset.len < 1 {
|
if s.len < 1 || cutset.len < 1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
mut pos_left := 0
|
mut pos_left := 0
|
||||||
mut pos_right := s.len - 1
|
mut pos_right := s.len - 1
|
||||||
|
@ -1096,7 +1096,7 @@ pub fn (s string) trim(cutset string) string {
|
||||||
// Example: assert 'd Hello V developer'.trim_left(' d') == 'Hello V developer'
|
// Example: assert 'd Hello V developer'.trim_left(' d') == 'Hello V developer'
|
||||||
pub fn (s string) trim_left(cutset string) string {
|
pub fn (s string) trim_left(cutset string) string {
|
||||||
if s.len < 1 || cutset.len < 1 {
|
if s.len < 1 || cutset.len < 1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
mut pos := 0
|
mut pos := 0
|
||||||
for pos < s.len {
|
for pos < s.len {
|
||||||
|
@ -1119,7 +1119,7 @@ pub fn (s string) trim_left(cutset string) string {
|
||||||
// Example: assert ' Hello V d'.trim_right(' d') == ' Hello V'
|
// Example: assert ' Hello V d'.trim_right(' d') == ' Hello V'
|
||||||
pub fn (s string) trim_right(cutset string) string {
|
pub fn (s string) trim_right(cutset string) string {
|
||||||
if s.len < 1 || cutset.len < 1 {
|
if s.len < 1 || cutset.len < 1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
mut pos := s.len - 1
|
mut pos := s.len - 1
|
||||||
for pos >= 0 {
|
for pos >= 0 {
|
||||||
|
@ -1143,7 +1143,7 @@ pub fn (s string) trim_prefix(str string) string {
|
||||||
if s.starts_with(str) {
|
if s.starts_with(str) {
|
||||||
return s[str.len..]
|
return s[str.len..]
|
||||||
}
|
}
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim_suffix strips `str` from the end of the string.
|
// trim_suffix strips `str` from the end of the string.
|
||||||
|
@ -1152,7 +1152,7 @@ pub fn (s string) trim_suffix(str string) string {
|
||||||
if s.ends_with(str) {
|
if s.ends_with(str) {
|
||||||
return s[..s.len - str.len]
|
return s[..s.len - str.len]
|
||||||
}
|
}
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
// compare_strings returns `-1` if `a < b`, `1` if `a > b` else `0`.
|
// compare_strings returns `-1` if `a < b`, `1` if `a > b` else `0`.
|
||||||
|
@ -1210,9 +1210,9 @@ pub fn (mut s []string) sort_by_len() {
|
||||||
s.sort_with_compare(compare_strings_by_len)
|
s.sort_with_compare(compare_strings_by_len)
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns the string itself.
|
// str returns a copy of the string
|
||||||
pub fn (s string) str() string {
|
pub fn (s string) str() string {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns the string itself.
|
// str returns the string itself.
|
||||||
|
@ -1488,7 +1488,7 @@ pub fn (s &string) free() {
|
||||||
pub fn (s string) before(dot string) string {
|
pub fn (s string) before(dot string) string {
|
||||||
pos := s.index_(dot)
|
pos := s.index_(dot)
|
||||||
if pos == -1 {
|
if pos == -1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
return s[..pos]
|
return s[..pos]
|
||||||
}
|
}
|
||||||
|
@ -1499,7 +1499,7 @@ pub fn (s string) all_before(dot string) string {
|
||||||
// TODO remove dup method
|
// TODO remove dup method
|
||||||
pos := s.index_(dot)
|
pos := s.index_(dot)
|
||||||
if pos == -1 {
|
if pos == -1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
return s[..pos]
|
return s[..pos]
|
||||||
}
|
}
|
||||||
|
@ -1509,7 +1509,7 @@ pub fn (s string) all_before(dot string) string {
|
||||||
pub fn (s string) all_before_last(dot string) string {
|
pub fn (s string) all_before_last(dot string) string {
|
||||||
pos := s.last_index_(dot)
|
pos := s.last_index_(dot)
|
||||||
if pos == -1 {
|
if pos == -1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
return s[..pos]
|
return s[..pos]
|
||||||
}
|
}
|
||||||
|
@ -1519,7 +1519,7 @@ pub fn (s string) all_before_last(dot string) string {
|
||||||
pub fn (s string) all_after(dot string) string {
|
pub fn (s string) all_after(dot string) string {
|
||||||
pos := s.index_(dot)
|
pos := s.index_(dot)
|
||||||
if pos == -1 {
|
if pos == -1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
return s[pos + dot.len..]
|
return s[pos + dot.len..]
|
||||||
}
|
}
|
||||||
|
@ -1529,7 +1529,7 @@ pub fn (s string) all_after(dot string) string {
|
||||||
pub fn (s string) all_after_last(dot string) string {
|
pub fn (s string) all_after_last(dot string) string {
|
||||||
pos := s.last_index_(dot)
|
pos := s.last_index_(dot)
|
||||||
if pos == -1 {
|
if pos == -1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
return s[pos + dot.len..]
|
return s[pos + dot.len..]
|
||||||
}
|
}
|
||||||
|
@ -1551,7 +1551,7 @@ pub fn (s string) after_char(dot byte) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if pos == 0 {
|
if pos == 0 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
return s[pos + 1..]
|
return s[pos + 1..]
|
||||||
}
|
}
|
||||||
|
@ -1608,7 +1608,7 @@ pub fn (s []string) join_lines() string {
|
||||||
// Example: assert 'Hello V'.reverse() == 'V olleH'
|
// Example: assert 'Hello V'.reverse() == 'V olleH'
|
||||||
pub fn (s string) reverse() string {
|
pub fn (s string) reverse() string {
|
||||||
if s.len == 0 || s.len == 1 {
|
if s.len == 0 || s.len == 1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
mut res := string{
|
mut res := string{
|
||||||
str: unsafe { malloc(s.len) }
|
str: unsafe { malloc(s.len) }
|
||||||
|
@ -1628,7 +1628,7 @@ pub fn (s string) reverse() string {
|
||||||
pub fn (s string) limit(max int) string {
|
pub fn (s string) limit(max int) string {
|
||||||
u := s.ustring()
|
u := s.ustring()
|
||||||
if u.len <= max {
|
if u.len <= max {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
return u.substr(0, max)
|
return u.substr(0, max)
|
||||||
}
|
}
|
||||||
|
@ -1662,7 +1662,7 @@ pub fn (s string) repeat(count int) string {
|
||||||
} else if count == 0 {
|
} else if count == 0 {
|
||||||
return ''
|
return ''
|
||||||
} else if count == 1 {
|
} else if count == 1 {
|
||||||
return s
|
return s.clone()
|
||||||
}
|
}
|
||||||
mut ret := unsafe { malloc(s.len * count + 1) }
|
mut ret := unsafe { malloc(s.len * count + 1) }
|
||||||
for i in 0 .. count {
|
for i in 0 .. count {
|
||||||
|
|
Loading…
Reference in New Issue