]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
glib-2.0: Improve performance of string.substring on large strings
authorJürg Billeter <j@bitron.ch>
Thu, 10 Feb 2011 16:55:18 +0000 (17:55 +0100)
committerJürg Billeter <j@bitron.ch>
Thu, 10 Feb 2011 16:55:18 +0000 (17:55 +0100)
vapi/glib-2.0.vapi

index 019339b086512de1477393269b10549d1979d5c4..291c444a94734b434cdd4bfd0f49b6d62fe7c9c2 100644 (file)
@@ -1277,11 +1277,20 @@ public class string {
        [CCode (cname = "g_strndup")]
        public string ndup (size_t n);
 
+       [CCode (cname = "strnlen")]
+       static long strnlen (char* str, size_t maxlen);
        [CCode (cname = "g_strndup")]
        static string strndup (char* str, size_t n);
 
        public string substring (long offset, long len = -1) {
-               long string_length = this.length;
+               long string_length;
+               if (offset >= 0 && len >= 0) {
+                       // avoid scanning whole string
+                       string_length = strnlen ((char*) this, offset + len);
+               } else {
+                       string_length = this.length;
+               }
+
                if (offset < 0) {
                        offset = string_length + offset;
                        GLib.return_val_if_fail (offset >= 0, null);