+2007-07-11 Jürg Billeter <j@bitron.ch>
+
+ * ccode/valaccodefunctioncall.vala, gobject/valacodegenerator.vala,
+ gobject/valacodegeneratorinvocationexpression.vala,
+ vapi/glib-2.0.vala: add substring method to the string type
+ * tests/test-021.vala, tests/test-021.out: test substring method
+
+ Fixes bug 443524
+
2007-07-11 Jürg Billeter <j@bitron.ch>
* vala/valasemanticanalyzer.vala, gobject/valacodegenerator.vala:
/* valaccodefunctioncall.vala
*
- * Copyright (C) 2006 Jürg Billeter
+ * Copyright (C) 2006-2007 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
private List<CCodeExpression> arguments;
- public CCodeFunctionCall (CCodeExpression _call = null) {
- call = _call;
+ public CCodeFunctionCall (construct CCodeExpression call = null) {
}
/**
public void add_argument (CCodeExpression! expr) {
arguments.append (expr);
}
-
+
+ /**
+ * Returns a copy of the list of arguments.
+ *
+ * @return list of arguments
+ */
+ public List<weak CCodeExpression> get_arguments () {
+ return arguments.copy ();
+ }
+
public override void write (CCodeWriter! writer) {
call.write (writer);
writer.write_string (" (");
TypeReference mutex_type;
DataType type_module_type;
+ Method substring_method;
+
private bool in_plugin = false;
private string module_init_param_name;
string_type = new TypeReference ();
string_type.data_type = (DataType) root_symbol.lookup ("string").node;
+ substring_method = (Method) string_type.data_type.symbol.lookup ("substring").node;
var glib_ns = root_symbol.lookup ("GLib");
string_h_needed = true;
var clen = get_array_length_cexpression (ma.inner, 1);
- var celems = (CCodeExpression)ma.inner.ccodenode;
+ var celems = (CCodeExpression) ma.inner.ccodenode;
var csizeof = new CCodeIdentifier ("sizeof (%s)".printf (ma.inner.static_type.data_type.get_cname ()));
var cdelta = new CCodeParenthesizedExpression (new CCodeBinaryExpression (CCodeBinaryOperator.MINUS, temp_ref, clen));
var ccheck = new CCodeBinaryExpression (CCodeBinaryOperator.GREATER_THAN, temp_ref, clen);
ccomma.append_expression (new CCodeConditionalExpression (ccheck, czero, new CCodeConstant ("NULL")));
ccomma.append_expression (new CCodeAssignment (get_array_length_cexpression (ma.inner, 1), temp_ref));
+ expr.ccodenode = ccomma;
+ } else if (m == substring_method) {
+ var temp_decl = get_temp_variable_declarator (string_type);
+ var temp_ref = new CCodeIdentifier (temp_decl.name);
+
+ temp_vars.prepend (temp_decl);
+
+ List<weak CCodeExpression> args = ccall.get_arguments ();
+
+ var coffsetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_utf8_offset_to_pointer"));
+ // full string
+ coffsetcall.add_argument (args.nth_data (0));
+ // offset
+ coffsetcall.add_argument (args.nth_data (1));
+
+ var coffsetcall2 = new CCodeFunctionCall (new CCodeIdentifier ("g_utf8_offset_to_pointer"));
+ coffsetcall2.add_argument (temp_ref);
+ // len
+ coffsetcall2.add_argument (args.nth_data (2));
+
+ var cndupcall = new CCodeFunctionCall (new CCodeIdentifier ("g_strndup"));
+ cndupcall.add_argument (temp_ref);
+ cndupcall.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MINUS, coffsetcall2, temp_ref));
+
+ var ccomma = new CCodeCommaExpression ();
+ ccomma.append_expression (new CCodeAssignment (temp_ref, coffsetcall));
+ ccomma.append_expression (cndupcall);
+
expr.ccodenode = ccomma;
}
}
-String + operator: 1 2 3 4 5 6
+String Test: 1 2 3 4 5 6 7 8
class Maman.Foo {
static int main (string[] args) {
- stdout.printf ("String + operator: 1");
-
+ stdout.printf ("String Test: 1");
+
stdout.printf (" 2" + " 3");
-
+
string s = " 4";
s += " 5";
-
stdout.printf ("%s", s);
-
- stdout.printf (" 6\n");
+
+ string t = " 5 6 7 8".substring (2, 4);
+ stdout.printf ("%s", t);
+
+ stdout.printf (" 8\n");
return 0;
}
[CCode (cname = "g_utf8_skip")]
public static char[] skip;
+
+ public string! substring (long offset, long len);
}
[Import ()]