aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2013-03-24 05:53:56 +0000
committerDavid Robillard <d@drobilla.net>2013-03-24 05:53:56 +0000
commit0c9f5eb2eeb4927e51f43ac2cba85cebca873234 (patch)
treea07bb718fc7ac5dad87664cc8c09d19ad6a91c04
parent217bc5d3bb4ba9be558b120d594239aea46acfa5 (diff)
downloadserd-0c9f5eb2eeb4927e51f43ac2cba85cebca873234.tar.gz
serd-0c9f5eb2eeb4927e51f43ac2cba85cebca873234.tar.bz2
serd-0c9f5eb2eeb4927e51f43ac2cba85cebca873234.zip
Set SERD_HAS_NEWLINE and SERD_HAS_QUOTE flags when unescaped newlines or quotes are read.
Don't escape legal ASCII characters when writing long literals. git-svn-id: http://svn.drobilla.net/serd/trunk@439 490d8e77-9747-427b-9fa3-0b8f29cee8a0
-rw-r--r--src/reader.c16
-rw-r--r--src/writer.c18
-rw-r--r--tests/good/test-backspace.nt1
-rw-r--r--tests/good/test-backspace.ttl5
4 files changed, 31 insertions, 9 deletions
diff --git a/src/reader.c b/src/reader.c
index fe2e309b..a2c900c7 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -436,9 +436,17 @@ read_utf8_character(SerdReader* reader, Ref dest, uint8_t c)
// Read one character (possibly multi-byte)
// The first byte, c, has already been eaten by caller
static inline SerdStatus
-read_character(SerdReader* reader, Ref dest, uint8_t c)
+read_character(SerdReader* reader, Ref dest, SerdNodeFlags* flags, uint8_t c)
{
if (!(c & 0x80)) {
+ switch (c) {
+ case 0xA: case 0xD:
+ *flags |= SERD_HAS_NEWLINE;
+ break;
+ case '"': case '\'':
+ *flags |= SERD_HAS_QUOTE;
+ break;
+ }
push_byte(reader, dest, c);
return SERD_SUCCESS;
} else {
@@ -533,10 +541,10 @@ read_STRING_LITERAL_LONG(SerdReader* reader, SerdNodeFlags* flags, uint8_t q)
} else {
*flags |= SERD_HAS_QUOTE;
push_byte(reader, ref, c);
- read_character(reader, ref, q2);
+ read_character(reader, ref, flags, q2);
}
} else {
- read_character(reader, ref, eat_byte_safe(reader, c));
+ read_character(reader, ref, flags, eat_byte_safe(reader, c));
}
}
}
@@ -568,7 +576,7 @@ read_STRING_LITERAL(SerdReader* reader, SerdNodeFlags* flags, uint8_t q)
eat_byte_check(reader, q);
return ref;
} else {
- read_character(reader, ref, eat_byte_safe(reader, c));
+ read_character(reader, ref, flags, eat_byte_safe(reader, c));
}
}
}
diff --git a/src/writer.c b/src/writer.c
index 78a8347f..83ad6e20 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -284,10 +284,20 @@ write_text(SerdWriter* writer, TextContext ctx,
uint8_t in = utf8[i++];
if (ctx == WRITE_LONG_STRING) {
- if (in == '\\') {
- len += sink("\\\\", 2, writer); continue;
- } else if (in == '\"' && i == n_bytes) {
- len += sink("\\\"", 2, writer); continue; // '"' at string end
+ switch (in) {
+ case '\\': len += sink("\\\\", 2, writer); continue;
+ case '\b': len += sink("\\b", 2, writer); continue;
+ case '\n': case '\r': case '\t': case '\f':
+ len += sink(&in, 1, writer); // Write character as-is
+ continue;
+ case '\"':
+ if (i == n_bytes) { // '"' at string end
+ len += sink("\\\"", 2, writer);
+ } else {
+ len += sink(&in, 1, writer);
+ }
+ continue;
+ default: break;
}
} else if (ctx == WRITE_STRING) {
switch (in) {
diff --git a/tests/good/test-backspace.nt b/tests/good/test-backspace.nt
index 3a88c436..3a110da6 100644
--- a/tests/good/test-backspace.nt
+++ b/tests/good/test-backspace.nt
@@ -1,2 +1,3 @@
<http://example.org/thing> <http://example.org/label> "\b" .
<http://example.org/thing> <http://example.org/label> "\b" .
+<http://example.org/thing> <http://example.org/label> "\n\b\n" .
diff --git a/tests/good/test-backspace.ttl b/tests/good/test-backspace.ttl
index 07375d76..1e33ba95 100644
--- a/tests/good/test-backspace.ttl
+++ b/tests/good/test-backspace.ttl
@@ -1,2 +1,5 @@
<http://example.org/thing> <http://example.org/label> "\u0008" .
-<http://example.org/thing> <http://example.org/label> "" . \ No newline at end of file
+<http://example.org/thing> <http://example.org/label> "" .
+<http://example.org/thing> <http://example.org/label> """
+
+""" . \ No newline at end of file