[svn:parrot] r47509 - in trunk: src/pmc t/pmc
NotFound at svn.parrot.org
NotFound at svn.parrot.org
Wed Jun 9 13:19:31 UTC 2010
Author: NotFound
Date: Wed Jun 9 13:19:30 2010
New Revision: 47509
URL: https://trac.parrot.org/parrot/changeset/47509
Log:
ByteBuffer methods to get the content as string with the charset and encoding specified
Modified:
trunk/src/pmc/bytebuffer.pmc
trunk/t/pmc/bytebuffer.t
Modified: trunk/src/pmc/bytebuffer.pmc
==============================================================================
--- trunk/src/pmc/bytebuffer.pmc Wed Jun 9 11:30:18 2010 (r47508)
+++ trunk/src/pmc/bytebuffer.pmc Wed Jun 9 13:19:30 2010 (r47509)
@@ -19,6 +19,21 @@
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+PARROT_CANNOT_RETURN_NULL
+static STRING * build_string(PARROT_INTERP,
+ ARGIN(const unsigned char *content),
+ INTVAL size,
+ ARGIN_NULLOK(const CHARSET *charset),
+ ARGIN_NULLOK(const ENCODING *encoding))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2);
+
+#define ASSERT_ARGS_build_string __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp) \
+ , PARROT_ASSERT_ARG(content))
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+/* HEADERIZER END: static */
+
pmclass ByteBuffer auto_attrs {
ATTR INTVAL allocated_size;
ATTR INTVAL size;
@@ -192,12 +207,106 @@
content[position] = value;
}
+/*
+
+=back
+
+=head2 Methods
+
+=over 4
+
+=item C<get_string(string charset, string encoding)>
+
+Create a string with the buffer content and the charset and encoding
+specified.
+
+=cut
+
+*/
+
+ METHOD get_string(STRING *charsetname, STRING *encodingname) {
+ STRING *result;
+ unsigned char *content;
+ INTVAL size;
+ const CHARSET *charset = Parrot_get_charset(INTERP,
+ Parrot_charset_number(INTERP, charsetname));
+ const ENCODING *encoding = Parrot_get_encoding(INTERP,
+ Parrot_encoding_number(INTERP, encodingname));
+ GET_ATTR_content(INTERP, SELF, content);
+ GET_ATTR_size(INTERP, SELF, size);
+ result = build_string(INTERP, content, size, charset, encoding);
+ RETURN(STRING *result);
+ }
+
+/*
+
+=item C<get_string_as(string as)>
+
+Create a string with the buffer content and the same charset and encoding
+as the string argument.
+
+=cut
+
+*/
+
+ METHOD get_string_as(STRING *as :optional) {
+ STRING *result;
+ unsigned char *content;
+ INTVAL size;
+ const CHARSET* charset = STRING_IS_NULL(as) ? PARROT_DEFAULT_CHARSET : as->charset;
+ const ENCODING *encoding = STRING_IS_NULL(as) ? PARROT_DEFAULT_ENCODING : as->encoding;
+ GET_ATTR_content(INTERP, SELF, content);
+ GET_ATTR_size(INTERP, SELF, size);
+ result = build_string(INTERP, content, size, charset, encoding);
+ RETURN(STRING *result);
+ }
+
} /* pmclass end */
/*
=back
+=head2 Auxiliar functions
+
+=over 4
+
+=item C<static STRING * build_string(PARROT_INTERP, const unsigned char
+*content, INTVAL size, const CHARSET *charset, const ENCODING *encoding)>
+
+Build a string fro the buffer content with the charset and encoding specified.
+
+=cut
+
+*/
+
+PARROT_CANNOT_RETURN_NULL
+static STRING *
+build_string(PARROT_INTERP, ARGIN(const unsigned char *content),
+ INTVAL size,
+ ARGIN_NULLOK(const CHARSET *charset),
+ ARGIN_NULLOK(const ENCODING *encoding))
+{
+ ASSERT_ARGS(build_string)
+ STRING *result;
+ if (charset == NULL)
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_ENCODING,
+ "Invalid charset");
+ if (encoding == NULL)
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_ENCODING,
+ "Invalid encoding");
+ result = Parrot_str_new_init(interp, (char *)content, size, encoding, charset, 0);
+ if (!CHARSET_VALIDATE(interp, result))
+ Parrot_ex_throw_from_c_args(interp, NULL,
+ EXCEPTION_INVALID_STRING_REPRESENTATION,
+ "Invalid buffer content");
+ return result;
+}
+
+/*
+
+=back
+
=cut
*/
Modified: trunk/t/pmc/bytebuffer.t
==============================================================================
--- trunk/t/pmc/bytebuffer.t Wed Jun 9 11:30:18 2010 (r47508)
+++ trunk/t/pmc/bytebuffer.t Wed Jun 9 13:19:30 2010 (r47509)
@@ -18,11 +18,12 @@
.sub 'main' :main
.include 'test_more.pir'
- plan(12)
+ plan(16)
test_init()
test_set_string()
test_set_byte()
+ test_get_string()
.end
.sub test_init
@@ -85,6 +86,29 @@
is(n, 9, "resized buffer preserve old value")
.end
+.sub test_get_string
+ .local pmc bb
+ bb = new ['ByteBuffer']
+ # Upper case n tilde: codepoint 0xD1, utf8 encoding 0xC3, 0x91
+ bb = utf16:unicode:"\x{D1}"
+ .local string s
+ s = bb.'get_string'('unicode', 'utf16')
+ .local int n
+ n = length s
+ is(n, 1, "getting utf16 from buffer gives correct length")
+ n = ord s
+ is(n, 0xD1, "getting utf16 from buffer gives correct codepoint")
+
+ bb = new ['ByteBuffer']
+ bb[0] = 0xC3
+ bb[1] = 0x91
+ s = bb.'get_string_as'(utf8:unicode:"")
+ n = length s
+ is(n, 1, "getting utf8 from buffer gives correct length")
+ n = ord s
+ is(n, 0xD1, "getting utf8 from buffer gives correct codepoint")
+.end
+
# Local Variables:
# mode: pir
# fill-column: 100
More information about the parrot-commits
mailing list