/// <summary> /// <p> /// Compare this CollationKey and the argument target CollationKey for /// equality. The collation rules of the Collator object which created these /// objects are applied. /// </p> /// <p> /// See note in compareTo(CollationKey) for warnings of incorrect results /// </p> /// </summary> /// /// <param name="target">the CollationKey to compare to.</param> /// <returns>true if two objects are equal, false otherwise.</returns> /// <exception cref="NullPointerException">is thrown when the argument is null.</exception> /// @stable ICU 2.8 public bool Equals(CollationKey target) { if (this == target) { return(true); } if (target == null) { return(false); } CollationKey other = (CollationKey)target; int i = 0; while (true) { if (m_key_[i] != other.m_key_[i]) { return(false); } if (m_key_[i] == 0) { break; } i++; } return(true); }
/// <summary> /// Obtains the <c>CollationKey</c> for the given source text. /// </summary> /// /// <param name="source">the specified source text</param> /// <returns>the <c>CollationKey</c> for the given source text.</returns> public override CollationKey GetCollationKey(String source) { IBM.ICU.Text.CollationKey icuKey = this.icuColl .GetCollationKey(source); if (icuKey == null) { return(null); } return(new CollationKey(source, icuKey)); }
// public other methods ------------------------------------------------- /// <summary> /// <p> /// Compare this CollationKey to another CollationKey. The collation rules of /// the Collator that created this key are applied. /// </p> /// <p> /// <strong>Note:</strong> Comparison between CollationKeys created by /// different Collators might return incorrect results. See class /// documentation. /// </p> /// </summary> /// /// <param name="target">target CollationKey</param> /// <returns>an integer value. If the value is less than zero this /// CollationKey is less than than target, if the value is zero they /// are equal, and if the value is greater than zero this /// CollationKey is greater than target.</returns> /// <exception cref="NullPointerException">is thrown if argument is null.</exception> /// <seealso cref="M:IBM.ICU.Text.Collator.Compare(System.String, System.String)"/> /// @stable ICU 2.8 public int CompareTo(CollationKey target) { for (int i = 0;; ++i) { int l = m_key_[i] & 0xff; int r = target.m_key_[i] & 0xff; if (l < r) { return(-1); } else if (l > r) { return(1); } else if (l == 0) { return(0); } } }
/// <summary> /// <p> /// Merges this CollationKey with another. Only the sorting order of the /// CollationKeys will be merged. This API does not attempt to merge the /// String representations of the CollationKeys, hence null will be returned /// as the String representation. /// </p> /// <p> /// The strength levels are merged with their corresponding counterparts /// (PRIMARIES with PRIMARIES, SECONDARIES with SECONDARIES etc.). /// </p> /// <p> /// The merged String representation of the result CollationKey will be a /// concatenation of the String representations of the 2 source /// CollationKeys. /// </p> /// <p> /// Between the values from the same level a separator is inserted. example /// (uncompressed): /// <pre> /// 191B1D 01 050505 01 910505 00 and 1F2123 01 050505 01 910505 00 /// will be merged as /// 191B1D 02 1F212301 050505 02 050505 01 910505 02 910505 00 /// </pre> /// </p> /// <p> /// This allows for concatenating of first and last names for sorting, among /// other things. /// </p> /// </p> /// </summary> /// /// <param name="source">CollationKey to merge with</param> /// <returns>a CollationKey that contains the valid merged sorting order with /// a null String representation, i.e. /// <tt>new CollationKey(null, merge_sort_order)</tt></returns> /// <exception cref="IllegalArgumentException">thrown if source CollationKey argument is null or of 0length.</exception> /// @stable ICU 2.6 public CollationKey Merge(CollationKey source) { // check arguments if (source == null || source.GetLength() == 0) { throw new ArgumentException( "CollationKey argument can not be null or of 0 length"); } GetLength(); // gets the length of this sort key int sourcelength = source.GetLength(); byte[] result = new byte[m_length_ + sourcelength + 2]; // merge the sort keys with the same number of levels int rindex = 0; int index = 0; int sourceindex = 0; while (true) { // while both have another level // copy level from src1 not including 00 or 01 // unsigned issues while (m_key_[index] < 0 || m_key_[index] >= MERGE_SEPERATOR_) { result[rindex++] = m_key_[index++]; } // add a 02 merge separator result[rindex++] = MERGE_SEPERATOR_; // copy level from src2 not including 00 or 01 while (source.m_key_[sourceindex] < 0 || source.m_key_[sourceindex] >= MERGE_SEPERATOR_) { result[rindex++] = source.m_key_[sourceindex++]; } // if both sort keys have another level, then add a 01 level // separator and continue if (m_key_[index] == IBM.ICU.Text.RuleBasedCollator.SORT_LEVEL_TERMINATOR_ && source.m_key_[sourceindex] == IBM.ICU.Text.RuleBasedCollator.SORT_LEVEL_TERMINATOR_) { ++index; ++sourceindex; result[rindex++] = IBM.ICU.Text.RuleBasedCollator.SORT_LEVEL_TERMINATOR_; } else { break; } } // here, at least one sort key is finished now, but the other one // might have some contents left from containing more levels; // that contents is just appended to the result if (m_key_[index] != 0) { System.Array.Copy((Array)(m_key_), index, (Array)(result), rindex, m_length_ - index); } else if (source.m_key_[sourceindex] != 0) { System.Array.Copy((Array)(source.m_key_), sourceindex, (Array)(result), rindex, source.m_length_ - sourceindex); } result[result.Length - 1] = 0; // trust that neither sort key contained illegally embedded zero bytes return(new CollationKey(null, result)); }