private void SortChildren() { if (childCount > 0) { TreeNodeEx[] sort = new TreeNodeEx[childCount]; CompareInfo compare = System.Windows.Forms.Application.CurrentCulture.CompareInfo; for (int i = 0; i < childCount; i++) { int pos = -1; for (int j = 0; j < childCount; j++) { if (children[j] != null) { if (pos == -1 || compare.Compare(children[j].Text, children[pos].Text) < 0) { pos = j; } } } sort[i] = children[pos]; children[pos] = null; sort[i].index = i; sort[i].SortChildren(); } children = sort; } }
public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign) { SortKey sk1 = compareInfo.GetSortKey(string1, options); SortKey sk2 = compareInfo.GetSortKey(string2, options); Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2))); Assert.Equal(expectedSign == 0, sk1.Equals(sk2)); if (!WindowsVersionHasTheCompareStringRegression) { Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2))); } Assert.Equal(compareInfo.GetHashCode(string1, options), sk1.GetHashCode()); Assert.Equal(compareInfo.GetHashCode(string2, options), sk2.GetHashCode()); Assert.Equal(string1, sk1.OriginalString); Assert.Equal(string2, sk2.OriginalString); // Now try the span-based versions - use BoundedMemory to detect buffer overruns RunSpanSortKeyTest(compareInfo, string1, options, sk1.KeyData); RunSpanSortKeyTest(compareInfo, string2, options, sk2.KeyData); unsafe static void RunSpanSortKeyTest(CompareInfo compareInfo, ReadOnlySpan <char> source, CompareOptions options, byte[] expectedSortKey)
private int LinearIndexOf(string fieldName, CompareOptions compareOptions) { CompareInfo compareInfo = this._compareInfo; if (compareInfo == null) { if (-1 != this._defaultLocaleID) { compareInfo = CompareInfo.GetCompareInfo(this._defaultLocaleID); } if (compareInfo == null) { compareInfo = CultureInfo.InvariantCulture.CompareInfo; } this._compareInfo = compareInfo; } int length = this._fieldNames.Length; for (int i = 0; i < length; i++) { if (compareInfo.Compare(fieldName, this._fieldNames[i], compareOptions) == 0) { this._fieldNameLookup[fieldName] = i; return(i); } } return(-1); }
private int LinearIndexOf(string fieldName, CompareOptions compareOptions) { CompareInfo compareInfo = _compareInfo; if (null == compareInfo) { if (-1 != _defaultLocaleID) { compareInfo = CompareInfo.GetCompareInfo(_defaultLocaleID); } if (null == compareInfo) { compareInfo = CultureInfo.InvariantCulture.CompareInfo; } _compareInfo = compareInfo; } int length = _fieldNames.Length; for (int i = 0; i < length; ++i) { if (0 == compareInfo.Compare(fieldName, _fieldNames[i], compareOptions)) { _fieldNameLookup[fieldName] = i; // add an exact match for the future return(i); } } return(-1); }
/// <summary> /// Returns the index of the <see cref="DbParameter"/> /// object with the specified name. /// </summary> /// <param name="parameterName"> /// The name of the <see cref="DbParameter"/> /// object in the collection. /// </param> /// <returns> /// The index of the <see cref="DbParameter"/> /// object with the specified name. /// </returns> public override int IndexOf(string parameterName) { int count = m_parameters.Count; if (count == 0) { return(-1); } CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo; CompareOptions co = CompareOptions.IgnoreWidth | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreCase; for (int i = 0; i < count; i++) { string targetName = m_parameters[i].ParameterName; if ((targetName == parameterName) || (0 == ci.Compare(targetName, parameterName, co))) { return(i); } } return(-1); }
public override int Compare(string?x, string?y) { if (object.ReferenceEquals(x, y)) { return(0); } if (x == null) { return(-1); } if (y == null) { return(1); } return(_compareInfo.Compare(x, y, _options)); }
private static int CanCharExpand(char ch, byte[] LocaleSpecificLigatureTable, CompareInfo Comparer, CompareOptions Options) { int num = 0; byte index = LigatureIndex(ch); if (index == 0) { return(0); } if (LocaleSpecificLigatureTable[index] == 0) { if (Comparer.Compare(Conversions.ToString(ch), LigatureExpansions[index]) == 0) { LocaleSpecificLigatureTable[index] = 1; } else { LocaleSpecificLigatureTable[index] = 2; } } if (LocaleSpecificLigatureTable[index] == 1) { return(index); } return(num); }
// Compares two Objects by calling CompareTo. // If a == b, 0 is returned. // If a implements IComparable, a.CompareTo(b) is returned. // If a doesn't implement IComparable and b does, -(b.CompareTo(a)) is returned. // Otherwise an exception is thrown. // public int Compare(object?a, object?b) { if (a == b) { return(0); } if (a == null) { return(-1); } if (b == null) { return(1); } string?sa = a as string; if (sa != null && b is string sb) { return(_compareInfo.Compare(sa, sb)); } if (a is IComparable ia) { return(ia.CompareTo(b)); } if (b is IComparable ib) { return(-ib.CompareTo(a)); } throw new ArgumentException(SR.Argument_ImplementIComparable); }
private void HighlightWords() { if (_words == null) { return; } PowerPoint.Slides slides = _presentation.Slides; int count = 0; foreach (PowerPoint.Slide slide in slides) { count++; PowerPoint.Shapes shapes = slide.Shapes; foreach (PowerPoint.Shape shape in shapes) { if (shape.HasTextFrame == MsoTriState.msoTrue) { if (cmp.Compare(shape.TextFrame.TextRange.Text, _words [0].Text, CompareOptions.StringSort | CompareOptions.IgnoreCase) == 0) { _presentation.SlideShowWindow.View.GotoSlide(count, MsoTriState.msoTrue); break; } //if ( shape.TextFrame.TextRange.Text.CompareTo() ) //_presentation.SlideShowWindow.View.GotoSlide( count, MsoTriState.msoTrue ); //shape.Select( MsoTriState.msoTrue ); //slide.MoveTo( 1 ); //break; } } } }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Compare two string "); try { string a = "hello"; string b = "aaaaa"; CultureInfo cultureInfo = new CultureInfo("en-US"); CompareInfo comparer = cultureInfo.CompareInfo; int result = comparer.Compare(b, a); if (result >= 0) { TestLibrary.TestFramework.LogError("003", "The result is not the value as expected"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public override int Compare(string x, string y) { if (Object.ReferenceEquals(x, y)) { return(0); } if (x == null) { return(-1); } if (y == null) { return(1); } return(_compareInfo.Compare(x, y, _ignoreCase? CompareOptions.IgnoreCase : CompareOptions.None)); }
internal static bool MatchString(LimitChecker limitChecker, CultureInfo cultureInfo, string content, string pattern, ContentFlags flags) { if (!limitChecker.CheckAndIncrementContentRestrictionCount(1, content)) { return(false); } CompareOptions compareOptions = RestrictionEvaluator.GetCompareOptions(flags); ContentFlags contentFlags = flags & (ContentFlags.SubString | ContentFlags.Prefix); CompareInfo compareInfo = cultureInfo.CompareInfo; switch (contentFlags) { case ContentFlags.FullString: return(compareInfo.Compare(content, pattern, compareOptions) == 0); case ContentFlags.SubString: return(compareInfo.IndexOf(content, pattern, compareOptions) != -1); case ContentFlags.Prefix: return(compareInfo.IsPrefix(content, pattern, compareOptions)); default: throw new InvalidRuleException(string.Format("Not supported content flags {0}", flags)); } }
public static void Go() { String output = String.Empty; String[] symbol = new String[] { "<", "=", ">" }; Int32 x; CultureInfo ci; // The code below demonstrates how strings compare // differently for different cultures. String s1 = "coté"; String s2 = "côte"; // Sorting strings for French in France. ci = new CultureInfo("fr-FR"); x = Math.Sign(ci.CompareInfo.Compare(s1, s2)); output += String.Format("{0} Compare: {1} {3} {2}", ci.Name, s1, s2, symbol[x + 1]); output += Environment.NewLine; // Sorting strings for Japanese in Japan. ci = new CultureInfo("ja-JP"); x = Math.Sign(ci.CompareInfo.Compare(s1, s2)); output += String.Format("{0} Compare: {1} {3} {2}", ci.Name, s1, s2, symbol[x + 1]); output += Environment.NewLine; // Sorting strings for the thread's culture ci = Thread.CurrentThread.CurrentCulture; x = Math.Sign(ci.CompareInfo.Compare(s1, s2)); output += String.Format("{0} Compare: {1} {3} {2}", ci.Name, s1, s2, symbol[x + 1]); output += Environment.NewLine + Environment.NewLine; // The code below demonstrates how to use CompareInfo.Compare's // advanced options with 2 Japanese strings. One string represents // the word "shinkansen" (the name for the Japanese high-speed // train) in hiragana (one subtype of Japanese writing), and the // other represents the same word in katakana (another subtype of // Japanese writing). s1 = "しんかんせん"; // ("\u3057\u3093\u304B\u3093\u305b\u3093") s2 = "シンカンセン"; // ("\u30b7\u30f3\u30ab\u30f3\u30bb\u30f3") // Here is the result of a default comparison ci = new CultureInfo("ja-JP"); x = Math.Sign(String.Compare(s1, s2, true, ci)); output += String.Format("Simple {0} Compare: {1} {3} {2}", ci.Name, s1, s2, symbol[x + 1]); output += Environment.NewLine; // Here is the result of a comparison that ignores // kana type (a type of Japanese writing) CompareInfo compareInfo = CompareInfo.GetCompareInfo("ja-JP"); x = Math.Sign(compareInfo.Compare(s1, s2, CompareOptions.IgnoreKanaType)); output += String.Format("Advanced {0} Compare: {1} {3} {2}", ci.Name, s1, s2, symbol[x + 1]); MessageBox.Show(output, "Comparing Strings For Sorting"); }
public int Compare(object a, object b) { if (a == b) { return(0); } if (a == null) { return(-1); } if (b == null) { return(1); } if (_compareInfo != null) { var sa = a as String; var sb = b as String; if (sa != null && sb != null) { return(_compareInfo.Compare(sa, sb)); } } var ia = a as IComparable; if (ia != null) { return(ia.CompareTo(b)); } throw new ArgumentException("Object should implement IComparable interface."); }
public int Compare(object item1, object item2) { if (item1 == null) { // If both are null, then they are equal // if (item2 == null) { return(0); } // Otherwise, item1 is null, but item2 is valid (greater) // return(-1); } if (item2 == null) { // item2 is null, so item 1 is greater // return(1); } String itemName1 = _converter.GetCultureName(((CultureInfo)item1)); String itemName2 = _converter.GetCultureName(((CultureInfo)item2)); CompareInfo compInfo = (CultureInfo.CurrentCulture).CompareInfo; return(compInfo.Compare(itemName1, itemName2, CompareOptions.StringSort)); }
// Compares two Objects by calling CompareTo. If a == // b,0 is returned. If a implements // IComparable, a.CompareTo(b) is returned. If a // doesn't implement IComparable and b does, // -(b.CompareTo(a)) is returned, otherwise an // exception is thrown. // public int Compare(Object a, Object b) { if (a == b) { return(0); } if (a == null) { return(-1); } if (b == null) { return(1); } if (m_compareInfo != null) { String sa = a as String; String sb = b as String; if (sa != null && sb != null) { return(m_compareInfo.Compare(sa, sb)); } } IComparable ia = a as IComparable; if (ia != null) { return(ia.CompareTo(b)); } throw new ArgumentException(EnvironmentX.GetResourceString("Argument_ImplementIComparable")); }
int IComparer.Compare(object item1, object item2) { if (item1 == null) { if (item2 == null) { return(0); //both null, then they are equal } return(-1); //item1 is null, but item2 is valid (greater) } if (item2 == null) { return(1); //item2 is null, so item 1 is greater } if (item1 is Entry) { item1 = ((Entry)item1).item; } if (item2 is Entry) { item2 = ((Entry)item2).item; } string itemName1 = listControl.GetItemText(item1); string itemName2 = listControl.GetItemText(item2); CompareInfo compInfo = CultureInfo.CurrentCulture.CompareInfo; return(compInfo.Compare(itemName1, itemName2, CompareOptions.StringSort)); }
public int Compare(object x, object y) { if (x == y) { return(0); } if (x == null) { return(-1); } if (y == null) { return(1); } if (x is string sa && y is string sb) { return(_compareInfo.Compare(sa, sb)); } if (x is IComparable ia) { return(ia.CompareTo(y)); } if (y is IComparable ib) { return(-ib.CompareTo(x)); } throw new ArgumentException("At least one object must implement IComparable."); }
public override int Compare(string x, string y) { CompareOptions co = _ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None; return(_compareInfo.Compare(x, y, co)); }
private static int FilterInternal (String[] source, String Match, bool Include, CompareMethod Compare, String[] result) { int count = 0; CompareInfo compare = CultureInfo.CurrentCulture.CompareInfo; foreach (String value in source) { if (value == null) { continue; } if (Include) { if (Compare == CompareMethod.Binary) { if (value.IndexOf(Match) != -1) { if (result != null) { result[count] = value; } ++count; } } else if (compare.IndexOf (value, Match, CompareOptions.IgnoreCase) != -1) { if (result != null) { result[count] = value; } ++count; } } else if (Compare == CompareMethod.Binary) { if (value == Match) { if (result != null) { result[count] = value; } ++count; } } else if (compare.Compare (value, Match, CompareOptions.IgnoreCase) == 0) { if (result != null) { result[count] = value; } ++count; } } return(count); }
public void TestLocaleAlternateSortOrder(string locale, string string1, string string2, int expected) { CultureInfo myTestCulture = new CultureInfo(locale); CompareInfo ci = myTestCulture.CompareInfo; int actual = ci.Compare(string1, string2); Assert.Equal(expected, actual); }
public int Compare(object x, object y) { TreeNode l = (TreeNode)x; TreeNode r = (TreeNode)y; int res = compare.Compare(l.Text, r.Text); return(res == 0 ? l.Index - r.Index : res); }
private static int CompareChars(string Left, string Right, CompareInfo Comparer, CompareOptions Options) { if (Options == CompareOptions.Ordinal) { return(Left[0] - Right[0]); } return(Comparer.Compare(Left, Right, Options)); }
private static int CompareChars(char Left, char Right, CompareInfo Comparer, CompareOptions Options) { if (Options == CompareOptions.Ordinal) { return(Left - Right); } return(Comparer.Compare(Conversions.ToString(Left), Conversions.ToString(Right), Options)); }
public override int Compare(string x, string y) { EnsureInitialization(); if (Object.ReferenceEquals(x, y)) { return(0); } if (x == null) { return(-1); } if (y == null) { return(1); } return(_compareInfo.Compare(x, y, _options)); }
/// <summary> /// Compares the the attributes of the first LdapEntry to the second. /// Only the values of the attributes named at the construction of this /// object will be compared. Multi-valued attributes compare on the first /// value only. /// </summary> /// <param name="object1"> /// Target entry for comparison. /// </param> /// <param name="object2"> /// Entry to be compared to. /// </param> /// <returns> /// Negative value if the first entry is less than the second and /// positive if the first is greater than the second. Zero is returned if all /// attributes to be compared are the same. /// </returns> public virtual int Compare(object object1, object object2) { var entry1 = (LdapEntry)object1; var entry2 = (LdapEntry)object2; LdapAttribute one, two; string[] first; //multivalued attributes are ignored. string[] second; //we just use the first element int compare, i = 0; if (collator == null) { //using default locale collator = CultureInfo.CurrentCulture.CompareInfo; } do { //while first and second are equal one = entry1.getAttribute(sortByNames[i]); two = entry2.getAttribute(sortByNames[i]); if (one != null && two != null) { first = one.StringValueArray; second = two.StringValueArray; compare = collator.Compare(first[0], second[0]); } //We could also use the other multivalued attributes to break ties. //one of the entries was null else { if (one != null) { compare = -1; } //one is greater than two else if (two != null) { compare = 1; } //one is lesser than two else { compare = 0; //tie - break it with the next attribute name } } i++; } while (compare == 0 && i < sortByNames.Length); if (sortAscending[i - 1]) { // return the normal ascending comparison. return(compare); } // negate the comparison for a descending comparison. return(-compare); }
public void CanIgnoreWhiteSpaceAndPunctuationInComparisons() { string s1 = "Some silly,sentance;"; string s2 = "Some silly sentance"; CompareInfo comparer = Thread.CurrentThread.CurrentCulture.CompareInfo; Assert.That(comparer.Compare(s1, s2, CompareOptions.IgnoreSymbols), Is.EqualTo(0)); }
private Stream CaseInsensitiveManifestResourceStreamLookup(RuntimeAssembly satellite, string name) { StringBuilder builder = new StringBuilder(); if (this._mediator.LocationInfo != null) { string str = this._mediator.LocationInfo.Namespace; if (str != null) { builder.Append(str); if (name != null) { builder.Append(Type.Delimiter); } } } builder.Append(name); string str2 = builder.ToString(); CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo; string str3 = null; foreach (string str4 in satellite.GetManifestResourceNames()) { if (compareInfo.Compare(str4, str2, CompareOptions.IgnoreCase) == 0) { if (str3 != null) { throw new MissingManifestResourceException(Environment.GetResourceString("MissingManifestResource_MultipleBlobs", new object[] { str2, satellite.ToString() })); } str3 = str4; } } if (FrameworkEventSource.IsInitialized) { if (str3 != null) { FrameworkEventSource.Log.ResourceManagerCaseInsensitiveResourceStreamLookupSucceeded(this._mediator.BaseName, this._mediator.MainAssembly, satellite.GetSimpleName(), str2); } else { FrameworkEventSource.Log.ResourceManagerCaseInsensitiveResourceStreamLookupFailed(this._mediator.BaseName, this._mediator.MainAssembly, satellite.GetSimpleName(), str2); } } if (str3 == null) { return(null); } bool skipSecurityCheck = (this._mediator.MainAssembly == satellite) && (this._mediator.CallingAssembly == this._mediator.MainAssembly); StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller; Stream stream = satellite.GetManifestResourceStream(str3, ref lookForMyCaller, skipSecurityCheck); if ((stream != null) && FrameworkEventSource.IsInitialized) { FrameworkEventSource.Log.ResourceManagerManifestResourceAccessDenied(this._mediator.BaseName, this._mediator.MainAssembly, satellite.GetSimpleName(), str3); } return(stream); }
public int Compare(object?a, object?b) { if (a is string sa && b is string sb) { return(_compareInfo.Compare(sa, sb)); } return(Comparer.Default.Compare(a, b)); }
int IComparer <Entry> .Compare(Entry entry1, Entry entry2) { string itemName1 = _owner.GetItemText(entry1.Item); string itemName2 = _owner.GetItemText(entry2.Item); CompareInfo compInfo = Application.CurrentCulture.CompareInfo; return(compInfo.Compare(itemName1, itemName2, CompareOptions.StringSort)); }
public void Compare(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expected) { if (options == CompareOptions.None) { // Use Compare(string, string) Assert.Equal(expected, NormalizeCompare(compareInfo.Compare(string1, string2))); Assert.Equal(-expected, NormalizeCompare(compareInfo.Compare(string2, string1))); } // Use Compare(string, string, CompareOptions) Assert.Equal(expected, NormalizeCompare(compareInfo.Compare(string1, string2, options))); Assert.Equal(-expected, NormalizeCompare(compareInfo.Compare(string2, string1, options))); }
public void Compare(CompareInfo compareInfo, string string1, int offset1, int length1, string string2, int offset2, int length2, CompareOptions options, int expected) { if (offset1 + length1 == (string1?.Length ?? 0) && offset2 + length2 == (string2?.Length ?? 0)) { if (offset1 == 0 && offset2 == 0) { if (options == CompareOptions.None) { // Use Compare(string, string) Assert.Equal(expected, Math.Sign(compareInfo.Compare(string1, string2))); Assert.Equal(-expected, Math.Sign(compareInfo.Compare(string2, string1))); } // Use Compare(string, string, CompareOptions) Assert.Equal(expected, Math.Sign(compareInfo.Compare(string1, string2, options))); Assert.Equal(-expected, Math.Sign(compareInfo.Compare(string2, string1, options))); } if (options == CompareOptions.None) { // Use Compare(string, int, string, int) Assert.Equal(expected, Math.Sign(compareInfo.Compare(string1, offset1, string2, offset2))); Assert.Equal(-expected, Math.Sign(compareInfo.Compare(string2, offset2, string1, offset1))); } // Use Compare(string, int, string, int, CompareOptions) Assert.Equal(expected, Math.Sign(compareInfo.Compare(string1, offset1, string2, offset2, options))); Assert.Equal(-expected, Math.Sign(compareInfo.Compare(string2, offset2, string1, offset1, options))); } if (options == CompareOptions.None) { // Use Compare(string, int, int, string, int, int) Assert.Equal(expected, Math.Sign(compareInfo.Compare(string1, offset1, length1, string2, offset2, length2))); Assert.Equal(-expected, Math.Sign(compareInfo.Compare(string2, offset2, length2, string1, offset1, length1))); } // Use Compare(string, int, int, string, int, int, CompareOptions) Assert.Equal(expected, Math.Sign(compareInfo.Compare(string1, offset1, length1, string2, offset2, length2, options))); Assert.Equal(-expected, Math.Sign(compareInfo.Compare(string2, offset2, length2, string1, offset1, length1, options))); }