[vala] glib-2.0: Add string.last_index_of_char



commit f586e13e182eee9d73e6d44fb3f4439fad4b5114
Author: Jürg Billeter <j bitron ch>
Date:   Thu Jan 20 22:10:27 2011 +0100

    glib-2.0: Add string.last_index_of_char
    
    This deprecates string.rchr.

 compiler/valacompiler.vala |   13 ++++++-------
 vala/valasourcefile.vala   |    2 +-
 vapi/glib-2.0.vapi         |   13 +++++++++++++
 3 files changed, 20 insertions(+), 8 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 1a4a9df..95e6d19 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -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));
diff --git a/vala/valasourcefile.vala b/vala/valasourcefile.vala
index 8ab0e2f..3c0eb39 100644
--- a/vala/valasourcefile.vala
+++ b/vala/valasourcefile.vala
@@ -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));
 	}
 
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 628192b..71846b1 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -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")]



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]