Skip to content

str.find/index_of: a start-offset form (find(needle, from)) #2377

Closed

Description

@ehartford

From #2358: s.find(block, first + 1) is what a tool script naturally writes to find the next occurrence. #2358 makes the two-argument call a clean diagnostic ("find expects 1 argument(s), found 2"); this issue tracks the enhancement itself.

What needs a ruling/spec words before implementation:

  • spelling: positional find(needle, from) vs named/default parameter (from: isize = 0);
  • semantics: negative from (clamp to 0? from end?), from > len() (no match/-1?), byte vs char offset (find returns a byte offset today);
  • whether index_of, contains, starts_with grow the same parameter or only find.

Implementation sketch once ruled: BuiltinSigs row gains the parameter (BuiltinSigs has no default-value spelling yet, so a defaulted parameter needs one, or Sema pads the omitted argument the way Atomic's Ordering is materialized in MirLower); MirIntrinsic.STR_FIND/STR_INDEX_OF carry the offset to with_str_index_of_ref (rt grows a from-variant or the offset is applied to a slice view); ComptimeEval's comptime_str_find and the emit-C path (CcBuiltin.STR_FIND) follow.

Activity

  1. added a commit that references this issue on Oct 14, 2026
  2. ehartford commented on Oct 14, 2026

    @ehartford
    ContributorAuthor

    Done in #2402 as D129 (delegated): find(needle: &str, from: isize = 0) -> isize is an impl str method in std.string over the index_of intrinsic; from past len() finds nothing, a negative from is out of range (D71), an empty needle is found at from. rfind no longer panics inside a multibyte character.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions