Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String slices #4996

Merged
merged 27 commits into from
Sep 11, 2023
Merged

String slices #4996

merged 27 commits into from
Sep 11, 2023

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Aug 22, 2023

Description

This PR introduces string slices.

The basic usage is at test/src/ir_generation/tests/str_slice.sw:

let a: str = "ABC";

Before this PR a would be of type str[3], a string array.

Now both string slices and string arrays exist. This PR contains a new intrinsic that converts from string literals to arrays.

let a: str = "ABC";
let b: str[3] = __to_str_array("ABC");

Runtime conversions can be done using

let a = "abcd";
let b: str[4] = a.try_as_str_array().unwrap();
let c = from_str_array(b);

string slices to string arrays can fail, so they return Option<str[N]>; and because of this try_as_str_array lives in std. The inverse, from_str_array only fails if alloc fails and lives in core.

At this PR string slices are forbidden at configurable, storage, const, and main arguments and returns. The reason for these limitations is the internal structure of string slices having a ptr.

The optimized IR for initializing the slice is:

v0 = const string<3> "abc"
v1 = ptr_to_int v0 to u64, !2
v2 = get_local ptr { u64, u64 }, __anon_0, !2
v3 = const u64 0
v4 = get_elem_ptr v2, ptr u64, v3
store v1 to v4, !2

v5 = const u64 1
v6 = get_elem_ptr v2, ptr u64, v5
v7 = const u64 3
store v7 to v6, !2

v8 = get_local ptr slice, __anon_1, !2
mem_copy_bytes v8, v2, 16

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@xunilrj xunilrj marked this pull request as draft August 22, 2023 10:56
@xunilrj xunilrj self-assigned this Aug 23, 2023
@xunilrj xunilrj force-pushed the xunilrj/str branch 2 times, most recently from 0068573 to 9a2b7fa Compare August 30, 2023 12:56
@xunilrj xunilrj force-pushed the xunilrj/str branch 3 times, most recently from 76402e1 to 94db2d9 Compare September 6, 2023 11:46
@xunilrj xunilrj requested review from esdrubal, tritao, vaivaswatha and a team September 6, 2023 15:21
@xunilrj xunilrj marked this pull request as ready for review September 6, 2023 15:22
Copy link
Contributor

@anton-trunov anton-trunov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@xunilrj xunilrj added the breaking May cause existing user code to break. Requires a minor or major release. label Sep 10, 2023
@anton-trunov anton-trunov added the language feature Core language features visible to end users label Sep 10, 2023
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@IGI-111 IGI-111 enabled auto-merge (squash) September 11, 2023 09:43
@IGI-111 IGI-111 merged commit f88bbf4 into master Sep 11, 2023
27 checks passed
@IGI-111 IGI-111 deleted the xunilrj/str branch September 11, 2023 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking May cause existing user code to break. Requires a minor or major release. language feature Core language features visible to end users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants