Browse Source

Removee duplicate functions.

Stefano Cossu 2 years ago
parent
commit
33c67a0f3e
5 changed files with 58 additions and 109 deletions
  1. 13 1
      include/codec.h
  2. 2 0
      include/codec/codec_ttl.h
  3. 43 0
      src/codec.c
  4. 0 54
      src/codec/codec_nt.c
  5. 0 54
      src/codec/codec_ttl.c

+ 13 - 1
include/codec.h

@@ -237,7 +237,19 @@ inline uint8_t
 { return (uint8_t *) strndup ((char *) str, size); }
 
 
-/** Replace non-printable characters with their literal byte.
+/** @brief Add escape character (backslash) to illegal literal characters.
+ *
+ * @param[in] in Input string.
+ *
+ * @param out[out] Output string.
+ *
+ * @return LSUP_OK on success; LSUP_MEM_ERR on memory error.
+ */
+LSUP_rc
+escape_lit (const char *in, char **out);
+
+
+/** @brief Replace non-printable characters with their literal byte.
  *
  *  Escape backslash is to be added separately.
  */

+ 2 - 0
include/codec/codec_ttl.h

@@ -4,6 +4,8 @@
 #include "codec/parser_ttl.h"
 
 /** @brief Turtle codec.
+ *
+ * @sa #codec_t
  */
 extern const LSUP_Codec ttl_codec;
 

+ 43 - 0
src/codec.c

@@ -1,5 +1,13 @@
 #include "codec.h"
 
+
+/** @brief List of characters to be escaped in serialized literals.
+ *
+ * @sa https://www.w3.org/TR/n-triples/#grammar-production-ECHAR
+ */
+#define LIT_ECHAR "\t\b\n\r\f\"\'\\"
+
+
 uint8_t *unescape_unicode (const uint8_t *esc_str, size_t size)
 {
     // Output will not be longer than the escaped sequence.
@@ -69,6 +77,41 @@ uint8_t *unescape_unicode (const uint8_t *esc_str, size_t size)
 }
 
 
+LSUP_rc
+escape_lit (const char *in, char **out_p)
+{
+    size_t out_size = strlen (in) + 1;
+
+    // Expand output string size to accommodate escape characters.
+    for (
+            size_t i = strcspn (in, LIT_ECHAR);
+            i < strlen (in);
+            i += strcspn (in + i + 1, LIT_ECHAR) + 1) {
+        out_size ++;
+    }
+
+    char *out = calloc (1, out_size);
+    if (UNLIKELY (!out)) return LSUP_MEM_ERR;
+
+    size_t boundary;
+    boundary = strcspn (in, LIT_ECHAR);
+    for (size_t i = 0, j = 0;;) {
+        out = strncat (out, in + i, boundary);
+
+        i += boundary;
+        j += boundary;
+        if (i >= strlen (in)) break;
+
+        out[j++] = '\\';
+        out[j++] = escape_char (in[i++]);
+        boundary = strcspn (in + i, LIT_ECHAR);
+    }
+
+    *out_p = out;
+    return LSUP_OK;
+}
+
+
 /*
  * Extern inline functions.
  */

+ 0 - 54
src/codec/codec_nt.c

@@ -1,22 +1,5 @@
 #include "codec/codec_nt.h"
 
-/** @brief List of characters to be escaped in serialized literals.
- *
- * @sa https://www.w3.org/TR/n-triples/#grammar-production-ECHAR
- */
-#define LIT_ECHAR "\t\b\n\r\f\"\'\\"
-
-/** @brief Regex of characters to be escaped in serialized IRIs.
- *
- * @sa https://www.w3.org/TR/n-triples/#grammar-production-IRIREF
- */
-#define IRI_ECHAR_PTN "[\x00-\x20<>\"\\{\\}\\|\\^`\\\\]"
-
-
-/* * * Static prototypes. * * */
-
-static LSUP_rc escape_lit (const char *in, char **out_p);
-
 
 /* * * Codec functions. * * */
 
@@ -202,40 +185,3 @@ gr_to_nt_init (const LSUP_Graph *gr)
 
     return it;
 }
-
-
-/** @brief Add escape character (backslash) to illegal literal characters.
- */
-static LSUP_rc
-escape_lit (const char *in, char **out_p)
-{
-    size_t out_size = strlen (in) + 1;
-
-    // Expand output string size to accommodate escape characters.
-    for (
-            size_t i = strcspn (in, LIT_ECHAR);
-            i < strlen (in);
-            i += strcspn (in + i + 1, LIT_ECHAR) + 1) {
-        out_size ++;
-    }
-
-    char *out = calloc (1, out_size);
-    if (UNLIKELY (!out)) return LSUP_MEM_ERR;
-
-    size_t boundary;
-    boundary = strcspn (in, LIT_ECHAR);
-    for (size_t i = 0, j = 0;;) {
-        out = strncat (out, in + i, boundary);
-
-        i += boundary;
-        j += boundary;
-        if (i >= strlen (in)) break;
-
-        out[j++] = '\\';
-        out[j++] = escape_char (in[i++]);
-        boundary = strcspn (in + i, LIT_ECHAR);
-    }
-
-    *out_p = out;
-    return 0;
-}

+ 0 - 54
src/codec/codec_ttl.c

@@ -1,22 +1,5 @@
 #include "codec/codec_ttl.h"
 
-/** @brief List of characters to be escaped in serialized literals.
- *
- * @sa https://www.w3.org/TR/n-triples/#grammar-production-ECHAR
- */
-#define LIT_ECHAR "\t\b\n\r\f\"\'\\"
-
-/** @brief Regex of characters to be escaped in serialized IRIs.
- *
- * @sa https://www.w3.org/TR/n-triples/#grammar-production-IRIREF
- */
-#define IRI_ECHAR_PTN "[\x00-\x20<>\"\\{\\}\\|\\^`\\\\]"
-
-
-/* * * Static prototypes. * * */
-
-static LSUP_rc escape_lit (const char *in, char **out_p);
-
 
 /* * * Codec functions. * * */
 
@@ -201,40 +184,3 @@ gr_to_ttl_init (const LSUP_Graph *gr)
 
     return it;
 }
-
-
-/** @brief Add escape character (backslash) to illegal literal characters.
- */
-static LSUP_rc
-escape_lit (const char *in, char **out_p)
-{
-    size_t out_size = strlen (in) + 1;
-
-    // Expand output string size to accommodate escape characters.
-    for (
-            size_t i = strcspn (in, LIT_ECHAR);
-            i < strlen (in);
-            i += strcspn (in + i + 1, LIT_ECHAR) + 1) {
-        out_size ++;
-    }
-
-    char *out = calloc (1, out_size);
-    if (UNLIKELY (!out)) return LSUP_MEM_ERR;
-
-    size_t boundary;
-    boundary = strcspn (in, LIT_ECHAR);
-    for (size_t i = 0, j = 0;;) {
-        out = strncat (out, in + i, boundary);
-
-        i += boundary;
-        j += boundary;
-        if (i >= strlen (in)) break;
-
-        out[j++] = '\\';
-        out[j++] = escape_char (in[i++]);
-        boundary = strcspn (in + i, LIT_ECHAR);
-    }
-
-    *out_p = out;
-    return 0;
-}