[svn:parrot] r49420 - in trunk: include/parrot src src/io src/ops src/pmc src/string src/string/encoding t/op

nwellnhof at svn.parrot.org nwellnhof at svn.parrot.org
Sat Oct 2 22:28:04 UTC 2010


Author: nwellnhof
Date: Sat Oct  2 22:28:04 2010
New Revision: 49420
URL: https://trac.parrot.org/parrot/changeset/49420

Log:
[str] Switch to STRING_index macro

Move the whole 'index' logic into the string vtable functions. This also
changes the index opcode to throw when the source string is null. If this
change causes problems, we can add a deprecation notice or revert it.

Modified:
   trunk/include/parrot/string.h
   trunk/src/io/api.c
   trunk/src/ops/core_ops.c
   trunk/src/ops/string.ops
   trunk/src/pmc.c
   trunk/src/pmc/codestring.pmc
   trunk/src/pmc/string.pmc
   trunk/src/pmc/stringbuilder.pmc
   trunk/src/pmc/sub.pmc
   trunk/src/string/api.c
   trunk/src/string/encoding/shared.c
   trunk/src/string/encoding/shared.h
   trunk/t/op/string.t

Modified: trunk/include/parrot/string.h
==============================================================================
--- trunk/include/parrot/string.h	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/include/parrot/string.h	Sat Oct  2 22:28:04 2010	(r49420)
@@ -90,8 +90,8 @@
 
 typedef INTVAL   (*str_vtable_equal_t)(PARROT_INTERP, ARGIN(const STRING *lhs), ARGIN(const STRING *rhs));
 typedef INTVAL   (*str_vtable_compare_t)(PARROT_INTERP, ARGIN(const STRING *lhs), ARGIN(const STRING *rhs));
-typedef INTVAL   (*str_vtable_index_t)(PARROT_INTERP, ARGIN(const STRING *src), ARGIN(const STRING *search_string), UINTVAL offset);
-typedef INTVAL   (*str_vtable_rindex_t)(PARROT_INTERP, ARGIN(const STRING *src), ARGIN(const STRING *search_string), UINTVAL offset);
+typedef INTVAL   (*str_vtable_index_t)(PARROT_INTERP, ARGIN(const STRING *src), ARGIN(const STRING *search_string), INTVAL offset);
+typedef INTVAL   (*str_vtable_rindex_t)(PARROT_INTERP, ARGIN(const STRING *src), ARGIN(const STRING *search_string), INTVAL offset);
 typedef size_t   (*str_vtable_hash_t)(PARROT_INTERP, ARGIN(const STRING *s), size_t hashval);
 typedef UINTVAL  (*str_vtable_validate_t)(PARROT_INTERP, ARGIN(const STRING *src));
 

Modified: trunk/src/io/api.c
==============================================================================
--- trunk/src/io/api.c	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/io/api.c	Sat Oct  2 22:28:04 2010	(r49420)
@@ -414,7 +414,7 @@
 
         orig_length = Parrot_str_byte_length(interp, result);
         GETATTR_StringHandle_read_offset(interp, pmc, offset);
-        newline_pos = Parrot_str_find_index(interp, result, CONST_STRING(interp, "\n"), offset);
+        newline_pos = STRING_index(interp, result, CONST_STRING(interp, "\n"), offset);
 
         /* No newline found, read the rest of the string. */
         if (newline_pos == -1)

Modified: trunk/src/ops/core_ops.c
==============================================================================
--- trunk/src/ops/core_ops.c	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/ops/core_ops.c	Sat Oct  2 22:28:04 2010	(r49420)
@@ -22764,84 +22764,84 @@
 opcode_t *
 Parrot_index_i_s_s(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SREG(2) && SREG(3)) ? Parrot_str_find_index(interp, SREG(2), SREG(3), 0) : -1;
+    IREG(1) = (SREG(2) && SREG(3)) ? STRING_index(interp, SREG(2), SREG(3), 0) : -1;
 
 return (opcode_t *)cur_opcode + 4;}
 
 opcode_t *
 Parrot_index_i_sc_s(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SCONST(2) && SREG(3)) ? Parrot_str_find_index(interp, SCONST(2), SREG(3), 0) : -1;
+    IREG(1) = (SCONST(2) && SREG(3)) ? STRING_index(interp, SCONST(2), SREG(3), 0) : -1;
 
 return (opcode_t *)cur_opcode + 4;}
 
 opcode_t *
 Parrot_index_i_s_sc(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SREG(2) && SCONST(3)) ? Parrot_str_find_index(interp, SREG(2), SCONST(3), 0) : -1;
+    IREG(1) = (SREG(2) && SCONST(3)) ? STRING_index(interp, SREG(2), SCONST(3), 0) : -1;
 
 return (opcode_t *)cur_opcode + 4;}
 
 opcode_t *
 Parrot_index_i_sc_sc(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SCONST(2) && SCONST(3)) ? Parrot_str_find_index(interp, SCONST(2), SCONST(3), 0) : -1;
+    IREG(1) = (SCONST(2) && SCONST(3)) ? STRING_index(interp, SCONST(2), SCONST(3), 0) : -1;
 
 return (opcode_t *)cur_opcode + 4;}
 
 opcode_t *
 Parrot_index_i_s_s_i(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SREG(2) && SREG(3)) ? Parrot_str_find_index(interp, SREG(2), SREG(3), IREG(4)) : -1;
+    IREG(1) = (SREG(2) && SREG(3)) ? STRING_index(interp, SREG(2), SREG(3), IREG(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 
 opcode_t *
 Parrot_index_i_sc_s_i(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SCONST(2) && SREG(3)) ? Parrot_str_find_index(interp, SCONST(2), SREG(3), IREG(4)) : -1;
+    IREG(1) = (SCONST(2) && SREG(3)) ? STRING_index(interp, SCONST(2), SREG(3), IREG(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 
 opcode_t *
 Parrot_index_i_s_sc_i(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SREG(2) && SCONST(3)) ? Parrot_str_find_index(interp, SREG(2), SCONST(3), IREG(4)) : -1;
+    IREG(1) = (SREG(2) && SCONST(3)) ? STRING_index(interp, SREG(2), SCONST(3), IREG(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 
 opcode_t *
 Parrot_index_i_sc_sc_i(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SCONST(2) && SCONST(3)) ? Parrot_str_find_index(interp, SCONST(2), SCONST(3), IREG(4)) : -1;
+    IREG(1) = (SCONST(2) && SCONST(3)) ? STRING_index(interp, SCONST(2), SCONST(3), IREG(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 
 opcode_t *
 Parrot_index_i_s_s_ic(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SREG(2) && SREG(3)) ? Parrot_str_find_index(interp, SREG(2), SREG(3), ICONST(4)) : -1;
+    IREG(1) = (SREG(2) && SREG(3)) ? STRING_index(interp, SREG(2), SREG(3), ICONST(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 
 opcode_t *
 Parrot_index_i_sc_s_ic(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SCONST(2) && SREG(3)) ? Parrot_str_find_index(interp, SCONST(2), SREG(3), ICONST(4)) : -1;
+    IREG(1) = (SCONST(2) && SREG(3)) ? STRING_index(interp, SCONST(2), SREG(3), ICONST(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 
 opcode_t *
 Parrot_index_i_s_sc_ic(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SREG(2) && SCONST(3)) ? Parrot_str_find_index(interp, SREG(2), SCONST(3), ICONST(4)) : -1;
+    IREG(1) = (SREG(2) && SCONST(3)) ? STRING_index(interp, SREG(2), SCONST(3), ICONST(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 
 opcode_t *
 Parrot_index_i_sc_sc_ic(opcode_t *cur_opcode, PARROT_INTERP)  {
     const Parrot_Context * const CUR_CTX = Parrot_pcc_get_context_struct(interp, interp->ctx);
-    IREG(1) = (SCONST(2) && SCONST(3)) ? Parrot_str_find_index(interp, SCONST(2), SCONST(3), ICONST(4)) : -1;
+    IREG(1) = (SCONST(2) && SCONST(3)) ? STRING_index(interp, SCONST(2), SCONST(3), ICONST(4)) : -1;
 
 return (opcode_t *)cur_opcode + 5;}
 

Modified: trunk/src/ops/string.ops
==============================================================================
--- trunk/src/ops/string.ops	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/ops/string.ops	Sat Oct  2 22:28:04 2010	(r49420)
@@ -284,11 +284,11 @@
 =cut
 
 inline op index(out INT, in STR, in STR) :base_core {
-    $1 = ($2 && $3) ? Parrot_str_find_index(interp, $2, $3, 0) : -1;
+    $1 = ($2 && $3) ? STRING_index(interp, $2, $3, 0) : -1;
 }
 
 inline op index(out INT, in STR, in STR, in INT) :base_core {
-    $1 = ($2 && $3) ? Parrot_str_find_index(interp, $2, $3, $4) : -1;
+    $1 = ($2 && $3) ? STRING_index(interp, $2, $3, $4) : -1;
 }
 
 

Modified: trunk/src/pmc.c
==============================================================================
--- trunk/src/pmc.c	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/pmc.c	Sat Oct  2 22:28:04 2010	(r49420)
@@ -996,7 +996,7 @@
 
     do {
         INTVAL len;
-        const INTVAL idx = Parrot_str_find_index(interp, what, role, (INTVAL)pos);
+        const INTVAL idx = STRING_index(interp, what, role, pos);
 
         if ((idx < 0) || (idx >= length))
             return 0;

Modified: trunk/src/pmc/codestring.pmc
==============================================================================
--- trunk/src/pmc/codestring.pmc	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/pmc/codestring.pmc	Sat Oct  2 22:28:04 2010	(r49420)
@@ -115,7 +115,7 @@
 
     while (pos >= 0) {
         pos += replen;
-        pos = Parrot_str_find_index(INTERP, fmt, percent, pos);
+        pos = STRING_index(INTERP, fmt, percent, pos);
         if (pos < 0)
             break;
 
@@ -277,14 +277,14 @@
 
     escaped_str = Parrot_str_concat(INTERP, quote, escaped_str);
     escaped_str = Parrot_str_concat(INTERP, escaped_str, quote);
-    x_pos       = Parrot_str_find_index(INTERP, escaped_str, x, 0);
+    x_pos       = STRING_index(INTERP, escaped_str, x, 0);
 
     if (x_pos != -1) {
         is_unicode = 1;
     }
     else {
         STRING * const u = CONST_STRING(INTERP, "\\u");
-        const INTVAL u_pos = Parrot_str_find_index(INTERP, escaped_str, u, 0);
+        const INTVAL u_pos = STRING_index(INTERP, escaped_str, u, 0);
         if (u_pos != -1)
             is_unicode = 1;
     }

Modified: trunk/src/pmc/string.pmc
==============================================================================
--- trunk/src/pmc/string.pmc	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/pmc/string.pmc	Sat Oct  2 22:28:04 2010	(r49420)
@@ -507,7 +507,7 @@
         STRING       * s       = VTABLE_get_string(INTERP, SELF);
         INTVAL         i       = 0;
 
-        while (-1 != (i = Parrot_str_find_index(INTERP, s, orig, i))) {
+        while (-1 != (i = STRING_index(INTERP, s, orig, i))) {
             s = Parrot_str_replace(INTERP, s, i, old_len, _new);
             i += new_len;
         }

Modified: trunk/src/pmc/stringbuilder.pmc
==============================================================================
--- trunk/src/pmc/stringbuilder.pmc	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/pmc/stringbuilder.pmc	Sat Oct  2 22:28:04 2010	(r49420)
@@ -360,7 +360,7 @@
          * for the string builder. */
         while (pos >= 0) {
             /* Find the next % */
-            percentPos = Parrot_str_find_index(INTERP, fmt, percent, pos);
+            percentPos = STRING_index(INTERP, fmt, percent, pos);
 
             if (percentPos < 0) {
                 if (pos == 0) {

Modified: trunk/src/pmc/sub.pmc
==============================================================================
--- trunk/src/pmc/sub.pmc	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/pmc/sub.pmc	Sat Oct  2 22:28:04 2010	(r49420)
@@ -1040,7 +1040,7 @@
                 EXCEPTION_INVALID_OPERATION,
                 "illegal register kind '%Ss'", reg);
 
-        kind = Parrot_str_find_index(INTERP, types, reg, 0);
+        kind = STRING_index(INTERP, types, reg, 0);
 
         if (kind == -1)
             Parrot_ex_throw_from_c_args(INTERP, NULL,

Modified: trunk/src/string/api.c
==============================================================================
--- trunk/src/string/api.c	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/string/api.c	Sat Oct  2 22:28:04 2010	(r49420)
@@ -789,7 +789,11 @@
 
 Returns the character position of the second Parrot string in the first at or
 after C<start>. The return value is a (0 based) offset in characters, not
-bytes. If second string is not found in the first string, returns -1.
+bytes. If the search string is not found in the first string or it is null or
+empty, returns -1. If C<start> is out of bounds, returns -1. Throws an
+exception if C<src> is null.
+
+Identical to the STRING_index macro.
 
 =cut
 
@@ -803,11 +807,10 @@
 {
     ASSERT_ARGS(Parrot_str_find_index)
 
-    if ((UINTVAL)start >= STRING_length(src)
-    ||  !STRING_length(search))
-        return -1;
+    if (src == NULL)
+        src = STRINGNULL;
 
-    return STRING_index(interp, src, search, (UINTVAL)start);
+    return STRING_index(interp, src, search, start);
 }
 
 

Modified: trunk/src/string/encoding/shared.c
==============================================================================
--- trunk/src/string/encoding/shared.c	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/string/encoding/shared.c	Sat Oct  2 22:28:04 2010	(r49420)
@@ -148,7 +148,7 @@
 /*
 
 =item C<INTVAL encoding_index(PARROT_INTERP, const STRING *src, const STRING
-*search, UINTVAL offs)>
+*search, INTVAL offset)>
 
 Searches for the first instance of STRING C<search> in STRING C<src>.
 returns the position where the substring is found if it is indeed found.
@@ -161,14 +161,18 @@
 
 PARROT_WARN_UNUSED_RESULT
 INTVAL
-encoding_index(PARROT_INTERP, ARGIN(const STRING *src), ARGIN(const STRING *search),
-    UINTVAL offs)
+encoding_index(PARROT_INTERP, ARGIN(const STRING *src),
+        ARGIN(const STRING *search), INTVAL offset)
 {
     ASSERT_ARGS(encoding_index)
     String_iter start, end;
 
+    if ((UINTVAL)offset >= STRING_length(src)
+    ||  !STRING_length(search))
+        return -1;
+
     STRING_ITER_INIT(interp, &start);
-    STRING_iter_set_position(interp, src, &start, offs);
+    STRING_iter_set_position(interp, src, &start, offset);
 
     return Parrot_str_iter_index(interp, src, &start, &end, search);
 }
@@ -177,7 +181,7 @@
 /*
 
 =item C<INTVAL encoding_rindex(PARROT_INTERP, const STRING *src, const STRING
-*search_string, UINTVAL offset)>
+*search_string, INTVAL offset)>
 
 Finds the last index of substring C<search_string> in STRING C<src>,
 starting from C<offset>. Not implemented.
@@ -189,7 +193,7 @@
 PARROT_WARN_UNUSED_RESULT
 INTVAL
 encoding_rindex(PARROT_INTERP, SHIM(const STRING *src),
-        SHIM(const STRING *search_string), SHIM(UINTVAL offset))
+        SHIM(const STRING *search_string), SHIM(INTVAL offset))
 {
     ASSERT_ARGS(encoding_rindex)
     /* TODO: https://trac.parrot.org/parrot/wiki/StringsTasklist Implement this. */
@@ -725,7 +729,7 @@
 /*
 
 =item C<INTVAL fixed8_index(PARROT_INTERP, const STRING *src, const STRING
-*search_string, UINTVAL offset)>
+*search, INTVAL offset)>
 
 Searches for the first instance of STRING C<search> in STRING C<src>.
 returns the position where the substring is found if it is indeed found.
@@ -738,26 +742,23 @@
 PARROT_WARN_UNUSED_RESULT
 INTVAL
 fixed8_index(PARROT_INTERP, ARGIN(const STRING *src),
-        ARGIN(const STRING *search_string), UINTVAL offset)
+        ARGIN(const STRING *search), INTVAL offset)
 {
     ASSERT_ARGS(fixed8_index)
     INTVAL retval;
 
-    if (STRING_max_bytes_per_codepoint(search_string) != 1) {
-        return encoding_index(interp, src, search_string, offset);
-    }
+    if ((UINTVAL)offset >= STRING_length(src)
+    ||  !STRING_length(search))
+        return -1;
 
-    PARROT_ASSERT(STRING_max_bytes_per_codepoint(src) == 1);
-    retval = Parrot_byte_index(interp, src,
-            search_string, offset);
-    return retval;
+    return Parrot_byte_index(interp, src, search, offset);
 }
 
 
 /*
 
 =item C<INTVAL fixed8_rindex(PARROT_INTERP, const STRING *src, const STRING
-*search_string, UINTVAL offset)>
+*search_string, INTVAL offset)>
 
 Searches for the last instance of STRING C<search_string> in STRING
 C<src>. Starts searching at C<offset>.
@@ -769,7 +770,7 @@
 PARROT_WARN_UNUSED_RESULT
 INTVAL
 fixed8_rindex(PARROT_INTERP, ARGIN(const STRING *src),
-        ARGIN(const STRING *search_string), UINTVAL offset)
+        ARGIN(const STRING *search_string), INTVAL offset)
 {
     ASSERT_ARGS(fixed8_rindex)
     INTVAL retval;

Modified: trunk/src/string/encoding/shared.h
==============================================================================
--- trunk/src/string/encoding/shared.h	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/src/string/encoding/shared.h	Sat Oct  2 22:28:04 2010	(r49420)
@@ -74,7 +74,7 @@
 INTVAL encoding_index(PARROT_INTERP,
     ARGIN(const STRING *src),
     ARGIN(const STRING *search),
-    UINTVAL offs)
+    INTVAL offset)
         __attribute__nonnull__(1)
         __attribute__nonnull__(2)
         __attribute__nonnull__(3);
@@ -97,7 +97,7 @@
 INTVAL encoding_rindex(PARROT_INTERP,
     SHIM(const STRING *src),
     SHIM(const STRING *search_string),
-    NULLOK(UINTVAL offset))
+    NULLOK(INTVAL offset))
         __attribute__nonnull__(1);
 
 UINTVAL encoding_scan(PARROT_INTERP, ARGIN(const STRING *src))
@@ -140,8 +140,8 @@
 PARROT_WARN_UNUSED_RESULT
 INTVAL fixed8_index(PARROT_INTERP,
     ARGIN(const STRING *src),
-    ARGIN(const STRING *search_string),
-    UINTVAL offset)
+    ARGIN(const STRING *search),
+    INTVAL offset)
         __attribute__nonnull__(1)
         __attribute__nonnull__(2)
         __attribute__nonnull__(3);
@@ -197,7 +197,7 @@
 INTVAL fixed8_rindex(PARROT_INTERP,
     ARGIN(const STRING *src),
     ARGIN(const STRING *search_string),
-    UINTVAL offset)
+    INTVAL offset)
         __attribute__nonnull__(1)
         __attribute__nonnull__(2)
         __attribute__nonnull__(3);
@@ -312,7 +312,7 @@
 #define ASSERT_ARGS_fixed8_index __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp) \
     , PARROT_ASSERT_ARG(src) \
-    , PARROT_ASSERT_ARG(search_string))
+    , PARROT_ASSERT_ARG(search))
 #define ASSERT_ARGS_fixed8_iter_get __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp) \
     , PARROT_ASSERT_ARG(str) \

Modified: trunk/t/op/string.t
==============================================================================
--- trunk/t/op/string.t	Sat Oct  2 22:27:21 2010	(r49419)
+++ trunk/t/op/string.t	Sat Oct  2 22:28:04 2010	(r49420)
@@ -957,10 +957,19 @@
     index $I1, $S0, $S1
     is( $I1, "-1", 'index, null strings' )
 
+    .local pmc eh
+    eh = new ['ExceptionHandler']
+    eh.'handle_types'(.EXCEPTION_UNEXPECTED_NULL)
+    set_addr eh, handler
+    push_eh eh
+    $I1 = 1
     null $S0
     null $S1
-    index $I1, $S0, $S1
-    is( $I1, "-1", 'index, null strings' )
+    index $I0, $S0, $S1
+    $I1 = 0
+  handler:
+    pop_eh
+    is( $I1, "1", "index with null string throws" )
 .end
 
 .sub index_embedded_nulls


More information about the parrot-commits mailing list