[svn:parrot] r47518 - branches/gsoc_nfg/src/string

darbelo at svn.parrot.org darbelo at svn.parrot.org
Wed Jun 9 22:13:01 UTC 2010


Author: darbelo
Date: Wed Jun  9 22:13:00 2010
New Revision: 47518
URL: https://trac.parrot.org/parrot/changeset/47518

Log:
Cleanup and headerize

Modified:
   branches/gsoc_nfg/src/string/api.c
   branches/gsoc_nfg/src/string/grapheme.c
   branches/gsoc_nfg/src/string/grapheme.h

Modified: branches/gsoc_nfg/src/string/api.c
==============================================================================
--- branches/gsoc_nfg/src/string/api.c	Wed Jun  9 21:54:57 2010	(r47517)
+++ branches/gsoc_nfg/src/string/api.c	Wed Jun  9 22:13:00 2010	(r47518)
@@ -497,6 +497,13 @@
     dest->bufused = a->bufused + b->bufused;
     dest->strlen  = a->strlen + b_len;
 
+#if PARROT_HAS_ICU
+    if (enc == Parrot_nfg_encoding_ptr) {
+        dest->extra = a->extra;
+        merge_tables_and_fixup_string(interp, dest, b->extra, a->strlen);
+    }
+#endif /* PARROT_HAS_ICU */
+
     return dest;
 }
 

Modified: branches/gsoc_nfg/src/string/grapheme.c
==============================================================================
--- branches/gsoc_nfg/src/string/grapheme.c	Wed Jun  9 21:54:57 2010	(r47517)
+++ branches/gsoc_nfg/src/string/grapheme.c	Wed Jun  9 22:13:00 2010	(r47518)
@@ -19,8 +19,9 @@
 {
     ASSERT_ARGS(create_grapheme_table)
     UINTVAL entries = (n > MIN_TABLE_LENGTH) ? n - MIN_TABLE_LENGTH : 0;
-    grapheme_table *table = mem_sys_allocate(sizeof (grapheme_table)
-                                              + entries * sizeof (grapheme));
+    grapheme_table *table = (grapheme_table *) mem_sys_allocate(sizeof (grapheme_table)
+        + entries * sizeof (grapheme));
+
     table->size = entries + MIN_TABLE_LENGTH;
     table->used = 0;
 
@@ -47,6 +48,14 @@
     return dst;
 }
 
+grapheme_table *
+grow_grapheme_table(SHIM_INTERP, grapheme_table *src, UINTVAL n)
+{
+    return (grapheme_table *) mem_sys_realloc(src,
+        sizeof (grapheme_table) + (src->size + n) * sizeof (grapheme));
+}
+
+
 void
 destroy_grapheme_table(PARROT_INTERP, grapheme_table *table)
 {
@@ -57,8 +66,63 @@
     }
     mem_gc_free(interp, table);
 }
+void
+merge_tables_and_fixup_string(PARROT_INTERP, STRING *dest, grapheme_table *table, UINTVAL offset)
+{
+    INTVAL i;
+    UChar32 *buf = (UChar32 *) dest->strstart;
+    UChar32 *new_codepoints;
+
+	if (table == NULL || table->used == 0)
+        return;
+
+	if (dest->extra == NULL) {
+        dest->extra = clone_grapheme_table(interp, table);
+        return;
+    }
+
+	new_codepoints = mem_gc_allocate_n_typed(interp, table->used, UChar32);
+
+    /* Add the new graphemes to the old table. */
+    for (i = 0; i < table->used; i++) {
+        new_codepoints[i] = add_grapheme(interp,
+            (grapheme_table *) dest->extra, &(table->graphemes[i]));
+    }
+
+    /* And fixup the string. */
+    for (i = offset; i < dest->strlen; i++) {
+		int32_t codepoint = buf[i];
+        if (codepoint < 0)
+            buf[i] = new_codepoints[(-1 - codepoint)];
+    }
+
+    mem_gc_free(interp, new_codepoints);
+}
+
+UChar32
+add_grapheme(PARROT_INTERP, grapheme_table *table, grapheme *src)
+{
+    ASSERT_ARGS(add_grapheme_from_substr)
+    int32_t i;
+
+    /* Check if it's in the table already... */
+    for (i = 0; i < table->used; i++) {
+        if (table->graphemes[i].hash == src->hash)
+            return (UChar32) (-1 - i);
+    }
+
+    /* ... and add it if it isn't */
+    table->graphemes[i].len = src->len;
+    table->graphemes[i].hash = src->hash;
+    table->graphemes[i].codepoints =
+        mem_gc_allocate_n_typed(interp, src->len, UChar32);
+    memcpy(table->graphemes[i].codepoints, src->codepoints,
+               src->len * sizeof (UChar32));
+
+    return (UChar32) (-1 - i);
+}
 
-UChar32 *
+UChar32
 add_grapheme_from_substr(PARROT_INTERP, grapheme_table *table, STRING *src,
                          UINTVAL start, UINTVAL len, UINTVAL hash)
 {
@@ -67,17 +131,20 @@
     /* Check if it's in the table already... */
     for (i = 0; i < table->used; i++) {
         if (table->graphemes[i].hash == hash)
-            return (UChar32) (-1 * (i + 1));
+            return (UChar32) (-1 - i);
     }
 
     /* ... and add it if it isn't */
     table->graphemes[table->used].len = len;
     table->graphemes[table->used].hash = hash;
-    table->graphemes[table->used].codepoints  = mem_gc_allocate_n_typed(interp, len, UChar32);
+    table->graphemes[table->used].codepoints =
+        mem_gc_allocate_n_typed(interp, len, UChar32);
     for (i = 0; i < len; i++){
         table->graphemes[table->used].codepoints[i] =
             src->encoding->get_codepoint(interp, src, start + i);
     };
+    i = table->used;
+    return (UChar32) (-1 - i);
 }
 
 #endif /* PARROT_HAS_ICU */

Modified: branches/gsoc_nfg/src/string/grapheme.h
==============================================================================
--- branches/gsoc_nfg/src/string/grapheme.h	Wed Jun  9 21:54:57 2010	(r47517)
+++ branches/gsoc_nfg/src/string/grapheme.h	Wed Jun  9 22:13:00 2010	(r47518)
@@ -39,7 +39,10 @@
 /* HEADERIZER BEGIN: src/string/grapheme.c */
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
 
-UChar32 * add_grapheme_from_substr(PARROT_INTERP,
+UChar32 add_grapheme(PARROT_INTERP, grapheme_table *table, grapheme *src)
+        __attribute__nonnull__(1);
+
+UChar32 add_grapheme_from_substr(PARROT_INTERP,
     grapheme_table *table,
     STRING *src,
     UINTVAL start,
@@ -56,6 +59,18 @@
 void destroy_grapheme_table(PARROT_INTERP, grapheme_table *table)
         __attribute__nonnull__(1);
 
+grapheme_table * grow_grapheme_table(SHIM_INTERP,
+    grapheme_table *src,
+    UINTVAL n);
+
+void merge_tables_and_fixup_string(PARROT_INTERP,
+    STRING *dest,
+    grapheme_table *table,
+    UINTVAL offset)
+        __attribute__nonnull__(1);
+
+#define ASSERT_ARGS_add_grapheme __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp))
 #define ASSERT_ARGS_add_grapheme_from_substr __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp))
 #define ASSERT_ARGS_clone_grapheme_table __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
@@ -64,6 +79,9 @@
        PARROT_ASSERT_ARG(interp))
 #define ASSERT_ARGS_destroy_grapheme_table __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp))
+#define ASSERT_ARGS_grow_grapheme_table __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
+#define ASSERT_ARGS_merge_tables_and_fixup_string __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp))
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
 /* HEADERIZER END: src/string/grapheme.c */
 


More information about the parrot-commits mailing list