[vala] glib-2.0: Add string.index_of_nth_char



commit bd05eda863e6a6395e4709d7cbebb2969efa16ca
Author: Jürg Billeter <j bitron ch>
Date:   Sat Jan 15 20:57:07 2011 +0100

    glib-2.0: Add string.index_of_nth_char
    
    This deprecates string.utf8_offset, string.offset,
    string.pointer_to_offset, and string.ndup.

 codegen/valagirwriter.vala     |    2 +-
 compiler/valacompiler.vala     |    4 ++--
 vala/valaclass.vala            |    4 ++--
 vala/valacodecontext.vala      |    8 ++++----
 vala/valagenieparser.vala      |    6 +++---
 vala/valageniescanner.vala     |    6 +++---
 vala/valagirparser.vala        |   18 +++++++++---------
 vala/valaintegerliteral.vala   |    4 ++--
 vala/valainterface.vala        |    4 ++--
 vala/valamarkupreader.vala     |   14 +++++++-------
 vala/valamethod.vala           |    2 +-
 vala/valaparser.vala           |    4 ++--
 vala/valascanner.vala          |    6 +++---
 vala/valasemanticanalyzer.vala |    2 +-
 vala/valasourcefile.vala       |    4 ++--
 vala/valasymbol.vala           |    4 ++--
 vapi/glib-2.0.vapi             |   34 +++++++++++++++++++++++-----------
 vapigen/valagidlparser.vala    |   38 +++++++++++++++++++-------------------
 vapigen/valavapigen.vala       |    2 +-
 19 files changed, 89 insertions(+), 77 deletions(-)
---
diff --git a/codegen/valagirwriter.vala b/codegen/valagirwriter.vala
index 394af03..f6b999f 100644
--- a/codegen/valagirwriter.vala
+++ b/codegen/valagirwriter.vala
@@ -735,7 +735,7 @@ public class Vala.GIRWriter : CodeVisitor {
 			name = m.get_cname ();
 			var parent_prefix = parent.get_lower_case_cprefix ();
 			if (name.has_prefix (parent_prefix)) {
-				name = name.offset (parent_prefix.length);
+				name = name.substring (parent_prefix.length);
 			}
 		} else {
 			name = m.name;
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index ae88a6b..1a4a9df 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -160,7 +160,7 @@ class Vala.Compiler {
 			// 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 = sources[0].pointer_to_offset (sources[0].rchr (-1, '.'));
+				long dot = (long) ((char*) sources[0].rchr (-1, '.') - (char*) sources[0]);
 				output = Path.get_basename (sources[0].substring (0, dot));
 			}
 		}
@@ -380,7 +380,7 @@ class Vala.Compiler {
 					if (last_hyphen == null || !gir.has_suffix (".gir")) {
 						Report.error (null, "GIR file name `%s' is not well-formed, expected NAME-VERSION.gir".printf (gir));
 					} else {
-						long offset = gir.pointer_to_offset (last_hyphen);
+						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);
 						gir_version.canon ("0123456789.", '?');
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index 0ba8032..d38135c 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -631,9 +631,9 @@ public class Vala.Class : ObjectTypeSymbol {
 
 			// remove underscores in some cases to avoid conflicts of type macros
 			if (lower_case_csuffix.has_prefix ("type_")) {
-				lower_case_csuffix = "type" + lower_case_csuffix.offset ("type_".length);
+				lower_case_csuffix = "type" + lower_case_csuffix.substring ("type_".length);
 			} else if (lower_case_csuffix.has_prefix ("is_")) {
-				lower_case_csuffix = "is" + lower_case_csuffix.offset ("is_".length);
+				lower_case_csuffix = "is" + lower_case_csuffix.substring ("is_".length);
 			}
 			if (lower_case_csuffix.has_suffix ("_class")) {
 				lower_case_csuffix = lower_case_csuffix.substring (0, lower_case_csuffix.length - "_class".length) + "class";
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index cfb0a9b..30d05f3 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -421,7 +421,7 @@ public class Vala.CodeContext {
 
 			add_source_file (source_file);
 			// look for a local .deps
-			var deps_filename = "%s.deps".printf (filename.ndup (filename.length - ".vapi".length));
+			var deps_filename = "%s.deps".printf (filename.substring (0, filename.length - ".vapi".length));
 			if (!add_packages_from_file (deps_filename)) {
 				return false;
 			}
@@ -547,7 +547,7 @@ public class Vala.CodeContext {
 	}
 
 	private static bool ends_with_dir_separator (string s) {
-		return Path.is_dir_separator (s.offset (s.length - 1).get_char ());
+		return Path.is_dir_separator (s.get_char (s.length - 1));
 	}
 
 	/* ported from glibc */
@@ -569,10 +569,10 @@ public class Vala.CodeContext {
 			start = end = Path.skip_root (name);
 
 			// extract root
-			rpath = name.substring (0, name.pointer_to_offset (start));
+			rpath = name.substring (0, (int) ((char*) start - (char*) name));
 		}
 
-		long root_len = rpath.pointer_to_offset (Path.skip_root (rpath));
+		long root_len = (long) ((char*) Path.skip_root (rpath) - (char*) rpath);
 
 		for (; start.get_char () != 0; start = end) {
 			// skip sequence of multiple path-separators
diff --git a/vala/valagenieparser.vala b/vala/valagenieparser.vala
index 51ba7c1..5d04204 100644
--- a/vala/valagenieparser.vala
+++ b/vala/valagenieparser.vala
@@ -182,12 +182,12 @@ public class Vala.Genie.Parser : CodeVisitor {
 	}
 	
 	string get_current_string () {
-		return ((string) tokens[index].begin.pos).ndup ((tokens[index].end.pos - tokens[index].begin.pos));
+		return ((string) tokens[index].begin.pos).substring (0, (int) (tokens[index].end.pos - tokens[index].begin.pos));
 	}
 
 	string get_last_string () {
 		int last_index = (index + BUFFER_SIZE - 1) % BUFFER_SIZE;
-		return ((string) tokens[last_index].begin.pos).ndup ((tokens[last_index].end.pos - tokens[last_index].begin.pos));
+		return ((string) tokens[last_index].begin.pos).substring (0, (int) (tokens[last_index].end.pos - tokens[last_index].begin.pos));
 	}
 
 	SourceReference get_src (SourceLocation begin) {
@@ -832,7 +832,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 							
 							if (len > 2) {
 								string s = "\\n\"";
-								var st =  s_exp.value.ndup (len-1);
+								var st =  s_exp.value.substring (0, len-1);
 								st += s;
 								s_exp.value = st;
 							}
diff --git a/vala/valageniescanner.vala b/vala/valageniescanner.vala
index e1f945a..c853827 100644
--- a/vala/valageniescanner.vala
+++ b/vala/valageniescanner.vala
@@ -1479,7 +1479,7 @@ public class Vala.Genie.Scanner {
 			}
 			
 			if (source_reference != null) {
-				push_comment (((string) begin).ndup ((long) (current - begin)), source_reference, file_comment);
+				push_comment (((string) begin).substring (0, (long) (current - begin)), source_reference, file_comment);
 			}
 			
 		} else {
@@ -1511,7 +1511,7 @@ public class Vala.Genie.Scanner {
 			}
 
 			if (source_reference != null) {
-				string comment = ((string) begin).ndup ((long) (current - begin));
+				string comment = ((string) begin).substring (0, (long) (current - begin));
 				push_comment (comment, source_reference, file_comment);
 			}
 
@@ -1728,7 +1728,7 @@ public class Vala.Genie.Scanner {
 			return false;
 		}
 
-		string identifier = ((string) (current - len)).ndup (len);
+		string identifier = ((string) (current - len)).substring (0, len);
 		bool defined;
 		if (identifier == "true") {
 			defined = true;
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index b1b733c..78afd72 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -280,7 +280,7 @@ public class Vala.GirParser : CodeVisitor {
 		}
 
 		string get_string () {
-			return ((string) begin.pos).ndup ((end.pos - begin.pos));
+			return ((string) begin.pos).substring (0, (int) (end.pos - begin.pos));
 		}
 
 		MetadataType? parse_metadata_access () {
@@ -575,7 +575,7 @@ public class Vala.GirParser : CodeVisitor {
 		girdata_stack = new ArrayList<HashMap<string,string>> ();
 
 		// load metadata
-		string metadata_filename = "%s.metadata".printf (source_file.filename.ndup (source_file.filename.length - ".gir".length));
+		string metadata_filename = "%s.metadata".printf (source_file.filename.substring (0, source_file.filename.length - ".gir".length));
 		if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
 			var metadata_parser = new MetadataParser ();
 			var metadata_file = new SourceFile (context, source_file.file_type, metadata_filename);
@@ -1429,17 +1429,17 @@ public class Vala.GirParser : CodeVisitor {
 			common_prefix = cname;
 			while (common_prefix.length > 0 && !common_prefix.has_suffix ("_")) {
 				// FIXME: could easily be made faster
-				common_prefix = common_prefix.ndup (common_prefix.length - 1);
+				common_prefix = common_prefix.substring (0, common_prefix.length - 1);
 			}
 		} else {
 			while (!cname.has_prefix (common_prefix)) {
-				common_prefix = common_prefix.ndup (common_prefix.length - 1);
+				common_prefix = common_prefix.substring (0, common_prefix.length - 1);
 			}
 		}
 		while (common_prefix.length > 0 && (!common_prefix.has_suffix ("_") ||
-		       (cname.offset (common_prefix.length).get_char ().isdigit ()) && (cname.length - common_prefix.length) <= 1)) {
+		       (cname.get_char (common_prefix.length).isdigit ()) && (cname.length - common_prefix.length) <= 1)) {
 			// enum values may not consist solely of digits
-			common_prefix = common_prefix.ndup (common_prefix.length - 1);
+			common_prefix = common_prefix.substring (0, common_prefix.length - 1);
 		}
 	}
 
@@ -2141,7 +2141,7 @@ public class Vala.GirParser : CodeVisitor {
 		if (m.name == "new") {
 			m.name = null;
 		} else if (m.name.has_prefix ("new_")) {
-			m.name = m.name.offset ("new_".length);
+			m.name = m.name.substring ("new_".length);
 		}
 		if (cname != null) {
 			m.set_cname (cname);
@@ -2778,7 +2778,7 @@ public class Vala.GirParser : CodeVisitor {
 					if (parent != null && (parent is Struct || parent is ObjectTypeSymbol || parent is Namespace)
 						&& cname.has_prefix (parent.get_lower_case_cprefix ())) {
 						// instance method
-						var new_name = method.name.offset (parent.get_lower_case_cprefix().length-ns_cprefix.length);
+						var new_name = method.name.substring (parent.get_lower_case_cprefix().length - ns_cprefix.length);
 						if (parent.scope.lookup (new_name) == null) {
 							method.name = new_name;
 							method.get_parameters().remove_at (0);
@@ -2794,7 +2794,7 @@ public class Vala.GirParser : CodeVisitor {
 				double match = 0;
 				Symbol parent = ns;
 				find_static_method_parent (cname, ns, ref parent, ref match, 1.0/cname.length);
-				var new_name = method.name.offset (parent.get_lower_case_cprefix().length-ns_cprefix.length);
+				var new_name = method.name.substring (parent.get_lower_case_cprefix().length - ns_cprefix.length);
 				if (parent.scope.lookup (new_name) == null) {
 					method.name = new_name;
 					add_symbol_to_container (parent, method);
diff --git a/vala/valaintegerliteral.vala b/vala/valaintegerliteral.vala
index 4a5295b..1f85a21 100644
--- a/vala/valaintegerliteral.vala
+++ b/vala/valaintegerliteral.vala
@@ -69,13 +69,13 @@ public class Vala.IntegerLiteral : Literal {
 		int l = 0;
 		while (value.has_suffix ("l") || value.has_suffix ("L")) {
 			l++;
-			value = value.ndup (value.length - 1);
+			value = value.substring (0, value.length - 1);
 		}
 
 		bool u = false;
 		if (value.has_suffix ("u") || value.has_suffix ("U")) {
 			u = true;
-			value = value.ndup (value.length - 1);
+			value = value.substring (0, value.length - 1);
 		}
 		
 		int64 n = value.to_int64 ();
diff --git a/vala/valainterface.vala b/vala/valainterface.vala
index eef3f3f..72e0f64 100644
--- a/vala/valainterface.vala
+++ b/vala/valainterface.vala
@@ -321,9 +321,9 @@ public class Vala.Interface : ObjectTypeSymbol {
 
 		// remove underscores in some cases to avoid conflicts of type macros
 		if (result.has_prefix ("type_")) {
-			result = "type" + result.offset ("type_".length);
+			result = "type" + result.substring ("type_".length);
 		} else if (result.has_prefix ("is_")) {
-			result = "is" + result.offset ("is_".length);
+			result = "is" + result.substring ("is_".length);
 		}
 		if (result.has_suffix ("_class")) {
 			result = result.substring (0, result.length - "_class".length) + "class";
diff --git a/vala/valamarkupreader.vala b/vala/valamarkupreader.vala
index 843fba6..9546faa 100644
--- a/vala/valamarkupreader.vala
+++ b/vala/valamarkupreader.vala
@@ -82,7 +82,7 @@ public class Vala.MarkupReader : Object {
 		if (current == begin) {
 			// syntax error: invalid name
 		}
-		return ((string) begin).ndup (current - begin);
+		return ((string) begin).substring (0, (int) (current - begin));
 	}
 
 	public MarkupTokenType read_token (out SourceLocation token_begin, out SourceLocation token_end) {
@@ -208,27 +208,27 @@ public class Vala.MarkupReader : Object {
 			} else if (u == '&') {
 				char* next_pos = current + u.to_utf8 (null);
 				if (((string) next_pos).has_prefix ("amp;")) {
-					content.append (((string) text_begin).ndup (current - text_begin));
+					content.append (((string) text_begin).substring (0, (int) (current - text_begin)));
 					content.append_c ('&');
 					current += 5;
 					text_begin = current;
 				} else if (((string) next_pos).has_prefix ("quot;")) {
-					content.append (((string) text_begin).ndup (current - text_begin));
+					content.append (((string) text_begin).substring (0, (int) (current - text_begin)));
 					content.append_c ('"');
 					current += 6;
 					text_begin = current;
 				} else if (((string) next_pos).has_prefix ("apos;")) {
-					content.append (((string) text_begin).ndup (current - text_begin));
+					content.append (((string) text_begin).substring (0, (int) (current - text_begin)));
 					content.append_c ('\'');
 					current += 6;
 					text_begin = current;
 				} else if (((string) next_pos).has_prefix ("lt;")) {
-					content.append (((string) text_begin).ndup (current - text_begin));
+					content.append (((string) text_begin).substring (0, (int) (current - text_begin)));
 					content.append_c ('<');
 					current += 4;
 					text_begin = current;
 				} else if (((string) next_pos).has_prefix ("gt;")) {
-					content.append (((string) text_begin).ndup (current - text_begin));
+					content.append (((string) text_begin).substring (0, (int) (current - text_begin)));
 					content.append_c ('>');
 					current += 4;
 					text_begin = current;
@@ -248,7 +248,7 @@ public class Vala.MarkupReader : Object {
 		}
 
 		if (text_begin != current) {
-			content.append (((string) text_begin).ndup (current - text_begin));
+			content.append (((string) text_begin).substring (0, (int) (current - text_begin)));
 		}
 
 		column += (int) (current - last_linebreak);
diff --git a/vala/valamethod.vala b/vala/valamethod.vala
index 65d062f..8ef46b0 100644
--- a/vala/valamethod.vala
+++ b/vala/valamethod.vala
@@ -366,7 +366,7 @@ public class Vala.Method : Subroutine {
 			// avoid conflict with generated main function
 			return "_vala_main";
 		} else if (name.has_prefix ("_")) {
-			return "_%s%s".printf (parent_symbol.get_lower_case_cprefix (), name.offset (1));
+			return "_%s%s".printf (parent_symbol.get_lower_case_cprefix (), name.substring (1));
 		} else {
 			return "%s%s".printf (parent_symbol.get_lower_case_cprefix (), name);
 		}
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 926654e..b062638 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -136,12 +136,12 @@ public class Vala.Parser : CodeVisitor {
 	}
 
 	string get_current_string () {
-		return ((string) tokens[index].begin.pos).ndup ((tokens[index].end.pos - tokens[index].begin.pos));
+		return ((string) tokens[index].begin.pos).substring (0, (int) (tokens[index].end.pos - tokens[index].begin.pos));
 	}
 
 	string get_last_string () {
 		int last_index = (index + BUFFER_SIZE - 1) % BUFFER_SIZE;
-		return ((string) tokens[last_index].begin.pos).ndup ((tokens[last_index].end.pos - tokens[last_index].begin.pos));
+		return ((string) tokens[last_index].begin.pos).substring (0, (int) (tokens[last_index].end.pos - tokens[last_index].begin.pos));
 	}
 
 	SourceReference get_src (SourceLocation begin) {
diff --git a/vala/valascanner.vala b/vala/valascanner.vala
index 955b9a0..352ab68 100644
--- a/vala/valascanner.vala
+++ b/vala/valascanner.vala
@@ -1367,7 +1367,7 @@ public class Vala.Scanner {
 			return false;
 		}
 
-		string identifier = ((string) (current - len)).ndup (len);
+		string identifier = ((string) (current - len)).substring (0, len);
 		bool defined;
 		if (identifier == "true") {
 			defined = true;
@@ -1512,7 +1512,7 @@ public class Vala.Scanner {
 			}
 
 			if (source_reference != null) {
-				push_comment (((string) begin).ndup ((long) (current - begin)), source_reference, file_comment);
+				push_comment (((string) begin).substring (0, (long) (current - begin)), source_reference, file_comment);
 			}
 		} else {
 			SourceReference source_reference = null;
@@ -1544,7 +1544,7 @@ public class Vala.Scanner {
 			}
 
 			if (source_reference != null) {
-				push_comment (((string) begin).ndup ((long) (current - begin)), source_reference, file_comment);
+				push_comment (((string) begin).substring (0, (long) (current - begin)), source_reference, file_comment);
 			}
 
 			current += 2;
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 7891bc5..6f8407f 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -486,7 +486,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 		if (diag && prev_arg != null) {
 			var format_arg = prev_arg as StringLiteral;
 			if (format_arg != null) {
-				format_arg.value = "\"%s:%d: %s".printf (Path.get_basename (expr.source_reference.file.filename), expr.source_reference.first_line, format_arg.value.offset (1));
+				format_arg.value = "\"%s:%d: %s".printf (Path.get_basename (expr.source_reference.file.filename), expr.source_reference.first_line, format_arg.value.substring (1));
 			}
 		}
 
diff --git a/vala/valasourcefile.vala b/vala/valasourcefile.vala
index 931f020..8ab0e2f 100644
--- a/vala/valasourcefile.vala
+++ b/vala/valasourcefile.vala
@@ -178,7 +178,7 @@ public class Vala.SourceFile {
 			var basename = Path.get_basename (filename);
 			var subdir = filename.substring (context.basedir.length, filename.length - context.basedir.length - basename.length);
 			while (subdir[0] == '/') {
-				subdir = subdir.offset (1);
+				subdir = subdir.substring (1);
 			}
 			return subdir;
 		}
@@ -193,7 +193,7 @@ public class Vala.SourceFile {
 	}
 
 	private string get_basename () {
-		long dot = filename.pointer_to_offset (filename.rchr (-1, '.'));
+		long dot = (long) ((char*) filename.rchr (-1, '.') - (char*) filename);
 		return Path.get_basename (filename.substring (0, dot));
 	}
 
diff --git a/vala/valasymbol.vala b/vala/valasymbol.vala
index 5228e0e..e65f37a 100644
--- a/vala/valasymbol.vala
+++ b/vala/valasymbol.vala
@@ -216,7 +216,7 @@ public abstract class Vala.Symbol : CodeNode {
 		}
 
 		string parent_gir_name = parent_symbol.get_full_gir_name ();
-		string self_gir_name = gir_name.has_prefix (".") ? gir_name.offset (1) : gir_name;
+		string self_gir_name = gir_name.has_prefix (".") ? gir_name.substring (1) : gir_name;
 		if (parent_gir_name.str (".") != null) {
 			return "%s%s".printf (parent_gir_name, self_gir_name);
 		} else {
@@ -349,7 +349,7 @@ public abstract class Vala.Symbol : CodeNode {
 					/* previous character wasn't upper case or
 					 * next character isn't upper case*/
 					long len = result_builder.str.length;
-					if (len != 1 && result_builder.str.offset (len - 2).get_char () != '_') {
+					if (len != 1 && result_builder.str.get_char (len - 2) != '_') {
 						/* we're not creating 1 character words */
 						result_builder.append_c ('_');
 					}
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index c3abf8b..5643c2d 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -1007,19 +1007,31 @@ public class string {
 	public unowned string next_char ();
 	[CCode (cname = "g_utf8_get_char")]
 	static unichar utf8_get_char (char* str);
-	public unichar get_char (int index = 0) {
+	public unichar get_char (long index = 0) {
 		return utf8_get_char ((char*) this + index);
 	}
 	[CCode (cname = "g_utf8_get_char_validated")]
 	public unichar get_char_validated (ssize_t max_len = -1);
+
+	[Deprecated (replacement = "string.index_of_nth_char")]
 	[CCode (cname = "g_utf8_offset_to_pointer")]
 	public unowned string utf8_offset (long offset);
+	[Deprecated (replacement = "string.substring")]
 	public unowned string offset (long offset) {
 		return (string) ((char*) this + offset);
 	}
+	[Deprecated (replacement = "string.char_count")]
 	public long pointer_to_offset (string pos) {
 		return (long) ((char*) pos - (char*) this);
 	}
+
+	[CCode (cname = "g_utf8_offset_to_pointer")]
+	char* utf8_offset_to_pointer (long offset);
+
+	public int index_of_nth_char (long c) {
+		return (int) (this.utf8_offset_to_pointer (c) - (char*) this);
+	}
+
 	[CCode (cname = "g_utf8_prev_char")]
 	public unowned string prev_char ();
 	[Deprecated (replacement = "string.length")]
@@ -1126,9 +1138,13 @@ public class string {
 
 	[CCode (cname = "g_strdup")]
 	public string dup ();
+	[Deprecated (replacement = "string.substring")]
 	[CCode (cname = "g_strndup")]
 	public string ndup (size_t n);
 
+	[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;
 		if (offset < 0) {
@@ -1141,8 +1157,7 @@ public class string {
 			len = string_length - offset;
 		}
 		GLib.return_val_if_fail (offset + len <= string_length, null);
-		unowned string start = this.offset (offset);
-		return start.ndup (((char*) start.offset (len)) - ((char*) start));
+		return strndup ((char*) this + offset, len);
 	}
 
 	public string slice (long start, long end) {
@@ -1156,8 +1171,7 @@ public class string {
 		GLib.return_val_if_fail (start >= 0 && start <= string_length, null);
 		GLib.return_val_if_fail (end >= 0 && end <= string_length, null);
 		GLib.return_val_if_fail (start <= end, null);
-		unowned string start_string = this.offset (start);
-		return start_string.ndup (((char*) start_string.offset (end - start)) - ((char*) start_string));
+		return strndup ((char*) this + start, end - start);
 	}
 
 	public string splice (long start, long end, string? str = null) {
@@ -1172,8 +1186,6 @@ public class string {
 		GLib.return_val_if_fail (end >= 0 && end <= string_length, null);
 		GLib.return_val_if_fail (start <= end, null);
 
-		unowned string start_string = this.offset (start);
-		unowned string end_string = start_string.offset (end - start);
 		size_t str_size;
 		if (str == null) {
 			str_size = 0;
@@ -1181,17 +1193,17 @@ public class string {
 			str_size = str.length;
 		}
 
-		string* result = GLib.malloc0 (this.length - ((char*) end_string - (char*) start_string) + str_size + 1);
+		string* result = GLib.malloc0 (this.length - (end - start) + str_size + 1);
 
 		char* dest = (char*) result;
 
-		GLib.Memory.copy (dest, this, (char*) start_string - (char*) this);
-		dest += (char*) start_string - (char*) this;
+		GLib.Memory.copy (dest, this, start);
+		dest += start;
 
 		GLib.Memory.copy (dest, str, str_size);
 		dest += str_size;
 
-		GLib.Memory.copy (dest, end_string, end_string.length);
+		GLib.Memory.copy (dest, (char*) this + end, string_length - end);
 
 		return (owned) result;
 	}
diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala
index 46d0747..ccc6e4f 100644
--- a/vapigen/valagidlparser.vala
+++ b/vapigen/valagidlparser.vala
@@ -100,7 +100,7 @@ public class Vala.GIdlParser : CodeVisitor {
 	}
 	
 	private void parse_file (SourceFile source_file) {
-		string metadata_filename = "%s.metadata".printf (source_file.filename.ndup (source_file.filename.length - ".gi".length));
+		string metadata_filename = "%s.metadata".printf (source_file.filename.substring (0, source_file.filename.length - ".gi".length));
 
 		current_source_file = source_file;
 
@@ -164,9 +164,9 @@ public class Vala.GIdlParser : CodeVisitor {
 		}
 
 		if (type_name.has_prefix (container.name)) {
-			return type_name.offset (container.name.length);
+			return type_name.substring (container.name.length);
 		} else if (container.name == "GLib" && type_name.has_prefix ("G")) {
-			return type_name.offset (1);
+			return type_name.substring (1);
 		} else  {
 			string best_match = null;
 			if (container is Namespace) {
@@ -181,7 +181,7 @@ public class Vala.GIdlParser : CodeVisitor {
                }
 
 			if (best_match != null) {
-				return type_name.offset (best_match.length);;
+				return type_name.substring (best_match.length);;
 			}
 		}
 
@@ -191,7 +191,7 @@ public class Vala.GIdlParser : CodeVisitor {
 	private string fix_const_name (string const_name, Symbol container) {
 		var pref = container.get_lower_case_cprefix ().up ();
 		if (const_name.has_prefix (pref)) {
-			return const_name.offset (pref.length);
+			return const_name.substring (pref.length);
 		}
 		return const_name;
 	}
@@ -1167,17 +1167,17 @@ public class Vala.GIdlParser : CodeVisitor {
 				common_prefix = value.name;
 				while (common_prefix.length > 0 && !common_prefix.has_suffix ("_")) {
 					// FIXME: could easily be made faster
-					common_prefix = common_prefix.ndup (common_prefix.length - 1);
+					common_prefix = common_prefix.substring (0, common_prefix.length - 1);
 				}
 			} else {
 				while (!value.name.has_prefix (common_prefix)) {
-					common_prefix = common_prefix.ndup (common_prefix.length - 1);
+					common_prefix = common_prefix.substring (0, common_prefix.length - 1);
 				}
 			}
 			while (common_prefix.length > 0 && (!common_prefix.has_suffix ("_") ||
-			       (value.name.offset (common_prefix.length).get_char ().isdigit ()) && (value.name.length - common_prefix.length) <= 1)) {
+			       (value.name.get_char (common_prefix.length).isdigit ()) && (value.name.length - common_prefix.length) <= 1)) {
 				// enum values may not consist solely of digits
-				common_prefix = common_prefix.ndup (common_prefix.length - 1);
+				common_prefix = common_prefix.substring (0, common_prefix.length - 1);
 			}
 		}
 
@@ -1239,7 +1239,7 @@ public class Vala.GIdlParser : CodeVisitor {
 			}
 
 			if (!is_hidden) {
-				var ev = new EnumValue (value2.name.offset (common_prefix.length), null);
+				var ev = new EnumValue (value2.name.substring (common_prefix.length), null);
 				en.add_value (ev);
 			}
 		}
@@ -1638,7 +1638,7 @@ public class Vala.GIdlParser : CodeVisitor {
 			}
 			
 			if (n.has_prefix ("const-")) {
-				n = n.offset ("const-".length);
+				n = n.substring ("const-".length);
 			}
 
 			if (type_node.is_pointer &&
@@ -1749,7 +1749,7 @@ public class Vala.GIdlParser : CodeVisitor {
 				var nv = attr.split ("=", 2);
 
 				if (nv[0] == "cprefix") {
-					type.unresolved_symbol = new UnresolvedSymbol (null, n.offset (eval (nv[1]).length));
+					type.unresolved_symbol = new UnresolvedSymbol (null, n.substring (eval (nv[1]).length));
 				} else if (nv[0] == "name") {
 					type.unresolved_symbol = new UnresolvedSymbol (null, eval (nv[1]));
 				} else if (nv[0] == "namespace") {
@@ -1771,9 +1771,9 @@ public class Vala.GIdlParser : CodeVisitor {
 		}
 
 		if (n.has_prefix (current_namespace.name)) {
-			type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, current_namespace.name), n.offset (current_namespace.name.length));
+			type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, current_namespace.name), n.substring (current_namespace.name.length));
 		} else if (n.has_prefix ("G")) {
-			type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), n.offset (1));
+			type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), n.substring (1));
 		} else {
 			var name_parts = n.split (".", 2);
 			if (name_parts[1] == null) {
@@ -1941,7 +1941,7 @@ public class Vala.GIdlParser : CodeVisitor {
 			if (m.name == "new") {
 				m.name = null;
 			} else if (m.name.has_prefix ("new_")) {
-				m.name = m.name.offset ("new_".length);
+				m.name = m.name.substring ("new_".length);
 			}
 			// For classes, check whether a creation method return type equals to the
 			// type of the class created. If the types do not match (e.g. in most
@@ -2064,7 +2064,7 @@ public class Vala.GIdlParser : CodeVisitor {
 					var prefix = container.get_lower_case_cprefix ();
 					if (symbol.has_prefix (prefix)) {
 						m.set_cname (m.name);
-						m.name = symbol.offset (prefix.length);
+						m.name = symbol.substring (prefix.length);
 					}
 				}
 			}
@@ -2228,7 +2228,7 @@ public class Vala.GIdlParser : CodeVisitor {
 							p.initializer = new StringLiteral ("\"\"", param_type.source_reference);
 						} else {
 							unowned string endptr;
-							unowned string val_end = val.offset (val.length);
+							char* val_end = (char*) val + val.length;
 
 							val.to_long (out endptr);
 							if ((long)endptr == (long)val_end) {
@@ -2668,7 +2668,7 @@ public class Vala.GIdlParser : CodeVisitor {
 				}
 			}
 
-			remaining = remaining.offset (1);
+			remaining = (string) ((char*) remaining + remaining.index_of_nth_char (1));
 		}
 
 		if (attr.len > 0) {
@@ -2685,7 +2685,7 @@ public class Vala.GIdlParser : CodeVisitor {
 	}
 	
 	private string eval (string s) {
-		return ((s.length >= 2) && s.has_prefix ("\"") && s.has_suffix ("\"")) ? s.offset (1).ndup (s.length - 2) : s;
+		return ((s.length >= 2) && s.has_prefix ("\"") && s.has_suffix ("\"")) ? s.substring (1, s.length - 2) : s;
 	}
 
 	private Signal? parse_signal (IdlNodeSignal sig_node) {
diff --git a/vapigen/valavapigen.vala b/vapigen/valavapigen.vala
index 2968b0a..5345b63 100644
--- a/vapigen/valavapigen.vala
+++ b/vapigen/valavapigen.vala
@@ -166,7 +166,7 @@ class Vala.VAPIGen : Object {
 			if (file.filename in sources) {
 				file.file_type = SourceFileType.SOURCE;
 			} else if (file.filename.has_suffix (".metadata")) {
-				string gir_filename = "%s.gir".printf (file.filename.ndup (file.filename.length - ".metadata".length));
+				string gir_filename = "%s.gir".printf (file.filename.substring (0, file.filename.length - ".metadata".length));
 				if (gir_filename in sources) {
 					file.file_type = SourceFileType.SOURCE;
 				}



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