Exemplo n.º 1
0
static Array HHVM_STATIC_METHOD(Locale, getKeywords, const String& locale) {
  UErrorCode error = U_ZERO_ERROR;
  String locname = localeOrDefault(locale);
  UEnumeration *e = uloc_openKeywords(locname.c_str(), &error);
  if (!e) return null_array;

  Array ret = Array::Create();
  const char *key;
  int key_len;
  String val(128, ReserveString);
  char *ptr = val->mutableData();
  error = U_ZERO_ERROR;
  while ((key = uenum_next(e, &key_len, &error))) {
tryagain:
    error = U_ZERO_ERROR;
    int val_len = uloc_getKeywordValue(locname.c_str(), key,
                                       ptr, val->capacity(), &error);
    if (error == U_BUFFER_OVERFLOW_ERROR) {
      val = String(val_len + 128, ReserveString);
      ptr = val->mutableData();
      goto tryagain;
    }
    if (U_FAILURE(error)) {
      s_intl_error->setError(error, "locale_get_keywords: Error encountered "
                                    "while getting the keyword  value for the "
                                    " keyword");
      return null_array;
    }
    ret.set(String(key, key_len, CopyString), String(ptr, val_len, CopyString));
  }
  return ret;
}
Exemplo n.º 2
0
static void HHVM_METHOD(ResourceBundle, __construct, const Variant& locale,
                                                     const Variant& bundleName,
                                                     bool fallback) {
  const char *bundle = bundleName.isNull() ? nullptr :
                       bundleName.toString().c_str();
  auto loc = Locale::createFromName(localeOrDefault(locale.toString()).c_str());
  auto data = Native::data<ResourceBundle>(this_.get());
  UErrorCode error = U_ZERO_ERROR;
  auto rsrc = new icu::ResourceBundle(bundle, loc, error);
  if (U_FAILURE(error)) {
    s_intl_error->setError(error, "resourcebundle_ctor: "
                                  "Cannot load libICU resource bundle");
    throw data->getException("%s", s_intl_error->getErrorMessage().c_str());
  }
  if (!fallback &&
      ((error == U_USING_FALLBACK_WARNING) ||
       (error == U_USING_DEFAULT_WARNING))) {
    UErrorCode dummy = U_ZERO_ERROR;
    s_intl_error->setError(error,
      "resourcebundle_ctor: Cannot load libICU resource "
      "'%s' without fallback from %s to %s",
      bundle ? bundle : "(default data)", loc.getName(),
      rsrc->getLocale(ULOC_ACTUAL_LOCALE, dummy).getName());
    delete rsrc;
    throw data->getException("%s", s_intl_error->getErrorMessage().c_str());
  }
  data->setResource(rsrc);
}
Exemplo n.º 3
0
static Variant HHVM_STATIC_METHOD(IntlCalendar, getKeywordValuesForLocale,
                                 const String& key, const String& locale,
                                 bool common) {
  UErrorCode error = U_ZERO_ERROR;
  UEnumeration *uenum = ucal_getKeywordValuesForLocale(key.c_str(),
                                   localeOrDefault(locale).c_str(),
                                   common, &error);
  if (U_FAILURE(error)) {
    if (uenum) { uenum_close(uenum); }
    s_intl_error->setError(error, "intlcal_get_keyword_values_for_locale: "
                                  "error calling underlying method");
    return false;
  }
  return IntlIterator::newInstance(new BugStringCharEnumeration(uenum));
}
Exemplo n.º 4
0
static Array HHVM_STATIC_METHOD(Locale, parseLocale, const String& locale) {
  String locname = localeOrDefault(locale);
  Array ret = Array::Create();
  if (std::find(g_grandfathered.begin(),
                g_grandfathered.end(), locale.data()) !=
                g_grandfathered.end()) {
    ret.set(s_GRANDFATHERED, locname);
    return ret;
  }
  add_array_entry(ret, locname, LOC_LANG);
  add_array_entry(ret, locname, LOC_SCRIPT);
  add_array_entry(ret, locname, LOC_REGION);
  add_array_entry(ret, locname, LOC_VARIANT);
  add_array_entry(ret, locname, LOC_PRIVATE);
  return ret;
}
Exemplo n.º 5
0
static String HHVM_STATIC_METHOD(Locale, lookup, CArrRef langtag,
                                 const String& locale,
                                 bool canonicalize, const String& def) {
  String locname(localeOrDefault(locale), CopyString);
  std::vector<std::pair<String,String>> cur_arr;
  for (ArrayIter iter(langtag); iter; ++iter) {
    auto val = iter.second();
    if (!val.isString()) {
      s_intl_error->setError(U_ILLEGAL_ARGUMENT_ERROR, "lookup_loc_range: "
                             "locale array element is not a string");
      return def;
    }
    String normalized(val.toString(), CopyString);
    normalize_for_match(normalized);
    if (canonicalize) {
      normalized = get_icu_value(normalized, LOC_CANONICALIZE);
      if (normalized.isNull()) {
        s_intl_error->setError(U_ILLEGAL_ARGUMENT_ERROR, "lookup_loc_range: "
                               "unable to canonicalize lang_tag");
        return def;
      }
      normalize_for_match(normalized);
    }
    cur_arr.push_back(std::make_pair(val,normalized));
  }

  if (canonicalize) {
    locname = get_icu_value(locname, LOC_CANONICALIZE);
    if (locname.isNull()) {
      s_intl_error->setError(U_ILLEGAL_ARGUMENT_ERROR, "lookup_loc_range: "
                             "unable to canonicalize loc_range");
      return def;
    }
  }

  normalize_for_match(locname);
  while (locname.size() > 0) {
    for (auto &p : cur_arr) {
      if (locname.same(p.second)) {
        return canonicalize ? p.second : p.first;
      }
    }
    locname = locale_suffix_strip(locname);
  }
  return def;
}
Exemplo n.º 6
0
static Variant HHVM_METHOD(IntlTimeZone, getDisplayName,
                           bool isDaylight, int64_t style,
                           const String& locale) {
  if (!IntlTimeZone::isValidStyle(style)) {
    s_intl_error->set(U_ILLEGAL_ARGUMENT_ERROR,
                      "intltz_get_display_name: wrong display type");
    return false;
  }
  TZ_GET(data, this_, false);
  icu::UnicodeString result;
  data->timezone()->getDisplayName((UBool)isDaylight,
                                   (icu::TimeZone::EDisplayType)style,
                                    icu::Locale::createFromName(
                                     localeOrDefault(locale).c_str()),
                                   result);
  UErrorCode error = U_ZERO_ERROR;
  String ret(u8(result, error));
  TZ_CHECK(data, error, false);
  return ret;
}
Exemplo n.º 7
0
static Object HHVM_STATIC_METHOD(IntlCalendar, createInstance,
                                 const Variant& timeZone, const String& locale) {
  icu::TimeZone *tz =
    IntlTimeZone::ParseArg(timeZone, "intlcal_create_instance",
                           s_intl_error.get());
  if (!tz) {
    return Object();
  }
  String loc = localeOrDefault(locale);
  UErrorCode error = U_ZERO_ERROR;
  icu::Calendar *cal =
    icu::Calendar::createInstance(tz, icu::Locale::createFromName(loc.c_str()),
                                  error);
  if (!cal) {
    delete tz;
    s_intl_error->setError(error, "Error creating ICU Calendar object");
    return Object();
  }
  return IntlCalendar::newInstance(cal);
}
Exemplo n.º 8
0
static Array HHVM_STATIC_METHOD(Locale, getAllVariants, const String& locale) {
  Variant val = get_icu_value(localeOrDefault(locale), LOC_VARIANT);
  String strval = val.toString();
  if (strval.empty()) {
    return null_array;
  }
  Array ret = Array::Create();
  const char *s = strval.c_str(), *e = s + strval.size(), *p;
  for (p = s; p < e; ++p) {
    if (!isIDSeparator(*p)) continue;
    if ((p - s) <= 1) {
      return ret;
    }
    ret.append(String(s, p - s, CopyString));
    s = p + 1;
  }
  if ((e - s) > 1) {
    ret.append(String(s, e - s, CopyString));
  }
  return ret;
}
Exemplo n.º 9
0
static String HHVM_STATIC_METHOD(Locale, getDisplayScript,
                                 const String& locale,
                                 const String& in_locale) {
  return get_icu_display_value(localeOrDefault(locale),
                               localeOrDefault(in_locale), LOC_SCRIPT);
}
Exemplo n.º 10
0
static void HHVM_METHOD(IntlGregorianCalendar, __ctor_array,
                        const Array& args) {
  assert(args.size() == 6);

  int32_t numargs;
  for (numargs = 0; numargs < 6; ++numargs) {
    if (args[numargs].isNull()) {
      break;
    }
  }

  if (numargs > 6) {
    s_intl_error->setError(U_ILLEGAL_ARGUMENT_ERROR,
                           "intlgregcal_create_instance: too many arguments");
    return;
  }

  icu::GregorianCalendar *gcal = nullptr;
  SCOPE_EXIT { if (gcal) { delete gcal; } };
  icu::TimeZone *tz = nullptr;
  SCOPE_EXIT { if (gcal && tz) { delete tz; } };

  UErrorCode error;
  if (numargs < 3) {
    tz = IntlTimeZone::ParseArg(args[0], "intlgregcal_create_instance",
                                         s_intl_error.get());
    String loc(localeOrDefault(args[1].toString()));
    error = U_ZERO_ERROR;
    gcal = new icu::GregorianCalendar(tz,
               icu::Locale::createFromName(loc.c_str()), error);
    if (U_FAILURE(error)) {
      s_intl_error->setError(error, "intlgregcal_create_instance: error "
               "creating ICU GregorianCalendar from time zone and locale");
      return;
    }
    goto success;
  }

  int32_t intarg[6];
  assert(numargs <= 6);
  for (int i = 0; i < numargs; ++i) {
    int64_t arg = args[i].toInt64();
    if ((arg < INT32_MIN) || (arg > INT32_MAX)) {
      s_intl_error->setError(U_ILLEGAL_ARGUMENT_ERROR,
                             "intlgregcal_create_instance: at least one of "
                             "the arguments has an absolute value that is "
                             "too large");
      return;
    }
    intarg[i] = (int32_t)arg;
  }

  error = U_ZERO_ERROR;
  switch (numargs) {
    case 3: // year, month, day
      gcal = new icu::GregorianCalendar(intarg[0], intarg[1], intarg[2],
                                        error);
      break;
    case 4:
      s_intl_error->setError(U_ILLEGAL_ARGUMENT_ERROR,
                             "intlgregcal_create_instance: no variant with "
                             "4 arguments (excluding trailing NULLs)");
      return;
    case 5: // ..., hour, minute
      gcal = new icu::GregorianCalendar(intarg[0], intarg[1], intarg[2],
                                        intarg[3], intarg[4],
                                        error);
      break;
    case 6: // ..., second
      gcal = new icu::GregorianCalendar(intarg[0], intarg[1], intarg[2],
                                        intarg[3], intarg[4], intarg[5],
                                        error);
      break;
    default:
      not_reached();
      return;
  }
  if (U_FAILURE(error)) {
    s_intl_error->setError(error, "intlgregcal_create_instance: error "
                                  "creating ICU GregorianCalendar from date");
    return;
  }

  tz = IntlTimeZone::ParseArg(uninit_null(), "intlgregcal_create_instance",
                                             s_intl_error.get());
  if (!tz) {
    // error already set
    return;
  }
  gcal->adoptTimeZone(tz);

success:
  Native::data<IntlCalendar>(this_)->setCalendar(gcal);
  gcal = nullptr; // prevent SCOPE_EXIT sweeps
}
Exemplo n.º 11
0
static Variant HHVM_STATIC_METHOD(Locale, canonicalize, const String& locale) {
  return get_icu_value(localeOrDefault(locale), LOC_CANONICALIZE);
}
Exemplo n.º 12
0
static Variant HHVM_STATIC_METHOD(Locale, getScript, const String& locale) {
  return get_icu_value(localeOrDefault(locale), LOC_SCRIPT);
}
Exemplo n.º 13
0
static Variant HHVM_STATIC_METHOD(Locale, getRegion, const String& locale) {
  return get_icu_value(localeOrDefault(locale), LOC_REGION);
}
Exemplo n.º 14
0
static String HHVM_STATIC_METHOD(Locale, getPrimaryLanguage,
                                 const String& locale) {
  return get_icu_value(localeOrDefault(locale), LOC_LANG);
}
Exemplo n.º 15
0
static String HHVM_STATIC_METHOD(Locale, getDisplayVariant,
                                 const String& locale,
                                 const String& in_locale) {
  return get_icu_display_value(localeOrDefault(locale),
                               localeOrDefault(in_locale), LOC_VARIANT);
}
Exemplo n.º 16
0
static String HHVM_STATIC_METHOD(Locale, getDisplayLanguage,
                                 const String& locale,
                                 const String& in_locale) {
  return get_icu_display_value(localeOrDefault(locale),
                               localeOrDefault(in_locale), LOC_LANG);
}
Exemplo n.º 17
0
static String HHVM_STATIC_METHOD(Locale, getDisplayRegion,
                                 const String& locale,
                                 const String& in_locale) {
  return get_icu_display_value(localeOrDefault(locale),
                               localeOrDefault(in_locale), LOC_REGION);
}
Exemplo n.º 18
0
static String HHVM_STATIC_METHOD(Locale, getDisplayName,
                                 const String& locale,
                                 const String& in_locale) {
  return get_icu_display_value(localeOrDefault(locale),
                               localeOrDefault(in_locale), LOC_DISPLAY);
}