From: handa Date: Fri, 1 May 2009 07:02:12 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=fd26e0940f7ab83cfdda0d244bbb1d1bcef52f4b;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/MDatabase.cs b/MDatabase.cs index 798599f..5557257 100644 --- a/MDatabase.cs +++ b/MDatabase.cs @@ -6,37 +6,35 @@ using M17N.Core; namespace M17N.Core { - public delegate object MDatabaseLoader (MDatabaseTag tag, - object extra_info); - - public struct MDatabaseTag + public class MDatabase { - public MSymbol Tag0, Tag1, Tag2, Tag3; + public struct Tag + { + public MSymbol Tag0, Tag1, Tag2, Tag3; - public MDatabaseTag (MSymbol tag0) - { - Tag0 = tag0; Tag1 = Tag2 = Tag3 = MSymbol.nil; - } + public Tag (MSymbol tag0) + { + Tag0 = tag0; Tag1 = Tag2 = Tag3 = MSymbol.nil; + } - public MDatabaseTag (MSymbol tag0, MSymbol tag1) - { - Tag0 = tag0; Tag1 = tag1; Tag2 = Tag3 = MSymbol.nil; - } + public Tag (MSymbol tag0, MSymbol tag1) + { + Tag0 = tag0; Tag1 = tag1; Tag2 = Tag3 = MSymbol.nil; + } - public MDatabaseTag (MSymbol tag0, MSymbol tag1, MSymbol tag2) - { - Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = MSymbol.nil; - } + public Tag (MSymbol tag0, MSymbol tag1, MSymbol tag2) + { + Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = MSymbol.nil; + } - public MDatabaseTag (MSymbol tag0, MSymbol tag1, - MSymbol tag2, MSymbol tag3) - { - Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = tag3; - } - } + public Tag (MSymbol tag0, MSymbol tag1, MSymbol tag2, MSymbol tag3) + { + Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = tag3; + } + } + + public delegate object Loader (Tag tag, object extra_info); - public class MDatabase - { private class MDatabaseDir { private const string ListFileName = "mdb.dir"; @@ -133,13 +131,13 @@ namespace M17N.Core internal MPlist Props; } - private static Dictionary DBDict - = new Dictionary (); + private static Dictionary DBDict + = new Dictionary (); private static MDatabaseDir[] DBDirs = new MDatabaseDir[3]; private const string SystemDirectory = "/usr/share/m17n"; - private readonly MSymbol Mversion = MSymbol.Of ("version"); + private static readonly MSymbol Mversion = MSymbol.Of ("version"); /// Type of database private enum MDBType @@ -152,10 +150,10 @@ namespace M17N.Core /// of the same kind. MULTIPLE, /// The database was defined explicitely by MDatabase.Define - /// without a special loader. + /// to use the normal loader. EXPLICIT, /// The database was defined explicitely by MDatabase.Define - /// with a special loader. + /// to use a special loader. UNKNOWN, }; @@ -177,8 +175,8 @@ namespace M17N.Core INVALID, }; - public readonly MDatabaseTag Tag; - private MDatabaseLoader Loader; + public readonly Tag tag; + private Loader loader; private object ExtraInfo; private MDBType DBType; private MDBStatus DBStatus; @@ -187,17 +185,32 @@ namespace M17N.Core public static string ApplicationDirectory; - private MDatabase (MDatabaseTag tag, MDatabaseLoader loader, - object extra_info) + static MDatabase () { - Tag = tag; - Loader = loader; + string share_dir = (Environment.GetFolderPath + (Environment.SpecialFolder.CommonApplicationData)); + string usr_dir = (Environment.GetFolderPath + (Environment.SpecialFolder.ApplicationData)); + + try { + DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, ".m17n.d")); + } catch (ArgumentException) { + DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, "_m17n_d")); + } + DBDirs[1] = null; + DBDirs[2] = new MDatabaseDir (Path.Combine (share_dir, "m17n")); + } + + private MDatabase (Tag tag, Loader loader, object extra_info) + { + this.tag = tag; + this.loader = loader; ExtraInfo = extra_info; } - private MDatabase (MDatabaseTag tag, string filename) + private MDatabase (Tag tag, string filename) { - Tag = tag; + this.tag = tag; Info = new MDatabaseInfo (); Info.Filename = new MText (filename); } @@ -211,7 +224,7 @@ namespace M17N.Core tags[i] = plist.Symbol; while (i < 4) tags[i++] = MSymbol.nil; - Tag = new MDatabaseTag (tags[0], tags[1], tags[2], tags[3]); + tag = new Tag (tags[0], tags[1], tags[2], tags[3]); if (plist.IsMText) { Info.Filename = plist.Text; @@ -276,14 +289,13 @@ namespace M17N.Core return ((major << 16) | (minor << 8) | release); } - public static MDatabase Define (MDatabaseTag tag, MDatabaseLoader loader, - object extra_info) + public static MDatabase Define (Tag tag, Loader loader, object extra_info) { MDatabase db = MDatabase.Find (tag); if (db != null) { - db.Loader = loader; + db.loader = loader; db.ExtraInfo = extra_info; db.DBType = MDBType.EXPLICIT; db.DBStatus = MDBStatus.OUTDATED; @@ -293,13 +305,13 @@ namespace M17N.Core return new MDatabase (tag, loader, extra_info); } - public static MDatabase Define (MDatabaseTag tag, string filename) + public static MDatabase Define (Tag tag, string filename) { MDatabase db = MDatabase.Find (tag); if (db != null) { - db.Loader = null; + db.loader = null; db.DBType = MDBType.EXPLICIT; db.DBStatus = MDBStatus.OUTDATED; db.Info = new MDatabaseInfo (); @@ -310,23 +322,16 @@ namespace M17N.Core return new MDatabase (tag, filename); } - static MDatabase () + private void update () { - string share_dir = (Environment.GetFolderPath - (Environment.SpecialFolder.CommonApplicationData)); - string usr_dir = (Environment.GetFolderPath - (Environment.SpecialFolder.ApplicationData)); - - try { - DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, ".m17n.d")); - } catch (ArgumentException) { - DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, "_m17n_d")); - } - DBDirs[1] = null; - DBDirs[2] = new MDatabaseDir (Path.Combine (share_dir, "m17n")); + for (int i = 0; i < 3; i++) + { + if (DBDirs[0].StatusChanged) + break; + } } - public static MDatabase Find (MDatabaseTag tag) + public static MDatabase Find (Tag tag) { MDatabase db; @@ -335,13 +340,13 @@ namespace M17N.Core public object Load () { - return (Loader != null ? Loader (Tag, ExtraInfo) + return (loader != null ? loader (tag, ExtraInfo) : load (MSymbol.nil, MSymbol.nil)); } public object Load (MSymbol key, MSymbol stop) { - if (Loader != null) + if (loader != null) return null; return load (key, stop); } @@ -354,7 +359,5 @@ namespace M17N.Core return null; } - } - } \ No newline at end of file diff --git a/MText.cs b/MText.cs index 6f529a8..f7b99dc 100644 --- a/MText.cs +++ b/MText.cs @@ -55,7 +55,7 @@ namespace M17N.Core public MProperty (MSymbol key, object val) { if (key.flags == null) - key.flags = MProperty.Flags.None; + key.flags = Flags.None; this.key = key; this.val = val; } @@ -66,6 +66,11 @@ namespace M17N.Core this.val = val; } + public static bool HasFlags (MSymbol key, Flags flags) + { + return ((key.flags & flags) == flags); + } + public override string ToString () { return key.ToString () + ":" + val; @@ -137,27 +142,27 @@ namespace M17N.Core public static MText operator+ (object obj, MText mt) { if (obj is string) - return new MText ((string) obj) + mt; + { + MText mtnew = new MText ((string) obj); + return mtnew.Ins (mtnew.Length, mt); + } throw new Exception ("Unknown object type: " + obj.GetType()); } - public static MText operator+ (MText mt1, MText mt2) + public static MText operator+ (MText mt, object obj) { - MText mt = new MText (); - - mt.sb.Append (mt1.sb); - mt.sb.Append (mt2.sb); - mt.nchars = mt1.nchars + mt2.nchars; - return mt; + if (obj is string) + return mt + new MText ((string) obj); + if (obj is int) + return mt.Dup ().Ins (mt.Length, (int) obj); + if (obj is char) + return mt.Dup ().Ins (mt.Length, (int) ((char) obj)); + throw new Exception ("Unknown object type: " + obj.GetType()); } - public static MText operator+ (string str, MText mt) + public static MText operator+ (MText mt1, MText mt2) { - MText mtnew = new MText (str); - - mtnew.sb.Append (mt.sb); - mtnew.nchars += mt.nchars; - return mtnew; + return mt1.Dup ().Ins (mt1.Length, mt2); } // Public properties @@ -630,20 +635,31 @@ namespace M17N.Core private bool isRearSticky { - get { return ((Key.flags & MProperty.Flags.RearSticky) - != MProperty.Flags.None); } + get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; } } private bool isFrontSticky { - get { return ((Key.flags & MProperty.Flags.FrontSticky) - != MProperty.Flags.None); } + get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; } } public bool isSensitive { - get { return ((Key.flags & MProperty.Flags.Sensitive) - != MProperty.Flags.None); } + get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; } + } + + public bool isFrontSensitive + { + get { return MProperty.HasFlags (Key, + (MProperty.Flags.Sensitive + | MProperty.Flags.FrontSticky)); } + } + + public bool isRearSensitive + { + get { return MProperty.HasFlags (Key, + (MProperty.Flags.Sensitive + | MProperty.Flags.RearSticky)); } } private void update_from_to () @@ -1054,119 +1070,137 @@ namespace M17N.Core Left.Insert (pos, interval, start, end); else if (pos == From) { - if (Left != null) - { - MInterval prev = Prev; + MInterval prev = Left != null ? Prev : null; - if (prev.isSensitive && prev.isRearSticky) - prev.Stack.Clear (); - start += prev.graft_forward (interval, start, end); - } - if (isSensitive && isFrontSticky) + if (isFrontSensitive) Stack.Clear (); - if (start < end) + if (prev != null && isRearSensitive) + prev.Stack.Clear (); + if (prev != null && isRearSticky && ! prev.Stack.IsEmpty) { - end -= graft_backward (interval, start, end); - if (start < end) - { - if (interval != null) - interval = interval.Copy (mtext, start, end); - else - interval = new MInterval (Key, mtext, end - start, null); + prev.enlarge (end - start); + return; + } + if (isFrontSticky && ! Stack.IsEmpty) + { + enlarge (end - start); + return; + } + if (prev != null) + { + start += prev.graft_forward (interval, start, end); + if (start == end) + return; + } + if ((end -= graft_backward (interval, start, end)) == start) + return; - MInterval i; - if (Left != null) - { - // .-this-. ==> .-this-. - // left-. .-left-. - // child child-. - // interval - i = Left.RightMost; - i.Right = interval; - } - else - { - Left = interval; - i = this; - } - interval.Parent = i; - for (; i != null; i = i.Parent) - i.Length += interval.Length; - } + if (interval != null) + interval = interval.Copy (mtext, start, end); + else + interval = new MInterval (Key, mtext, end - start, null); + + MInterval i; + if (Left != null) + { + // .-this-. ==> .-this-. + // left-. .-left-. + // child child-. + // interval + i = Left.RightMost; + i.Right = interval; } + else + { + Left = interval; + i = this; + } + interval.Parent = i; + for (; i != null; i = i.Parent) + i.Length += interval.Length; } else if (pos < To) { if (isSensitive) Stack.Clear (); - + else if (! Stack.IsEmpty && (isFrontSticky || isRearSticky)) + { + enlarge (end - start); + return; + } int len = graft_forward (interval, start, end); start += len; + if (start == end) + return; + if ((end -= graft_backward (interval, start, end)) == start) + return; pos += len; - if (start < end) + if (interval != null) + interval = interval.Copy (mtext, start, end); + else + interval = new MInterval (Key, mtext, end - start, null); + + divide_right (pos); + Right.Left = interval; + interval.Parent = Right; + for (MInterval i = Right; i != null; i = i.Parent) + i.Length += interval.Length; + } + else if (pos == To) + { + MInterval next = Right != null ? Next : null; + + if (isRearSensitive) + Stack.Clear (); + if (next != null && isFrontSensitive) + next.Stack.Clear (); + if (isRearSticky && ! Stack.IsEmpty) + { + enlarge (end - start); + return; + } + if (next != null) { - end -= graft_backward (interval, start, end); - if (start < end) + if (isFrontSticky && ! next.Stack.IsEmpty) { - if (interval != null) - interval = interval.Copy (mtext, start, end); - else - interval = new MInterval (Key, mtext, end - start, null); - - divide_right (pos); - Right.Left = interval; - interval.Parent = Right; - for (MInterval i = Right; i != null; i = i.Parent) - i.Length += interval.Length; + next.enlarge (end - start); + return; } + end -= next.graft_backward (interval, start, end); + if (start == end) + return; } - } - else if (pos == To) - { + + if ((start += graft_forward (interval, start, end)) == end) + return; + + if (interval != null) + interval = interval.Copy (mtext, start, end); + else + interval = new MInterval (Key, mtext, end - start, null); + + MInterval i; if (Right != null) { - MInterval next = Next; + // .-this-. ==> .-this-. + // .-right .-right + // child .-child + // interval - if (next.isSensitive && next.isFrontSticky) - next.Stack.Clear (); - end -= next.graft_backward (interval, start, end); + i = Right.LeftMost; + i.Left = interval; } - if (isSensitive && isRearSticky) - Stack.Clear (); - if (start < end) + else { - start += graft_forward (interval, start, end); - if (start < end) - { - if (interval != null) - interval = interval.Copy (mtext, start, end); - else - interval = new MInterval (Key, mtext, end - start, null); - - MInterval i; - if (Right != null) - { - // .-this-. ==> .-this-. - // .-right .-right - // child .-child - // interval - - i = Right.LeftMost; - i.Left = interval; - } - else - { - Right = interval; - i = this; - } - interval.Parent = i; - for (; i != null; i = i.Parent) - i.Length += interval.Length; - } + Right = interval; + i = this; } + interval.Parent = i; + for (; i != null; i = i.Parent) + i.Length += interval.Length; } else // (pos > To) - Next.Insert (pos, interval, start, end); + Right.Insert (pos, interval, start, end); M17n.DebugPrint (" done\n"); } @@ -1303,7 +1337,6 @@ namespace M17N.Core int to = tail.To; // The nearest common parent of HEAD and TAIL. MInterval root; - for (root = head; root.To + root.RightLength < to; root = root.Parent); @@ -1345,39 +1378,40 @@ namespace M17N.Core } } + private void check_from_to (string str) + { + int save_from = From; + int save_to = To; + update_from_to (); + if (save_from != From || save_to != To) + throw new Exception (str + ":incorrect from or to"); + } + public void MergeAfterChange (int start, int end) { update_from_to (); - MInterval head = find_head (start), tail = head, i; + MInterval head = find_head (start), i = head; + MInterval tail = find_tail (end).Next; if (start == head.From && start > 0) { i = head.Prev; - if (head.mergeable (i)) - head = i; + if (! head.mergeable (i)) + i = head; } - while (tail.To < end) + while (i != tail) { - i = tail.Next; - if (! tail.mergeable (i)) + MInterval next = i.Next; + + if (next == null || ! i.mergeable (next)) { - if (head != tail) - combine (head, tail); - i.update_from_to (); - head = i; + if (head != i) + combine (head, i); + head = next; } - tail = i; - } - while (true) - { - i = tail.Next; - if (i == null || ! tail.mergeable (i)) - break; - tail = i; + i = next; } - if (head != tail) - combine (head, tail); } public void Pop (int start, int end) @@ -1449,9 +1483,16 @@ namespace M17N.Core if (force || M17n.debug) { Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To); - if (with_prop) - foreach (MPlist p in Stack) - Console.Write (" " + p.Val); + if (with_prop && ! Stack.IsEmpty) + { + string prepend = " ["; + foreach (MPlist p in Stack) + { + Console.Write (prepend + ((MProperty) p.Val).Val); + prepend = " "; + } + Console.Write ("]"); + } Console.Write (")"); if (newline) Console.WriteLine (); diff --git a/Makefile b/Makefile index 77b2d0a..772d2ce 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CS=gmcs M17N_SRC = M17N.cs CORE_SRC = MSymbol.cs MPlist.cs MText.cs MDatabase.cs -TEST_PROG = symbol.exe plist.exe mtext.exe textprop.exe +TEST_PROG = symbol.exe plist.exe mtext.exe textprop.exe r__.cs all: ${TEST_PROG}