*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index 090d58b..cd9b9db 100644 (file)
--- a/MText.cs
+++ b/MText.cs
@@ -1,8 +1,10 @@
 using System;
+using System.Text;
 using M17N.Core;
 
 namespace M17N.Core
 {
+#if false
   public enum MTextFormat
   {
     MTEXT_FORMAT_US_ASCII,
@@ -12,9 +14,14 @@ namespace M17N.Core
     MTEXT_FORMAT_UTF_32BE,
     MTEXT_FORMAT_UTF_32LE,
   }
+#endif
 
   public class MText
   {
+#if false
+    public enum MTextFormat format;
+#endif
+
     private class MTextPlist : MPlist
     {
       public class MInterval
@@ -31,22 +38,105 @@ namespace M17N.Core
       {
        head = tail = new MInterval ();
        head.start = 0;
-       head.end = mt.nchars;
+       head.end = mt.sb.Length;
       }
     }
 
-    private string str;
-    private int nchars;
-    private int nunits;
+    private StringBuilder sb;
     private int cache_pos;
     private int cache_idx;
     private MTextPlist plist;
 
-    public MText (byte str, MTextFormat format)
+    private static UTF8Encoding utf8 = new UTF8Encoding ();
+    private static UTF32Encoding utf32 = new UTF8Encoding ();
+
+    public MText ()
     {
-      
+      cache_pos = cache_idx = 0;
+      plist = null;
+      sb = new StringBuilder ();
+    }
+
+    public MText (byte[] str)
+    {
+      sb = new StringBuilder (utf8.GetString (str));
+    }
+
+    public MText (String str)
+    {
+      sb = new StringBuilder (str);
+    }
+
+    private static int inc_idx (int i)
+    {
+      return ((sb[i] >= 0xD800 && sb[i] < 0xDC00)
+             ? i + 2 : i + 1);
+    }
+
+    private static int dec_idx (int i)
+    {
+      return ((sb[i - 1] >= 0xDC00 && sb[i - 1] < 0xE000)
+             ? i - 2 : i - 1);
+    }
+
+    private static int pos_to_idx (int pos)
+    {
+      int p, i;
+      bool forward;
+
+      if (pos < cache_pos)
+       {
+         if (cache_pos == cache_idx)
+           return cache_idx;
+         if (pos < cache_pos - pos)
+           {
+             p = i = 0;
+             forward = true;
+           }
+         else
+           {
+             p = cache_pos; i = cache_idx;
+             forward = false;
+           }
+       }
+      else
+       {
+         if (nchars - cache_pos == sb.Length - cache_idx)
+           return (cache_idx + pos - cache_pos);
+         if (pos - cache_pos < nchars - pos)
+           {
+             p = char_pos; i = char_idx;
+             forward = true;
+           }
+         else
+           {
+             p = nchars; i = sb.Length;
+             forward = false;
+           }
+       }
+      if (forward)
+       for (; p < pos; i = inc_idx (i), p++);
+      else
+       for (; p > pos; i = dec_idx (i), p--);
+      cache_pos = p;
+      cache_idx = i;
+      return i;
+    }
+
+    public int this[int i]
+    {
+      set {
+       if (i != cache_pos)
+         i = pos_to_idx (i);
+       if (value < 0x10000)
+         this.sb[i] = (char) value;
+       else
+         {
+           
+         }
+      }
+      get { return this.sb[i]; }
     }
-  }
 
   public class MTextProperty
   {