From: Jürg Billeter Date: Tue, 16 Dec 2008 08:27:59 +0000 (+0000) Subject: Move substring method to VAPI file, handle negative and out of bounds X-Git-Tag: VALA_0_5_3~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bca83502ae7ffc08789f98bdffe735f6103c2886;p=thirdparty%2Fvala.git Move substring method to VAPI file, handle negative and out of bounds 2008-12-16 Jürg Billeter * gobject/valaccodebasemodule.vala: * gobject/valaccodemethodcallmodule.vala: * vapi/glib-2.0.vapi: Move substring method to VAPI file, handle negative and out of bounds values, fixes bug 443524 svn path=/trunk/; revision=2175 --- diff --git a/ChangeLog b/ChangeLog index 2400d0dd6..acd0c47e1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-12-16 Jürg Billeter + + * gobject/valaccodebasemodule.vala: + * gobject/valaccodemethodcallmodule.vala: + * vapi/glib-2.0.vapi: + + Move substring method to VAPI file, handle negative and out of + bounds values, fixes bug 443524 + 2008-12-16 Jürg Billeter * vapi/packages/gnome-keyring-1/: diff --git a/gobject/valaccodebasemodule.vala b/gobject/valaccodebasemodule.vala index d8773136c..43597cc9f 100644 --- a/gobject/valaccodebasemodule.vala +++ b/gobject/valaccodebasemodule.vala @@ -120,8 +120,6 @@ public class Vala.CCodeBaseModule : CCodeModule { public Interface map_type; public TypeSymbol dbus_object_type; - public Method substring_method; - public bool in_plugin = false; public string module_init_param_name; @@ -566,7 +564,6 @@ public class Vala.CCodeBaseModule : CCodeModule { float_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("float")); double_type = new ValueType ((TypeSymbol) root_symbol.scope.lookup ("double")); string_type = new ObjectType ((Class) root_symbol.scope.lookup ("string")); - substring_method = (Method) string_type.data_type.scope.lookup ("substring"); var glib_ns = root_symbol.scope.lookup ("GLib"); diff --git a/gobject/valaccodemethodcallmodule.vala b/gobject/valaccodemethodcallmodule.vala index 00eece34c..cba212ee5 100644 --- a/gobject/valaccodemethodcallmodule.vala +++ b/gobject/valaccodemethodcallmodule.vala @@ -460,34 +460,6 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule { ccomma.append_expression (new CCodeConditionalExpression (ccheck, czero, new CCodeConstant ("NULL"))); ccomma.append_expression (new CCodeAssignment (head.get_array_length_cexpression (ma.inner, 1), temp_ref)); - expr.ccodenode = ccomma; - } else if (m == substring_method) { - var temp_decl = get_temp_variable (string_type); - var temp_ref = new CCodeIdentifier (temp_decl.name); - - temp_vars.insert (0, temp_decl); - - Gee.List args = ccall.get_arguments (); - - var coffsetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_utf8_offset_to_pointer")); - // full string - coffsetcall.add_argument (args[0]); - // offset - coffsetcall.add_argument (args[1]); - - var coffsetcall2 = new CCodeFunctionCall (new CCodeIdentifier ("g_utf8_offset_to_pointer")); - coffsetcall2.add_argument (temp_ref); - // len - coffsetcall2.add_argument (args[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; } } diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi index 4242e67ad..4085e429c 100644 --- a/vapi/glib-2.0.vapi +++ b/vapi/glib-2.0.vapi @@ -7,16 +7,20 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + * As a special exception, if you use inline functions from this file, this + * file does not by itself cause the resulting executable to be covered by + * the GNU Lesser General Public License. + * * Author: * Jürg Billeter * Raffaele Sandrini @@ -712,8 +716,6 @@ public class string { public int scanf (...); [CCode (cname = "g_strconcat")] public string concat (string string2, ...); - [CCode (cname = "g_strndup")] - public string ndup (ulong n); /* FIXME: only UTF-8 */ [CCode (cname = "g_strescape")] public string escape (string exceptions); [CCode (cname = "g_strcompress")] @@ -800,8 +802,25 @@ public class string { [CCode (cname = "g_strcanon")] public void canon (string valid_chars, char substitutor); - /* internal method */ - public string substring (long offset, long len); + // n is size in bytes, not length in characters + [CCode (cname = "g_strndup")] + public string ndup (size_t n); + + public string substring (long offset, long len = -1) { + long string_length = this.len (); + if (offset < 0) { + offset = string_length + offset; + GLib.warn_if_fail (offset >= 0); + } else { + GLib.warn_if_fail (offset <= string_length); + } + if (len < 0) { + len = string_length - offset; + } + GLib.warn_if_fail (offset + len <= string_length); + weak string start = this.offset (offset); + return start.ndup (((char*) start.offset (len)) - ((char*) start)); + } public bool contains (string needle) { return this.str (needle) != null;