Skip to content

Commit

Permalink
fix details
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Dec 10, 2024
1 parent 554a0b6 commit a3bfe40
Showing 1 changed file with 4 additions and 40 deletions.
44 changes: 4 additions & 40 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -784,42 +784,8 @@ struct String(

@always_inline
@implicit
fn __init__(out self, owned impl: List[UInt8, *_]):
"""Construct a string from a buffer of bytes without copying the
allocated data.
The buffer must be terminated with a null byte:
```mojo
var buf = List[UInt8]()
buf.append(ord('H'))
buf.append(ord('i'))
buf.append(0)
var hi = String(buf)
```
Args:
impl: The buffer.
"""
debug_assert(
len(impl) > 0 and impl[-1] == 0,
"expected last element of String buffer to be null terminator",
)
# We make a backup because steal_data() will clear length and capacity.
var length = len(impl)
debug_assert(
length > 0 and impl[length - 1] == 0,
"expected last element of String buffer to be null terminator",
)
var capacity = impl.capacity
self._buffer = Self._buffer_type(
ptr=impl.steal_data(), length=length, capacity=capacity
)

@always_inline
@implicit
fn __init__(out self, impl: Self._buffer_type):
"""Construct a string from a buffer of null terminated bytes, copying
fn __init__(out self, impl: List[Byte, *_]):
"""Construct a string from a buffer of null-terminated bytes, copying
the allocated data. Use the transfer operator `^` to avoid the copy.
Args:
Expand All @@ -832,13 +798,11 @@ struct String(
```
.
"""
# We make a backup because steal_data() will clear length and capacity.
var length = len(impl)
debug_assert(
length > 0 and impl[length - 1] == 0,
len(impl) > 0 and impl[-1] == 0,
"expected last element of String buffer to be null terminator",
)
self._buffer = impl
self._buffer = rebind[Self._buffer_type](impl)

@always_inline
fn __init__(out self):
Expand Down

0 comments on commit a3bfe40

Please sign in to comment.