String Handling#
Python accepts a str or ovstage.Token for scalar dual-mode
attribute-name arguments, such as write, map, delete, and attribute-floor lookup.
The list arguments Stage.read_attributes(attrs), Stage.query(attrs=...),
and Stage.advance_write_floor(attributes=...) require Token objects.
Use PathDictionary.intern_token()
for those lists, and reuse tokens when passing the same names repeatedly.
Use Token(raw_id) to explicitly reconstruct a token from a numeric ID in
the same dictionary, and token.raw_id when a numeric API needs its ID.
Bare integers are not accepted as tokens.
In C, ovx_string_t contains a pointer and a byte length. The text is
UTF-8. An input string need not end with a null character; use its length when
reading it. The view does not own its buffer.
Passing Strings or Tokens#
In C, arguments of type ovx_token_or_string_t accept a string or a
token. When string.ptr is non-null, the API uses string
and its length and ignores token, including for an empty string with length
zero. Otherwise, the API uses token: ID 0 represents the empty string,
and OVX_INVALID_TOKEN (UINT64_MAX) is invalid. Reuse a token when passing
the same name repeatedly, leaving string.ptr = NULL to select it.
Python accepts a str or a Token for these arguments. The snippets
below show an attribute passed as a string in Python and as a token in C.
The Python snippet assumes an existing stage and query; the C snippet uses the
previously resolved attr token.
# Attribute arguments accept an interned token (int) or a plain str:
# a token skips the per-call dictionary lookup, a str is interned for
# you at call time.
stage.write_attribute(
query, "temperature", ordinal=2, tensors=np.array([4.0, 5.0, 6.0], np.float32),
is_array=False,
).wait()
stage.advance_write_floor(ordinal=2).wait()
// Attributes pass as ovx_token_or_string_t. We already hold an interned
// token, so use its ID and leave string.ptr null to skip a string lookup.
ovx_token_or_string_t attrArg{ attr, {} };
Use the stage’s dictionary to create tokens. See Path Dictionary for the Python and C examples and token lifetimes.
Reading Returned Strings in C#
Read
lengthbytes. A null pointer with zero length represents no value; a null pointer with nonzero length is invalid.Compare lengths first, then compare that many bytes with
memcmp(). For zero-length strings, no byte comparison is needed.strncmp()stops at a null character and is not a general byte comparison.In C++, use
std::string_viewto borrow a string orstd::stringto copy it.A nonempty library-produced
ovx_string_thas a trailing null character, as specified by its header. This does not extend the buffer’s lifetime.
In C, write the specified bytes without requiring a trailing null character.
For a valid name view, this also avoids narrowing its length to int.
Include <stdio.h> and check the returned byte count for output errors:
// An empty view may have a null pointer.
if (name.length != 0)
fwrite(name.ptr, 1, name.length, stdout);
The C++ snippet below borrows the returned string using its pointer and length:
// ovx_string_t is a non-owning (ptr, length) view; wrap it in string_view
// for zero-copy use. Copy it if it must outlive the dictionary.
std::string_view name{ resolved.ptr, resolved.length };
std::printf("attribute token %llu = '%.*s'\n", (unsigned long long)attr.raw_id, (int)name.size(), name.data());
Keep the owner alive while using a borrowed string. Dictionary strings last for the dictionary’s lifetime. Operation error messages have a shorter lifetime; see Error Handling and Diagnostics. Copy a string if you need it after its owner releases or replaces the buffer.