Skip to content

Commit

Permalink
[External] [stdlib] [NFC] Rename String UnsafePointer constructor…
Browse files Browse the repository at this point in the history
… args (#50486)

[External] [stdlib] [NFC] Rename `String` `UnsafePointer` constructor
args

Rename `String` `UnsafePointer` constructor args. Part of
#3704.

ORIGINAL_AUTHOR=martinvuyk
<[email protected]>
PUBLIC_PR_LINK=#3741

Co-authored-by: martinvuyk <[email protected]>
Closes #3741
MODULAR_ORIG_COMMIT_REV_ID: 7c043cc298711e9d981a61a80693ca4b7278f4c0
  • Loading branch information
modularbot and martinvuyk committed Dec 17, 2024
1 parent 0b19358 commit 3057347
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion stdlib/src/builtin/file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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)


# ===----------------------------------------------------------------------=== #
Expand Down Expand Up @@ -828,22 +828,20 @@ 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.
The buffer must be terminated with a null byte.
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)
)

# ===------------------------------------------------------------------=== #
Expand Down Expand Up @@ -966,7 +964,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:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/sys/info.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/test/collections/test_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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")

# Construction with capacity
Expand Down

0 comments on commit 3057347

Please sign in to comment.