Example #1
0
void *swift::_swift_stdlib_unicodeCollationIterator_create(
    const __swift_uint16_t *Str, __swift_uint32_t Length) {
  UErrorCode ErrorCode = U_ZERO_ERROR;
#if defined(__CYGWIN__) || defined( _MSC_VER) || defined(__linux__)
  UCollationElements *CollationIterator = ucol_openElements(
    GetRootCollator(), reinterpret_cast<const UChar *>(Str), Length,
    &ErrorCode);
#else
  UCollationElements *CollationIterator = ucol_openElements(
    GetRootCollator(), Str, Length, &ErrorCode);
#endif
  if (U_FAILURE(ErrorCode)) {
    swift::crash("_swift_stdlib_unicodeCollationIterator_create: ucol_openElements() failed.");
  }
  return CollationIterator;
}
  /// Construct the ASCII collation table.
  ASCIICollation() {
    const UCollator *Collator = GetRootCollator();
    for (unsigned char c = 0; c < 128; ++c) {
      UErrorCode ErrorCode = U_ZERO_ERROR;
      intptr_t NumCollationElts = 0;
#if defined(__CYGWIN__) || defined(_MSC_VER) || defined(__MINGW32__)
      UChar Buffer[1];
#else
      uint16_t Buffer[1];
#endif
      Buffer[0] = c;

      UCollationElements *CollationIterator =
          ucol_openElements(Collator, Buffer, 1, &ErrorCode);

      while (U_SUCCESS(ErrorCode)) {
        intptr_t Elem = ucol_next(CollationIterator, &ErrorCode);
        if (Elem != UCOL_NULLORDER) {
          CollationTable[c] = Elem;
          ++NumCollationElts;
        } else {
          break;
        }
      }

      ucol_closeElements(CollationIterator);
      if (U_FAILURE(ErrorCode) || NumCollationElts != 1) {
        swift::crash("Error setting up the ASCII collation table");
      }
    }
  }
int32_t _swift_stdlib_unicode_compare_utf16_utf16(const uint16_t *LeftString,
                                                  int32_t LeftLength,
                                                  const uint16_t *RightString,
                                                  int32_t RightLength) {
  return ucol_strcoll(GetRootCollator(),
    LeftString, LeftLength,
    RightString, RightLength);
}
/// Compares the strings via the Unicode Collation Algorithm on the root locale.
/// Results are the usual string comparison results:
///  <0 the left string is less than the right string.
/// ==0 the strings are equal according to their collation.
///  >0 the left string is greater than the right string.
int32_t
swift::_swift_stdlib_unicode_compare_utf16_utf16(const uint16_t *LeftString,
                                                 int32_t LeftLength,
                                                 const uint16_t *RightString,
                                                 int32_t RightLength) {
#if defined(__CYGWIN__) || defined(_MSC_VER) || defined(__MINGW32__)
  // ICU UChar type is platform dependent. In Cygwin, it is defined
  // as wchar_t which size is 2. It seems that the underlying binary
  // representation is same with swift utf16 representation.
  return ucol_strcoll(GetRootCollator(),
    reinterpret_cast<const UChar *>(LeftString), LeftLength,
    reinterpret_cast<const UChar *>(RightString), RightLength);
#else
  return ucol_strcoll(GetRootCollator(),
    LeftString, LeftLength,
    RightString, RightLength);
#endif
}
Example #5
0
void *swift::_swift_stdlib_unicodeCollationIterator_create(
    const __swift_uint16_t *Str, __swift_uint32_t Length) {
  UErrorCode ErrorCode = U_ZERO_ERROR;
  UCollationElements *CollationIterator =
      ucol_openElements(GetRootCollator(), reinterpret_cast<const UChar *>(Str),
                        Length, &ErrorCode);
  if (U_FAILURE(ErrorCode)) {
    swift::crash("_swift_stdlib_unicodeCollationIterator_create: ucol_openElements() failed.");
  }
  return CollationIterator;
}
int32_t _swift_stdlib_unicode_compare_utf8_utf8(const char *LeftString,
                                                int32_t LeftLength,
                                                const char *RightString,
                                                int32_t RightLength) {
  UCharIterator LeftIterator;
  UCharIterator RightIterator;
  UErrorCode ErrorCode = U_ZERO_ERROR;

  uiter_setUTF8(&LeftIterator, LeftString, LeftLength);
  uiter_setUTF8(&RightIterator, RightString, RightLength);

  uint32_t Diff = ucol_strcollIter(GetRootCollator(),
    &LeftIterator, &RightIterator, &ErrorCode);
  if (U_FAILURE(ErrorCode)) {
    swift::crash("ucol_strcollIter: Unexpected error doing utf8<->utf8 string comparison.");
  }
  return Diff;
}
/// Compares the strings via the Unicode Collation Algorithm on the root locale.
/// Results are the usual string comparison results:
///  <0 the left string is less than the right string.
/// ==0 the strings are equal according to their collation.
///  >0 the left string is greater than the right string.
int32_t
swift::_swift_stdlib_unicode_compare_utf8_utf16(const char *LeftString,
                                                int32_t LeftLength,
                                                const uint16_t *RightString,
                                                int32_t RightLength) {
  UCharIterator LeftIterator;
  UCharIterator RightIterator;
  UErrorCode ErrorCode = U_ZERO_ERROR;

  uiter_setUTF8(&LeftIterator, LeftString, LeftLength);
#if defined(__CYGWIN__) || defined(_MSC_VER) || defined(__MINGW32__)
  uiter_setString(&RightIterator, reinterpret_cast<const UChar *>(RightString),
                  RightLength);
#else
  uiter_setString(&RightIterator, RightString, RightLength);
#endif

  uint32_t Diff = ucol_strcollIter(GetRootCollator(),
    &LeftIterator, &RightIterator, &ErrorCode);
  if (U_FAILURE(ErrorCode)) {
    swift::crash("ucol_strcollIter: Unexpected error doing utf8<->utf16 string comparison.");
  }
  return Diff;
}