builtin: update last_index_u8, deprecate index_u8_last string methods, make consistent with last_index (#21604)

This commit is contained in:
Turiiya 2024-06-04 12:21:47 +02:00 committed by GitHub
parent 2250d60559
commit a2ce55d922
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 50 additions and 52 deletions

View file

@ -765,6 +765,26 @@ pub fn (s string) last_index(needle string) ?int {
return idx
}
// index_u8_last returns the index of the *last* occurrence of the byte `c` (if found) in the string.
// It returns -1, if `c` is not found.
@[deprecated: 'use `.last_index_u8(c u8)` instead']
@[deprecated_after: '2024-06-30']
@[inline]
pub fn (s string) index_u8_last(c u8) int {
return s.last_index_u8(c)
}
// last_index_u8 returns the index of the last occurrence of byte `c` if it was found in the string.
@[direct_array_access]
pub fn (s string) last_index_u8(c u8) int {
for i := s.len - 1; i >= 0; i-- {
if s[i] == c {
return i
}
}
return -1
}
pub fn (s string) trim_space() string {
res := ''
#res.str = s.str.trim()
@ -977,27 +997,6 @@ pub fn (s string) index(search string) ?int {
return res
}
// index_u8_last returns the index of the *last* occurrence of the byte `c` (if found) in the string.
// It returns -1, if `c` is not found.
@[direct_array_access]
pub fn (s string) index_u8_last(c u8) int {
for i := s.len - 1; i >= 0; i-- {
if s[i] == c {
return i
}
}
return -1
}
// last_index_u8 returns the index of the last occurrence of byte `c` if found in the string.
// It returns -1, if the byte `c` is not found.
@[deprecated: 'use `.index_u8_last(c u8)` instead']
@[deprecated_after: '2023-12-18']
@[inline]
pub fn (s string) last_index_u8(c u8) int {
return s.index_u8_last(c)
}
pub fn (_rune string) utf32_code() int {
res := 0
#res.val = s.str.charCodeAt()