[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gnash-commit] gnash ChangeLog server/asobj/string.cpp testsui...
From: |
Sandro Santilli |
Subject: |
[Gnash-commit] gnash ChangeLog server/asobj/string.cpp testsui... |
Date: |
Wed, 22 Nov 2006 09:28:37 +0000 |
CVSROOT: /sources/gnash
Module name: gnash
Changes by: Sandro Santilli <strk> 06/11/22 09:28:37
Modified files:
. : ChangeLog
server/asobj : string.cpp
testsuite/actionscript.all: String.as
Log message:
Patch by Mark Cox <address@hidden>:
* server/asobj/string.cpp: implemented string methods
slice, concat, slice, lastindexof; added String
property length
* testsuite/actionscript.all/String.as: tests for the
newly implemented features.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.1696&r2=1.1697
http://cvs.savannah.gnu.org/viewcvs/gnash/server/asobj/string.cpp?cvsroot=gnash&r1=1.6&r2=1.7
http://cvs.savannah.gnu.org/viewcvs/gnash/testsuite/actionscript.all/String.as?cvsroot=gnash&r1=1.4&r2=1.5
Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.1696
retrieving revision 1.1697
diff -u -b -r1.1696 -r1.1697
--- ChangeLog 21 Nov 2006 20:14:02 -0000 1.1696
+++ ChangeLog 22 Nov 2006 09:28:37 -0000 1.1697
@@ -1,3 +1,11 @@
+2006-11-22 Mark Cox <address@hidden>
+
+ * server/asobj/string.cpp: implemented string methods
+ slice, concat, slice, lastindexof; added String
+ property length
+ * testsuite/actionscript.all/String.as: tests for the
+ newly implemented features.
+
2006-11-21 Sandro Santilli <address@hidden>
* server/asobj/Makefile.am: added missing FFMPEG_LIBS dependency.
Index: server/asobj/string.cpp
===================================================================
RCS file: /sources/gnash/gnash/server/asobj/string.cpp,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- server/asobj/string.cpp 11 Nov 2006 22:44:54 -0000 1.6
+++ server/asobj/string.cpp 22 Nov 2006 09:28:37 -0000 1.7
@@ -18,7 +18,7 @@
//
//
-/* $Id: string.cpp,v 1.6 2006/11/11 22:44:54 strk Exp $ */
+/* $Id: string.cpp,v 1.7 2006/11/22 09:28:37 strk Exp $ */
// Implementation of ActionScript String class.
@@ -28,10 +28,16 @@
#include "fn_call.h"
#include "as_object.h"
#include "builtin_function.h" // need builtin_function
+#include "log.h"
+#include "array.h"
namespace gnash {
// Forward declarations
+static void string_concat(const fn_call& fn);
+static void string_slice(const fn_call& fn);
+static void string_split(const fn_call& fn);
+static void string_last_index_of(const fn_call& fn);
static void string_sub_str(const fn_call& fn);
static void string_sub_string(const fn_call& fn);
static void string_index_of(const fn_call& fn);
@@ -47,11 +53,10 @@
attachStringInterface(as_object& o)
{
// TODO fill in the rest
- // concat()
- // length property
- // slice()
- // split()
- // lastIndexOf()
+ o.set_member("concat", &string_concat);
+ o.set_member("slice", &string_slice);
+ o.set_member("split", &string_split);
+ o.set_member("lastindexof", &string_last_index_of);
o.set_member("substr", &string_sub_str);
o.set_member("substring", &string_sub_string);
o.set_member("indexOf", &string_index_of);
@@ -85,8 +90,193 @@
as_object(getStringInterface())
{
}
+
+ virtual bool get_member(const tu_stringi& name, as_value* val) {
+
+ if (name == "length")
+ {
+ val->set_int(m_string.utf8_length());
+ return true;
+ }
+
+ return get_member_default(name,val);
+ }
+
};
+// all the arguments will be converted to string and concatenated
+static void
+string_concat(const fn_call& fn)
+{
+ tu_string this_string = ((tu_string_as_object*) fn.this_ptr)->m_string;
+
+ int len = 0;
+ for (int i = 0; i < fn.nargs; i++) len += strlen(fn.arg(i).to_string());
+
+ char *newstr = new char[len + 1];
+ int pos = 0;
+ for (int i = 0; i < fn.nargs; i++)
+ {
+ int len = strlen(fn.arg(i).to_string());
+ memcpy((newstr + pos),fn.arg(i).to_string(),len);
+ pos += len;
+ }
+ newstr[len] = '\0';
+
+ tu_string returnstring(newstr);
+ fn.result->set_tu_string(returnstring);
+ delete[] newstr;
+
+ //FIXME: is the "delete newstr[];" okay ?
+ // Because I don't know if tu_string copies newstr or not. Michael
Meier 2006/11/21
+}
+
+// 1st param: start_index, 2nd param: end_index
+static void
+string_slice(const fn_call& fn)
+{
+ tu_string this_string = ((tu_string_as_object*) fn.this_ptr)->m_string;
+ // Pull a slice out of this_string.
+ int start = 0;
+ int utf8_len = this_string.utf8_length();
+ int end = utf8_len;
+ if (fn.nargs >= 1)
+ {
+ start = static_cast<int>(fn.arg(0).to_number());
+ if (start < 0) start = utf8_len + start;
+ start = iclamp(start, 0, utf8_len);
+ }
+ if (fn.nargs >= 2)
+ {
+ end = static_cast<int>(fn.arg(1).to_number());
+ if (end < 0) end = utf8_len + end;
+ end = iclamp(end, 0, utf8_len);
+ }
+
+ assert(end >= start);
+
+ fn.result->set_tu_string(this_string.utf8_substring(start, end));
+ return;
+}
+
+static void
+string_split(const fn_call& fn)
+{
+
+ boost::intrusive_ptr<tu_string_as_object>
this_string_ptr((tu_string_as_object*) fn.this_ptr);
+
+ as_value val;
+
+ boost::intrusive_ptr<as_array_object> array(new as_array_object());
+
+ if (fn.nargs == 0)
+ {
+ val.set_tu_string(this_string_ptr->m_string);
+ array->push(val);
+
+ fn.result->set_as_object(array.get());
+ return;
+ }
+
+ if (fn.nargs >= 1)
+ {
+ tu_string this_string = ((tu_string_as_object*)
fn.this_ptr)->m_string;
+
+ int utf8_len = this_string.utf8_length();
+
+ if (strcmp("",fn.arg(0).to_string()) == 0)
+ {
+ for (int i = 0; i < utf8_len; i++) {
+
val.set_tu_string(this_string.utf8_substring(i,i+1));
+ array->push(val);
+ }
+ fn.result->set_as_object(array.get());
+ return;
+ }
+ else
+ {
+ const char *str = this_string.c_str();
+ const char *delimeter = fn.arg(0).to_string();
+
+ tu_string str_tu(str);
+ tu_string delimeter_tu(str);
+
+ int start = 0;
+ int end;
+ //int utf8_str_len = str_tu.utf8_length();
+ //int utf8_delimeter_len = delimeter_tu.utf8_length();
+ int delimeter_len = strlen(delimeter);
+
+ const char *pstart = str;
+ const char *pend = strstr(pstart,delimeter);
+ while (pend != NULL)
+ {
+ //tu_string fromstart(pstart);
+ //tu_string fromend(pend);
+ start =
tu_string::utf8_char_count(str,int(pstart-str));
+ end = start +
tu_string::utf8_char_count(pstart,int(pend-pstart));
+
+
val.set_tu_string(this_string.utf8_substring(start,end));
+ array->push(val);
+ pstart = pend + delimeter_len;
+ if (!(*pstart))
+ {
+ fn.result->set_as_object(array.get());
+ return;
+ }
+ pend = strstr(pstart,delimeter);
+
+ }
+ val.set_tu_string(tu_string(pstart));
+ array->push(val);
+ fn.result->set_as_object(array.get());
+ return;
+ }
+ }
+
+}
+
+static void
+string_last_index_of(const fn_call& fn)
+{
+ tu_string_as_object* this_string_ptr = (tu_string_as_object*)
fn.this_ptr;
+ assert(this_string_ptr);
+
+ if (fn.nargs < 1)
+ {
+ fn.result->set_double(-1);
+ return;
+ }
+ else
+ {
+ int start_index = 0;
+ if (fn.nargs > 1)
+ {
+ start_index = static_cast<int>(fn.arg(1).to_number());
+ }
+ const char* str = this_string_ptr->m_string.c_str();
+ const char* p = strstr(
+ str + start_index, // FIXME: not UTF-8 correct!
+ fn.arg(0).to_string());
+ if (p == NULL)
+ {
+ fn.result->set_double(-1);
+ return;
+ }
+
+ const char* lastocc = p;
+ while (p != NULL)
+ {
+ if (!(*p)) break;
+ p = strstr((p+1),fn.arg(0).to_string()); // FIXME: also
not UTF-8 correct!
+ if (p) lastocc = p;
+ }
+
+ fn.result->set_double(tu_string::utf8_char_count(str,
int(lastocc - str)));
+ return;
+ }
+}
+
// 1st param: start_index, 2nd param: length (NOT end_index)
static void
string_sub_str(const fn_call& fn)
Index: testsuite/actionscript.all/String.as
===================================================================
RCS file: /sources/gnash/gnash/testsuite/actionscript.all/String.as,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- testsuite/actionscript.all/String.as 5 Nov 2006 20:36:35 -0000
1.4
+++ testsuite/actionscript.all/String.as 22 Nov 2006 09:28:37 -0000
1.5
@@ -1,7 +1,7 @@
// Mike Carlson's test program for actionscript strings
// June 19th, 2006
-rcsid="$Id: String.as,v 1.4 2006/11/05 20:36:35 strk Exp $";
+rcsid="$Id: String.as,v 1.5 2006/11/22 09:28:37 strk Exp $";
#include "check.as"
@@ -18,9 +18,20 @@
check_equals ( a.charAt(3), "l" );
check_equals ( a.charAt(4), "a" );
check_equals ( a.indexOf("lawa"), 3 );
+check_equals ( a.lastIndexOf("lawa"), 8);
check_equals ( a.indexOf("lawas"), 8 );
check_equals ( a.indexOf("hinG"), 13 );
check_equals ( a.indexOf("hing"), -1 );
+check_equals ( a.split()[0], "wallawallawashinGTON" );
+check_equals ( a.split().length, 1 );
+check ( a.split() instanceof Array );
+check_equals ( a.split("")[0], "w" );
+check_equals ( a.split("")[19], "N" );
+check_equals ( a.split("la")[0], "wal" );
+check_equals ( a.split("la")[1], "wal" );
+check_equals ( a.split("la")[2], "washinGTON" );
+check_equals ( a.split("la").length, 3 );
+
// This is the correct usage pattern
var b = String.fromCharCode(97,98,99,100);
@@ -34,8 +45,12 @@
check_equals ( a.substr(-1,1), "z" );
check_equals ( a.substr(-2,3), "yz" );
check_equals ( a.substr(-3,2), "xy" );
+check_equals ( a.slice(-5,-3), "vw" );
+check_equals ( a.slice(-4), "wxyz" );
check_equals ( a.substring(5,2), "cde" );
check_equals ( a.substring(5,7), "fg" );
+check_equals ( a.length, 26 );
+check_equals ( a.concat("sir ","william",15), "sir william15");
// Test inheritance with built-in functions
var stringInstance = new String();
- [Gnash-commit] gnash ChangeLog server/asobj/string.cpp testsui...,
Sandro Santilli <=