Implement BB_Utf8_To_String
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/ae8edad3 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/ae8edad3 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/ae8edad3 Branch: refs/heads/master Commit: ae8edad36e156b90ea0750a170c8a8ec15667828 Parents: d30a279 Author: Nick Wellnhofer <[email protected]> Authored: Tue Nov 10 13:04:48 2015 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Thu Nov 12 14:08:17 2015 +0100 ---------------------------------------------------------------------- runtime/core/Clownfish/ByteBuf.c | 10 ++++++++++ runtime/core/Clownfish/ByteBuf.cfh | 12 ++++++++++++ runtime/core/Clownfish/Test/TestByteBuf.c | 23 ++++++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ae8edad3/runtime/core/Clownfish/ByteBuf.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/ByteBuf.c b/runtime/core/Clownfish/ByteBuf.c index 16a8292..11a0acc 100644 --- a/runtime/core/Clownfish/ByteBuf.c +++ b/runtime/core/Clownfish/ByteBuf.c @@ -207,6 +207,16 @@ BB_Yield_Blob_IMP(ByteBuf *self) { return blob; } +String* +BB_Utf8_To_String_IMP(ByteBuf *self) { + return Str_new_from_utf8(self->buf, self->size); +} + +String* +BB_Trusted_Utf8_To_String_IMP(ByteBuf *self) { + return Str_new_from_trusted_utf8(self->buf, self->size); +} + int BB_compare(const void *va, const void *vb) { ByteBuf *a = *(ByteBuf**)va; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ae8edad3/runtime/core/Clownfish/ByteBuf.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/ByteBuf.cfh b/runtime/core/Clownfish/ByteBuf.cfh index 95e503f..230bb86 100644 --- a/runtime/core/Clownfish/ByteBuf.cfh +++ b/runtime/core/Clownfish/ByteBuf.cfh @@ -117,6 +117,18 @@ final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj { incremented Blob* Yield_Blob(ByteBuf *self); + /** Return a String which holds a copy of the UTF-8 character data in + * the ByteBuf after checking for validity. + */ + public incremented String* + Utf8_To_String(ByteBuf *self); + + /** Return a String which holds a copy of the UTF-8 character data in + * the ByteBuf, skipping validity checks. + */ + public incremented String* + Trusted_Utf8_To_String(ByteBuf *self); + /** Test whether the ByteBuf matches the passed-in bytes. */ bool http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ae8edad3/runtime/core/Clownfish/Test/TestByteBuf.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestByteBuf.c b/runtime/core/Clownfish/Test/TestByteBuf.c index d7eb045..57238bc 100644 --- a/runtime/core/Clownfish/Test/TestByteBuf.c +++ b/runtime/core/Clownfish/Test/TestByteBuf.c @@ -146,14 +146,35 @@ test_Cat(TestBatchRunner *runner) { DECREF(wanted); } +static void +test_Utf8_To_String(TestBatchRunner *runner) { + ByteBuf *bb = BB_new_bytes("foo", 3); + + { + String *string = BB_Utf8_To_String(bb); + TEST_TRUE(runner, Str_Equals_Utf8(string, "foo", 3), "Utf8_To_String"); + DECREF(string); + } + + { + String *string = BB_Trusted_Utf8_To_String(bb); + TEST_TRUE(runner, Str_Equals_Utf8(string, "foo", 3), + "Trusted_Utf8_To_String"); + DECREF(string); + } + + DECREF(bb); +} + void TestBB_Run_IMP(TestByteBuf *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 20); + TestBatchRunner_Plan(runner, (TestBatch*)self, 22); test_Equals(runner); test_Grow(runner); test_Clone(runner); test_compare(runner); test_Mimic(runner); + test_Utf8_To_String(runner); test_Cat(runner); }
