Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Add _write_hex util #3873

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion mojo/stdlib/src/utils/write.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# ===----------------------------------------------------------------------=== #
"""Establishes the contract between `Writer` and `Writable` types."""

from bit import byte_swap
from collections import InlineArray
from sys.info import is_amd_gpu, is_gpu, is_nvidia_gpu

from memory import Span, UnsafePointer, memcpy
from memory import UnsafePointer, memcpy, Span, bitcast

from utils import StaticString

Expand Down Expand Up @@ -466,3 +467,104 @@ fn write_buffered[
var buffer = _WriteBufferStack(writer)
buffer.write_list(values, sep=sep)
buffer.flush()


# ===-----------------------------------------------------------------------===#
# Utils
# ===-----------------------------------------------------------------------===#


# fmt: off
alias _hex_table = SIMD[DType.uint8, 16](
ord("0"), ord("1"), ord("2"), ord("3"), ord("4"), ord("5"), ord("6"),
ord("7"), ord("8"), ord("9"), ord("a"), ord("b"), ord("c"), ord("d"),
ord("e"), ord("f"),
)
# fmt: on


@always_inline
fn _hex_digits_to_hex_chars(ptr: UnsafePointer[Byte], decimal: Scalar):
"""Write a fixed width hexadecimal value into an uninitialized pointer
location, assumed to be large enough for the value to be written.

Examples:

```mojo
%# from memory import memset_zero
%# from testing import assert_equal
%# from utils import StringSlice
%# from utils.write import _hex_digits_to_hex_chars
items = List[Byte](0, 0, 0, 0, 0, 0, 0, 0, 0)
alias S = StringSlice[__origin_of(items)]
ptr = items.unsafe_ptr()
_hex_digits_to_hex_chars(ptr, UInt32(ord("🔥")))
assert_equal("0001f525", S(ptr=ptr, length=8))
memset_zero(ptr, len(items))
_hex_digits_to_hex_chars(ptr, UInt16(ord("你")))
assert_equal("4f60", S(ptr=ptr, length=4))
memset_zero(ptr, len(items))
_hex_digits_to_hex_chars(ptr, UInt8(ord("Ö")))
assert_equal("d6", S(ptr=ptr, length=2))
```
.
"""

alias size = decimal.type.sizeof()
var data: SIMD[DType.uint8, size]

@parameter
if size == 1:
data = bitcast[DType.uint8, size](decimal)
else:
data = bitcast[DType.uint8, size](byte_swap(decimal))
var nibbles = (data >> 4).interleave(data & 0xF)
ptr.store(_hex_table._dynamic_shuffle(nibbles))


@always_inline
fn _write_hex[amnt_hex_bytes: Int](p: UnsafePointer[Byte], decimal: Int):
"""Write a python compliant hexadecimal value into an uninitialized pointer
location, assumed to be large enough for the value to be written.

Examples:

```mojo
%# from memory import memset_zero
%# from testing import assert_equal
%# from utils import StringSlice
%# from utils.write import _write_hex
items = List[Byte](0, 0, 0, 0, 0, 0, 0, 0, 0)
alias S = StringSlice[__origin_of(items)]
ptr = items.unsafe_ptr()
_write_hex[8](ptr, ord("🔥"))
assert_equal(r"\\U0001f525", S(ptr=ptr, length=10))
memset_zero(ptr, len(items))
_write_hex[4](ptr, ord("你"))
assert_equal(r"\\u4f60", S(ptr=ptr, length=6))
memset_zero(ptr, len(items))
_write_hex[2](ptr, ord("Ö"))
assert_equal(r"\\xd6", S(ptr=ptr, length=4))
```
.
"""

constrained[amnt_hex_bytes in (2, 4, 8), "only 2 or 4 or 8 sequences"]()

alias `\\` = Byte(ord("\\"))
alias `x` = Byte(ord("x"))
alias `u` = Byte(ord("u"))
alias `U` = Byte(ord("U"))

p.init_pointee_move(`\\`)

@parameter
if amnt_hex_bytes == 2:
(p + 1).init_pointee_move(`x`)
_hex_digits_to_hex_chars(p + 2, UInt8(decimal))
elif amnt_hex_bytes == 4:
(p + 1).init_pointee_move(`u`)
_hex_digits_to_hex_chars(p + 2, UInt16(decimal))
else:
(p + 1).init_pointee_move(`U`)
_hex_digits_to_hex_chars(p + 2, UInt32(decimal))
85 changes: 68 additions & 17 deletions mojo/stdlib/test/utils/test_write.mojo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2025, Modular Inc. All rights reserved.
# Copyright (c) 2024, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
Expand All @@ -16,17 +16,15 @@ from collections.string.inline_string import _FixedString

from testing import assert_equal

from utils import Writable, Writer


fn main() raises:
test_writer_of_string()
test_string_format_seq()
test_stringable_based_on_format()

test_writer_of_fixed_string()

test_write_int_padded()
from memory.memory import memset_zero
from utils import StringSlice
from utils.write import (
Writable,
Writer,
_write_hex,
_hex_digits_to_hex_chars,
)
from collections.string.inline_string import _FixedString


@value
Expand All @@ -43,7 +41,7 @@ struct Point(Writable, Stringable):
return String.write(self)


fn test_writer_of_string() raises:
def test_writer_of_string():
#
# Test write_to(String)
#
Expand All @@ -59,7 +57,7 @@ fn test_writer_of_string() raises:
assert_equal(s2, "Point(3, 8)")


fn test_string_format_seq() raises:
def test_string_write_seq():
var s1 = String.write("Hello, ", "World!")
assert_equal(s1, "Hello, World!")

Expand All @@ -70,17 +68,17 @@ fn test_string_format_seq() raises:
assert_equal(s3, "")


fn test_stringable_based_on_format() raises:
def test_stringable_based_on_format():
assert_equal(String(Point(10, 11)), "Point(10, 11)")


fn test_writer_of_fixed_string() raises:
def test_writer_of_fixed_string():
var s1 = _FixedString[100]()
s1.write("Hello, World!")
assert_equal(String(s1), "Hello, World!")


fn test_write_int_padded() raises:
def test_write_int_padded():
var s1 = String()

Int(5).write_padded(s1, width=5)
Expand All @@ -100,3 +98,56 @@ fn test_write_int_padded() raises:
Int(12345).write_padded(s2, width=3)

assert_equal(s2, "12345")


def test_hex_digits_to_hex_chars():
items = List[Byte](0, 0, 0, 0, 0, 0, 0, 0, 0)
alias S = StringSlice[__origin_of(items)]
ptr = items.unsafe_ptr()
_hex_digits_to_hex_chars(ptr, UInt32(ord("🔥")))
assert_equal("0001f525", String(S(ptr=ptr, length=8)))
memset_zero(ptr, len(items))
_hex_digits_to_hex_chars(ptr, UInt16(ord("你")))
assert_equal("4f60", String(S(ptr=ptr, length=4)))
memset_zero(ptr, len(items))
_hex_digits_to_hex_chars(ptr, UInt8(ord("Ö")))
assert_equal("d6", String(S(ptr=ptr, length=2)))
_hex_digits_to_hex_chars(ptr, UInt8(0))
assert_equal("00", String(S(ptr=ptr, length=2)))
_hex_digits_to_hex_chars(ptr, UInt16(0))
assert_equal("0000", String(S(ptr=ptr, length=4)))
_hex_digits_to_hex_chars(ptr, UInt32(0))
assert_equal("00000000", String(S(ptr=ptr, length=8)))
_hex_digits_to_hex_chars(ptr, ~UInt8(0))
assert_equal("ff", String(S(ptr=ptr, length=2)))
_hex_digits_to_hex_chars(ptr, ~UInt16(0))
assert_equal("ffff", String(S(ptr=ptr, length=4)))
_hex_digits_to_hex_chars(ptr, ~UInt32(0))
assert_equal("ffffffff", String(S(ptr=ptr, length=8)))


def test_write_hex():
items = List[Byte](0, 0, 0, 0, 0, 0, 0, 0, 0)
alias S = StringSlice[__origin_of(items)]
ptr = items.unsafe_ptr()
_write_hex[8](ptr, ord("🔥"))
assert_equal(r"\U0001f525", String(S(ptr=ptr, length=10)))
memset_zero(ptr, len(items))
_write_hex[4](ptr, ord("你"))
assert_equal(r"\u4f60", String(S(ptr=ptr, length=6)))
memset_zero(ptr, len(items))
_write_hex[2](ptr, ord("Ö"))
assert_equal(r"\xd6", String(S(ptr=ptr, length=4)))


def main():
test_writer_of_string()
test_string_write_seq()
test_stringable_based_on_format()

test_writer_of_fixed_string()

test_write_int_padded()

test_hex_digits_to_hex_chars()
test_write_hex()