static bool collator_sort_internal(bool renumber, Variant &array,
                                   int sort_flags, bool ascending,
                                   UCollator *coll, intl_error * errcode) {
  assert(coll);
  errcode->clear();
  s_intl_error->m_error.clear();
  Array temp = array.toArray();
  Array::PFUNC_CMP cmp_func;

  switch (sort_flags) {
  case COLLATOR_SORT_NUMERIC:
    cmp_func = ascending ? collator_numeric_compare_ascending
                         : collator_numeric_compare_descending;
    break;
  case COLLATOR_SORT_STRING:
    cmp_func = ascending ? collator_string_compare_ascending
                         : collator_string_compare_descending;
    break;
  case COLLATOR_SORT_REGULAR:
  default:
    cmp_func = ascending ? collator_regular_compare_ascending
                         : collator_regular_compare_descending;
    break;
  }

  /* Convert strings in the specified array from UTF-8 to UTF-16. */
  collator_convert_array_from_utf8_to_utf16(temp, &(errcode->code));
  if (U_FAILURE(errcode->code)) {
    errcode->custom_error_message =
      "Error converting array from UTF-8 to UTF-16";
    s_intl_error->m_error.code = errcode->code;
    s_intl_error->m_error.custom_error_message = errcode->custom_error_message;
    return false;
  }

  /* Sort specified array. */
  temp.sort(cmp_func, false, renumber, coll);

  /* Convert strings in the specified array back to UTF-8. */
  errcode->clear();
  s_intl_error->m_error.clear();
  collator_convert_array_from_utf16_to_utf8(temp, &(errcode->code));
  if (U_FAILURE(errcode->code)) {
    errcode->custom_error_message =
      "Error converting array from UTF-16 to UTF-8";
    s_intl_error->m_error.code = errcode->code;
    s_intl_error->m_error.custom_error_message = errcode->custom_error_message;
    return false;
  }
  array = temp;
  return true;
}
Beispiel #2
0
static bool collator_sort_internal(bool renumber, Variant &array,
                                   int sort_flags, bool ascending, bool byKey,
                                   UCollator *coll, Intl::IntlError *errcode) {
  assert(coll);
  errcode->clearError();
  Array temp = array.toArray();
  Array::PFUNC_CMP cmp_func;

  switch (sort_flags) {
  case COLLATOR_SORT_NUMERIC:
    cmp_func = ascending ? collator_numeric_compare_ascending
                         : collator_numeric_compare_descending;
    break;
  case COLLATOR_SORT_STRING:
    cmp_func = ascending ? collator_string_compare_ascending
                         : collator_string_compare_descending;
    break;
  case COLLATOR_SORT_REGULAR:
  default:
    cmp_func = ascending ? collator_regular_compare_ascending
                         : collator_regular_compare_descending;
    break;
  }

  /* Convert strings in the specified array from UTF-8 to UTF-16. */
  UErrorCode error = U_ZERO_ERROR;
  collator_convert_array_from_utf8_to_utf16(temp, &error);
  if (U_FAILURE(error)) {
    errcode->setError(error, "Error converting array from UTF-8 to UTF-16");
    return false;
  }

  /* Sort specified array. */
  temp.sort(cmp_func, byKey, renumber, coll);

  /* Convert strings in the specified array back to UTF-8. */
  errcode->clearError();
  error = U_ZERO_ERROR;
  collator_convert_array_from_utf16_to_utf8(temp, &error);
  if (U_FAILURE(error)) {
    errcode->setError(error, "Error converting array from UTF-16 to UTF-8");
    return false;
  }
  array = temp;
  return true;
}