diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 2aef31cfed..e7717f29f3 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -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: @@ -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):