Skip to content

Commit

Permalink
builtin: update last_index_u8, deprecate index_u8_last string met…
Browse files Browse the repository at this point in the history
…hods, make consistent with `last_index` (#21604)
  • Loading branch information
ttytm authored Jun 4, 2024
1 parent 2250d60 commit a2ce55d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 52 deletions.
41 changes: 20 additions & 21 deletions vlib/builtin/js/string.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 10 additions & 11 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -1340,25 +1340,24 @@ pub fn (s string) index_u8(c u8) int {

// 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]
@[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.
@[inline]
pub fn (s string) last_index_u8(c u8) int {
for i := s.len - 1; i >= 0; i-- {
if unsafe { s.str[i] == c } {
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)
}

// count returns the number of occurrences of `substr` in the string.
// count returns -1 if no `substr` could be found.
@[direct_array_access]
Expand Down
20 changes: 10 additions & 10 deletions vlib/builtin/string_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ fn test_index_u8() {
//
}

fn test_index_last() {
fn test_last_index() {
assert 'abcabca'.last_index('ca')? == 5
assert 'abcabca'.last_index('ab')? == 3
assert 'abcabca'.last_index('b')? == 4
Expand All @@ -1500,16 +1500,16 @@ fn test_index_last() {
// TODO: `assert 'Zabcabca'.index_last('Y') == none` is a cgen error, 2023/12/04
}

fn test_index_u8_last() {
assert 'abcabca'.index_u8_last(`a`) == 6
assert 'abcabca'.index_u8_last(`c`) == 5
assert 'abcabca'.index_u8_last(`b`) == 4
assert 'Zabcabca'.index_u8_last(`Z`) == 0
fn test_last_index_u8() {
assert 'abcabca'.last_index_u8(`a`) == 6
assert 'abcabca'.last_index_u8(`c`) == 5
assert 'abcabca'.last_index_u8(`b`) == 4
assert 'Zabcabca'.last_index_u8(`Z`) == 0
//
assert 'abc'.index_u8(`d`) == -1
assert 'abc'.index_u8(`A`) == -1
assert 'abc'.index_u8(`B`) == -1
assert 'abc'.index_u8(`C`) == -1
assert 'abc'.last_index_u8(`d`) == -1
assert 'abc'.last_index_u8(`A`) == -1
assert 'abc'.last_index_u8(`B`) == -1
assert 'abc'.last_index_u8(`C`) == -1
}

fn test_contains_byte() {
Expand Down
10 changes: 5 additions & 5 deletions vlib/net/urllib/urllib.v
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ struct ParseAuthorityRes {
}

fn parse_authority(authority string) !ParseAuthorityRes {
i := authority.index_u8_last(`@`)
i := authority.last_index_u8(`@`)
if i < 0 {
return ParseAuthorityRes{
host: parse_host(authority)!
Expand Down Expand Up @@ -564,7 +564,7 @@ fn parse_host(host string) !string {
if host.len > 0 && host[0] == `[` {
// parse an IP-Literal in RFC 3986 and RFC 6874.
// E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'.
i := host.index_u8_last(`]`)
i := host.last_index_u8(`]`)
if i == -1 {
return error(error_msg("parse_host: missing ']' in host", ''))
}
Expand All @@ -586,7 +586,7 @@ fn parse_host(host string) !string {
return host1 + host2 + host3
}
} else {
i := host.index_u8_last(`:`)
i := host.last_index_u8(`:`)
if i != -1 {
colon_port := host[i..]
if !valid_optional_port(colon_port) {
Expand Down Expand Up @@ -859,7 +859,7 @@ fn resolve_path(base string, ref string) string {
if ref == '' {
full = base
} else if ref[0] != `/` {
i := base.index_u8_last(`/`)
i := base.last_index_u8(`/`)
full = base[..i + 1] + ref
} else {
full = ref
Expand Down Expand Up @@ -993,7 +993,7 @@ pub fn (u &URL) port() string {
pub fn split_host_port(hostport string) (string, string) {
mut host := hostport
mut port := ''
colon := host.index_u8_last(`:`)
colon := host.last_index_u8(`:`)
if colon != -1 {
if valid_optional_port(host[colon..]) {
port = host[colon + 1..]
Expand Down
2 changes: 1 addition & 1 deletion vlib/os/os.v
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub fn file_ext(opath string) string {
return ''
}
path := file_name(opath)
pos := path.index_u8_last(`.`)
pos := path.last_index_u8(`.`)
if pos == -1 {
return ''
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/semver/parse.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn parse(input string) RawVersion {
mut raw_version := input
mut prerelease := ''
mut metadata := ''
plus_idx := raw_version.index_u8_last(`+`)
plus_idx := raw_version.last_index_u8(`+`)
if plus_idx > 0 {
metadata = raw_version[(plus_idx + 1)..]
raw_version = raw_version[0..plus_idx]
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
// but `x := T{}` is ok.
if !c.is_builtin_mod && !c.inside_unsafe && type_sym.language == .v
&& c.table.cur_concrete_types.len == 0 {
pos := type_sym.name.index_u8_last(`.`)
pos := type_sym.name.last_index_u8(`.`)
first_letter := type_sym.name[pos + 1]
if !first_letter.is_capital() && (type_sym.kind != .struct_
|| !(type_sym.info is ast.Struct && type_sym.info.is_anon))
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) stri
current_segment := g.out.substr(int(sm_pos), int(sourcemap_ns_entry.ns_pos))
current_line += u32(current_segment.count('\n'))
mut current_column := u32(0)
last_nl_pos := current_segment.index_u8_last(`\n`)
last_nl_pos := current_segment.last_index_u8(`\n`)
if last_nl_pos != -1 {
current_column = u32(current_segment.len - last_nl_pos - 1)
}
Expand Down Expand Up @@ -536,7 +536,7 @@ pub fn (mut g JsGen) new_tmp_var() string {
// 'fn' => ''
@[inline]
fn get_ns(s string) string {
idx := s.index_u8_last(`.`)
idx := s.last_index_u8(`.`)
if idx == -1 {
return ''
}
Expand Down

0 comments on commit a2ce55d

Please sign in to comment.