{
internal class SimpleCollator
{
- struct PreviousInfo
+ // this environment variable is for debugging quick check.
+ static bool QuickCheckDisabled =
+ Environment.GetEnvironmentVariable (
+ "MONO_COLLATION_QUICK_CHECK_DISABLED") == "yes";
+
+ unsafe internal struct Context
+ {
+ public Context (CompareOptions opt, byte* alwaysMatchFlags, byte* neverMatchFlags, byte* buffer1, byte* buffer2, byte* prev1, bool quickCheckPossible)
+ {
+ Option = opt;
+ AlwaysMatchFlags = alwaysMatchFlags;
+ NeverMatchFlags = neverMatchFlags;
+ Buffer1 = buffer1;
+ Buffer2 = buffer2;
+ PrevSortKey = prev1;
+ PrevCode = -1;
+ QuickCheckPossible = quickCheckPossible;
+ }
+
+ public readonly CompareOptions Option;
+ public readonly byte* NeverMatchFlags;
+ public readonly byte* AlwaysMatchFlags;
+ public byte* Buffer1;
+ public byte* Buffer2;
+ public int PrevCode;
+ public byte* PrevSortKey;
+ public readonly bool QuickCheckPossible;
+
+ public void ClearPrevInfo ()
+ {
+ PrevCode = -1;
+ PrevSortKey = null;
+ }
+ }
+
+ unsafe struct PreviousInfo
{
public int Code;
- public byte [] SortKey;
+ public byte* SortKey;
public PreviousInfo (bool dummy)
{
//
// Now that it should be thread safe, this array is allocated
// at every time.
-// byte [] checkedFlags = new byte [128 / 8];
+// byte [] neverMatchFlags = new byte [128 / 8];
#region .ctor() and split functions
/*
// dump tailoring table
-Console.WriteLine ("******** building table for {0} : c - {1} d - {2}",
+Console.WriteLine ("******** building table for {0} : contractions - {1} diacritical - {2}",
culture.LCID, contractions.Length, level2Maps.Length);
foreach (Contraction c in contractions) {
foreach (char cc in c.Source)
Contraction GetTailContraction (string s, int start, int end, Contraction [] clist)
{
+ if (start == end || end < -1 || start >= s.Length || s.Length <= end + 1)
+ throw new SystemException (String.Format ("MONO internal error. Failed to get TailContraction. start = {0} end = {1} string = '{2}'", start, end, s));
for (int i = 0; i < clist.Length; i++) {
Contraction ct = clist [i];
- int diff = ct.Source [0] - s [end];
+ int diff = ct.Source [0] - s [end + 1];
if (diff > 0)
return null; // it's already sorted
else if (diff < 0)
continue;
char [] chars = ct.Source;
- if (start - end + 1 < chars.Length)
- continue;
+
bool match = true;
- int offset = start - chars.Length + 1;
+ if (chars.Length > start - end)
+ continue;
for (int n = 0; n < chars.Length; n++)
- if (s [offset + n] != chars [n]) {
+ if (s [start - n] != chars [chars.Length - 1 - n]) {
match = false;
break;
}
return buf.GetResultAndReset ();
}
- void GetSortKey (string s, int start, int end,
+ unsafe void GetSortKey (string s, int start, int end,
SortKeyBuffer buf, CompareOptions opt)
{
- PreviousInfo prev = new PreviousInfo (false);
+ byte* prevbuf = stackalloc byte [4];
+ ClearBuffer (prevbuf, 4);
+ Context ctx = new Context (opt, null, null, null, null, prevbuf, false);
for (int n = start; n < end; n++) {
int i = s [n];
ExtenderType ext = GetExtenderType (i);
if (ext != ExtenderType.None) {
- i = FilterExtender (prev.Code, ext, opt);
+ i = FilterExtender (ctx.PrevCode, ext, opt);
if (i >= 0)
FillSortKeyRaw (i, ext, buf, opt);
- else if (prev.SortKey != null) {
- byte [] b = prev.SortKey;
+ else if (ctx.PrevSortKey != null) {
+ byte* b = ctx.PrevSortKey;
buf.AppendNormal (
b [0],
b [1],
if (ct.Replacement != null) {
GetSortKey (ct.Replacement, 0, ct.Replacement.Length, buf, opt);
} else {
- byte [] b = ct.SortKey;
+ byte* b = ctx.PrevSortKey;
+ for (int bi = 0; bi < ct.SortKey.Length; bi++)
+ b [bi] = ct.SortKey [bi];
buf.AppendNormal (
b [0],
b [1],
b [2] != 1 ? b [2] : Level2 (i, ext),
b [3] != 1 ? b [3] : Uni.Level3 (i));
- prev.SortKey = b;
- prev.Code = -1;
+ ctx.PrevCode = -1;
}
n += ct.Source.Length - 1;
}
else {
if (!Uni.IsIgnorableNonSpacing (i))
- prev.Code = i;
+ ctx.PrevCode = i;
FillSortKeyRaw (i, ExtenderType.None, buf, opt);
}
}
len1 == min ? - 1 : 1;
}
- public int Compare (string s1, int idx1, int len1,
+ // mostly equivalent to CompareOrdinal, but the return value is
+ // not based on codepoints.
+ private int CompareQuick (string s1, int idx1, int len1,
+ string s2, int idx2, int len2, out bool sourceConsumed,
+ out bool targetConsumed, bool immediateBreakup)
+ {
+ sourceConsumed = false;
+ targetConsumed = false;
+ int min = len1 < len2 ? len1 : len2;
+ int end1 = idx1 + min;
+ int end2 = idx2 + min;
+ if (idx1 < 0 || idx2 < 0 || end1 > s1.Length || end2 > s2.Length)
+ throw new SystemException (String.Format ("CompareInfo Internal Error: Should not happen. {0} {1} {2} {3} {4} {5}", idx1, idx2, len1, len2, s1.Length, s2.Length));
+ for (int i1 = idx1, i2 = idx2;
+ i1 < end1 && i2 < end2; i1++, i2++)
+ if (s1 [i1] != s2 [i2]) {
+ if (immediateBreakup)
+ return -1;
+ int ret = Category (s1 [i1]) - Category (s2 [i2]);
+ if (ret == 0)
+ ret = Level1 (s1 [i1]) - Level1 (s2 [i2]);
+ // no level2 and 4
+ if (ret == 0)
+ ret = Uni.Level3 (s1 [i1]) - Uni.Level3 (s2 [i2]);
+ if (ret == 0)
+ throw new SystemException (String.Format ("CompareInfo Internal Error: Should not happen. '{0}' {2} {3} '{1}' {4} {5}", s1, s2, idx1, end1, idx2, end2));
+ return ret;
+ }
+ sourceConsumed = len1 <= len2;
+ targetConsumed = len1 >= len2;
+ return len1 == len2 ? 0 :
+ len1 == min ? - 1 : 1;
+ }
+
+ private int CompareOrdinalIgnoreCase (string s1, int idx1, int len1,
+ string s2, int idx2, int len2)
+ {
+ int min = len1 < len2 ? len1 : len2;
+ int end1 = idx1 + min;
+ int end2 = idx2 + min;
+ if (idx1 < 0 || idx2 < 0 || end1 > s1.Length || end2 > s2.Length)
+ throw new SystemException (String.Format ("CompareInfo Internal Error: Should not happen. {0} {1} {2} {3} {4} {5}", idx1, idx2, len1, len2, s1.Length, s2.Length));
+ TextInfo ti = invariant.textInfo;
+ for (int i1 = idx1, i2 = idx2;
+ i1 < end1 && i2 < end2; i1++, i2++)
+ if (ti.ToLower (s1 [i1]) != ti.ToLower (s2 [i2]))
+ return ti.ToLower (s1 [i1]) - ti.ToLower (s2 [i2]);
+ return len1 == len2 ? 0 :
+ len1 == min ? - 1 : 1;
+ }
+
+ public unsafe int Compare (string s1, int idx1, int len1,
string s2, int idx2, int len2, CompareOptions options)
{
// quick equality check
// return 0;
if (options == CompareOptions.Ordinal)
return CompareOrdinal (s1, idx1, len1, s2, idx2, len2);
+#if NET_2_0
+ if (options == CompareOptions.OrdinalIgnoreCase)
+ return CompareOrdinalIgnoreCase (s1, idx1, len1, s2, idx2, len2);
+#endif
#if false // stable easy version, depends on GetSortKey().
SortKey sk1 = GetSortKey (s1, idx1, len1, options);
return d1 [i] < d2 [i] ? -1 : 1;
return d1.Length == d2.Length ? 0 : d1.Length < d2.Length ? -1 : 1;
#else
- PreviousInfo prev1 = new PreviousInfo (false);
- byte [] sk1 = new byte [4];
- byte [] sk2 = new byte [4];
+ byte* sk1 = stackalloc byte [4];
+ byte* sk2 = stackalloc byte [4];
+ ClearBuffer (sk1, 4);
+ ClearBuffer (sk2, 4);
+ Context ctx = new Context (options, null, null, sk1, sk2, null,
+ QuickCheckPossible (s1, idx1, idx1 + len1, s2, idx2, idx2 + len2));
+
bool dummy, dummy2;
- int ret = CompareInternal (options, s1, idx1, len1, s2, idx2, len2, out dummy, out dummy2, true, ref prev1, sk1, sk2);
+ int ret = CompareInternal (s1, idx1, len1, s2, idx2, len2, out dummy, out dummy2, true, false, ref ctx);
return ret == 0 ? 0 : ret < 0 ? -1 : 1;
#endif
}
- int CompareInternal (COpt opt, string s1, int idx1, int len1, string s2,
+ unsafe void ClearBuffer (byte* buffer, int size)
+ {
+ for (int i = 0; i < size; i++)
+ buffer [i] = 0;
+ }
+
+ bool QuickCheckPossible (string s1, int idx1, int end1,
+ string s2, int idx2, int end2)
+ {
+ if (QuickCheckDisabled)
+ return false;
+// if (s1.Length > 100 || s2.Length > 100)
+// return false;
+ for (int i = idx1; i < end1; i++)
+ if (s1 [i] < 0x20 && (s1 [i] < '\x9' || s1 [i] > '\xD') || s1 [i] >= 0x80 || s1 [i] == '-' || s1 [i] == '\'')
+ return false;
+ for (int i = idx2; i < end2; i++)
+ if (s2 [i] < 0x20 && (s2 [i] < '\x9' || s2 [i] > '\xD') || s2 [i] >= 0x80 || s2 [i] == '-' || s2 [i] == '\'')
+ return false;
+ return true;
+ }
+
+ unsafe int CompareInternal (string s1, int idx1, int len1, string s2,
int idx2, int len2,
out bool targetConsumed, out bool sourceConsumed,
- bool skipHeadingExtenders, ref PreviousInfo prev1,
- byte [] charSortKey, byte [] charSortKey2)
+ bool skipHeadingExtenders, bool immediateBreakup,
+ ref Context ctx)
{
+ COpt opt = ctx.Option;
int start1 = idx1;
int start2 = idx2;
int end1 = idx1 + len1;
sourceConsumed = false;
PreviousInfo prev2 = new PreviousInfo (false);
+ if (opt == CompareOptions.None && ctx.QuickCheckPossible)
+ return CompareQuick (s1, idx1, len1, s2, idx2, len2, out sourceConsumed, out targetConsumed, immediateBreakup);
+
// It holds final result that comes from the comparison
// at level 2 or lower. Even if Compare() found the
// difference at level 2 or lower, it still has to
int cur1 = idx1;
int cur2 = idx2;
- byte [] sk1 = null;
- byte [] sk2 = null;
+ byte* sk1 = null;
+ byte* sk2 = null;
int i1 = FilterOptions (s1 [idx1], opt);
int i2 = FilterOptions (s2 [idx2], opt);
bool special1 = false;
// repeat the previous character.
ext1 = GetExtenderType (i1);
if (ext1 != ExtenderType.None) {
- if (prev1.Code < 0) {
- if (prev1.SortKey == null) {
+ if (ctx.PrevCode < 0) {
+ if (ctx.PrevSortKey == null) {
// nothing to extend
idx1++;
continue;
}
- sk1 = prev1.SortKey;
+ sk1 = ctx.PrevSortKey;
}
else
- i1 = FilterExtender (prev1.Code, ext1, opt);
+ i1 = FilterExtender (ctx.PrevCode, ext1, opt);
}
ext2 = GetExtenderType (i2);
if (ext2 != ExtenderType.None) {
// here Windows has a bug that it does
// not consider thirtiary weight.
lv5Value1 = Level1 (i1) << 8 + Uni.Level3 (i1);
- prev1.Code = i1;
+ ctx.PrevCode = i1;
idx1++;
}
if (cat2 == 6) {
else if (ct1 != null) {
offset1 = ct1.Source.Length;
if (ct1.SortKey != null) {
- sk1 = charSortKey;
+ sk1 = ctx.Buffer1;
for (int i = 0; i < ct1.SortKey.Length; i++)
sk1 [i] = ct1.SortKey [i];
- prev1.Code = -1;
- prev1.SortKey = sk1;
+ ctx.PrevCode = -1;
+ ctx.PrevSortKey = sk1;
}
else if (escape1.Source == null) {
escape1.Source = s1;
}
}
else {
- sk1 = charSortKey;
+ sk1 = ctx.Buffer1;
sk1 [0] = cat1;
sk1 [1] = Level1 (i1);
if (!ignoreNonSpace && currentLevel > 1)
if (currentLevel > 3)
special1 = Uni.HasSpecialWeight ((char) i1);
if (cat1 > 1)
- prev1.Code = i1;
+ ctx.PrevCode = i1;
}
Contraction ct2 = null;
else if (ct2 != null) {
idx2 += ct2.Source.Length;
if (ct2.SortKey != null) {
- sk2 = charSortKey2;
+ sk2 = ctx.Buffer2;
for (int i = 0; i < ct2.SortKey.Length; i++)
sk2 [i] = ct2.SortKey [i];
prev2.Code = -1;
}
}
else {
- sk2 = charSortKey2;
+ sk2 = ctx.Buffer2;
sk2 [0] = cat2;
sk2 [1] = Level1 (i2);
if (!ignoreNonSpace && currentLevel > 1)
ret = sk1 [2] - sk2 [2];
if (ret != 0) {
finalResult = ret;
+ if (immediateBreakup)
+ return -1; // different
currentLevel = frenchSort ? 2 : 1;
continue;
}
ret = sk1 [3] - sk2 [3];
if (ret != 0) {
finalResult = ret;
+ if (immediateBreakup)
+ return -1; // different
currentLevel = 2;
continue;
}
if (currentLevel == 3)
continue;
if (special1 != special2) {
+ if (immediateBreakup)
+ return -1; // different
finalResult = special1 ? 1 : -1;
currentLevel = 3;
continue;
!IsHalfKana ((char) i1, opt),
!IsHalfKana ((char) i2, opt));
if (ret != 0) {
+ if (immediateBreakup)
+ return -1; // different
finalResult = ret;
currentLevel = 3;
continue;
}
}
if (currentLevel == 1 && finalResult != 0) {
- while (idx1 < end1)
+ while (idx1 < end1) {
if (Uni.IsIgnorableNonSpacing (s1 [idx1]))
idx1++;
- while (idx2 < end2)
+ else
+ break;
+ }
+ while (idx2 < end2) {
if (Uni.IsIgnorableNonSpacing (s2 [idx2]))
idx2++;
+ else
+ break;
+ }
}
// we still have to handle level 5
if (finalResult == 0) {
return IsPrefix (src, target, 0, src.Length, opt);
}
- public bool IsPrefix (string s, string target, int start, int length, CompareOptions opt)
+ public unsafe bool IsPrefix (string s, string target, int start, int length, CompareOptions opt)
{
- PreviousInfo prev = new PreviousInfo (false);
- byte [] sk1 = new byte [4];
- byte [] sk2 = new byte [4];
- return IsPrefix (opt, s, target, start, length, true, ref prev, sk1, sk2);
+ if (target.Length == 0)
+ return true;
+ byte* sk1 = stackalloc byte [4];
+ byte* sk2 = stackalloc byte [4];
+ ClearBuffer (sk1, 4);
+ ClearBuffer (sk2, 4);
+ Context ctx = new Context (opt, null, null, sk1, sk2, null,
+ QuickCheckPossible (s, start, start + length, target, 0, target.Length));
+ return IsPrefix (s, target, start, length, true, ref ctx);
}
- bool IsPrefix (COpt opt, string s, string target, int start, int length, bool skipHeadingExtenders, ref PreviousInfo prev, byte [] sk1, byte [] sk2)
+ unsafe bool IsPrefix (string s, string target, int start, int length, bool skipHeadingExtenders, ref Context ctx)
{
bool consumed, dummy;
- CompareInternal (opt, s, start, length,
+ CompareInternal (s, start, length,
target, 0, target.Length,
out consumed, out dummy, skipHeadingExtenders,
- ref prev, sk1, sk2);
+ true, ref ctx);
return consumed;
}
return IsSuffix (src, target, src.Length - 1, src.Length, opt);
}
- public bool IsSuffix (string s, string target, int start, int length, CompareOptions opt)
+ public unsafe bool IsSuffix (string s, string target, int start, int length, CompareOptions opt)
{
+ if (target.Length == 0)
+ return true;
+ int last = LastIndexOf (s, target, start, length, opt);
+ return last >= 0 && Compare (s, last, s.Length - last, target, 0, target.Length, opt) == 0;
+/*
// quick check : simple codepoint comparison
if (s.Length >= target.Length) {
int si = start;
}
PreviousInfo prev = new PreviousInfo (false);
- byte [] sk1 = new byte [4];
- byte [] sk2 = new byte [4];
+ byte* sk1 = stackalloc byte [4];
+ byte* sk2 = stackalloc byte [4];
+ ClearBuffer (sk1, 4);
+ ClearBuffer (sk2, 4);
return IsSuffix (opt, s, target, start, length, ref prev, sk1, sk2);
+*/
}
- bool IsSuffix (COpt opt, string s, string t, int start, int length, ref PreviousInfo prev, byte [] sk1, byte [] sk2)
+ unsafe bool IsSuffix (string s, string t, int start, int length, ref Context ctx)
{
int tstart = 0;
+ COpt opt = ctx.Option;
+
for (;tstart < t.Length; tstart++)
if (!IsIgnorable (t [tstart], opt))
break;
bool sourceConsumed, targetConsumed;
int mismatchCount = 0;
for (int i = 0; i < length; i++) {
- prev = new PreviousInfo (false); // prev.Reset();
+ ctx.ClearPrevInfo ();
- int ret = CompareInternal (opt, s, start - i, i + 1,
+ int ret = CompareInternal (s, start - i, i + 1,
t, tstart, t.Length - tstart,
out targetConsumed,
- out sourceConsumed, true, ref prev,
- sk1, sk2);
+ // FIXME: could immediately breakup
+ out sourceConsumed, true, true, ref ctx);
if (ret == 0)
return true;
if (!sourceConsumed && targetConsumed)
return IndexOf (s, target, 0, s.Length, opt);
}
- public int IndexOf (string s, string target, int start, int length, CompareOptions opt)
+ public unsafe int IndexOf (string s, string target, int start, int length, CompareOptions opt)
{
- PreviousInfo prev = new PreviousInfo (false);
- byte [] checkedFlags = s.Length > 50 ? new byte [16] : null;
- byte [] targetSortKey = new byte [4];
- byte [] sk1 = new byte [4];
- byte [] sk2 = new byte [4];
-
- return IndexOf (opt, s, target, start, length,
- checkedFlags, targetSortKey, ref prev, sk1, sk2);
+ byte* alwaysMatchFlags = stackalloc byte [16];
+ byte* neverMatchFlags = stackalloc byte [16];
+ byte* targetSortKey = stackalloc byte [4];
+ byte* sk1 = stackalloc byte [4];
+ byte* sk2 = stackalloc byte [4];
+ ClearBuffer (alwaysMatchFlags, 16);
+ ClearBuffer (neverMatchFlags, 16);
+ ClearBuffer (targetSortKey, 4);
+ ClearBuffer (sk1, 4);
+ ClearBuffer (sk2, 4);
+ Context ctx = new Context (opt, alwaysMatchFlags, neverMatchFlags, sk1, sk2, null,
+ QuickCheckPossible (s, start, start + length, target, 0, target.Length));
+
+ return IndexOf (s, target, start, length,
+ targetSortKey, ref ctx);
}
public int IndexOf (string s, char target, CompareOptions opt)
return IndexOf (s, target, 0, s.Length, opt);
}
- public int IndexOf (string s, char target, int start, int length, CompareOptions opt)
+ public unsafe int IndexOf (string s, char target, int start, int length, CompareOptions opt)
{
- PreviousInfo prev = new PreviousInfo (false);
- byte [] sk1 = new byte [4];
- byte [] sk2 = new byte [4];
- byte [] checkedFlags = s.Length > 50 ? new byte [16] : null;
- byte [] targetSortKey = new byte [4];
+ byte* alwaysMatchFlags = stackalloc byte [16];
+ byte* neverMatchFlags = stackalloc byte [16];
+ byte* targetSortKey = stackalloc byte [4];
+ byte* sk1 = stackalloc byte [4];
+ byte* sk2 = stackalloc byte [4];
+ ClearBuffer (alwaysMatchFlags, 16);
+ ClearBuffer (neverMatchFlags, 16);
+ ClearBuffer (targetSortKey, 4);
+ ClearBuffer (sk1, 4);
+ ClearBuffer (sk2, 4);
+ Context ctx = new Context (opt, alwaysMatchFlags, neverMatchFlags, sk1, sk2, null, false);
// If target is contraction, then use string search.
Contraction ct = GetContraction (target);
if (ct != null) {
if (ct.Replacement != null)
- return IndexOf (opt, s, ct.Replacement,
- start, length, checkedFlags, targetSortKey, ref prev, sk1, sk2);
- else
- return IndexOfSortKey (opt, s, start, length, ct.SortKey, char.MinValue, -1, true, checkedFlags, ref prev, sk1);
+ return IndexOf (s, ct.Replacement,
+ start, length, targetSortKey, ref ctx);
+ else {
+ for (int i = 0; i < ct.SortKey.Length; i++)
+ sk2 [i] = ct.SortKey [i];
+ return IndexOfSortKey (s, start, length, sk2, char.MinValue, -1, true, ref ctx);
+ }
} else {
int ti = FilterOptions ((int) target, opt);
targetSortKey [0] = Category (ti);
targetSortKey [2] =
Level2 (ti, ExtenderType.None);
targetSortKey [3] = Uni.Level3 (ti);
- return IndexOfSortKey (opt, s, start, length,
+ return IndexOfSortKey (s, start, length,
targetSortKey, target, ti,
- !Uni.HasSpecialWeight ((char) ti), checkedFlags, ref prev, sk1);
+ !Uni.HasSpecialWeight ((char) ti), ref ctx);
}
}
// Searches target byte[] keydata
- int IndexOfSortKey (COpt opt, string s, int start, int length, byte [] sortkey, char target, int ti, bool noLv4, byte [] checkedFlags, ref PreviousInfo prev, byte [] sk)
+ unsafe int IndexOfSortKey (string s, int start, int length, byte* sortkey, char target, int ti, bool noLv4, ref Context ctx)
{
int end = start + length;
int idx = start;
while (idx < end) {
int cur = idx;
- if (MatchesForward (opt, s, ref idx, end, ti, sortkey, noLv4, checkedFlags, ref prev, sk))
+ if (MatchesForward (s, ref idx, end, ti, sortkey, noLv4, ref ctx))
return cur;
}
return -1;
// Searches string. Search head character (or keydata when
// the head is contraction sortkey) and try IsPrefix().
- int IndexOf (COpt opt, string s, string target, int start, int length, byte [] checkedFlags, byte [] targetSortKey, ref PreviousInfo prev, byte [] sk1, byte [] sk2)
+ unsafe int IndexOf (string s, string target, int start, int length, byte* targetSortKey, ref Context ctx)
{
+ COpt opt = ctx.Option;
int tidx = 0;
for (; tidx < target.Length; tidx++)
if (!IsIgnorable (target [tidx], opt))
return start;
Contraction ct = GetContraction (target, tidx, target.Length - tidx);
string replace = ct != null ? ct.Replacement : null;
- byte [] sk = replace == null ? targetSortKey : null;
+ byte* sk = replace == null ? targetSortKey : null;
bool noLv4 = true;
char tc = char.MinValue;
int ti = -1;
do {
int idx = 0;
if (replace != null)
- idx = IndexOf (opt, s, replace, start, length, checkedFlags, targetSortKey, ref prev, sk1, sk2);
+ idx = IndexOf (s, replace, start, length, targetSortKey, ref ctx);
else
- idx = IndexOfSortKey (opt, s, start, length, sk, tc, ti, noLv4, checkedFlags, ref prev, sk1);
+ idx = IndexOfSortKey (s, start, length, sk, tc, ti, noLv4, ref ctx);
if (idx < 0)
return -1;
length -= idx - start;
start = idx;
- if (IsPrefix (opt, s, target, start, length, false, ref prev, sk1, sk2))
+ if (IsPrefix (s, target, start, length, false, ref ctx))
return idx;
Contraction cts = GetContraction (s, start, length);
if (cts != null) {
return LastIndexOf (s, target, s.Length - 1, s.Length, opt);
}
- public int LastIndexOf (string s, string target, int start, int length, CompareOptions opt)
+ public unsafe int LastIndexOf (string s, string target, int start, int length, CompareOptions opt)
{
- PreviousInfo prev = new PreviousInfo (false);
- byte [] sk1 = new byte [4];
- byte [] sk2 = new byte [4];
- byte [] checkedFlags = s.Length > 50 ? new byte [16] : null;
- byte [] targetSortKey = new byte [4];
- return LastIndexOf (opt, s, target, start, length,
- checkedFlags, targetSortKey, ref prev, sk1, sk2);
+ byte* alwaysMatchFlags = stackalloc byte [16];
+ byte* neverMatchFlags = stackalloc byte [16];
+ byte* targetSortKey = stackalloc byte [4];
+ byte* sk1 = stackalloc byte [4];
+ byte* sk2 = stackalloc byte [4];
+ ClearBuffer (alwaysMatchFlags, 16);
+ ClearBuffer (neverMatchFlags, 16);
+ ClearBuffer (targetSortKey, 4);
+ ClearBuffer (sk1, 4);
+ ClearBuffer (sk2, 4);
+ // For some unknown reason CompareQuick() does not work fine w/ LastIndexOf().
+ Context ctx = new Context (opt, alwaysMatchFlags, neverMatchFlags, sk1, sk2, null, false);
+ return LastIndexOf (s, target, start, length,
+ targetSortKey, ref ctx);
}
public int LastIndexOf (string s, char target, CompareOptions opt)
return LastIndexOf (s, target, s.Length - 1, s.Length, opt);
}
- public int LastIndexOf (string s, char target, int start, int length, CompareOptions opt)
+ public unsafe int LastIndexOf (string s, char target, int start, int length, CompareOptions opt)
{
- PreviousInfo prev = new PreviousInfo (false);
- byte [] sk1 = new byte [4];
- byte [] sk2 = new byte [4];
- byte [] checkedFlags = s.Length > 50 ? new byte [16] : null;
- byte [] targetSortKey = new byte [4];
-
- // If target is contraction, then use string search.
+ byte* alwaysMatchFlags = stackalloc byte [16];
+ byte* neverMatchFlags = stackalloc byte [16];
+ byte* targetSortKey = stackalloc byte [4];
+ byte* sk1 = stackalloc byte [4];
+ byte* sk2 = stackalloc byte [4];
+ ClearBuffer (alwaysMatchFlags, 16);
+ ClearBuffer (neverMatchFlags, 16);
+ ClearBuffer (targetSortKey, 4);
+ ClearBuffer (sk1, 4);
+ ClearBuffer (sk2, 4);
+ Context ctx = new Context (opt, alwaysMatchFlags, neverMatchFlags, sk1, sk2, null, false);
+
+ // If target is a replacement contraction, then use
+ // string search.
Contraction ct = GetContraction (target);
if (ct != null) {
if (ct.Replacement != null)
- return LastIndexOf (opt, s,
+ return LastIndexOf (s,
ct.Replacement, start, length,
- checkedFlags, targetSortKey, ref prev, sk1, sk2);
- else
- return LastIndexOfSortKey (opt, s, start,
- start, length, ct.SortKey,
- char.MinValue, -1, true,
- checkedFlags, ref prev, sk1);
+ targetSortKey, ref ctx);
+ else {
+ for (int bi = 0; bi < ct.SortKey.Length; bi++)
+ sk2 [bi] = ct.SortKey [bi];
+ return LastIndexOfSortKey (s, start,
+ start, length, sk2,
+ -1, true, ref ctx);
+ }
}
else {
int ti = FilterOptions ((int) target, opt);
if ((opt & COpt.IgnoreNonSpace) == 0)
targetSortKey [2] = Level2 (ti, ExtenderType.None);
targetSortKey [3] = Uni.Level3 (ti);
- return LastIndexOfSortKey (opt, s, start, start,
- length, targetSortKey, target,
+ return LastIndexOfSortKey (s, start, start,
+ length, targetSortKey,
ti, !Uni.HasSpecialWeight ((char) ti),
- checkedFlags, ref prev, sk1);
+ ref ctx);
}
}
// Searches target byte[] keydata
- int LastIndexOfSortKey (COpt opt, string s, int start, int orgStart, int length, byte [] sortkey, char target, int ti, bool noLv4, byte [] checkedFlags, ref PreviousInfo prev, byte [] sk)
+ unsafe int LastIndexOfSortKey (string s, int start, int orgStart, int length, byte* sortkey, int ti, bool noLv4, ref Context ctx)
{
int end = start - length;
int idx = start;
while (idx > end) {
int cur = idx;
- if (MatchesBackward (opt, s, ref idx, end, orgStart,
- ti, sortkey, noLv4, checkedFlags, ref prev, sk))
+ if (MatchesBackward (s, ref idx, end, orgStart,
+ ti, sortkey, noLv4, ref ctx))
return cur;
}
return -1;
// Searches string. Search head character (or keydata when
// the head is contraction sortkey) and try IsPrefix().
- int LastIndexOf (COpt opt, string s, string target, int start, int length, byte [] checkedFlags, byte [] targetSortKey, ref PreviousInfo prev, byte [] sk1, byte [] sk2)
+ unsafe int LastIndexOf (string s, string target, int start, int length, byte* targetSortKey, ref Context ctx)
{
+ COpt opt = ctx.Option;
int orgStart = start;
int tidx = 0;
for (; tidx < target.Length; tidx++)
return start;
Contraction ct = GetContraction (target, tidx, target.Length - tidx);
string replace = ct != null ? ct.Replacement : null;
- byte [] sk = replace == null ? targetSortKey : null;
+ byte* sk = replace == null ? targetSortKey : null;
bool noLv4 = true;
- char tc = char.MinValue;
int ti = -1;
if (ct != null && sk != null) {
for (int i = 0; i < ct.SortKey.Length; i++)
sk [i] = ct.SortKey [i];
} else if (sk != null) {
- tc = target [tidx];
ti = FilterOptions (target [tidx], opt);
sk [0] = Category (ti);
sk [1] = Level1 (ti);
int idx = 0;
if (replace != null)
- idx = LastIndexOf (opt, s, replace,
- start, length, checkedFlags,
- targetSortKey, ref prev, sk1, sk2);
+ idx = LastIndexOf (s, replace,
+ start, length,
+ targetSortKey, ref ctx);
else
- idx = LastIndexOfSortKey (opt, s, start, orgStart, length, sk, tc, ti, noLv4, checkedFlags, ref prev, sk1);
+ idx = LastIndexOfSortKey (s, start, orgStart, length, sk, ti, noLv4, ref ctx);
if (idx < 0)
return -1;
length -= start - idx;
start = idx;
- if (IsPrefix (opt, s, target, idx, orgStart - idx + 1, false, ref prev, sk1, sk2)) {
+ if (IsPrefix (s, target, idx, orgStart - idx + 1, false, ref ctx)) {
for (;idx < orgStart; idx++)
if (!IsIgnorable (s [idx], opt))
break;
return -1;
}
- private bool MatchesForward (COpt opt, string s, ref int idx, int end, int ti, byte [] sortkey, bool noLv4, byte [] checkedFlags, ref PreviousInfo prev, byte [] sk)
+ unsafe bool MatchesForward (string s, ref int idx, int end, int ti, byte* sortkey, bool noLv4, ref Context ctx)
{
+ COpt opt = ctx.Option;
int si = s [idx];
- if (checkedFlags != null && si < 128 && (checkedFlags [si / 8] & (1 << (si % 8))) != 0) {
+ if (ctx.AlwaysMatchFlags != null && si < 128 && (ctx.AlwaysMatchFlags [si / 8] & (1 << (si % 8))) != 0)
+ return true;
+ if (ctx.NeverMatchFlags != null &&
+ si < 128 &&
+ (ctx.NeverMatchFlags [si / 8] & (1 << (si % 8))) != 0) {
idx++;
return false;
}
ExtenderType ext = GetExtenderType (s [idx]);
Contraction ct = null;
- if (MatchesForwardCore (opt, s, ref idx, end, ti, sortkey, noLv4, ext, ref ct, checkedFlags, ref prev, sk))
+ if (MatchesForwardCore (s, ref idx, end, ti, sortkey, noLv4, ext, ref ct, ref ctx)) {
+ if (ctx.AlwaysMatchFlags != null && ct == null && ext == ExtenderType.None && si < 128)
+ ctx.AlwaysMatchFlags [si / 8] |= (byte) (1 << (si % 8));
return true;
- if (checkedFlags != null && ct == null && ext == ExtenderType.None && si < 128) {
- checkedFlags [si / 8] |= (byte) (1 << (si % 8));
}
+ if (ctx.NeverMatchFlags != null && ct == null && ext == ExtenderType.None && si < 128)
+ ctx.NeverMatchFlags [si / 8] |= (byte) (1 << (si % 8));
return false;
}
- private bool MatchesForwardCore (COpt opt, string s, ref int idx, int end, int ti, byte [] sortkey, bool noLv4, ExtenderType ext, ref Contraction ct, byte [] checkedFlags, ref PreviousInfo prev, byte [] charSortKey)
+ unsafe bool MatchesForwardCore (string s, ref int idx, int end, int ti, byte* sortkey, bool noLv4, ExtenderType ext, ref Contraction ct, ref Context ctx)
{
+ COpt opt = ctx.Option;
+ byte* charSortKey = ctx.Buffer1;
bool ignoreNonSpace = (opt & COpt.IgnoreNonSpace) != 0;
int si = -1;
if (ext == ExtenderType.None)
ct = GetContraction (s, idx, end);
- else if (prev.Code < 0) {
- if (prev.SortKey == null) {
+ else if (ctx.PrevCode < 0) {
+ if (ctx.PrevSortKey == null) {
idx++;
return false;
}
- charSortKey = prev.SortKey;
+ charSortKey = ctx.PrevSortKey;
}
else
- si = FilterExtender (prev.Code, ext, opt);
+ si = FilterExtender (ctx.PrevCode, ext, opt);
// if lv4 exists, it never matches contraction
if (ct != null) {
idx += ct.Source.Length;
if (!noLv4)
return false;
if (ct.SortKey != null) {
- for (int i = 0; i < sortkey.Length; i++)
+ for (int i = 0; i < 4; i++)
charSortKey [i] = sortkey [i];
- prev.Code = -1;
- prev.SortKey = charSortKey;
+ ctx.PrevCode = -1;
+ ctx.PrevSortKey = charSortKey;
} else {
// Here is the core of LAMESPEC
// described at the top of the source.
int dummy = 0;
- return MatchesForward (opt, ct.Replacement, ref dummy,
- ct.Replacement.Length, ti, sortkey, noLv4, checkedFlags, ref prev, charSortKey);
+ return MatchesForward (ct.Replacement, ref dummy,
+ ct.Replacement.Length, ti, sortkey, noLv4, ref ctx);
}
} else {
if (si < 0)
}
charSortKey [3] = Uni.Level3 (si);
if (charSortKey [0] != 1)
- prev.Code = si;
+ ctx.PrevCode = si;
}
for (; idx < end; idx++) {
if (Category (s [idx]) != 1)
return MatchesPrimitive (opt, charSortKey, si, ext, sortkey, ti, noLv4);
}
- private bool MatchesPrimitive (COpt opt, byte [] source, int si, ExtenderType ext, byte [] target, int ti, bool noLv4)
+ unsafe bool MatchesPrimitive (COpt opt, byte* source, int si, ExtenderType ext, byte* target, int ti, bool noLv4)
{
bool ignoreNonSpace = (opt & COpt.IgnoreNonSpace) != 0;
if (source [0] != target [0] ||
return true;
}
- private bool MatchesBackward (COpt opt, string s, ref int idx, int end, int orgStart, int ti, byte [] sortkey, bool noLv4, byte [] checkedFlags, ref PreviousInfo prev, byte [] sk)
+ unsafe bool MatchesBackward (string s, ref int idx, int end, int orgStart, int ti, byte* sortkey, bool noLv4, ref Context ctx)
{
int si = s [idx];
- if (checkedFlags != null && si < 128 && (checkedFlags [si / 8] & (1 << (si % 8))) != 0) {
+ if (ctx.AlwaysMatchFlags != null && si < 128 && (ctx.AlwaysMatchFlags [si / 8] & (1 << (si % 8))) != 0)
+ return true;
+ if (ctx.NeverMatchFlags != null && si < 128 && (ctx.NeverMatchFlags [si / 8] & (1 << (si % 8))) != 0) {
idx--;
return false;
}
ExtenderType ext = GetExtenderType (s [idx]);
Contraction ct = null;
- if (MatchesBackwardCore (opt, s, ref idx, end, orgStart, ti, sortkey, noLv4, ext, ref ct, checkedFlags, ref prev, sk))
+ if (MatchesBackwardCore (s, ref idx, end, orgStart, ti, sortkey, noLv4, ext, ref ct, ref ctx)) {
+ if (ctx.AlwaysMatchFlags != null && ct == null && ext == ExtenderType.None && si < 128)
+ ctx.AlwaysMatchFlags [si / 8] |= (byte) (1 << (si % 8));
return true;
- if (checkedFlags != null && ct == null && ext == ExtenderType.None && si < 128) {
- checkedFlags [si / 8] |= (byte) (1 << (si % 8));
+ }
+ if (ctx.NeverMatchFlags != null && ct == null && ext == ExtenderType.None && si < 128) {
+ ctx.NeverMatchFlags [si / 8] |= (byte) (1 << (si % 8));
}
return false;
}
- private bool MatchesBackwardCore (COpt opt, string s, ref int idx, int end, int orgStart, int ti, byte [] sortkey, bool noLv4, ExtenderType ext, ref Contraction ct, byte [] checkedFlags, ref PreviousInfo prev, byte [] charSortKey)
+ unsafe bool MatchesBackwardCore (string s, ref int idx, int end, int orgStart, int ti, byte* sortkey, bool noLv4, ExtenderType ext, ref Contraction ct, ref Context ctx)
{
+ COpt opt = ctx.Option;
+ byte* charSortKey = ctx.Buffer1;
bool ignoreNonSpace = (opt & COpt.IgnoreNonSpace) != 0;
int cur = idx;
int si = -1;
idx--;
}
if (ext == ExtenderType.None)
- ct = GetContraction (s, idx, end);
+ ct = GetTailContraction (s, idx, end);
// if lv4 exists, it never matches contraction
if (ct != null) {
idx -= ct.Source.Length;
if (!noLv4)
return false;
if (ct.SortKey != null) {
- for (int i = 0; i < sortkey.Length; i++)
+ for (int i = 0; i < 4; i++)
charSortKey [i] = sortkey [i];
- prev.Code = -1;
- prev.SortKey = charSortKey;
+ ctx.PrevCode = -1;
+ ctx.PrevSortKey = charSortKey;
} else {
// Here is the core of LAMESPEC
// described at the top of the source.
int dummy = ct.Replacement.Length - 1;
- return MatchesBackward (opt,
- ct.Replacement, ref dummy,
- dummy, -1, ti, sortkey, noLv4,
- checkedFlags, ref prev, charSortKey);
+ return 0 <= LastIndexOfSortKey (
+ ct.Replacement, dummy, dummy,
+ ct.Replacement.Length, sortkey,
+ ti, noLv4, ref ctx);
}
} else if (ext == ExtenderType.None) {
if (si < 0)
return false;
charSortKey [3] = Uni.Level3 (si);
if (charSortKey [0] != 1)
- prev.Code = si;
+ ctx.PrevCode = si;
}
if (ext == ExtenderType.None) {
for (int tmp = cur + 1; tmp < orgStart; tmp++) {