]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
add substring method to the string type test substring method
authorJuerg Billeter <j@bitron.ch>
Wed, 11 Jul 2007 16:19:45 +0000 (16:19 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Wed, 11 Jul 2007 16:19:45 +0000 (16:19 +0000)
2007-07-11  Juerg 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

svn path=/trunk/; revision=347

ChangeLog
ccode/valaccodefunctioncall.vala
gobject/valacodegenerator.vala
gobject/valacodegeneratorinvocationexpression.vala
tests/test-021.out
tests/test-021.vala
vapi/glib-2.0.vala

index dd9e99d7eac5b7405e8795dc8a1e4d9824fc0bda..d5f991fce2996a7e02733a38a70e6364a5960f2b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+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:
index 03c778896e3bb3dc1cab7fcd73e6c2c603edb831..fa6a7d449a9c8e1f9f1724d02c8c9e4ab3740a0f 100644 (file)
@@ -1,6 +1,6 @@
 /* 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
@@ -33,8 +33,7 @@ public class Vala.CCodeFunctionCall : CCodeExpression {
        
        private List<CCodeExpression> arguments;
        
-       public CCodeFunctionCall (CCodeExpression _call = null) {
-               call = _call;
+       public CCodeFunctionCall (construct CCodeExpression call = null) {
        }
        
        /**
@@ -45,7 +44,16 @@ public class Vala.CCodeFunctionCall : CCodeExpression {
        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 (" (");
index 4191377f23e155e43b21031cc04ef1cfca817ef6..88cb7ccc4b84d64df67ad8336a957a936bfb78c7 100644 (file)
@@ -96,6 +96,8 @@ public class Vala.CodeGenerator : CodeVisitor {
        TypeReference mutex_type;
        DataType type_module_type;
 
+       Method substring_method;
+
        private bool in_plugin = false;
        private string module_init_param_name;
        
@@ -224,6 +226,7 @@ public class Vala.CodeGenerator : CodeVisitor {
 
                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");
                
index cabe05d59a11d19fee37ec1f8fe605d7d8cb59f5..bbbb3b2f9a122e9e1ccfb8bf1c837931fef25948 100644 (file)
@@ -242,7 +242,7 @@ public class Vala.CodeGenerator {
                        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);
@@ -258,6 +258,34 @@ public class Vala.CodeGenerator {
                        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;
                }
        }
index 58dfb1b3fc31cb5b27a8ea898fddc006f331fb8d..5b9feab5e7fee879989a6197f378900ca5131d15 100644 (file)
@@ -1 +1 @@
-String + operator: 1 2 3 4 5 6
+String Test: 1 2 3 4 5 6 7 8
index 0bb38e353986dfeeebce068c63cef0b13eada9a6..c2a75beb1cd50726ea10fa8cea9b13a16c64f008 100644 (file)
@@ -2,16 +2,18 @@ using GLib;
 
 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;
        }
index ca8b7bdbb1ad575fa9c4df86bbbba5c6b443bf37..0948bd8f3876ac1dd043292ffb466525f6b6108a 100644 (file)
@@ -518,6 +518,8 @@ public struct string {
 
        [CCode (cname = "g_utf8_skip")]
        public static char[] skip;
+
+       public string! substring (long offset, long len);
 }
 
 [Import ()]