aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-06 20:59:12 +0200
committerDavid Robillard <d@drobilla.net>2022-01-13 23:03:44 -0500
commitd88b5a797f8502c40d0da964d653a1cd3028c872 (patch)
treebf2cbf8370515f24333b2c859fedf6c27877da12 /include
parent2ddf10fb8bfabff3ecdbe9ea866a905631ec2866 (diff)
downloadserd-d88b5a797f8502c40d0da964d653a1cd3028c872.tar.gz
serd-d88b5a797f8502c40d0da964d653a1cd3028c872.tar.bz2
serd-d88b5a797f8502c40d0da964d653a1cd3028c872.zip
Clean up numeric node construction and access API
Diffstat (limited to 'include')
-rw-r--r--include/serd/serd.h87
1 files changed, 75 insertions, 12 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index 80a12452..56dbcaaa 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -222,18 +222,6 @@ size_t
serd_strlen(const char* SERD_NONNULL str, SerdNodeFlags* SERD_NULLABLE flags);
/**
- Parse a string to a double.
-
- The API of this function is identical to the standard C strtod function,
- except this function is locale-independent and always matches the lexical
- format used in the Turtle grammar (the decimal point is always ".").
-*/
-SERD_API
-double
-serd_strtod(const char* SERD_NONNULL str,
- const char* SERD_NONNULL* SERD_NULLABLE endptr);
-
-/**
Decode a base64 string.
This function can be used to decode a node created with serd_new_base64().
@@ -618,6 +606,36 @@ SerdNode* SERD_ALLOCATED
serd_new_decimal(double d, const SerdNode* SERD_NULLABLE datatype);
/**
+ Create a new canonical xsd:double literal.
+
+ The returned node will always 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 `d`,
+ 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.
@param i Integer value of literal.
@@ -643,6 +661,51 @@ serd_new_base64(const void* SERD_NONNULL buf,
size_t size,
const SerdNode* SERD_NULLABLE datatype);
+/**
+ 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);
+
/// Return a deep copy of `node`
SERD_API
SerdNode* SERD_ALLOCATED