From 0e76b241817469ff9a35b3ec4c61d3da88b75804 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 5 Nov 2024 12:12:09 -0300 Subject: [PATCH] rename String UnsafePointer constructor args Signed-off-by: martinvuyk --- stdlib/src/builtin/file.mojo | 2 +- stdlib/src/collections/string.mojo | 12 +++++------- stdlib/src/sys/info.mojo | 2 +- stdlib/test/collections/test_string.mojo | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/stdlib/src/builtin/file.mojo b/stdlib/src/builtin/file.mojo index 15afa6606a..f725e05bd0 100644 --- a/stdlib/src/builtin/file.mojo +++ b/stdlib/src/builtin/file.mojo @@ -201,7 +201,7 @@ struct FileHandle: if err_msg: raise err_msg^.consume_as_error() - return String(buf, int(size_copy) + 1) + return String(ptr=buf, length=int(size_copy) + 1) fn read[ type: DType diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 3f9e28aa47..506a75c1d4 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -146,7 +146,7 @@ fn chr(c: Int) -> String: # p.free() # return chr(0xFFFD) p[num_bytes] = 0 - return String(ptr=p, len=num_bytes + 1) + return String(ptr=p, length=num_bytes + 1) # ===----------------------------------------------------------------------=== # @@ -819,7 +819,7 @@ struct String( self = literal.__str__() @always_inline - fn __init__(inout self, ptr: UnsafePointer[UInt8], len: Int): + fn __init__(inout self, *, ptr: UnsafePointer[Byte], length: Int): """Creates a string from the buffer. Note that the string now owns the buffer. @@ -827,14 +827,12 @@ struct String( Args: ptr: The pointer to the buffer. - len: The length of the buffer, including the null terminator. + length: The length of the buffer, including the null terminator. """ # we don't know the capacity of ptr, but we'll assume it's the same or # larger than len self = Self( - Self._buffer_type( - unsafe_pointer=ptr.bitcast[UInt8](), size=len, capacity=len - ) + Self._buffer_type(unsafe_pointer=ptr, size=length, capacity=length) ) # ===------------------------------------------------------------------=== # @@ -957,7 +955,7 @@ struct String( buff: The buffer. This should have an existing terminator. """ - return String(buff, len(StringRef(ptr=buff)) + 1) + return String(ptr=buff, length=len(StringRef(ptr=buff)) + 1) @staticmethod fn _from_bytes(owned buff: Self._buffer_type) -> String: diff --git a/stdlib/src/sys/info.mojo b/stdlib/src/sys/info.mojo index 004c77a7ef..9ff016075b 100644 --- a/stdlib/src/sys/info.mojo +++ b/stdlib/src/sys/info.mojo @@ -803,7 +803,7 @@ fn _macos_version() raises -> Tuple[Int, Int, Int]: if err: raise "Unable to query macOS version" - var osver = String(buf.steal_data(), buf_len) + var osver = String(ptr=buf.steal_data(), length=buf_len) var major = 0 var minor = 0 diff --git a/stdlib/test/collections/test_string.mojo b/stdlib/test/collections/test_string.mojo index b5a1d3007c..bf8db84bbd 100644 --- a/stdlib/test/collections/test_string.mojo +++ b/stdlib/test/collections/test_string.mojo @@ -94,7 +94,7 @@ def test_constructors(): ptr[1] = ord("b") ptr[2] = ord("c") ptr[3] = 0 - var s3 = String(ptr, 4) + var s3 = String(ptr=ptr, length=4) assert_equal(s3, "abc")