From: handa Date: Fri, 16 Jan 2009 00:02:03 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22add524be98fb1fac4e7be0d68ca8ca7f7d3ca2;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/M17N.cs b/M17N.cs index 4ded12d..65eabfb 100644 --- a/M17N.cs +++ b/M17N.cs @@ -34,6 +34,7 @@ public class Test mt = new MText ("abc"); Console.WriteLine (mt); Console.WriteLine (mt + new MText ("def")); + mt += new MText ("ghi"); Console.WriteLine (mt); } diff --git a/MText.cs b/MText.cs index 261ffaa..185ec08 100644 --- a/MText.cs +++ b/MText.cs @@ -8,14 +8,14 @@ namespace M17N.Core { #if false public enum MTextFormat - { - MTEXT_FORMAT_US_ASCII, - MTEXT_FORMAT_UTF_8, - MTEXT_FORMAT_UTF_16BE, - MTEXT_FORMAT_UTF_16LE, - MTEXT_FORMAT_UTF_32BE, - MTEXT_FORMAT_UTF_32LE, - } + { + MTEXT_FORMAT_US_ASCII, + MTEXT_FORMAT_UTF_8, + MTEXT_FORMAT_UTF_16BE, + MTEXT_FORMAT_UTF_16LE, + MTEXT_FORMAT_UTF_32BE, + MTEXT_FORMAT_UTF_32LE, + } #endif public class MTextProperty @@ -42,7 +42,7 @@ namespace M17N.Core } } - public class MText + public class MText : IEnumerable, IComparable, IEquatable { #if false public enum MTextFormat format; @@ -61,8 +61,9 @@ namespace M17N.Core { int len = str.Length, n = 0; - for (int i = 0; i < len; i++) - n += surrogate_high_p (str[i]) ? 2 : 1; + for (int i = 0; i < len; i++, n++) + if (surrogate_high_p (str[i])) + i++; return n; } @@ -70,46 +71,59 @@ namespace M17N.Core { int len = str.Length, n = 0; - for (int i = 0; i < len; i++) - n += surrogate_high_p (str[i]) ? 2 : 1; + for (int i = 0; i < len; i++, n++) + if (surrogate_high_p (str[i])) + i++; return n; } public MText () - { - sb = new StringBuilder (); - } + { + sb = new StringBuilder (); + } public MText (byte[] str) - { - sb = new StringBuilder (utf8.GetString (str)); - nchars = count_chars (sb); - } + { + sb = new StringBuilder (utf8.GetString (str)); + nchars = count_chars (sb); + } public MText (String str) - { - sb = new StringBuilder (str); - nchars = count_chars (str); - } + { + sb = new StringBuilder (str); + nchars = count_chars (str); + } public MText (StringBuilder str) - { - sb = str; - nchars = count_chars (str); - } + { + sb = str; + nchars = count_chars (str); + } public static MText operator+ (MText mt1, MText mt2) { - MText mt = new MText (mt1.sb.ToString); + MText mt = new MText (); + mt.sb.Append (mt1.sb); mt.sb.Append (mt2.sb); mt.nchars = mt1.nchars + mt2.nchars; return mt; } public bool ReadOnly { get { return read_only; } } + public int Length { get { return nchars; } } + + public int CompareTo (MText other) + { + return this.sb.ToString ().CompareTo (other.sb.ToString ()); + } - public override string ToString () + public bool Equals (MText other) + { + return this.sb.Equals (other.sb); + } + + public override String ToString () { return sb.ToString (); } @@ -251,6 +265,11 @@ namespace M17N.Core root_interval.Push (from, to, prop); } + public IEnumerator GetEnumerator() + { + return new MTextEnum (this); + } + private class MInterval { // Start and end positions of this interval and its children. @@ -455,5 +474,36 @@ namespace M17N.Core stack.Push (prop); } } + + private class MTextEnum : IEnumerator + { + private MText mt; + private int pos = -1; + + public MTextEnum (MText mt) + { + this.mt = mt; + } + + public bool MoveNext () + { + pos++; + return (pos < mt.nchars); + } + + public void Reset () + { + pos = -1; + } + + public object Current + { + get { + //if (pos < 0 || pos >= mt.nchars) + //throw new InvalidOperationException (); + return mt[pos]; + } + } + } } } diff --git a/temp.cs b/temp.cs index d327f3a..2dc7619 100644 --- a/temp.cs +++ b/temp.cs @@ -9,20 +9,12 @@ public class Test MText mt = new MText ("a𝄀あc"); MText mt2; - Console.WriteLine (mt[0]); - Console.WriteLine (mt[1]); - Console.WriteLine (mt[2]); - Console.WriteLine (mt[3]); + Console.WriteLine ("Length={0}", mt.Length); + foreach (int c in mt) + Console.WriteLine ("U+{0:X4}", c); Console.WriteLine (mt + new MText ("漢字")); mt2 = mt.dup (); mt[1] = 'b'; Console.WriteLine (mt2); - - List list = new List(); - - list.Add (1); - list.Add (2); - for (int i = 0; i < 2; i++) - Console.WriteLine (list[i]); } }