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

[stdlib] Addition of a new constructor and reserve method to the String struct #3755

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ what we publish.
- Added `os.path.expandvars` to expand environment variables in a string.
([PR #3735](https://github.com/modularml/mojo/pull/3735) by [@thatstoasty](https://github.com/thatstoasty)).

- Added a `reserve` method and new constructor to the `String` struct to
allocate additional capacity.
([PR #3755](https://github.com/modularml/mojo/pull/3755) by [@thatstoasty](https://github.com/thatstoasty)).

### 🦋 Changed

- More things have been removed from the auto-exported set of entities in the `prelude`
Expand Down
21 changes: 21 additions & 0 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,15 @@ struct String(
"""Construct an uninitialized string."""
self._buffer = Self._buffer_type()

@always_inline
fn __init__(inout self, *, capacity: Int):
"""Construct an uninitialized string with the given capacity.

Args:
capacity: The capacity of the string.
"""
self._buffer = Self._buffer_type(capacity=capacity)

fn __init__(inout self, *, other: Self):
"""Explicitly copy the provided value.

Expand Down Expand Up @@ -2296,6 +2305,18 @@ struct String(
var result = String(buffer)
return result^

fn reserve(inout self, new_capacity: Int):
"""Reserves the requested capacity.

Args:
new_capacity: The new capacity.

Notes:
If the current capacity is greater or equal, this is a no-op.
Otherwise, the storage is reallocated and the data is moved.
"""
self._buffer.reserve(new_capacity)


# ===----------------------------------------------------------------------=== #
# Utilities
Expand Down
11 changes: 11 additions & 0 deletions stdlib/test/collections/test_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def test_constructors():
var s3 = String(ptr, 4)
assert_equal(s3, "abc")

# Construction with capacity
var s4 = String(capacity=1)
assert_equal(s4._buffer.capacity, 1)


def test_copy():
var s0 = String("find")
Expand Down Expand Up @@ -1575,6 +1579,13 @@ def test_slice_contains():
)


def test_reserve():
var s = String()
assert_true(s._buffer.capacity == 0)
s.reserve(1)
assert_true(s._buffer.capacity == 1)


def main():
test_constructors()
test_copy()
Expand Down