2007-06-03 Kamil Skalski <nazgul@nemerle.org>
authorMarek Habersack <grendel@twistedcode.net>
Tue, 5 Jun 2007 10:07:19 +0000 (10:07 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Tue, 5 Jun 2007 10:07:19 +0000 (10:07 -0000)
        * SQLiteConvert.cs: Port function for converting native UTF8 string
      into managed string from the old SqliteClient implementation.
      Use it instead of PtrToStrAuto, which fails on Windows.

svn path=/trunk/mcs/; revision=78615

mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/ChangeLog
mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteConvert.cs

index 4b275d3755b748cfe1b80e0f53eae667591071c2..2474055fa535918863c465697077a58321585c7b 100644 (file)
@@ -1,3 +1,9 @@
+2007-06-03  Kamil Skalski  <nazgul@nemerle.org>
+
+       * SQLiteConvert.cs: Port function for converting native UTF8 string\r
+      into managed string from the old SqliteClient implementation.\r
+      Use it instead of PtrToStrAuto, which fails on Windows.
+\r
 2007-02-20  Marek Habersack  <grendello@gmail.com>
 
        * SQLite3.cs: handle sqlite3_prepare_v2 absent from the library,
index 2488166c17fc0bd17a42328f048cc9f04637dd00..92d2fee1707705a2bcdb756dd57a10177f202ada 100644 (file)
@@ -235,7 +235,24 @@ namespace Mono.Data.Sqlite
     /// <returns>A string containing the translated character(s)</returns>
     public virtual string UTF8ToString(IntPtr nativestring)
     {
-           return Marshal.PtrToStringAuto (nativestring);
+      if (nativestring == IntPtr.Zero)
+        return null;
+    
+      // This assumes a single byte terminates the string.
+
+      int len = 0;
+      while (Marshal.ReadByte (nativestring, len) != 0)
+        checked {++len;}
+\r
+      unsafe { \r
+        string s = new string ((sbyte*) nativestring, 0, len, _utf8);
+        len = s.Length;
+        while (len > 0 && s [len-1] == 0)
+          --len;
+        if (len == s.Length) 
+          return s;
+        return s.Substring (0, len);\r
+      }
     }