/* {{{ */ static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) { char* locale; char* pattern; int locale_len = 0, pattern_len = 0; UChar* spattern = NULL; int spattern_len = 0; zval* object; MessageFormatter_object* mfo; intl_error_reset( NULL TSRMLS_CC ); object = return_value; /* Parse parameters. */ if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "ss", &locale, &locale_len, &pattern, &pattern_len ) == FAILURE ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "msgfmt_create: unable to parse input parameters", 0 TSRMLS_CC ); zval_dtor(return_value); RETURN_NULL(); } INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value); MSG_FORMAT_METHOD_FETCH_OBJECT; /* Convert pattern (if specified) to UTF-16. */ if(pattern && pattern_len) { intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo)); INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16"); } else { spattern_len = 0; spattern = NULL; } if(locale_len == 0) { locale = INTL_G(default_locale); } #ifdef MSG_FORMAT_QUOTE_APOS if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) { INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format"); } #endif if ((mfo)->mf_data.orig_format) { msgformat_data_free(&mfo->mf_data TSRMLS_CC); } (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len); (mfo)->mf_data.orig_format_len = pattern_len; /* Create an ICU message formatter. */ MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(mfo)); if(spattern) { efree(spattern); } INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed"); }
static void TestErrorChaining(void) { UErrorCode status = U_USELESS_COLLATOR_ERROR; umsg_open(NULL, 0, NULL, NULL, &status); umsg_applyPattern(NULL, NULL, 0, NULL, &status); umsg_toPattern(NULL, NULL, 0, &status); umsg_clone(NULL, &status); umsg_format(NULL, NULL, 0, &status); umsg_parse(NULL, NULL, 0, NULL, &status); umsg_close(NULL); /* All of this code should have done nothing. */ if (status != U_USELESS_COLLATOR_ERROR) { log_err("Status got changed to %s\n", u_errorName(status)); } status = U_ZERO_ERROR; umsg_open(NULL, 0, NULL, NULL, &status); if (status != U_ILLEGAL_ARGUMENT_ERROR) { log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); } status = U_ZERO_ERROR; umsg_applyPattern(NULL, NULL, 0, NULL, &status); if (status != U_ILLEGAL_ARGUMENT_ERROR) { log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); } status = U_ZERO_ERROR; umsg_toPattern(NULL, NULL, 0, &status); if (status != U_ILLEGAL_ARGUMENT_ERROR) { log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); } status = U_ZERO_ERROR; umsg_clone(NULL, &status); if (status != U_ILLEGAL_ARGUMENT_ERROR) { log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); } }
U_CAPI void U_EXPORT2 u_vparseMessage(const char *locale, const UChar *pattern, int32_t patternLength, const UChar *source, int32_t sourceLength, va_list ap, UErrorCode *status) { //argument checking defered to subsequent method calls UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,NULL,status); int32_t count = 0; umsg_vparse(fmt,source,sourceLength,&count,ap,status); umsg_close(fmt); }
U_CAPI int32_t U_EXPORT2 u_vformatMessage( const char *locale, const UChar *pattern, int32_t patternLength, UChar *result, int32_t resultLength, va_list ap, UErrorCode *status) { //argument checking defered to subsequent method calls UMessageFormat *fmt = umsg_open(pattern,patternLength,locale,NULL,status); int32_t retVal = umsg_vformat(fmt,result,resultLength,ap,status); umsg_close(fmt); return retVal; }
// Try LocalXyzPointer types with NULL pointers. void LocalPointerTest::TestLocalXyzPointerNull() { { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer"); static const char *const encoding="ISO-8859-1"; LocalUConverterSelectorPointer null; LocalUConverterSelectorPointer sel( ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode)); sel.adoptInstead(NULL); } #if !UCONFIG_NO_FORMATTING { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer"); LocalUCalendarPointer null; LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode)); if(!errorCode.logDataIfFailureAndReset("ucal_open()")) { cal.adoptInstead(NULL); } } { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer"); LocalUDateTimePatternGeneratorPointer null; LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode)); patgen.adoptInstead(NULL); } { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer"); UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!"); LocalUMessageFormatPointer null; LocalUMessageFormatPointer msg( umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode)); msg.adoptInstead(NULL); } #endif /* !UCONFIG_NO_FORMATTING */ #if !UCONFIG_NO_REGULAR_EXPRESSIONS { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer"); UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z"); LocalURegularExpressionPointer null; LocalURegularExpressionPointer regex( uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode)); if(!errorCode.logDataIfFailureAndReset("urege_open()")) { regex.adoptInstead(NULL); } } #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */ #if !UCONFIG_NO_TRANSLITERATION { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer"); UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn"); LocalUTransliteratorPointer null; LocalUTransliteratorPointer trans( utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode)); if(!errorCode.logDataIfFailureAndReset("utrans_openU()")) { trans.adoptInstead(NULL); } } #endif /* !UCONFIG_NO_TRANSLITERATION */ }
// Use LocalXyzPointer types that are not covered elsewhere in the intltest suite. void LocalPointerTest::TestLocalXyzPointer() { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointer"); static const char *const encoding="ISO-8859-1"; LocalUConverterSelectorPointer sel( ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode)); if(errorCode.logIfFailureAndReset("ucnvsel_open()")) { return; } if(sel.isNull()) { errln("LocalUConverterSelectorPointer failure"); return; } #if !UCONFIG_NO_FORMATTING LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode)); if(errorCode.logDataIfFailureAndReset("ucal_open()")) { return; } if(cal.isNull()) { errln("LocalUCalendarPointer failure"); return; } LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode)); if(errorCode.logDataIfFailureAndReset("udatpg_open()")) { return; } if(patgen.isNull()) { errln("LocalUDateTimePatternGeneratorPointer failure"); return; } LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode)); if(errorCode.logIfFailureAndReset("uldn_open()")) { return; } if(ldn.isNull()) { errln("LocalULocaleDisplayNamesPointer failure"); return; } UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!"); LocalUMessageFormatPointer msg( umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode)); if(errorCode.logIfFailureAndReset("umsg_open()")) { return; } if(msg.isNull()) { errln("LocalUMessageFormatPointer failure"); return; } #endif /* UCONFIG_NO_FORMATTING */ #if !UCONFIG_NO_NORMALIZATION const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode); UnicodeSet emptySet; LocalUNormalizer2Pointer fn2(unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode)); if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) { return; } if(fn2.isNull()) { errln("LocalUNormalizer2Pointer failure"); return; } #endif /* !UCONFIG_NO_NORMALIZATION */ #if !UCONFIG_NO_IDNA LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode)); if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) { return; } if(idna.isNull()) { errln("LocalUIDNAPointer failure"); return; } #endif /* !UCONFIG_NO_IDNA */ #if !UCONFIG_NO_REGULAR_EXPRESSIONS UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z"); LocalURegularExpressionPointer regex( uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode)); if(errorCode.logIfFailureAndReset("uregex_open()")) { return; } if(regex.isNull()) { errln("LocalURegularExpressionPointer failure"); return; } #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ #if !UCONFIG_NO_TRANSLITERATION UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn"); LocalUTransliteratorPointer trans( utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode)); if(errorCode.logIfFailureAndReset("utrans_open()")) { return; } if(trans.isNull()) { errln("LocalUTransliteratorPointer failure"); return; } #endif /* !UCONFIG_NO_TRANSLITERATION */ // destructors }
static void OpenMessageFormatTest(void) { UMessageFormat *f1, *f2, *f3; UChar pattern[256]; UChar result[256]; char cresult[256]; UParseError parseError; const char* locale = "hi_IN"; char* retLoc; const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}"; int32_t length=0; UErrorCode status = U_ZERO_ERROR; u_uastrncpy(pattern, PAT, sizeof(pattern)/sizeof(pattern[0])); /* Test umsg_open */ f1 = umsg_open(pattern,length,NULL,NULL,&status); if(U_FAILURE(status)) { log_err("umsg_open failed with pattern %s. Error: \n", PAT, u_errorName(status)); return; } /* Test umsg_open with parse error */ status = U_ZERO_ERROR; f2 = umsg_open(pattern,length,NULL,&parseError,&status); if(U_FAILURE(status)) { log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT, u_errorName(status)); return; } /* Test umsg_clone */ status = U_ZERO_ERROR; f3 = umsg_clone(f1,&status); if(U_FAILURE(status)) { log_err("umsg_clone failed. Error %s \n", u_errorName(status)); } /* Test umsg_setLocale */ umsg_setLocale(f1,locale); /* Test umsg_getLocale */ retLoc = (char*)umsg_getLocale(f1); if(strcmp(retLoc,locale)!=0) { log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale, retLoc); } /* Test umsg_applyPattern */ status = U_ZERO_ERROR; umsg_applyPattern(f1,pattern,(int32_t)strlen(PAT),NULL,&status); if(U_FAILURE(status)) { log_data_err("umsg_applyPattern failed. Error %s (Are you missing data?)\n",u_errorName(status)); } /* Test umsg_toPattern */ umsg_toPattern(f1,result,256,&status); if(U_FAILURE(status) ){ log_data_err("umsg_toPattern method failed. Error: %s (Are you missing data?)\n",u_errorName(status)); } else { if(u_strcmp(result,pattern)!=0){ u_UCharsToChars(result,cresult,256); log_err("umsg_toPattern method failed. Expected: %s Got: %s \n",PAT,cresult); } } /* umsg_format umsg_parse */ umsg_close(f1); umsg_close(f2); umsg_close(f3); }
/* Test u_formatMessage() with various test patterns() */ static void MessageFormatTest( void ) { UChar *str; UChar* result; int32_t resultLengthOut,resultlength,i, patternlength; UErrorCode status = U_ZERO_ERROR; UDate d1=1000000000.0; ctest_setTimeZone(NULL, &status); str=(UChar*)malloc(sizeof(UChar) * 7); u_uastrncpy(str, "MyDisk", 7); resultlength=1; result=(UChar*)malloc(sizeof(UChar) * 1); log_verbose("Testing u_formatMessage()\n"); InitStrings(); for (i = 0; i < cnt_testCases; i++) { status=U_ZERO_ERROR; patternlength=u_strlen(testCasePatterns[i]); resultLengthOut=u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); if(status== U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result,sizeof(UChar) * resultlength); u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); } if(U_FAILURE(status)){ log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); continue; } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } } free(result); result = NULL; free(str); { for (i = 0; i < cnt_testCases; i++) { UParseError parseError; status=U_ZERO_ERROR; patternlength=u_strlen(testCasePatterns[i]); resultlength=0; resultLengthOut=u_formatMessageWithError( "en_US",testCasePatterns[i], patternlength, result, resultlength, &parseError,&status, 1, 3456.00, d1); if(status== U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)malloc(sizeof(UChar) * resultlength); u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); } if(U_FAILURE(status)){ log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); continue; } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } free(result); result=NULL; } } { UErrorCode ec = U_ZERO_ERROR; int32_t patternLength = u_strlen(testCasePatterns[0]); UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec); if(U_FAILURE(ec)){ log_data_err("umsg_open() failed for testCasePattens[%d]. -> %s (Are you missing data?)\n",i, u_errorName(ec)); return; } for(i = 0;i<cnt_testCases; i++){ UParseError parseError; int32_t resultLength =0,count=0; int32_t one=0; int32_t two=0; UDate d2=0; result=NULL; patternLength = u_strlen(testCasePatterns[i]); umsg_applyPattern(formatter,testCasePatterns[i],patternLength,&parseError,&ec); if(U_FAILURE(ec)){ log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i); return; } /* pre-flight */ resultLength = umsg_format(formatter,result,resultLength,&ec,1,3456.00,d1); if(ec==U_BUFFER_OVERFLOW_ERROR){ ec=U_ZERO_ERROR; result = (UChar*) malloc(U_SIZEOF_UCHAR*resultLength+2); resultLength = umsg_format(formatter,result,resultLength+2,&ec,1,3456.00,d1); if(U_FAILURE(ec)){ log_err("ERROR: failure in message format on testcase %d: %s\n", i, u_errorName(status) ); free(result); return; } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } if (returnsNullForType(1, (double)2.0)) { /* HP/UX and possibly other platforms don't properly check for this case. We pass in a UDate, but the function expects a UDate *. When va_arg is used, most compilers will return NULL, but HP-UX won't do that and will return 2 in this case. This is a platform dependent test. This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2 */ umsg_parse(formatter,result,resultLength,&count,&ec,one,two,d2); if(ec!=U_ILLEGAL_ARGUMENT_ERROR){ log_err("FAIL: Did not get expected error for umsg_parse(). Expected: U_ILLEGAL_ARGUMENT_ERROR Got: %s \n",u_errorName(ec)); }else{ ec = U_ZERO_ERROR; } } else { log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i); } umsg_parse(formatter,result,resultLength,&count,&ec,&one,&two,&d2); if(U_FAILURE(ec)){ log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec)); } free(result); }else{ log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec),i); } } umsg_close(formatter); } FreeStrings(); ctest_resetTimeZone(); }
/* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */ static void TestNewFormatAndParseAPI(void) { UChar *result, tzID[4], str[25]; UChar pattern[100]; UChar expected[100]; int32_t resultLengthOut, resultlength; UCalendar *cal; UDate d1,d; UDateFormat *def1; UErrorCode status = U_ZERO_ERROR; int32_t value = 0; UChar ret[30]; UParseError parseError; UMessageFormat* fmt = NULL; int32_t count=0; ctest_setTimeZone(NULL, &status); log_verbose("Testing format and parse with parse error\n"); u_uastrcpy(str, "disturbance in force"); u_uastrcpy(tzID, "PST"); cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) ); return; } ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status); d1=ucal_getMillis(cal, &status); if(U_FAILURE(status)){ log_err("Error: failure in get millis: %s\n", myErrorName(status) ); return; } log_verbose("\nTesting with pattern test#4"); u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}"); u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7"); resultlength=1; fmt = umsg_open(pattern,u_strlen(pattern),"en_US",&parseError,&status); if(U_FAILURE(status)){ log_data_err("error in umsg_open : %s (Are you missing data?)\n", u_errorName(status) ); return; } result=(UChar*)malloc(sizeof(UChar) * resultlength); resultLengthOut=umsg_format(fmt ,result, resultlength,&status, d1, str, 7); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result, sizeof(UChar) * resultlength); u_formatMessageWithError( "en_US", pattern, u_strlen(pattern), result, resultlength,&parseError, &status, d1, str, 7); } if(U_FAILURE(status)){ log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status)); } if(u_strcmp(result, expected)==0) log_verbose("PASS: MessagFormat successful on test#4\n"); else{ log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result), austrdup(expected) ); } /*try to parse this and check*/ log_verbose("\nTesting the parse Message test#5\n"); umsg_parse(fmt, result, u_strlen(result),&count,&status, &d, ret, &value); if(U_FAILURE(status)){ log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status)); } if(value!=7 && u_strcmp(str,ret)!=0) log_err("FAIL: Error in parseMessage on test#5 \n"); else log_verbose("PASS: parseMessage successful on test#5\n"); def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status); if(U_FAILURE(status)) { log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status)); }else{ if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0) log_verbose("PASS: parseMessage successful test#5\n"); else{ log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n", austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) ); } } umsg_close(fmt); udat_close(def1); ucal_close(cal); free(result); ctest_resetTimeZone(); }