From: handa Date: Tue, 6 Jan 2009 15:35:00 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=b3bc0744a8c44e8b49609757e9765c78a39481bb;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/MText.cs b/MText.cs index ecc24eb..cff99d4 100644 --- a/MText.cs +++ b/MText.cs @@ -47,9 +47,9 @@ namespace M17N.Core private int cache_pos; private int cache_idx; private MTextPlist plist; + private bool unmodifiable; private static UTF8Encoding utf8 = new UTF8Encoding (); - private static UTF32Encoding utf32 = new UTF32Encoding (true, false); private static int count_chars (String str) { @@ -111,74 +111,77 @@ namespace M17N.Core return (c >= 0xDC00 && c < 0xE000); } - private int inc_idx (int i) + private static int inc_idx (StringBuilder sb, int i) { return (i + (surrogate_high_p (sb[i]) ? 2 : 1)); } - private int dec_idx (int i) + private static int dec_idx (StringBuilder sb, int i) { return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1)); } - private int pos_to_idx (int pos) + private static int pos_to_idx (MText mt, int pos) { + if (pos == mt.cache_pos) + return mt.cache_idx; + int p, i; bool forward; - if (pos < cache_pos) + if (pos < mt.cache_pos) { - if (cache_pos == cache_idx) - return cache_idx; - if (pos < cache_pos - pos) + if (mt.cache_pos == mt.cache_idx) + return mt.cache_idx; + if (pos < mt.cache_pos - pos) { p = i = 0; forward = true; } else { - p = cache_pos; i = cache_idx; + p = mt.cache_pos; i = mt.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) + if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx) + return (mt.cache_idx + pos - mt.cache_pos); + if (pos - mt.cache_pos < mt.nchars - pos) { - p = cache_pos; i = cache_idx; + p = mt.cache_pos; i = mt.cache_idx; forward = true; } else { - p = nchars; i = sb.Length; + p = mt.nchars; i = mt.sb.Length; forward = false; } } if (forward) - for (; p < pos; i = inc_idx (i), p++); + for (; p < pos; i = inc_idx (mt.sb, i), p++); else - for (; p > pos; i = dec_idx (i), p--); - cache_pos = p; - cache_idx = i; + for (; p > pos; i = dec_idx (mt.sb, i), p--); + mt.cache_pos = p; + mt.cache_idx = i; return i; } - private readonly bool readonlyp; - private void insert (int pos, MText mt2, int from, int to) { - if (pos != cache_pos) - pos = pos_to_idx (pos); + int pos_idx = pos_to_idx (this, pos); + int from_idx = pos_to_idx (mt2, from); + int to_idx = pos_to_idx (mt2, to); + sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx)); + nchars += to - from; } public int this[int i] { set { - if (i != cache_pos) - i = pos_to_idx (i); + i = pos_to_idx (this, i); if (value < 0x10000) sb[i] = (char) value; else @@ -193,8 +196,7 @@ namespace M17N.Core } } get { - if (i != cache_pos) - i = pos_to_idx (i); + i = pos_to_idx (this, i); return (surrogate_high_p (sb[i]) ? i = ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000 : sb[i]); @@ -217,6 +219,15 @@ namespace M17N.Core insert (pos, mt, from, to); return this; } + + public MText del (int from, int to) + { + sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from)); + nchars -= to - from; + return this; + } + + } public class MTextProperty