From: handa Date: Mon, 5 Jan 2009 15:29:15 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=4b35a76d029d447f0b0ebcee26ea42316773662d;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/MText.cs b/MText.cs index 477d7e6..cd9b9db 100644 --- a/MText.cs +++ b/MText.cs @@ -48,6 +48,7 @@ namespace M17N.Core private MTextPlist plist; private static UTF8Encoding utf8 = new UTF8Encoding (); + private static UTF32Encoding utf32 = new UTF8Encoding (); public MText () { @@ -58,24 +59,84 @@ namespace M17N.Core public MText (byte[] str) { - cache_pos = cache_idx = 0; - plist = null; sb = new StringBuilder (utf8.GetString (str)); } public MText (String str) { - cache_pos = cache_idx = 0; - plist = null; 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 { this.sb[i] = (char) value; } + 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 {