From 9e5fa742b6ef66a546c0a77c14f834f2268c5f71 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 15 Mar 2016 23:37:09 -0400 Subject: Remove useless character counting --- src/node.c | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index 8b1ad2c3..dac03b55 100644 --- a/src/node.c +++ b/src/node.c @@ -21,7 +21,6 @@ #include "serd/serd.h" -#include #include #include #include @@ -46,10 +45,9 @@ serd_node_from_string(SerdType type, const uint8_t* str) return SERD_NODE_NULL; } - SerdNodeFlags flags = 0; - size_t buf_n_bytes = 0; - const size_t buf_n_chars = serd_strlen(str, &buf_n_bytes, &flags); - SerdNode ret = {str, buf_n_bytes, buf_n_chars, flags, type}; + SerdNodeFlags flags = 0; + const size_t n_bytes = serd_strlen(str, &flags); + const SerdNode ret = {str, n_bytes, flags, type}; return ret; } @@ -60,11 +58,9 @@ serd_node_from_substring(SerdType type, const uint8_t* str, const size_t len) return SERD_NODE_NULL; } - SerdNodeFlags flags = 0; - size_t buf_n_bytes = 0; - const size_t buf_n_chars = serd_substrlen(str, len, &buf_n_bytes, &flags); - assert(buf_n_bytes <= len); - SerdNode ret = { str, buf_n_bytes, buf_n_chars, flags, type }; + SerdNodeFlags flags = 0; + const size_t n_bytes = serd_substrlen(str, len, &flags); + const SerdNode ret = {str, n_bytes, flags, type}; return ret; } @@ -88,7 +84,6 @@ serd_node_equals(const SerdNode* a, const SerdNode* b) return (a == b) || (a->type == b->type && a->n_bytes == b->n_bytes - && a->n_chars == b->n_chars && ((a->buf == b->buf) || !memcmp((const char*)a->buf, (const char*)b->buf, a->n_bytes + 1))); @@ -221,13 +216,12 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out) const size_t len = serd_uri_string_length(&abs_uri); uint8_t* buf = (uint8_t*)malloc(len + 1); - SerdNode node = { buf, 0, 0, 0, SERD_URI }; + SerdNode node = { buf, len, 0, SERD_URI }; uint8_t* ptr = buf; const size_t actual_len = serd_uri_serialise(&abs_uri, string_sink, &ptr); buf[actual_len] = '\0'; node.n_bytes = actual_len; - node.n_chars = serd_strlen(buf, NULL, NULL); if (out) { serd_uri_parse(buf, out); // TODO: cleverly avoid double parse @@ -245,14 +239,13 @@ serd_node_new_relative_uri(const SerdURI* uri, const size_t uri_len = serd_uri_string_length(uri); const size_t base_len = serd_uri_string_length(base); uint8_t* buf = (uint8_t*)malloc(uri_len + base_len + 1); - SerdNode node = { buf, 0, 0, 0, SERD_URI }; + SerdNode node = { buf, 0, 0, SERD_URI }; uint8_t* ptr = buf; const size_t actual_len = serd_uri_serialise_relative( uri, base, root, string_sink, &ptr); buf[actual_len] = '\0'; node.n_bytes = actual_len; - node.n_chars = serd_strlen(buf, NULL, NULL); if (out) { serd_uri_parse(buf, out); // TODO: cleverly avoid double parse @@ -278,7 +271,7 @@ serd_node_new_decimal(double d, unsigned frac_digits) const double abs_d = fabs(d); const unsigned int_digits = serd_digits(abs_d); char* buf = (char*)calloc(int_digits + frac_digits + 3, 1); - SerdNode node = { (const uint8_t*)buf, 0, 0, 0, SERD_LITERAL }; + SerdNode node = { (const uint8_t*)buf, 0, 0, SERD_LITERAL }; const double int_part = floor(abs_d); // Point s to decimal point location @@ -301,7 +294,7 @@ serd_node_new_decimal(double d, unsigned frac_digits) double frac_part = fabs(d - int_part); if (frac_part < DBL_EPSILON) { *s++ = '0'; - node.n_bytes = node.n_chars = (size_t)(s - buf); + node.n_bytes = (size_t)(s - buf); } else { uint64_t frac = (uint64_t)llround(frac_part * pow(10.0, (int)frac_digits)); s += frac_digits - 1; @@ -310,7 +303,7 @@ serd_node_new_decimal(double d, unsigned frac_digits) // Skip trailing zeros for (; i < frac_digits - 1 && !(frac % 10); ++i, --s, frac /= 10) {} - node.n_bytes = node.n_chars = (size_t)(s - buf) + 1u; + node.n_bytes = (size_t)(s - buf) + 1u; // Write digits from last trailing zero to decimal point for (; i < frac_digits; ++i) { @@ -328,7 +321,7 @@ serd_node_new_integer(int64_t i) int64_t abs_i = (i < 0) ? -i : i; const unsigned digits = serd_digits((double)abs_i); char* buf = (char*)calloc(digits + 2, 1); - SerdNode node = { (const uint8_t*)buf, 0, 0, 0, SERD_LITERAL }; + SerdNode node = { (const uint8_t*)buf, 0, 0, SERD_LITERAL }; // Point s to the end char* s = buf + digits - 1; @@ -337,7 +330,7 @@ serd_node_new_integer(int64_t i) ++s; } - node.n_bytes = node.n_chars = (size_t)(s - buf) + 1u; + node.n_bytes = (size_t)(s - buf) + 1u; // Write integer part (right to left) do { @@ -352,7 +345,7 @@ serd_node_new_blob(const void* buf, size_t size, bool wrap_lines) { const size_t len = serd_base64_get_length(size, wrap_lines); uint8_t* str = (uint8_t*)calloc(len + 2, 1); - SerdNode node = { str, len, len, 0, SERD_LITERAL }; + SerdNode node = {str, len, 0, SERD_LITERAL}; if (serd_base64_encode(str, buf, size, wrap_lines)) { node.flags |= SERD_HAS_NEWLINE; -- cgit v1.2.1