]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
glib-2.0: Add string.last_index_of_char
authorJürg Billeter <j@bitron.ch>
Thu, 20 Jan 2011 21:10:27 +0000 (22:10 +0100)
committerJürg Billeter <j@bitron.ch>
Thu, 20 Jan 2011 21:15:14 +0000 (22:15 +0100)
This deprecates string.rchr.

compiler/valacompiler.vala
vala/valasourcefile.vala
vapi/glib-2.0.vapi

index 1a4a9df19038ab8b1b6fee38a73ef300271f9c60..95e6d198fd9500f44ba2485f72075c3ea9cac95f 100644 (file)
@@ -159,8 +159,8 @@ class Vala.Compiler {
                if (!ccode_only && !compile_only && output == null) {
                        // strip extension if there is one
                        // else we use the default output file of the C compiler
-                       if (sources[0].rchr (-1, '.') != null) {
-                               long dot = (long) ((char*) sources[0].rchr (-1, '.') - (char*) sources[0]);
+                       if (sources[0].last_index_of_char ('.') != -1) {
+                               int dot = sources[0].last_index_of_char ('.');
                                output = Path.get_basename (sources[0].substring (0, dot));
                        }
                }
@@ -375,14 +375,13 @@ class Vala.Compiler {
                        if (gir != null) {
                                if (context.profile == Profile.GOBJECT) {
                                        long gir_len = gir.length;
-                                       unowned string? last_hyphen = gir.rchr (gir_len, '-');
+                                       int last_hyphen = gir.last_index_of_char ('-');
 
-                                       if (last_hyphen == null || !gir.has_suffix (".gir")) {
+                                       if (last_hyphen == -1 || !gir.has_suffix (".gir")) {
                                                Report.error (null, "GIR file name `%s' is not well-formed, expected NAME-VERSION.gir".printf (gir));
                                        } else {
-                                               long offset = (long) ((char*) last_hyphen - (char*) gir);
-                                               string gir_namespace = gir.substring (0, offset);
-                                               string gir_version = gir.substring (offset + 1, gir_len - offset - 5);
+                                               string gir_namespace = gir.substring (0, last_hyphen);
+                                               string gir_version = gir.substring (last_hyphen + 1, gir_len - last_hyphen - 5);
                                                gir_version.canon ("0123456789.", '?');
                                                if (gir_namespace == "" || gir_version == "" || !gir_version[0].isdigit () || gir_version.contains ("?")) {
                                                        Report.error (null, "GIR file name `%s' is not well-formed, expected NAME-VERSION.gir".printf (gir));
index 8ab0e2f2f309427528c07fd5fb7be7376ca4a614..3c0eb3929e6518dfa95a692e82f5dd086992294e 100644 (file)
@@ -193,7 +193,7 @@ public class Vala.SourceFile {
        }
 
        private string get_basename () {
-               long dot = (long) ((char*) filename.rchr (-1, '.') - (char*) filename);
+               int dot = filename.last_index_of_char ('.');
                return Path.get_basename (filename.substring (0, dot));
        }
 
index 628192b88e357707521592fdcadf5347f1e5a5d1..71846b1d9bfb88112eeb8115e294e9edf4ce4181 100644 (file)
@@ -970,6 +970,8 @@ public class string {
        static char* strrstr (char* haystack, char* needle);
        [CCode (cname = "g_utf8_strchr")]
        static char* utf8_strchr (char* str, ssize_t len, unichar c);
+       [CCode (cname = "g_utf8_strrchr")]
+       static char* utf8_strrchr (char* str, ssize_t len, unichar c);
 
        public int index_of (string needle, int start_index = 0) {
                char* result = strstr ((char*) this + start_index, (char*) needle);
@@ -1001,6 +1003,16 @@ public class string {
                }
        }
 
+       public int last_index_of_char (unichar c, int start_index = 0) {
+               char* result = utf8_strrchr ((char*) this + start_index, -1, c);
+
+               if (result != null) {
+                       return (int) (result - (char*) this);
+               } else {
+                       return -1;
+               }
+       }
+
        [CCode (cname = "g_str_has_prefix")]
        public bool has_prefix (string prefix);
        [CCode (cname = "g_str_has_suffix")]
@@ -1080,6 +1092,7 @@ public class string {
        [Deprecated (replacement = "string.index_of_char")]
        [CCode (cname = "g_utf8_strchr")]
        public unowned string chr (ssize_t len, unichar c);
+       [Deprecated (replacement = "string.last_index_of_char")]
        [CCode (cname = "g_utf8_strrchr")]
        public unowned string rchr (ssize_t len, unichar c);
        [CCode (cname = "g_utf8_strreverse")]