diff --git a/stdlib/src/collections/string/string.mojo b/stdlib/src/collections/string/string.mojo index b31dbf6a49..f57cb879d9 100644 --- a/stdlib/src/collections/string/string.mojo +++ b/stdlib/src/collections/string/string.mojo @@ -781,68 +781,25 @@ 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) - ``` + fn __init__(out self, owned 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: - 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 size and capacity. - var size = impl.size - debug_assert( - impl[size - 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=size, capacity=capacity - ) - - @always_inline - @implicit - fn __init__(out self, impl: Self._buffer_type): - """Construct a string from a buffer of bytes, copying the allocated - data. Use the transfer operator ^ to avoid the copy. + impl: The null-terminated buffer. - The buffer must be terminated with a null byte: + Examples: ```mojo - var buf = List[UInt8]() - buf.append(ord('H')) - buf.append(ord('i')) - buf.append(0) - var hi = String(buf) + print(String(List[Byte](ord('h'), ord('i'), 0))) # hi ``` - - 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 size and capacity. - var size = impl.size - debug_assert( - impl[size - 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):