Explorar o código

Added a split function to string that uses a char as delimiter, the original split function that uses strtok was renamed to split_by_strtok.

mingodad %!s(int64=13) %!d(string=hai) anos
pai
achega
4bb28b62f4
Modificáronse 3 ficheiros con 21 adicións e 4 borrados
  1. 1 1
      ourbiz/ourbiz-fltk.nut
  2. 1 1
      ourbiz/pdf-table.nut
  3. 19 2
      squirrel/sqbaselib.cpp

+ 1 - 1
ourbiz/ourbiz-fltk.nut

@@ -436,7 +436,7 @@ class Fl_Data_Table extends Flv_Data_Table {
 		calc_cols();
 	}
 	function parse_field_header(str, col_info){
-		local ci = str.split("|");
+		local ci = str.split('|');
 		local ci_size = ci.size();
 		local curr_ci = 0;
 		col_info.colname <- (ci_size > curr_ci++) ? ci[curr_ci-1] : "?";

+ 1 - 1
ourbiz/pdf-table.nut

@@ -21,7 +21,7 @@ class PdfSqlTable extends Sq_Fpdf {
 	}
 
 	function parse_field_header(col_name){
-		local ci = col_name.split("|");
+		local ci = col_name.split('|');
 		local col_info = {};
 		col_info.colname <- ci.get(0, "?");
 		col_info.header <- ci.get(1, "?");

+ 19 - 2
squirrel/sqbaselib.cpp

@@ -1518,7 +1518,7 @@ static SQInteger string_rstrip(HSQUIRRELVM v)
 	return 1;
 }
 
-static SQInteger string_split(HSQUIRRELVM v)
+static SQInteger string_split_by_strtok(HSQUIRRELVM v)
 {
 	const SQChar *str,*seps;
 	SQChar *stemp,*tok;
@@ -1536,6 +1536,22 @@ static SQInteger string_split(HSQUIRRELVM v)
 		tok = scstrtok( NULL, seps );
 	}
 	return 1;
+}
+
+static int string_split(HSQUIRRELVM v) {
+    SQ_FUNC_VARS_NO_TOP(v);
+    SQ_GET_STRING(v, 1, str);
+    SQ_GET_INTEGER(v, 2, sep);
+    const SQChar *token;
+    sq_newarray(v,0);
+    while ((token = scstrchr(str, sep)) != NULL) {
+        sq_pushstring(v, str, token - str);
+        sq_arrayappend(v, -2);
+        str = token + 1;
+    }
+    sq_pushstring(v, str, -1);
+    sq_arrayappend(v, -2);
+    return 1;
 }
 
 static SQInteger string_empty(HSQUIRRELVM v)
@@ -1665,7 +1681,8 @@ SQRegFunction SQSharedState::_string_default_delegate_funcz[]={
 	{_SC("strip"),string_strip,1, _SC("s")},
 	{_SC("lstrip"),string_lstrip,1, _SC("s")},
 	{_SC("rstrip"),string_rstrip,1, _SC("s")},
-	{_SC("split"),string_split,2, _SC("ss")},
+	{_SC("split"),string_split,2, _SC("si")},
+	{_SC("split_by_strtok"),string_split_by_strtok,2, _SC("ss")},
 	{_SC("empty"),string_empty,1, _SC("s")},
 #ifdef SQ_SUBLATIN
 	{_SC("sl_len"),string_sl_len,1, _SC("s")},