[svn:parrot] r49413 - in trunk: . config/gen/makefiles include/parrot src/string src/string/encoding
nwellnhof at svn.parrot.org
nwellnhof at svn.parrot.org
Sat Oct 2 22:22:43 UTC 2010
Author: nwellnhof
Date: Sat Oct 2 22:22:42 2010
New Revision: 49413
URL: https://trac.parrot.org/parrot/changeset/49413
Log:
[str] Add own encoding for null string
Added:
trunk/src/string/encoding/null.c
Modified:
trunk/MANIFEST
trunk/config/gen/makefiles/root.in
trunk/include/parrot/encoding.h
trunk/src/string/api.c
trunk/src/string/encoding/shared.h
Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST Sat Oct 2 17:20:12 2010 (r49412)
+++ trunk/MANIFEST Sat Oct 2 22:22:42 2010 (r49413)
@@ -1457,6 +1457,7 @@
src/string/encoding/ascii.c []
src/string/encoding/binary.c []
src/string/encoding/latin1.c []
+src/string/encoding/null.c []
src/string/encoding/shared.c []
src/string/encoding/shared.h []
src/string/encoding/tables.c []
Modified: trunk/config/gen/makefiles/root.in
==============================================================================
--- trunk/config/gen/makefiles/root.in Sat Oct 2 17:20:12 2010 (r49412)
+++ trunk/config/gen/makefiles/root.in Sat Oct 2 22:22:42 2010 (r49413)
@@ -423,6 +423,7 @@
ENCODING_O_FILES = \
src/string/encoding/shared$(O) \
src/string/encoding/tables$(O) \
+ src/string/encoding/null$(O) \
src/string/encoding/ascii$(O) \
src/string/encoding/latin1$(O) \
src/string/encoding/binary$(O) \
@@ -1593,6 +1594,7 @@
src/string/encoding/shared$(O) : $(PARROT_H_HEADERS) \
src/string/encoding/shared.h \
src/string/encoding/tables.h
+src/string/encoding/null$(O) : $(PARROT_H_HEADERS)
src/string/encoding/ascii$(O) : $(PARROT_H_HEADERS) \
src/string/encoding/shared.h \
src/string/encoding/tables.h
Modified: trunk/include/parrot/encoding.h
==============================================================================
--- trunk/include/parrot/encoding.h Sat Oct 2 17:20:12 2010 (r49412)
+++ trunk/include/parrot/encoding.h Sat Oct 2 22:22:42 2010 (r49413)
@@ -25,6 +25,10 @@
PARROT_DATA STR_VTABLE *Parrot_default_encoding_ptr;
+#ifdef PARROT_IN_CORE
+STR_VTABLE *Parrot_null_encoding_ptr;
+#endif
+
/* HEADERIZER BEGIN: src/string/encoding.c */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
Modified: trunk/src/string/api.c
==============================================================================
--- trunk/src/string/api.c Sat Oct 2 17:20:12 2010 (r49412)
+++ trunk/src/string/api.c Sat Oct 2 22:22:42 2010 (r49413)
@@ -145,7 +145,7 @@
#if PARROT_CATCH_NULL
/* initialize STRINGNULL, but not in the constant table */
STRINGNULL = Parrot_str_new_init(interp, NULL, 0,
- Parrot_default_encoding_ptr,
+ Parrot_null_encoding_ptr,
PObj_constant_FLAG);
#endif
@@ -329,9 +329,14 @@
Parrot_str_clone(PARROT_INTERP, ARGIN(const STRING *s))
{
ASSERT_ARGS(Parrot_str_clone)
+ size_t alloc_size;
+ STRING *result;
+
+ if (STRING_IS_NULL(s))
+ return STRINGNULL;
- const size_t alloc_size = s->bufused;
- STRING * const result = Parrot_gc_new_string_header(interp, 0);
+ result = Parrot_gc_new_string_header(interp, 0);
+ alloc_size = s->bufused;
if (alloc_size) {
/* Allocate new chunk of memory */
@@ -368,7 +373,10 @@
{
ASSERT_ARGS(Parrot_str_copy)
STRING *d;
- const int is_movable = PObj_is_movable_TESTALL(s);
+ int is_movable;
+
+ if (STRING_IS_NULL(s))
+ return STRINGNULL;
d = Parrot_gc_new_string_header(interp,
PObj_get_FLAGS(s) & ~PObj_constant_FLAG);
@@ -382,6 +390,8 @@
/* Set the string copy flag */
PObj_is_string_copy_SET(d);
+ is_movable = PObj_is_movable_TESTALL(s);
+
/* Now check that buffer allocated from pool and affected by compacting */
if (is_movable && Buffer_bufstart(s)) {
/* If so, mark it as shared */
@@ -1195,17 +1205,22 @@
if (true_length > (src->strlen - true_offset))
true_length = (UINTVAL)(src->strlen - true_offset);
- /* may have different reps..... */
- enc = string_rep_compatible(interp, src, rep);
-
- if (!enc) {
- if (src->encoding != Parrot_utf8_encoding_ptr)
- src = Parrot_utf8_encoding_ptr->to_encoding(interp, src);
- if (rep->encoding != Parrot_utf8_encoding_ptr)
- rep = Parrot_utf8_encoding_ptr->to_encoding(interp, rep);
- /* Remember selected encoding */
+ if (STRING_IS_NULL(rep)) {
enc = src->encoding;
}
+ else {
+ /* may have different reps..... */
+ enc = string_rep_compatible(interp, src, rep);
+
+ if (!enc) {
+ if (src->encoding != Parrot_utf8_encoding_ptr)
+ src = Parrot_utf8_encoding_ptr->to_encoding(interp, src);
+ if (rep->encoding != Parrot_utf8_encoding_ptr)
+ rep = Parrot_utf8_encoding_ptr->to_encoding(interp, rep);
+ /* Remember selected encoding */
+ enc = src->encoding;
+ }
+ }
/* get byte position of the part that will be replaced */
STRING_ITER_INIT(interp, &iter);
Added: trunk/src/string/encoding/null.c
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/src/string/encoding/null.c Sat Oct 2 22:22:42 2010 (r49413)
@@ -0,0 +1,254 @@
+/*
+Copyright (C) 2010, Parrot Foundation.
+$Id$
+
+=head1 NAME
+
+src/string/encoding/null.c
+
+=head1 DESCRIPTION
+
+This file implements encoding functions for the null string.
+
+=over 4
+
+=cut
+
+*/
+
+#include "parrot/parrot.h"
+
+/* HEADERIZER HFILE: none */
+
+/* HEADERIZER BEGIN: static */
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+
+PARROT_WARN_UNUSED_RESULT
+static INTVAL null_compare(PARROT_INTERP,
+ ARGIN(const STRING *lhs),
+ ARGIN(const STRING *rhs))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2)
+ __attribute__nonnull__(3);
+
+PARROT_WARN_UNUSED_RESULT
+static INTVAL null_equal(PARROT_INTERP,
+ ARGIN(const STRING *lhs),
+ ARGIN(const STRING *rhs))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2)
+ __attribute__nonnull__(3);
+
+static void null_error(PARROT_INTERP)
+ __attribute__nonnull__(1);
+
+PARROT_WARN_UNUSED_RESULT
+static size_t null_hash(SHIM_INTERP, ARGIN(const STRING *s), size_t hashval)
+ __attribute__nonnull__(2);
+
+PARROT_WARN_UNUSED_RESULT
+static UINTVAL null_scan(PARROT_INTERP, ARGIN(const STRING *src))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2);
+
+PARROT_WARN_UNUSED_RESULT
+static UINTVAL null_validate(PARROT_INTERP, ARGIN(const STRING *src))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2);
+
+#define ASSERT_ARGS_null_compare __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp) \
+ , PARROT_ASSERT_ARG(lhs) \
+ , PARROT_ASSERT_ARG(rhs))
+#define ASSERT_ARGS_null_equal __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp) \
+ , PARROT_ASSERT_ARG(lhs) \
+ , PARROT_ASSERT_ARG(rhs))
+#define ASSERT_ARGS_null_error __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp))
+#define ASSERT_ARGS_null_hash __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(s))
+#define ASSERT_ARGS_null_scan __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp) \
+ , PARROT_ASSERT_ARG(src))
+#define ASSERT_ARGS_null_validate __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(interp) \
+ , PARROT_ASSERT_ARG(src))
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
+/* HEADERIZER END: static */
+
+
+/*
+
+=item C<static void null_error(PARROT_INTERP)>
+
+Generic encoding function for null strings that throws an exception.
+
+=cut
+
+*/
+
+static void
+null_error(PARROT_INTERP)
+{
+ ASSERT_ARGS(null_error)
+
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNEXPECTED_NULL,
+ "Invalid operation on null string");
+}
+
+
+/*
+
+=item C<static INTVAL null_equal(PARROT_INTERP, const STRING *lhs, const STRING
+*rhs)>
+
+Compares two STRINGs, C<lhs> and C<rhs>. If STRING C<lhs> == C<rhs>,
+returns 1. If C<lhs> != C<rhs> returns 0.
+
+=cut
+
+*/
+
+PARROT_WARN_UNUSED_RESULT
+static INTVAL
+null_equal(PARROT_INTERP, ARGIN(const STRING *lhs), ARGIN(const STRING *rhs))
+{
+ ASSERT_ARGS(null_equal)
+
+ return STRING_length(rhs) == 0;
+}
+
+
+/*
+
+=item C<static INTVAL null_compare(PARROT_INTERP, const STRING *lhs, const
+STRING *rhs)>
+
+Compares two STRINGs, C<lhs> and C<rhs>. Returns -1 if C<lhs> < C<rhs>. Returns
+0 if C<lhs> = C<rhs>. Returns 1 if C<lhs> > C<rhs>.
+
+=cut
+
+*/
+
+PARROT_WARN_UNUSED_RESULT
+static INTVAL
+null_compare(PARROT_INTERP, ARGIN(const STRING *lhs), ARGIN(const STRING *rhs))
+{
+ ASSERT_ARGS(null_compare)
+
+ return STRING_length(rhs) ? -1 : 0;
+}
+
+
+/*
+
+=item C<static size_t null_hash(PARROT_INTERP, const STRING *s, size_t hashval)>
+
+Returns the hashed value of the string, given a seed in hashval.
+
+=cut
+
+*/
+
+PARROT_WARN_UNUSED_RESULT
+static size_t
+null_hash(SHIM_INTERP, ARGIN(const STRING *s), size_t hashval)
+{
+ ASSERT_ARGS(null_hash)
+
+ return hashval;
+}
+
+
+/*
+
+=item C<static UINTVAL null_validate(PARROT_INTERP, const STRING *src)>
+
+Verifies the null string. Always returns 1.
+
+=cut
+
+*/
+
+PARROT_WARN_UNUSED_RESULT
+static UINTVAL
+null_validate(PARROT_INTERP, ARGIN(const STRING *src))
+{
+ ASSERT_ARGS(null_validate)
+ return 1;
+}
+
+
+/*
+
+=item C<static UINTVAL null_scan(PARROT_INTERP, const STRING *src)>
+
+Scans the null string. Always returns 0.
+
+=cut
+
+*/
+
+PARROT_WARN_UNUSED_RESULT
+static UINTVAL
+null_scan(PARROT_INTERP, ARGIN(const STRING *src))
+{
+ ASSERT_ARGS(null_scan)
+ return 0;
+}
+
+
+static STR_VTABLE Parrot_null_encoding = {
+ 0,
+ "null",
+ NULL,
+ 1, /* Max bytes per codepoint */
+
+ (str_vtable_to_encoding_t)null_error,
+ (str_vtable_chr_t)null_error,
+
+ null_equal,
+ null_compare,
+ (str_vtable_index_t)null_error,
+ (str_vtable_rindex_t)null_error,
+ null_hash,
+ null_validate,
+
+ null_scan,
+ (str_vtable_ord_t)null_error,
+ (str_vtable_substr_t)null_error,
+
+ (str_vtable_is_cclass_t)null_error,
+ (str_vtable_find_cclass_t)null_error,
+ (str_vtable_find_not_cclass_t)null_error,
+
+ (str_vtable_get_graphemes_t)null_error,
+ (str_vtable_compose_t)null_error,
+ (str_vtable_decompose_t)null_error,
+
+ (str_vtable_upcase_t)null_error,
+ (str_vtable_downcase_t)null_error,
+ (str_vtable_titlecase_t)null_error,
+ (str_vtable_upcase_first_t)null_error,
+ (str_vtable_downcase_first_t)null_error,
+ (str_vtable_titlecase_first_t)null_error,
+
+ (str_vtable_iter_get_t)null_error,
+ (str_vtable_iter_skip_t)null_error,
+ (str_vtable_iter_get_and_advance_t)null_error,
+ (str_vtable_iter_set_and_advance_t)null_error,
+ (str_vtable_iter_set_position_t)null_error
+};
+
+STR_VTABLE *Parrot_null_encoding_ptr = &Parrot_null_encoding;
+
+
+/*
+ * Local variables:
+ * c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */
+
Modified: trunk/src/string/encoding/shared.h
==============================================================================
--- trunk/src/string/encoding/shared.h Sat Oct 2 17:20:12 2010 (r49412)
+++ trunk/src/string/encoding/shared.h Sat Oct 2 22:22:42 2010 (r49413)
@@ -1,9 +1,9 @@
-/* fixed_8.h
+/* shared.h
* Copyright (C) 2004-2007, Parrot Foundation.
* SVN Info
* $Id$
* Overview:
- * This is the header for the 8-bit fixed-width encoding
+ * This is the header for general encoding functions
* Data Structure and Algorithms:
* History:
* Notes:
More information about the parrot-commits
mailing list