Skip to content

Commit

Permalink
Rename Span UnsafePointer constructor args
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Nov 5, 2024
1 parent 98067b5 commit 7d8457c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 37 deletions.
12 changes: 5 additions & 7 deletions stdlib/src/builtin/sort.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn _quicksort[
var imm_interval = stack.pop()
var ptr = imm_interval.unsafe_ptr()
var len = len(imm_interval)
var interval = Span[type, origin](unsafe_ptr=ptr, len=len)
var interval = Span[type, origin](ptr=ptr, length=len)

if len <= 5:
_delegate_small_sort[cmp_fn](interval)
Expand All @@ -242,19 +242,17 @@ fn _quicksort[
var pivot = _quicksort_partition_left[cmp_fn](interval)
if len > pivot + 2:
stack.append(
ImmSpan(unsafe_ptr=ptr + pivot + 1, len=len - pivot - 1)
ImmSpan(ptr=ptr + pivot + 1, length=len - pivot - 1)
)
continue

var pivot = _quicksort_partition_right[cmp_fn](interval)

if len > pivot + 2:
stack.append(
ImmSpan(unsafe_ptr=ptr + pivot + 1, len=len - pivot - 1)
)
stack.append(ImmSpan(ptr=ptr + pivot + 1, length=len - pivot - 1))

if pivot > 1:
stack.append(ImmSpan(unsafe_ptr=ptr, len=pivot))
stack.append(ImmSpan(ptr=ptr, length=pivot))


# ===----------------------------------------------------------------------===#
Expand Down Expand Up @@ -357,7 +355,7 @@ fn _stable_sort[
](span: Span[type, origin]):
var temp_buff = UnsafePointer[type].alloc(len(span))
var temp_buff_span = Span[type, __origin_of(temp_buff)](
unsafe_ptr=temp_buff, len=len(span)
ptr=temp_buff, length=len(span)
)
_stable_sort_impl[cmp_fn](span, temp_buff_span)
temp_buff.free()
Expand Down
6 changes: 2 additions & 4 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ struct StringLiteral(
"""

return Span[Byte, StaticConstantOrigin](
unsafe_ptr=self.unsafe_ptr(),
len=self.byte_length(),
ptr=self.unsafe_ptr(), length=self.byte_length()
)

@always_inline
Expand All @@ -474,8 +473,7 @@ struct StringLiteral(
"""
# Does NOT include the NUL terminator.
return Span[Byte, __origin_of(self)](
unsafe_ptr=self.unsafe_ptr(),
len=self.byte_length(),
ptr=self.unsafe_ptr(), length=self.byte_length()
)

@always_inline
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ struct String(

# Does NOT include the NUL terminator.
return Span[Byte, __origin_of(self)](
unsafe_ptr=self._buffer.unsafe_ptr(), len=self.byte_length()
ptr=self._buffer.unsafe_ptr(), length=self.byte_length()
)

@always_inline
Expand Down
8 changes: 2 additions & 6 deletions stdlib/src/utils/inline_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ struct InlineString(Sized, Stringable, CollectionElement, CollectionElementNew):
"""

return Span[Byte, __origin_of(self)](
unsafe_ptr=self.unsafe_ptr(),
# Does NOT include the NUL terminator.
len=len(self),
ptr=self.unsafe_ptr(), length=len(self)
)


Expand Down Expand Up @@ -532,7 +530,5 @@ struct _FixedString[CAP: Int](
"""

return Span[Byte, __origin_of(self)](
unsafe_ptr=self.unsafe_ptr(),
# Does NOT include the NUL terminator.
len=self.size,
ptr=self.unsafe_ptr(), length=self.size
)
15 changes: 7 additions & 8 deletions stdlib/src/utils/span.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ struct Span[
# ===------------------------------------------------------------------===#

@always_inline
fn __init__(inout self, *, unsafe_ptr: UnsafePointer[T], len: Int):
fn __init__(inout self, *, ptr: UnsafePointer[T], length: Int):
"""Unsafe construction from a pointer and length.
Args:
unsafe_ptr: The underlying pointer of the span.
len: The length of the view.
ptr: The underlying pointer of the span.
length: The length of the view.
"""
self._data = unsafe_ptr
self._len = len
self._data = ptr
self._len = length

@always_inline
fn __init__(inout self, *, other: Self):
Expand Down Expand Up @@ -202,8 +202,7 @@ struct Span[
step == 1, "Slice must be within bounds and step must be 1"
)
var res = Self(
unsafe_ptr=(self._data + start),
len=len(range(start, end, step)),
ptr=(self._data + start), length=len(range(start, end, step))
)

return res
Expand Down Expand Up @@ -351,5 +350,5 @@ struct Span[
A span covering the same elements, but without mutability.
"""
return Span[T, _lit_mut_cast[origin, False].result](
unsafe_ptr=self._data, len=self._len
ptr=self._data, length=self._len
)
11 changes: 5 additions & 6 deletions stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ struct _StringSliceIter[
self.ptr = unsafe_pointer
self.length = length
alias S = Span[Byte, StaticConstantOrigin]
var s = S(unsafe_ptr=self.ptr, len=self.length)
var s = S(ptr=self.ptr, length=self.length)
self.continuation_bytes = _count_utf8_continuation_bytes(s)

fn __iter__(self) -> Self:
Expand Down Expand Up @@ -317,8 +317,8 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
var strref = unsafe_from_utf8_strref

var byte_slice = Span[Byte, origin](
unsafe_ptr=strref.unsafe_ptr(),
len=len(strref),
ptr=strref.unsafe_ptr(),
length=len(strref),
)

self = Self(unsafe_from_utf8=byte_slice)
Expand All @@ -345,8 +345,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
duration of `origin`.
"""
var byte_slice = Span[Byte, origin](
unsafe_ptr=unsafe_from_utf8_ptr,
len=len,
ptr=unsafe_from_utf8_ptr, length=len
)

self._slice = byte_slice
Expand Down Expand Up @@ -398,7 +397,7 @@ struct StringSlice[is_mutable: Bool, //, origin: Origin[is_mutable].type,](
"""
var b_len = self.byte_length()
alias S = Span[Byte, StaticConstantOrigin]
var s = S(unsafe_ptr=self.unsafe_ptr(), len=b_len)
var s = S(ptr=self.unsafe_ptr(), length=b_len)
return b_len - _count_utf8_continuation_bytes(s)

fn write_to[W: Writer](self, inout writer: W):
Expand Down
6 changes: 2 additions & 4 deletions stdlib/src/utils/stringref.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ struct StringRef(
Returns:
A contiguous slice pointing to the bytes owned by this string.
"""
return Span[Byte, __origin_of(self)](
unsafe_ptr=self.data, len=self.length
)
return Span[Byte, __origin_of(self)](ptr=self.data, length=self.length)

# ===-------------------------------------------------------------------===#
# Operator dunders
Expand Down Expand Up @@ -413,7 +411,7 @@ struct StringRef(
# Safe because our use of this Span does not outlive `self`.
writer.write_bytes(
Span[Byte, ImmutableAnyOrigin](
unsafe_ptr=self.unsafe_ptr(), len=len(self)
ptr=self.unsafe_ptr(), length=len(self)
)
)

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/utils/write.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ struct _WriteBuffer[W: MovableWriter, //, capacity: Int](Writer):
fn flush(inout self):
self.writer.write_bytes(
Span[Byte, ImmutableAnyOrigin](
unsafe_ptr=self.data.unsafe_ptr(), len=self.pos
ptr=self.data.unsafe_ptr(), length=self.pos
)
)
self.pos = 0
Expand Down

0 comments on commit 7d8457c

Please sign in to comment.