From bd803a92082a85d72734aef2f5aaf18e960e219d Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 25 Dec 2025 23:02:17 +0200 Subject: [PATCH] builtin: make string.index_ public, to simplify code using a redundant `x := s.index(sub) or { -1 }` pattern (#26123) --- vlib/builtin/string.v | 4 ++-- vlib/net/http/request.v | 19 ++++++++++--------- vlib/semver/parse.v | 2 +- vlib/time/parse.c.v | 2 +- vlib/toml/checker/checker.v | 2 +- vlib/v/ast/cflags.v | 2 +- vlib/v/fmt/fmt.v | 2 +- 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 99e6cd1d3..021b87283 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1265,10 +1265,10 @@ pub fn (s string) substr_ni(_start int, _end int) string { return res } -// index returns the position of the first character of the input string. +// index_ returns the position of the first character of the input string. // It will return `-1` if the input string can't be found. @[direct_array_access] -fn (s string) index_(p string) int { +pub fn (s string) index_(p string) int { if p.len > s.len || p.len == 0 { return -1 } diff --git a/vlib/net/http/request.v b/vlib/net/http/request.v index 628b562cf..e1ad0b00d 100644 --- a/vlib/net/http/request.v +++ b/vlib/net/http/request.v @@ -323,7 +323,7 @@ fn (req &Request) receive_all_data_from_cb_in_builder(mut content strings.Builde req.on_progress(req, bchunk, u64(new_len))! } if body_pos == 0 { - bidx := schunk.index('\r\n\r\n') or { -1 } + bidx := schunk.index_(headers_body_boundary) if bidx > 0 { body_buffer_offset := bidx + 4 bchunk = unsafe { (&u8(bchunk.data) + body_buffer_offset).vbytes(len - body_buffer_offset) } @@ -333,7 +333,7 @@ fn (req &Request) receive_all_data_from_cb_in_builder(mut content strings.Builde body_so_far := u64(new_len) - body_pos if req.on_progress_body != unsafe { nil } { if expected_size == 0 { - lidx := schunk.index('Content-Length: ') or { -1 } + lidx := schunk.index_('Content-Length: ') if lidx > 0 { esize := schunk[lidx..].all_before('\r\n').all_after(': ').u64() if esize > 0 { @@ -435,7 +435,10 @@ pub fn parse_request_head(mut reader io.BufferedReader) !Request { // parse_request_head parses *only* the header of a raw HTTP request into a Request object pub fn parse_request_head_str(s string) !Request { - pos0 := s.index('\n') or { return error('malformed request: no request line found') } + pos0 := s.index_('\n') + if pos0 == -1 { + return error('malformed request: no request line found') + } line0 := s[..pos0].trim_space() method, target, version := parse_request_line(line0)! @@ -487,17 +490,15 @@ pub fn parse_request_head_str(s string) !Request { } } +const headers_body_boundary = '\r\n\r\n' + // parse_request_str parses a raw HTTP request string into a Request object. pub fn parse_request_str(s string) !Request { mut request := parse_request_head_str(s)! - - delim := '\r\n\r\n' - body_pos := s.index(delim) or { -1 } - + body_pos := s.index_(headers_body_boundary) if body_pos != -1 { - request.data = s[body_pos + delim.len..] + request.data = s[body_pos + headers_body_boundary.len..] } - return request } diff --git a/vlib/semver/parse.v b/vlib/semver/parse.v index 5d28023cd..b38cd0539 100644 --- a/vlib/semver/parse.v +++ b/vlib/semver/parse.v @@ -24,7 +24,7 @@ fn parse(input string) RawVersion { metadata = raw_version[(plus_idx + 1)..] raw_version = raw_version[0..plus_idx] } - hyphen_idx := raw_version.index('-') or { -1 } + hyphen_idx := raw_version.index_('-') if hyphen_idx > 0 { prerelease = raw_version[(hyphen_idx + 1)..] raw_version = raw_version[0..hyphen_idx] diff --git a/vlib/time/parse.c.v b/vlib/time/parse.c.v index afc31ed9b..175f49041 100644 --- a/vlib/time/parse.c.v +++ b/vlib/time/parse.c.v @@ -397,7 +397,7 @@ pub fn parse_iso8601(s string) !Time { if s == '' { return error_invalid_time(0, 'datetime string is empty') } - t_i := s.index('T') or { -1 } + t_i := s.index_('T') parts := if t_i != -1 { [s[..t_i], s[t_i + 1..]] } else { s.split(' ') } if !(parts.len == 1 || parts.len == 2) { return error_invalid_time(12, 'malformed date') diff --git a/vlib/toml/checker/checker.v b/vlib/toml/checker/checker.v index a82947f34..3109f132f 100644 --- a/vlib/toml/checker/checker.v +++ b/vlib/toml/checker/checker.v @@ -116,7 +116,7 @@ fn (c &Checker) check_number(num ast.Number) ! { mut is_bin, mut is_oct, mut is_hex := false, false, false is_float := lit_lower_case.all_before('e').contains('.') has_exponent_notation := lit_lower_case.contains('e') - float_decimal_index := lit.index('.') or { -1 } + float_decimal_index := lit.index_('.') // mut is_first_digit := u8(lit[0]).is_digit() mut ascii := u8(lit[0]).ascii_str() is_sign_prefixed := lit[0] in [`+`, `-`] diff --git a/vlib/v/ast/cflags.v b/vlib/v/ast/cflags.v index a0b53d73d..413b7ec7c 100644 --- a/vlib/v/ast/cflags.v +++ b/vlib/v/ast/cflags.v @@ -50,7 +50,7 @@ pub fn (mut t Table) parse_cflag(cflg string, mod string, ctimedefines []string) } } // -I/usr/local/a b c/include -m64 -I/usr/include - index := flag.index(' -') or { -1 } + index := flag.index_(' -') if index > -1 { value = flag[..index].trim_space() flag = flag[index..].trim_space() diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e5dc9f031..8e2129ac7 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2043,7 +2043,7 @@ pub fn (mut f Fmt) at_expr(node ast.AtExpr) { fn (mut f Fmt) write_static_method(name string, short_name string) { if short_name.contains('.') { - indx := short_name.index('.') or { -1 } + 1 + indx := short_name.index_('.') + 1 f.write(short_name[0..indx] + short_name[indx..].replace('__static__', '.').capitalize()) } else { f.write(short_name.replace('__static__', '.').capitalize()) -- 2.39.5