Skip to content

str.trim() copies; it should be a view like trim_start/trim_end (D71, D105) #2225

Closed

Description

@ehartford

str.trim() is the STR_TRIM intrinsic over with_str_trim_ref, which allocates an owned copy (rt_core.w: "#747: owned copy, never a view", written before D71 made s[a..b] a &str view). D105 (#2206) added trim_start()/trim_end() as views (-> &str, O(1), no allocation), so trim is now the odd one out and the only one that allocates.

Make trim a view too: -> &str over the same ASCII whitespace set, spelled in one place with the other two (an impl str method in std.string, retiring the intrinsic), and migrate the callers that hold the result as an owned str (.to_owned() where an owned value is actually needed). The runtime function retires with the intrinsic.

Spec: none; the no-implicit-copy rule and D71 already decide it. Decision log: D105.

Activity

  1. ehartford commented on Oct 7, 2026

    @ehartford
    ContributorAuthor

    Fixed in 60e53a2 (stack #2251): str.trim() is a view (pub fn trim() -> &str in lib/std/string.w; the STR_TRIM intrinsic is gone from Sema, codegen and the evaluator). test/behavior/behav_2225_trim_is_a_view.w and test/compile_errors/err_2225_view_of_temporary_receiver.w pin it; the callee temporary-view rule (§21.1) refuses temp().trim() escaping its statement.

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