static Variant HHVM_METHOD(IntlDateFormatter, parse, const String& value, VRefParam position) { DATFMT_GET(data, this_, 0); int32_t pos = position.toInt64(); if (pos > value.size()) { return false; } UErrorCode error = U_ZERO_ERROR; String str(u16(value, error)); if (U_FAILURE(error)) { data->setError(error, "Error converting timezone to UTF-16"); return false; } error = U_ZERO_ERROR; UDate timestamp = udat_parse(data->datefmt(), (UChar*)str.c_str(), str.size() / sizeof(UChar), &pos, &error); position = (int64_t)pos; if (U_FAILURE(error)) { data->setError(error, "Date parsing failed"); return false; } double result = (double)timestamp / U_MILLIS_PER_SECOND; if ((result > LONG_MAX) || (result < -LONG_MAX)) { return (double)((result > 0) ? ceil(result) : floor(result)); } else { return (int64_t)result; } }
static Variant HHVM_METHOD(NumberFormatter, parse, const String& value, int64_t type, VRefParam position) { NUMFMT_GET(obj, this_, false); UErrorCode error = U_ZERO_ERROR; icu::UnicodeString val(u16(value, error)); NUMFMT_CHECK(obj, error, false); Variant ret; int32_t pos = position.toInt64(); error = U_ZERO_ERROR; switch (type) { case UNUM(TYPE_INT32): ret = unum_parse(obj->formatter(), val.getBuffer(), val.length(), &pos, &error); break; case UNUM(TYPE_INT64): ret = unum_parseInt64(obj->formatter(), val.getBuffer(), val.length(), &pos, &error); break; case UNUM(TYPE_DOUBLE): ret = unum_parseDouble(obj->formatter(), val.getBuffer(), val.length(), &pos, &error); break; default: obj->setError(U_UNSUPPORTED_ERROR); return false; } NUMFMT_CHECK(obj, error, false); position = pos; return ret; }
static Variant HHVM_METHOD(IntlDateFormatter, localtime, const String& value, VRefParam position) { DATFMT_GET(data, this_, uninit_null()); int32_t parse_pos = -1; if (!position.isNull()) { parse_pos = position.toInt64(); if (parse_pos > value.size()) { return false; } } UErrorCode error = U_ZERO_ERROR; String uValue(u16(value, error)); if (U_FAILURE(error)) { data->setError(error, "Error converting timezone to UTF-16"); return false; } error = U_ZERO_ERROR; UCalendar *cal = const_cast<UCalendar*>(udat_getCalendar(data->datefmt())); udat_parseCalendar(data->datefmt(), cal, (UChar*)uValue.c_str(), uValue.size() / sizeof(UChar), &parse_pos, &error); Array ret = Array::Create(); error = U_ZERO_ERROR; add_to_localtime_arr(ret, cal, UCAL_SECOND, s_tm_sec, error); add_to_localtime_arr(ret, cal, UCAL_MINUTE, s_tm_min, error); add_to_localtime_arr(ret, cal, UCAL_HOUR_OF_DAY, s_tm_hour, error); add_to_localtime_arr(ret, cal, UCAL_YEAR, s_tm_year, error, -1900); add_to_localtime_arr(ret, cal, UCAL_DAY_OF_MONTH, s_tm_mday, error); add_to_localtime_arr(ret, cal, UCAL_DAY_OF_WEEK, s_tm_wday, error, -1); add_to_localtime_arr(ret, cal, UCAL_DAY_OF_YEAR, s_tm_yday, error); add_to_localtime_arr(ret, cal, UCAL_MONTH, s_tm_mon, error); if (U_FAILURE(error)) { data->setError(error, "Date parsing - localtime failed : " "could not get a field from calendar"); return false; } error = U_ZERO_ERROR; auto isDST = ucal_inDaylightTime(cal, &error); if (U_FAILURE(error)) { data->setError(error, "Date parsing - localtime failed : " "while checking if currently in DST."); return false; } ret.set(s_tm_isdst, isDST ? 1 : 0); position = (int64_t)parse_pos; return ret; }
bool f_settype(VRefParam var, CStrRef type) { if (type == "boolean") var = var.toBoolean(); else if (type == "bool" ) var = var.toBoolean(); else if (type == "integer") var = var.toInt64(); else if (type == "int" ) var = var.toInt64(); else if (type == "float" ) var = var.toDouble(); else if (type == "string" ) var = var.toString(); else if (type == "array" ) var = var.toArray(); else if (type == "object" ) var = var.toObject(); else if (type == "null" ) var = null; else return false; return true; }
bool f_settype(VRefParam var, CStrRef type) { if (type == s_boolean) var = var.toBoolean(); else if (type == s_bool ) var = var.toBoolean(); else if (type == s_integer) var = var.toInt64(); else if (type == s_int ) var = var.toInt64(); else if (type == s_float ) var = var.toDouble(); else if (type == s_string ) var = var.toString(); else if (type == s_array ) var = var.toArray(); else if (type == s_object ) var = var.toObject(); else if (type == s_null ) var = uninit_null(); else return false; return true; }
bool HHVM_FUNCTION(settype, VRefParam var, const String& type) { if (type == s_boolean) var = var.toBoolean(); else if (type == s_bool ) var = var.toBoolean(); else if (type == s_integer) var = var.toInt64(); else if (type == s_int ) var = var.toInt64(); else if (type == s_float ) var = var.toDouble(); else if (type == s_string ) var = var.toString(); else if (type == s_array ) var = var.toArray(); else if (type == s_object ) var = var.toObject(); else if (type == s_null ) var = uninit_null(); else return false; return true; }
static Variant preg_replace_callback_array_impl( const Variant& patterns_and_callbacks, const Array& subjects, int limit, VRefParam count) { Array ret = Array::Create(); auto key = 0; auto total_replacement_count = 0; for (ArrayIter s_iter(subjects); s_iter; ++s_iter) { assert(s_iter.second().isString()); auto subj = s_iter.second(); for (ArrayIter pc_iter(patterns_and_callbacks.toArray()); pc_iter; ++pc_iter) { Variant pattern(pc_iter.first()); assert(pattern.isString()); Variant callback(pc_iter.second()); subj = HHVM_FN(preg_replace_callback)(pattern, callback, subj, limit, count); // If we got an error on the replacement, the subject will be null, // and then we will return null. if (subj.isNull()) { return init_null(); } if (count.isReferenced()) { total_replacement_count += count.toInt64(); } } ret.add(key++, subj); } // If count was passed in as an explicit reference, we will assign it to our // total replacement count; otherwise, count will just remained unassigned count.assignIfRef(total_replacement_count); // If there were no replacements (i.e., matches) return original subject(s) if (ret.empty()) { return subjects; } return ret; }
static Variant HHVM_METHOD(NumberFormatter, parseCurrency, const String& value, VRefParam currency, VRefParam position) { NUMFMT_GET(obj, this_, false); UErrorCode error = U_ZERO_ERROR; icu::UnicodeString val(u16(value, error)); NUMFMT_CHECK(obj, error, false); int32_t pos = position.toInt64(); UChar cur[5] = {0}; error = U_ZERO_ERROR; double parsed = unum_parseDoubleCurrency(obj->formatter(), val.getBuffer(), val.length(), &pos, cur, &error); NUMFMT_CHECK(obj, error, false); position = (int64_t)pos; error = U_ZERO_ERROR; currency = u8(cur, u_strlen(cur), error); NUMFMT_CHECK(obj, error, false); return parsed; }