Skip to content

string

String Utility API

This module provides utility functions for string manipulation and formatting.

Functions:

Copyright 2025 Ambiq. All Rights Reserved.

Functions

array_to_string

array_to_string(data: NDArray, row_len: int = 20) -> str

Generate csv like string from array.

Given a numpy array, this function converts it to a string representation with a specified number of elements per row. Useful for dumping data in C code.

Parameters:

  • data

    (NDArray) –

    Data array

  • row_len

    (int, default: 20 ) –

    Elements to write per row. Defaults to 20.

Returns:

  • str ( str ) –

    String representation of the array.

Example:

data = np.random.rand(10, 10).astype(np.float32)
data_str = array_to_string(
    data,
    row_len=20
)
print(data_str)

sanitize_for_c_comment

sanitize_for_c_comment(text: str | bytes | bytearray) -> str

Sanitize a string (or bytes) so it can safely live inside a C block comment (/ ... /).

Transforms: - If bytes/bytearray, decode as UTF-8 (replacing errors). - Replaces '/' with '/ ' and '/' with ' /' - Strips out non-printable/control chars except tab/newline/carriage-return.

Parameters:

Returns:

  • str ( str ) –

    Sanitized text.