diff options
author | David Robillard <d@drobilla.net> | 2022-12-18 21:06:25 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:07 -0500 |
commit | 9c613b337712314c169d4add61212f4fc4102022 (patch) | |
tree | fe9191bed8d6aaf6339c82fe447def74e22ef11b /include/serd/node.h | |
parent | a970f06aba98736223214a6fa995f4e82acd7132 (diff) | |
download | serd-9c613b337712314c169d4add61212f4fc4102022.tar.gz serd-9c613b337712314c169d4add61212f4fc4102022.tar.bz2 serd-9c613b337712314c169d4add61212f4fc4102022.zip |
Clean up numeric node construction and access API
Diffstat (limited to 'include/serd/node.h')
-rw-r--r-- | include/serd/node.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/include/serd/node.h b/include/serd/node.h index 79c9ffe1..427bbdd1 100644 --- a/include/serd/node.h +++ b/include/serd/node.h @@ -189,6 +189,34 @@ SERD_API SerdNode* SERD_ALLOCATED serd_new_decimal(double d, const SerdNode* SERD_NULLABLE datatype); /** + Create a new canonical xsd:double literal. + + The node will be in scientific notation, like "1.23E4", except for NaN and + negative/positive infinity, which are "NaN", "-INF", and "INF", + respectively. + + Uses the shortest possible representation that precisely describes the + value, which has at most 17 significant digits (under 24 characters total). + + @param d Double value to write. + @return A literal node with datatype xsd:double. +*/ +SERD_API SerdNode* SERD_ALLOCATED +serd_new_double(double d); + +/** + Create a new canonical xsd:float literal. + + Uses identical formatting to serd_new_double(), except with at most 9 + significant digits (under 14 characters total). + + @param f Float value of literal. + @return A literal node with datatype xsd:float. +*/ +SERD_API SerdNode* SERD_ALLOCATED +serd_new_float(float f); + +/** Create a new canonical xsd:integer literal. The node will be an xsd:integer literal like "1234", with datatype @@ -291,6 +319,47 @@ SERD_PURE_API const SerdNode* SERD_NULLABLE serd_node_language(const SerdNode* SERD_NONNULL node); /** + Return the value of `node` as a boolean. + + This will work for booleans, and numbers of any datatype if they are 0 or + 1. + + @return The value of `node` as a `bool`, or `false` on error. +*/ +SERD_API bool +serd_get_boolean(const SerdNode* SERD_NONNULL node); + +/** + Return the value of `node` as a double. + + This will coerce numbers of any datatype to double, if the value fits. + + @return The value of `node` as a `double`, or NaN on error. +*/ +SERD_API double +serd_get_double(const SerdNode* SERD_NONNULL node); + +/** + Return the value of `node` as a float. + + This will coerce numbers of any datatype to float, if the value fits. + + @return The value of `node` as a `float`, or NaN on error. +*/ +SERD_API float +serd_get_float(const SerdNode* SERD_NONNULL node); + +/** + Return the value of `node` as a long (signed 64-bit integer). + + This will coerce numbers of any datatype to long, if the value fits. + + @return The value of `node` as a `int64_t`, or 0 on error. +*/ +SERD_API int64_t +serd_get_integer(const SerdNode* SERD_NONNULL node); + +/** @} @defgroup serd_node_operators Operators @{ |