diff --git a/.clang-format b/.clang-format index 75850f5..2f9feac 100644 --- a/.clang-format +++ b/.clang-format @@ -5,4 +5,44 @@ UseTab: AlignWithSpaces IndentWidth: 4 TabWidth: 4 AllowShortFunctionsOnASingleLine: Empty +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: true + AfterNamespace: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: Always +IndentAccessModifiers: false +IndentCaseLabels: true +IndentExternBlock: NoIndent +IndentGotoLabels: false +IndentPPDirectives: AfterHash +IndentWrappedFunctionNames: true +InsertBraces: true +InsertNewlineAtEOF: true +KeepEmptyLinesAtTheStartOfBlocks: false +LineEnding: LF +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: All +PPIndentWidth: -1 +PackConstructorInitializers: NextLine +PointerAlignment: Right +ReferenceAlignment: Pointer +ReflowComments: true +SeparateDefinitionBlocks: Always +SortIncludes: CaseInsensitive ... diff --git a/.editorconfig b/.editorconfig index d3f1a59..fe5a087 100644 --- a/.editorconfig +++ b/.editorconfig @@ -15,7 +15,7 @@ indent_size = 4 [*.{c,cpp,h,hpp}] indent_style = tab -[*.{yml,yaml,m4,w32}] +[*.{yml,yaml,m4,w32,clang-format}] indent_size = 2 [package.xml] diff --git a/src/ecma402/calendar.c b/src/ecma402/calendar.c index c7bbded..46f1891 100644 --- a/src/ecma402/calendar.c +++ b/src/ecma402/calendar.c @@ -21,7 +21,8 @@ #include #include -int ecma402_availableCanonicalCalendars(const char **values) { +int ecma402_availableCanonicalCalendars(const char **values) +{ UEnumeration *enumeration = NULL; UErrorCode status = U_ZERO_ERROR; const char *identifier; @@ -49,6 +50,7 @@ int ecma402_availableCanonicalCalendars(const char **values) { return ecma402_sortAndRemoveDuplicates((char **)values, valuesCount, ecma402_strToLower); } -int ecma402_calendarsOfLocale(ecma402_locale *locale, const char **values) { +int ecma402_calendarsOfLocale(ecma402_locale *locale, const char **values) +{ return ecma402_keywordsOfLocale(locale, ICU_KEYWORD_CALENDAR, values); } diff --git a/src/ecma402/category.c b/src/ecma402/category.c index 58905e2..95ed827 100644 --- a/src/ecma402/category.c +++ b/src/ecma402/category.c @@ -21,7 +21,8 @@ #include -int ecma402_supportedValuesForCategory(const char *category, const char **values) { +int ecma402_supportedValuesForCategory(const char *category, const char **values) +{ if (strcmp(ECMA402_CATEGORY_CALENDAR, category) == 0) { return ecma402_availableCanonicalCalendars(values); } else if (strcmp(ECMA402_CATEGORY_COLLATION, category) == 0) { @@ -39,7 +40,8 @@ int ecma402_supportedValuesForCategory(const char *category, const char **values return 0; } -int ecma402_capacityForCategory(const char *category) { +int ecma402_capacityForCategory(const char *category) +{ if (strcmp(ECMA402_CATEGORY_CALENDAR, category) == 0) { return ECMA402_CALENDAR_CAPACITY; } else if (strcmp(ECMA402_CATEGORY_COLLATION, category) == 0) { diff --git a/src/ecma402/collation.c b/src/ecma402/collation.c index 7e95302..c6b6677 100644 --- a/src/ecma402/collation.c +++ b/src/ecma402/collation.c @@ -21,7 +21,8 @@ #include #include -int ecma402_availableCanonicalCollations(const char **values) { +int ecma402_availableCanonicalCollations(const char **values) +{ UEnumeration *enumeration = NULL; UErrorCode status = U_ZERO_ERROR; const char *identifier; @@ -56,6 +57,7 @@ int ecma402_availableCanonicalCollations(const char **values) { return ecma402_sortAndRemoveDuplicates((char **)values, valuesCount, ecma402_strToLower); } -int ecma402_collationsOfLocale(ecma402_locale *locale, const char **values) { +int ecma402_collationsOfLocale(ecma402_locale *locale, const char **values) +{ return ecma402_keywordsOfLocale(locale, ICU_KEYWORD_COLLATION, values); } diff --git a/src/ecma402/currency.c b/src/ecma402/currency.c index 378b414..e06b884 100644 --- a/src/ecma402/currency.c +++ b/src/ecma402/currency.c @@ -20,7 +20,8 @@ #include #include -int ecma402_availableCanonicalCurrencies(const char **values) { +int ecma402_availableCanonicalCurrencies(const char **values) +{ UEnumeration *enumeration = NULL; UErrorCode status = U_ZERO_ERROR; const char *identifier; @@ -44,7 +45,8 @@ int ecma402_availableCanonicalCurrencies(const char **values) { return ecma402_sortAndRemoveDuplicates((char **)values, valuesCount, ecma402_strToUpper); } -int ecma402_currenciesOfLocale(ecma402_locale *locale, const char **values) { +int ecma402_currenciesOfLocale(ecma402_locale *locale, const char **values) +{ int count = ecma402_keywordsOfLocale(locale, ICU_KEYWORD_CURRENCY, values); for (int i = 0; i < count; i++) { diff --git a/src/ecma402/error.c b/src/ecma402/error.c index 01a759c..dda095c 100644 --- a/src/ecma402/error.c +++ b/src/ecma402/error.c @@ -21,7 +21,8 @@ static void storeError(ecma402_errorStatus *status, const char *format, va_list vargs); -ecma402_errorStatus *ecma402_initErrorStatus(void) { +ecma402_errorStatus *ecma402_initErrorStatus(void) +{ ecma402_errorStatus *status; status = (ecma402_errorStatus *)malloc(sizeof(*status)); @@ -36,7 +37,8 @@ ecma402_errorStatus *ecma402_initErrorStatus(void) { return status; } -void ecma402_freeErrorStatus(ecma402_errorStatus *status) { +void ecma402_freeErrorStatus(ecma402_errorStatus *status) +{ if (status->errorMessage) { free(status->errorMessage); } @@ -44,7 +46,8 @@ void ecma402_freeErrorStatus(ecma402_errorStatus *status) { free(status); } -bool ecma402_hasError(ecma402_errorStatus *status) { +bool ecma402_hasError(ecma402_errorStatus *status) +{ if (!status) { return false; } @@ -52,7 +55,8 @@ bool ecma402_hasError(ecma402_errorStatus *status) { return status->ecma != ZERO_ERROR; } -void ecma402_error(ecma402_errorStatus *status, const char *format, ...) { +void ecma402_error(ecma402_errorStatus *status, const char *format, ...) +{ va_list args; if (!status) { @@ -64,7 +68,8 @@ void ecma402_error(ecma402_errorStatus *status, const char *format, ...) { va_end(args); } -void ecma402_ecmaError(ecma402_errorStatus *status, ecma402_errorCode errorCode, const char *format, ...) { +void ecma402_ecmaError(ecma402_errorStatus *status, ecma402_errorCode errorCode, const char *format, ...) +{ va_list args; if (!status) { @@ -78,7 +83,8 @@ void ecma402_ecmaError(ecma402_errorStatus *status, ecma402_errorCode errorCode, va_end(args); } -void ecma402_icuError(ecma402_errorStatus *status, UErrorCode errorCode, const char *format, ...) { +void ecma402_icuError(ecma402_errorStatus *status, UErrorCode errorCode, const char *format, ...) +{ va_list args; if (!status) { @@ -93,7 +99,8 @@ void ecma402_icuError(ecma402_errorStatus *status, UErrorCode errorCode, const c va_end(args); } -static void storeError(ecma402_errorStatus *status, const char *format, va_list vargs) { +static void storeError(ecma402_errorStatus *status, const char *format, va_list vargs) +{ char *message; if (status->ecma == ZERO_ERROR) { diff --git a/src/ecma402/hour_cycle.c b/src/ecma402/hour_cycle.c index 19f0565..b6eca89 100644 --- a/src/ecma402/hour_cycle.c +++ b/src/ecma402/hour_cycle.c @@ -14,6 +14,7 @@ #include "ecma402/locale.h" -int ecma402_hourCyclesOfLocale(ecma402_locale *locale, const char **values) { +int ecma402_hourCyclesOfLocale(ecma402_locale *locale, const char **values) +{ return ecma402_keywordsOfLocale(locale, ICU_KEYWORD_HOUR_CYCLE, values); } diff --git a/src/ecma402/language_tag.cpp b/src/ecma402/language_tag.cpp index 29ad07d..23154ce 100644 --- a/src/ecma402/language_tag.cpp +++ b/src/ecma402/language_tag.cpp @@ -22,36 +22,40 @@ namespace { -using LChar = unsigned char; -using VariantCode = uint64_t; - -unsigned convertToUnicodeSingletonIndex(UChar singleton); -bool isPartValid(const std::string &string); -bool isUnicodeExtensionAttribute(const std::string &string); -bool isUnicodeExtensionKey(const std::string &string); -bool isUnicodeExtensionTypeComponent(const std::string &string); -bool isUnicodeOtherExtensionValue(const std::string &string); -bool isUnicodePrivateUseExtensionValue(const std::string &string); -bool isUnicodeTypeKey(const std::string &string); -bool isUnicodeTypeValueComponent(const std::string &string); -VariantCode parseVariantCode(const std::string &string); + using LChar = unsigned char; + using VariantCode = uint64_t; + + unsigned convertToUnicodeSingletonIndex(UChar singleton); + bool isPartValid(const std::string &string); + bool isUnicodeExtensionAttribute(const std::string &string); + bool isUnicodeExtensionKey(const std::string &string); + bool isUnicodeExtensionTypeComponent(const std::string &string); + bool isUnicodeOtherExtensionValue(const std::string &string); + bool isUnicodePrivateUseExtensionValue(const std::string &string); + bool isUnicodeTypeKey(const std::string &string); + bool isUnicodeTypeValueComponent(const std::string &string); + VariantCode parseVariantCode(const std::string &string); } // namespace -bool ecma402_isStructurallyValidLanguageTag(const char *tag) { +bool ecma402_isStructurallyValidLanguageTag(const char *tag) +{ ecma402::LanguageTagParser parser(tag); return parser.parseUnicodeLocaleId(); } -bool ecma402_isUnicodeCurrencyType(const char *currency) { +bool ecma402_isUnicodeCurrencyType(const char *currency) +{ return ecma402::isUnicodeCurrencyType(currency); } -bool ecma402_isUnicodeLanguageSubtag(const char *language) { +bool ecma402_isUnicodeLanguageSubtag(const char *language) +{ return ecma402::isUnicodeLanguageSubtag(language); } -bool ecma402_isUnicodeLocaleIdentifierType(const char *identifier) { +bool ecma402_isUnicodeLocaleIdentifierType(const char *identifier) +{ std::string const delimiter = "-"; std::string const s = identifier; std::string token; @@ -73,34 +77,41 @@ bool ecma402_isUnicodeLocaleIdentifierType(const char *identifier) { return isUnicodeTypeValueComponent(s.substr(start, end)); } -bool ecma402_isUnicodeRegionSubtag(const char *region) { +bool ecma402_isUnicodeRegionSubtag(const char *region) +{ return ecma402::isUnicodeRegionSubtag(region); } -bool ecma402_isUnicodeScriptSubtag(const char *script) { +bool ecma402_isUnicodeScriptSubtag(const char *script) +{ return ecma402::isUnicodeScriptSubtag(script); } -bool ecma402::isUnicodeCurrencyType(const std::string &string) { +bool ecma402::isUnicodeCurrencyType(const std::string &string) +{ return string.length() == 3 && std::all_of(string.cbegin(), string.cend(), util::isAsciiAlpha); } -bool ecma402::isUnicodeLanguageSubtag(const std::string &string) { +bool ecma402::isUnicodeLanguageSubtag(const std::string &string) +{ auto length = string.length(); return length >= 2 && length <= 8 && length != 4 && std::all_of(string.cbegin(), string.cend(), util::isAsciiAlpha); } -bool ecma402::isUnicodeRegionSubtag(const std::string &string) { +bool ecma402::isUnicodeRegionSubtag(const std::string &string) +{ auto length = string.length(); return (length == 2 && std::all_of(string.cbegin(), string.cend(), util::isAsciiAlpha)) || (length == 3 && std::all_of(string.cbegin(), string.cend(), util::isAsciiDigit)); } -bool ecma402::isUnicodeScriptSubtag(const std::string &string) { +bool ecma402::isUnicodeScriptSubtag(const std::string &string) +{ return string.length() == 4 && std::all_of(string.cbegin(), string.cend(), util::isAsciiAlpha); } -bool ecma402::isUnicodeVariantSubtag(const std::string &string) { +bool ecma402::isUnicodeVariantSubtag(const std::string &string) +{ auto length = string.length(); if (length >= 5 && length <= 8) { @@ -111,18 +122,21 @@ bool ecma402::isUnicodeVariantSubtag(const std::string &string) { std::all_of(std::next(string.cbegin()), string.cend(), util::isAsciiAlnum); } -ecma402::LanguageTagParser::LanguageTagParser(const std::string &tag) { +ecma402::LanguageTagParser::LanguageTagParser(const std::string &tag) +{ tagParts = util::split(tag, "-"); partsCursor = tagParts.begin(); assert(partsCursor != tagParts.end()); currentPart = *partsCursor; } -bool ecma402::LanguageTagParser::isEos() { +bool ecma402::LanguageTagParser::isEos() +{ return partsCursor == tagParts.end(); } -bool ecma402::LanguageTagParser::next() { +bool ecma402::LanguageTagParser::next() +{ if (isEos()) { return false; } @@ -138,7 +152,8 @@ bool ecma402::LanguageTagParser::next() { return true; } -bool ecma402::LanguageTagParser::parseUnicodeLanguageId() { +bool ecma402::LanguageTagParser::parseUnicodeLanguageId() +{ assert(!isEos()); if (!isUnicodeLanguageSubtag(currentPart)) { @@ -180,7 +195,8 @@ bool ecma402::LanguageTagParser::parseUnicodeLanguageId() { } } -bool ecma402::LanguageTagParser::parseUnicodeLocaleId() { +bool ecma402::LanguageTagParser::parseUnicodeLocaleId() +{ assert(!isEos()); if (!parseUnicodeLanguageId()) { @@ -198,7 +214,8 @@ bool ecma402::LanguageTagParser::parseUnicodeLocaleId() { return true; } -bool ecma402::LanguageTagParser::parseExtensionsAndPrivateUseExtensions() { +bool ecma402::LanguageTagParser::parseExtensionsAndPrivateUseExtensions() +{ assert(!isEos()); std::unordered_set singletons{}; @@ -230,75 +247,75 @@ bool ecma402::LanguageTagParser::parseExtensionsAndPrivateUseExtensions() { singletons.insert(convertToUnicodeSingletonIndex(prefixCode)); switch (prefixCode) { + case 'u': + case 'U': { + if (!next()) { + return false; + } - case 'u': - case 'U': { - if (!next()) { - return false; - } + if (!parseUnicodeExtensionAfterPrefix()) { + return false; + } - if (!parseUnicodeExtensionAfterPrefix()) { - return false; - } + if (isEos()) { + return true; + } - if (isEos()) { - return true; + break; // Next extension. } - break; // Next extension. - } + case 't': + case 'T': { + if (!next()) { + return false; + } - case 't': - case 'T': { - if (!next()) { - return false; - } + if (!parseTransformedExtensionAfterPrefix()) { + return false; + } - if (!parseTransformedExtensionAfterPrefix()) { - return false; - } + if (isEos()) { + return true; + } - if (isEos()) { - return true; + break; // Next extension. } - break; // Next extension. - } + case 'x': + case 'X': { + if (!next()) { + return false; + } - case 'x': - case 'X': { - if (!next()) { - return false; - } + if (!parsePrivateUseExtensionAfterPrefix()) { + return false; + } - if (!parsePrivateUseExtensionAfterPrefix()) { - return false; + return true; // If pu_extensions appear, no extensions can follow after + // that. This must be the end of unicode_locale_id. } - return true; // If pu_extensions appear, no extensions can follow after - // that. This must be the end of unicode_locale_id. - } + default: { + if (!next()) { + return false; + } - default: { - if (!next()) { - return false; - } + if (!parseOtherExtensionAfterPrefix()) { + return false; + } - if (!parseOtherExtensionAfterPrefix()) { - return false; - } + if (isEos()) { + return true; + } - if (isEos()) { - return true; + break; // Next extension. } - - break; // Next extension. - } } // switch(prefixCode) } } -bool ecma402::LanguageTagParser::parseUnicodeExtensionAfterPrefix() { +bool ecma402::LanguageTagParser::parseUnicodeExtensionAfterPrefix() +{ assert(!isEos()); bool isAttributeOrKeyword = false; @@ -347,7 +364,8 @@ bool ecma402::LanguageTagParser::parseUnicodeExtensionAfterPrefix() { return true; } -bool ecma402::LanguageTagParser::parseTransformedExtensionAfterPrefix() { +bool ecma402::LanguageTagParser::parseTransformedExtensionAfterPrefix() +{ assert(!isEos()); bool found = false; @@ -398,7 +416,8 @@ bool ecma402::LanguageTagParser::parseTransformedExtensionAfterPrefix() { return found; } -bool ecma402::LanguageTagParser::parsePrivateUseExtensionAfterPrefix() { +bool ecma402::LanguageTagParser::parsePrivateUseExtensionAfterPrefix() +{ assert(!isEos()); if (!isUnicodePrivateUseExtensionValue(currentPart)) { @@ -420,7 +439,8 @@ bool ecma402::LanguageTagParser::parsePrivateUseExtensionAfterPrefix() { } } -bool ecma402::LanguageTagParser::parseOtherExtensionAfterPrefix() { +bool ecma402::LanguageTagParser::parseOtherExtensionAfterPrefix() +{ assert(!isEos()); if (!isUnicodeOtherExtensionValue(currentPart)) { @@ -444,82 +464,92 @@ bool ecma402::LanguageTagParser::parseOtherExtensionAfterPrefix() { namespace { -using namespace ecma402::util; + using namespace ecma402::util; -unsigned convertToUnicodeSingletonIndex(UChar singleton) { - assert(isAsciiAlnum(singleton)); - singleton = toAsciiLower(singleton); + unsigned convertToUnicodeSingletonIndex(UChar singleton) + { + assert(isAsciiAlnum(singleton)); + singleton = toAsciiLower(singleton); - // 0 - 9 => numeric - // 10 - 35 => alpha - if (isAsciiDigit(singleton)) { - return singleton - '0'; - } + // 0 - 9 => numeric + // 10 - 35 => alpha + if (isAsciiDigit(singleton)) { + return singleton - '0'; + } - return (singleton - 'a') + 10; -} + return (singleton - 'a') + 10; + } -bool isPartValid(const std::string &string) { - return string.length() > 0 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); -} + bool isPartValid(const std::string &string) + { + return string.length() > 0 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); + } -bool isUnicodeExtensionAttribute(const std::string &string) { - auto length = string.length(); - return length >= 3 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); -} + bool isUnicodeExtensionAttribute(const std::string &string) + { + auto length = string.length(); + return length >= 3 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); + } -bool isUnicodeExtensionKey(const std::string &string) { - return string.length() == 2 && isAsciiAlnum(string[0]) && isAsciiAlpha(string[1]); -} + bool isUnicodeExtensionKey(const std::string &string) + { + return string.length() == 2 && isAsciiAlnum(string[0]) && isAsciiAlpha(string[1]); + } -bool isUnicodeExtensionTypeComponent(const std::string &string) { - auto length = string.length(); - return length >= 3 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); -} + bool isUnicodeExtensionTypeComponent(const std::string &string) + { + auto length = string.length(); + return length >= 3 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); + } -bool isUnicodeOtherExtensionValue(const std::string &string) { - auto length = string.length(); - return length >= 2 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); -} + bool isUnicodeOtherExtensionValue(const std::string &string) + { + auto length = string.length(); + return length >= 2 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); + } -bool isUnicodePrivateUseExtensionValue(const std::string &string) { - auto length = string.length(); - return length >= 1 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); -} + bool isUnicodePrivateUseExtensionValue(const std::string &string) + { + auto length = string.length(); + return length >= 1 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); + } -bool isUnicodeTypeKey(const std::string &string) { - return string.length() == 2 && isAsciiAlpha(string[0]) && isAsciiDigit(string[1]); -} + bool isUnicodeTypeKey(const std::string &string) + { + return string.length() == 2 && isAsciiAlpha(string[0]) && isAsciiDigit(string[1]); + } -bool isUnicodeTypeValueComponent(const std::string &string) { - auto length = string.length(); - return length >= 3 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); -} + bool isUnicodeTypeValueComponent(const std::string &string) + { + auto length = string.length(); + return length >= 3 && length <= 8 && std::all_of(string.cbegin(), string.cend(), isAsciiAlnum); + } -VariantCode parseVariantCode(const std::string &string) { - assert(ecma402::isUnicodeVariantSubtag(string)); - assert(std::all_of(string.cbegin(), string.cend(), isAscii)); - assert(string.length() <= 8); - assert(string.length() >= 1); + VariantCode parseVariantCode(const std::string &string) + { + assert(ecma402::isUnicodeVariantSubtag(string)); + assert(std::all_of(string.cbegin(), string.cend(), isAscii)); + assert(string.length() <= 8); + assert(string.length() >= 1); - struct Code { - std::array characters{}; - }; + struct Code { + std::array characters{}; + }; - static_assert(std::is_unsigned::value, "must be unsigned"); - static_assert(sizeof(VariantCode) == sizeof(Code), "size must be equal"); + static_assert(std::is_unsigned::value, "must be unsigned"); + static_assert(sizeof(VariantCode) == sizeof(Code), "size must be equal"); - Code code{}; - for (unsigned index = 0; index < string.length(); ++index) { - code.characters[index] = toAsciiLower(string[index]); - } + Code code{}; + for (unsigned index = 0; index < string.length(); ++index) { + code.characters[index] = toAsciiLower(string[index]); + } - VariantCode const result = reinterpret_cast(code); + VariantCode const result = reinterpret_cast(code); - assert(result); - assert(result != static_cast(-1)); + assert(result); + assert(result != static_cast(-1)); - return result; -} + return result; + } } // namespace diff --git a/src/ecma402/language_tag.h b/src/ecma402/language_tag.h index c8697dc..68a656f 100644 --- a/src/ecma402/language_tag.h +++ b/src/ecma402/language_tag.h @@ -18,7 +18,7 @@ #ifdef __cplusplus extern "C" { #else -#include +# include #endif /** @@ -63,167 +63,168 @@ bool ecma402_isUnicodeScriptSubtag(const char *script); #ifdef __cplusplus -#include -#include +# include +# include namespace ecma402 { -/** - * Returns true if the string is a valid Unicode language subtag. - * - *

The currency type consists of:

- * - *
- * Codes consisting of 3 ASCII letters that are or have been valid in ISO 4217, - * plus certain additional codes that are or have been in common use. - *
- */ -bool isUnicodeCurrencyType(const std::string &string); - -/** - * Returns true if the string is a valid Unicode language subtag. - * - *

- * unicode_language_subtag = alpha{2,3} | alpha{5,8} ;
- * 
- */ -bool isUnicodeLanguageSubtag(const std::string &string); - -/** - * Returns true if the string is a valid Unicode region subtag. - * - *

- * unicode_region_subtag = (alpha{2} | digit{3}) ;
- * 
- */ -bool isUnicodeRegionSubtag(const std::string &string); - -/** - * Returns true if the string is a valid Unicode script subtag. - * - *

- * unicode_script_subtag = alpha{4} ;
- * 
- */ -bool isUnicodeScriptSubtag(const std::string &string); - -/** - * Returns true if the string is a valid Unicode variant subtag. - * - *

- * unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3}) ;
- * 
- */ -bool isUnicodeVariantSubtag(const std::string &string); - -/** - * A parser to check validity of language tags. - * - *

- * (* TR35 defines sep as [-_] but ECMA-402 uses only the hyphen. *)
- * sep = "-" ;
- * digit = [0-9] ;
- * alpha = [A-Z a-z] ;
- * alphanum = [0-9 A-Z a-z] ;
- * 
- */ -class LanguageTagParser { - public: - LanguageTagParser(const std::string &tag); - - /** - * Returns true if the parser has reached the end of the string. - */ - bool isEos(); - - /** - * Advances the parser and returns true if it encounters another tag segment. - */ - bool next(); - /** - * Parses the language tag, returning true if it is a valid Unicode language - * ID. + * Returns true if the string is a valid Unicode language subtag. * - *

-	 * (* TR35 defines unicode_language_id as optionally being "root", a special
-	 *    unicode_language_subtag. It also allows for starting with a
-	 *    unicode_script_subtag, but this is not supported in ECMA-402, so this
-	 *    EBNF is slightly different from TR35. *)
-	 * unicode_language_id = unicode_language_subtag
-	 *                       (sep unicode_script_subtag)?
-	 *                       (sep unicode_region_subtag)?
-	 *                       (sep unicode_variant_subtag)* ;
-	 * 
+ *

The currency type consists of:

+ * + *
+ * Codes consisting of 3 ASCII letters that are or have been valid in ISO 4217, + * plus certain additional codes that are or have been in common use. + *
*/ - bool parseUnicodeLanguageId(); + bool isUnicodeCurrencyType(const std::string &string); /** - * Parses the language tag, returning true if it is a valid Unicode locale ID. + * Returns true if the string is a valid Unicode language subtag. * *

-	 * unicode_locale_id = unicode_language_id
-	 *                     extensions*
-	 *                     pu_extensions? ;
+	 * unicode_language_subtag = alpha{2,3} | alpha{5,8} ;
 	 * 
*/ - bool parseUnicodeLocaleId(); + bool isUnicodeLanguageSubtag(const std::string &string); - private: /** + * Returns true if the string is a valid Unicode region subtag. + * *

-	 * extensions = unicode_locale_extensions
-	 *            | transformed_extensions
-	 *            | other_extensions ;
+	 * unicode_region_subtag = (alpha{2} | digit{3}) ;
 	 * 
*/ - bool parseExtensionsAndPrivateUseExtensions(); + bool isUnicodeRegionSubtag(const std::string &string); /** - *

-	 * unicode_locale_extensions = sep [uU] ((sep keyword)+
-	 *                             | (sep attribute)+ (sep keyword)*) ;
+	 * Returns true if the string is a valid Unicode script subtag.
 	 *
-	 * keyword = key (sep type)? ;
-	 * key = alphanum alpha ;
-	 * type = alphanum{3,8} (sep alphanum{3,8})* ;
-	 * attribute = alphanum{3,8} ;
-	 * 
- */ - bool parseUnicodeExtensionAfterPrefix(); - - /** *

-	 * transformed_extensions = sep [tT]
-	 *                          ((sep tlang (sep tfield)*) | (sep tfield)+) ;
-	 *
-	 * tlang = unicode_language_id
-	 * tfield = tkey tvalue;
-	 * tkey = alpha digit ;
-	 * tvalue = (sep alphanum{3,8})+ ;
+	 * unicode_script_subtag = alpha{4} ;
 	 * 
*/ - bool parseTransformedExtensionAfterPrefix(); + bool isUnicodeScriptSubtag(const std::string &string); /** + * Returns true if the string is a valid Unicode variant subtag. + * *

-	 * other_extensions = sep [alphanum-[tTuUxX]] (sep alphanum{2,8})+ ;
+	 * unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3}) ;
 	 * 
*/ - bool parseOtherExtensionAfterPrefix(); + bool isUnicodeVariantSubtag(const std::string &string); /** + * A parser to check validity of language tags. + * *

-	 * pu_extensions = sep [xX] (sep alphanum{1,8})+ ;
+	 * (* TR35 defines sep as [-_] but ECMA-402 uses only the hyphen. *)
+	 * sep = "-" ;
+	 * digit = [0-9] ;
+	 * alpha = [A-Z a-z] ;
+	 * alphanum = [0-9 A-Z a-z] ;
 	 * 
*/ - bool parsePrivateUseExtensionAfterPrefix(); - - std::string currentPart; - std::vector::iterator partsCursor; - std::string tag; - std::vector tagParts; -}; + class LanguageTagParser + { + public: + LanguageTagParser(const std::string &tag); + + /** + * Returns true if the parser has reached the end of the string. + */ + bool isEos(); + + /** + * Advances the parser and returns true if it encounters another tag segment. + */ + bool next(); + + /** + * Parses the language tag, returning true if it is a valid Unicode language + * ID. + * + *

+		 * (* TR35 defines unicode_language_id as optionally being "root", a special
+		 *    unicode_language_subtag. It also allows for starting with a
+		 *    unicode_script_subtag, but this is not supported in ECMA-402, so this
+		 *    EBNF is slightly different from TR35. *)
+		 * unicode_language_id = unicode_language_subtag
+		 *                       (sep unicode_script_subtag)?
+		 *                       (sep unicode_region_subtag)?
+		 *                       (sep unicode_variant_subtag)* ;
+		 * 
+ */ + bool parseUnicodeLanguageId(); + + /** + * Parses the language tag, returning true if it is a valid Unicode locale ID. + * + *

+		 * unicode_locale_id = unicode_language_id
+		 *                     extensions*
+		 *                     pu_extensions? ;
+		 * 
+ */ + bool parseUnicodeLocaleId(); + + private: + /** + *

+		 * extensions = unicode_locale_extensions
+		 *            | transformed_extensions
+		 *            | other_extensions ;
+		 * 
+ */ + bool parseExtensionsAndPrivateUseExtensions(); + + /** + *

+		 * unicode_locale_extensions = sep [uU] ((sep keyword)+
+		 *                             | (sep attribute)+ (sep keyword)*) ;
+		 *
+		 * keyword = key (sep type)? ;
+		 * key = alphanum alpha ;
+		 * type = alphanum{3,8} (sep alphanum{3,8})* ;
+		 * attribute = alphanum{3,8} ;
+		 * 
+ */ + bool parseUnicodeExtensionAfterPrefix(); + + /** + *

+		 * transformed_extensions = sep [tT]
+		 *                          ((sep tlang (sep tfield)*) | (sep tfield)+) ;
+		 *
+		 * tlang = unicode_language_id
+		 * tfield = tkey tvalue;
+		 * tkey = alpha digit ;
+		 * tvalue = (sep alphanum{3,8})+ ;
+		 * 
+ */ + bool parseTransformedExtensionAfterPrefix(); + + /** + *

+		 * other_extensions = sep [alphanum-[tTuUxX]] (sep alphanum{2,8})+ ;
+		 * 
+ */ + bool parseOtherExtensionAfterPrefix(); + + /** + *

+		 * pu_extensions = sep [xX] (sep alphanum{1,8})+ ;
+		 * 
+ */ + bool parsePrivateUseExtensionAfterPrefix(); + + std::string currentPart; + std::vector::iterator partsCursor; + std::string tag; + std::vector tagParts; + }; } // namespace ecma402 diff --git a/src/ecma402/locale.cpp b/src/ecma402/locale.cpp index 27b25d8..37f3083 100644 --- a/src/ecma402/locale.cpp +++ b/src/ecma402/locale.cpp @@ -49,25 +49,26 @@ namespace { -enum maxOrMin { MAXIMIZE, MINIMIZE }; - -int getHourCyclesForLocale(char *localeId, const char **values); -int getKeywordValue(const char *keyword, const char *localeId, char *returnValue, ecma402_errorStatus *status, - bool isCanonicalized); -int getLocaleCode(const char *localeId, char *returnValue, const char *codeType, - int (*callback)(const char *, char *, int, UErrorCode *), int capacity, ecma402_errorStatus *status, - bool isCanonicalized); -int getMaxOrMin(enum maxOrMin type, const char *localeId, char *value, ecma402_errorStatus *status, - bool isCanonicalized); -int getNumberingSystemsForLocale(char *localeId, const char **values); -int getTimeZonesForLocale(char *localeId, const char **values); + enum maxOrMin { MAXIMIZE, MINIMIZE }; + + int getHourCyclesForLocale(char *localeId, const char **values); + int getKeywordValue(const char *keyword, const char *localeId, char *returnValue, ecma402_errorStatus *status, + bool isCanonicalized); + int getLocaleCode(const char *localeId, char *returnValue, const char *codeType, + int (*callback)(const char *, char *, int, UErrorCode *), int capacity, + ecma402_errorStatus *status, bool isCanonicalized); + int getMaxOrMin(enum maxOrMin type, const char *localeId, char *value, ecma402_errorStatus *status, + bool isCanonicalized); + int getNumberingSystemsForLocale(char *localeId, const char **values); + int getTimeZonesForLocale(char *localeId, const char **values); } // namespace ecma402_locale *ecma402_applyLocaleOptions(ecma402_locale *locale, const char *calendar, const char *caseFirst, const char *collation, const char *currency, const char *hourCycle, const char *language, const char *numberingSystem, int numeric, - const char *region, const char *script) { + const char *region, const char *script) +{ icu::Locale icuLocale; icu::LocaleBuilder icuLocaleBuilder; UErrorCode icuStatus = U_ZERO_ERROR; @@ -143,7 +144,8 @@ ecma402_locale *ecma402_applyLocaleOptions(ecma402_locale *locale, const char *c } int ecma402_canonicalizeLocaleList(const char **locales, int localesLength, char **canonicalized, - ecma402_errorStatus *status) { + ecma402_errorStatus *status) +{ int i, canonicalizedLength = 0; for (i = 0; i < localesLength; i++) { @@ -169,7 +171,8 @@ int ecma402_canonicalizeLocaleList(const char **locales, int localesLength, char return ecma402_removeDuplicates(canonicalized, canonicalizedLength, nullptr); } -int ecma402_canonicalizeUnicodeLocaleId(const char *localeId, char *canonicalized, ecma402_errorStatus *status) { +int ecma402_canonicalizeUnicodeLocaleId(const char *localeId, char *canonicalized, ecma402_errorStatus *status) +{ icu::Locale canonicalLocale; UErrorCode icuStatus = U_ZERO_ERROR; UBool const strict = 1; @@ -227,7 +230,8 @@ int ecma402_canonicalizeUnicodeLocaleId(const char *localeId, char *canonicalize return length; } -void ecma402_freeLocale(ecma402_locale *locale) { +void ecma402_freeLocale(ecma402_locale *locale) +{ if (locale == nullptr) { return; } @@ -249,7 +253,8 @@ void ecma402_freeLocale(ecma402_locale *locale) { free(locale); } -int ecma402_getBaseName(const char *localeId, char *baseName, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getBaseName(const char *localeId, char *baseName, ecma402_errorStatus *status, bool isCanonicalized) +{ char *icuBaseName, *bcp47BaseName; UErrorCode icuStatus = U_ZERO_ERROR; int icuBaseNameLength, bcp47BaseNameLength; @@ -280,19 +285,23 @@ int ecma402_getBaseName(const char *localeId, char *baseName, ecma402_errorStatu return bcp47BaseNameLength; } -int ecma402_getCalendar(const char *localeId, char *calendar, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getCalendar(const char *localeId, char *calendar, ecma402_errorStatus *status, bool isCanonicalized) +{ return getKeywordValue(ICU_KEYWORD_CALENDAR, localeId, calendar, status, isCanonicalized); } -int ecma402_getCaseFirst(const char *localeId, char *caseFirst, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getCaseFirst(const char *localeId, char *caseFirst, ecma402_errorStatus *status, bool isCanonicalized) +{ return getKeywordValue(ICU_KEYWORD_CASE_FIRST, localeId, caseFirst, status, isCanonicalized); } -int ecma402_getCollation(const char *localeId, char *collation, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getCollation(const char *localeId, char *collation, ecma402_errorStatus *status, bool isCanonicalized) +{ return getKeywordValue(ICU_KEYWORD_COLLATION, localeId, collation, status, isCanonicalized); } -int ecma402_getCurrency(const char *localeId, char *currency, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getCurrency(const char *localeId, char *currency, ecma402_errorStatus *status, bool isCanonicalized) +{ char *canonicalized; UChar buffer[4]; UErrorCode icuStatus = U_ZERO_ERROR; @@ -354,28 +363,34 @@ int ecma402_getCurrency(const char *localeId, char *currency, ecma402_errorStatu return icuValue.length(); } -int ecma402_getHourCycle(const char *localeId, char *hourCycle, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getHourCycle(const char *localeId, char *hourCycle, ecma402_errorStatus *status, bool isCanonicalized) +{ return getKeywordValue(ICU_KEYWORD_HOUR_CYCLE, localeId, hourCycle, status, isCanonicalized); } -int ecma402_getLanguage(const char *localeId, char *language, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getLanguage(const char *localeId, char *language, ecma402_errorStatus *status, bool isCanonicalized) +{ return getLocaleCode(localeId, language, "language", uloc_getLanguage, ULOC_LANG_CAPACITY, status, isCanonicalized); } int ecma402_getNumberingSystem(const char *localeId, char *numberingSystem, ecma402_errorStatus *status, - bool isCanonicalized) { + bool isCanonicalized) +{ return getKeywordValue(ICU_KEYWORD_NUMBERING_SYSTEM, localeId, numberingSystem, status, isCanonicalized); } -int ecma402_getRegion(const char *localeId, char *region, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getRegion(const char *localeId, char *region, ecma402_errorStatus *status, bool isCanonicalized) +{ return getLocaleCode(localeId, region, "region", uloc_getCountry, ULOC_COUNTRY_CAPACITY, status, isCanonicalized); } -int ecma402_getScript(const char *localeId, char *script, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_getScript(const char *localeId, char *script, ecma402_errorStatus *status, bool isCanonicalized) +{ return getLocaleCode(localeId, script, "script", uloc_getScript, ULOC_SCRIPT_CAPACITY, status, isCanonicalized); } -ecma402_locale *ecma402_initEmptyLocale(void) { +ecma402_locale *ecma402_initEmptyLocale(void) +{ ecma402_locale *locale; locale = (struct ecma402_locale *)malloc(sizeof(ecma402_locale)); @@ -402,7 +417,8 @@ ecma402_locale *ecma402_initEmptyLocale(void) { return locale; } -ecma402_locale *ecma402_initLocale(const char *localeId) { +ecma402_locale *ecma402_initLocale(const char *localeId) +{ ecma402_locale *locale; if (localeId == nullptr) { @@ -444,7 +460,8 @@ ecma402_locale *ecma402_initLocale(const char *localeId) { return locale; } -bool ecma402_isNumeric(const char *localeId, ecma402_errorStatus *status, bool isCanonicalized) { +bool ecma402_isNumeric(const char *localeId, ecma402_errorStatus *status, bool isCanonicalized) +{ char *numeric; int length; bool isNumeric = false; @@ -461,7 +478,8 @@ bool ecma402_isNumeric(const char *localeId, ecma402_errorStatus *status, bool i return isNumeric; } -int ecma402_keywordsOfLocale(ecma402_locale *locale, const char *keyword, const char **values) { +int ecma402_keywordsOfLocale(ecma402_locale *locale, const char *keyword, const char **values) +{ UEnumeration *items = nullptr; UErrorCode icuStatus = U_ZERO_ERROR; char *canonical, *preferred; @@ -549,266 +567,274 @@ int ecma402_keywordsOfLocale(ecma402_locale *locale, const char *keyword, const return count; } -int ecma402_maximize(const char *localeId, char *maximized, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_maximize(const char *localeId, char *maximized, ecma402_errorStatus *status, bool isCanonicalized) +{ return getMaxOrMin(MAXIMIZE, localeId, maximized, status, isCanonicalized); } -int ecma402_minimize(const char *localeId, char *minimized, ecma402_errorStatus *status, bool isCanonicalized) { +int ecma402_minimize(const char *localeId, char *minimized, ecma402_errorStatus *status, bool isCanonicalized) +{ return getMaxOrMin(MINIMIZE, localeId, minimized, status, isCanonicalized); } namespace { -int getHourCyclesForLocale(char *localeId, const char **values) { - UDateTimePatternGenerator *patternGenerator; - UDateFormatHourCycle hourCycle; - UErrorCode status = U_ZERO_ERROR; + int getHourCyclesForLocale(char *localeId, const char **values) + { + UDateTimePatternGenerator *patternGenerator; + UDateFormatHourCycle hourCycle; + UErrorCode status = U_ZERO_ERROR; - patternGenerator = udatpg_open(localeId, &status); - if (U_FAILURE(status) != U_ZERO_ERROR) { - return 0; - } + patternGenerator = udatpg_open(localeId, &status); + if (U_FAILURE(status) != U_ZERO_ERROR) { + return 0; + } - hourCycle = udatpg_getDefaultHourCycle(patternGenerator, &status); - udatpg_close(patternGenerator); + hourCycle = udatpg_getDefaultHourCycle(patternGenerator, &status); + udatpg_close(patternGenerator); - if (U_FAILURE(status) != U_ZERO_ERROR) { - return 0; - } + if (U_FAILURE(status) != U_ZERO_ERROR) { + return 0; + } - values[0] = (const char *)malloc(4); + values[0] = (const char *)malloc(4); + + switch (hourCycle) { + case UDAT_HOUR_CYCLE_11: + values[0] = strdup("h11"); + break; + case UDAT_HOUR_CYCLE_12: + values[0] = strdup("h12"); + break; + case UDAT_HOUR_CYCLE_23: + values[0] = strdup("h23"); + break; + case UDAT_HOUR_CYCLE_24: + values[0] = strdup("h24"); + break; + default: + break; + } - switch (hourCycle) { - case UDAT_HOUR_CYCLE_11: - values[0] = strdup("h11"); - break; - case UDAT_HOUR_CYCLE_12: - values[0] = strdup("h12"); - break; - case UDAT_HOUR_CYCLE_23: - values[0] = strdup("h23"); - break; - case UDAT_HOUR_CYCLE_24: - values[0] = strdup("h24"); - break; - default: - break; + return 1; } - return 1; -} + int getKeywordValue(const char *keyword, const char *localeId, char *returnValue, ecma402_errorStatus *status, + bool isCanonicalized) + { + char *canonicalized, *icuValue; + const char *bcp47Value; + UErrorCode icuStatus = U_ZERO_ERROR; -int getKeywordValue(const char *keyword, const char *localeId, char *returnValue, ecma402_errorStatus *status, - bool isCanonicalized) { - char *canonicalized, *icuValue; - const char *bcp47Value; - UErrorCode icuStatus = U_ZERO_ERROR; + if (localeId == nullptr) { + return -1; + } - if (localeId == nullptr) { - return -1; - } + if (isCanonicalized) { + canonicalized = strdup(localeId); + } else { + canonicalized = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); + ecma402_canonicalizeUnicodeLocaleId(localeId, canonicalized, status); - if (isCanonicalized) { - canonicalized = strdup(localeId); - } else { - canonicalized = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); - ecma402_canonicalizeUnicodeLocaleId(localeId, canonicalized, status); + if (ecma402_hasError(status)) { + free(canonicalized); + return -1; + } + } - if (ecma402_hasError(status)) { - free(canonicalized); + icuValue = (char *)malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY); + uloc_getKeywordValue(canonicalized, keyword, icuValue, ULOC_KEYWORDS_CAPACITY, &icuStatus); + free(canonicalized); + + if (U_FAILURE(icuStatus) != U_ZERO_ERROR) { + free(icuValue); + ecma402_icuError(status, icuStatus, "Unable to get keyword %s from language tag \"%s\"", keyword, localeId); return -1; } - } - icuValue = (char *)malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY); - uloc_getKeywordValue(canonicalized, keyword, icuValue, ULOC_KEYWORDS_CAPACITY, &icuStatus); - free(canonicalized); - - if (U_FAILURE(icuStatus) != U_ZERO_ERROR) { - free(icuValue); - ecma402_icuError(status, icuStatus, "Unable to get keyword %s from language tag \"%s\"", keyword, localeId); - return -1; - } + if (strcmp(keyword, ICU_KEYWORD_NUMERIC) == 0 && strcmp(icuValue, "yes") == 0) { + strcpy(returnValue, "yes"); + free(icuValue); + return 3; + } - if (strcmp(keyword, ICU_KEYWORD_NUMERIC) == 0 && strcmp(icuValue, "yes") == 0) { - strcpy(returnValue, "yes"); - free(icuValue); - return 3; - } + bcp47Value = uloc_toUnicodeLocaleType(keyword, icuValue); - bcp47Value = uloc_toUnicodeLocaleType(keyword, icuValue); + if (bcp47Value == nullptr) { + free(icuValue); + return -1; + } - if (bcp47Value == nullptr) { + const size_t bcp47Length = strlen(bcp47Value); + strcpy(returnValue, bcp47Value); free(icuValue); - return -1; + + return bcp47Length; } - const size_t bcp47Length = strlen(bcp47Value); - strcpy(returnValue, bcp47Value); - free(icuValue); + int getLocaleCode(const char *localeId, char *returnValue, const char *codeType, + int (*callback)(const char *, char *, int, UErrorCode *), int capacity, + ecma402_errorStatus *status, bool isCanonicalized) + { + char *canonicalized, *icuValue; + UErrorCode icuStatus = U_ZERO_ERROR; + int icuValueLength; - return bcp47Length; -} + if (localeId == nullptr) { + return -1; + } -int getLocaleCode(const char *localeId, char *returnValue, const char *codeType, - int (*callback)(const char *, char *, int, UErrorCode *), int capacity, ecma402_errorStatus *status, - bool isCanonicalized) { - char *canonicalized, *icuValue; - UErrorCode icuStatus = U_ZERO_ERROR; - int icuValueLength; + if (isCanonicalized) { + canonicalized = strdup(localeId); + } else { + canonicalized = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); + ecma402_canonicalizeUnicodeLocaleId(localeId, canonicalized, status); - if (localeId == nullptr) { - return -1; - } + if (ecma402_hasError(status)) { + free(canonicalized); + return -1; + } + } - if (isCanonicalized) { - canonicalized = strdup(localeId); - } else { - canonicalized = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); - ecma402_canonicalizeUnicodeLocaleId(localeId, canonicalized, status); + icuValue = (char *)malloc(sizeof(char) * capacity); + icuValueLength = (*callback)(canonicalized, icuValue, capacity, &icuStatus); + free(canonicalized); - if (ecma402_hasError(status)) { - free(canonicalized); + if (U_FAILURE(icuStatus) != U_ZERO_ERROR) { + free(icuValue); + ecma402_icuError(status, icuStatus, "Unable to get %s from language tag \"%s\"", codeType, localeId); return -1; } - } - icuValue = (char *)malloc(sizeof(char) * capacity); - icuValueLength = (*callback)(canonicalized, icuValue, capacity, &icuStatus); - free(canonicalized); + if (strcmp(icuValue, "") == 0 || strcmp(icuValue, UNDETERMINED_LANGUAGE) == 0) { + free(icuValue); + return -1; + } - if (U_FAILURE(icuStatus) != U_ZERO_ERROR) { + memcpy(returnValue, icuValue, icuValueLength + 1); free(icuValue); - ecma402_icuError(status, icuStatus, "Unable to get %s from language tag \"%s\"", codeType, localeId); - return -1; - } - if (strcmp(icuValue, "") == 0 || strcmp(icuValue, UNDETERMINED_LANGUAGE) == 0) { - free(icuValue); - return -1; + return icuValueLength; } - memcpy(returnValue, icuValue, icuValueLength + 1); - free(icuValue); + int getMaxOrMin(enum maxOrMin type, const char *localeId, char *value, ecma402_errorStatus *status, + bool isCanonicalized) + { + char *icuValue, *bcp47Value; + UErrorCode icuStatus = U_ZERO_ERROR; + int icuValueLength, bcp47ValueLength; + ecma402_errorStatus *subStatus; + const char *maxOrMin; + int (*callback)(const char *, char *, int, UErrorCode *); - return icuValueLength; -} + if (type == MAXIMIZE) { + maxOrMin = "maximize"; + callback = uloc_addLikelySubtags; + } else { + maxOrMin = "minimize"; + callback = uloc_minimizeSubtags; + } -int getMaxOrMin(enum maxOrMin type, const char *localeId, char *value, ecma402_errorStatus *status, - bool isCanonicalized) { - char *icuValue, *bcp47Value; - UErrorCode icuStatus = U_ZERO_ERROR; - int icuValueLength, bcp47ValueLength; - ecma402_errorStatus *subStatus; - const char *maxOrMin; - int (*callback)(const char *, char *, int, UErrorCode *); - - if (type == MAXIMIZE) { - maxOrMin = "maximize"; - callback = uloc_addLikelySubtags; - } else { - maxOrMin = "minimize"; - callback = uloc_minimizeSubtags; - } + subStatus = ecma402_initErrorStatus(); - subStatus = ecma402_initErrorStatus(); + icuValue = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); + icuValueLength = getLocaleCode(localeId, icuValue, "maximized", callback, ULOC_FULLNAME_CAPACITY, subStatus, + isCanonicalized); - icuValue = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); - icuValueLength = - getLocaleCode(localeId, icuValue, "maximized", callback, ULOC_FULLNAME_CAPACITY, subStatus, isCanonicalized); + if (ecma402_hasError(subStatus)) { + status->ecma = subStatus->ecma; + status->icu = subStatus->icu; + ecma402_error(status, "Unable to %s language tag \"%s\"", maxOrMin, localeId); - if (ecma402_hasError(subStatus)) { - status->ecma = subStatus->ecma; - status->icu = subStatus->icu; - ecma402_error(status, "Unable to %s language tag \"%s\"", maxOrMin, localeId); + free(icuValue); + ecma402_freeErrorStatus(subStatus); - free(icuValue); - ecma402_freeErrorStatus(subStatus); + return -1; + } - return -1; - } + ecma402_freeErrorStatus(subStatus); - ecma402_freeErrorStatus(subStatus); + if (icuValueLength < 1) { + free(icuValue); + return icuValueLength; + } - if (icuValueLength < 1) { + bcp47Value = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); + bcp47ValueLength = uloc_toLanguageTag(icuValue, bcp47Value, ULOC_FULLNAME_CAPACITY, 1, &icuStatus); free(icuValue); - return icuValueLength; - } - bcp47Value = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); - bcp47ValueLength = uloc_toLanguageTag(icuValue, bcp47Value, ULOC_FULLNAME_CAPACITY, 1, &icuStatus); - free(icuValue); + if (U_FAILURE(icuStatus) != U_ZERO_ERROR) { + free(bcp47Value); + ecma402_icuError(status, icuStatus, "Unable to %s BCP 47 language tag \"%s\"", maxOrMin, localeId); - if (U_FAILURE(icuStatus) != U_ZERO_ERROR) { + return -1; + } + + strcpy(value, bcp47Value); free(bcp47Value); - ecma402_icuError(status, icuStatus, "Unable to %s BCP 47 language tag \"%s\"", maxOrMin, localeId); - return -1; + return bcp47ValueLength; } - strcpy(value, bcp47Value); - free(bcp47Value); - - return bcp47ValueLength; -} + int getNumberingSystemsForLocale(char *localeId, const char **values) + { + UNumberingSystem *numberingSystem; + UErrorCode status = U_ZERO_ERROR; + const char *name, *tmpName; -int getNumberingSystemsForLocale(char *localeId, const char **values) { - UNumberingSystem *numberingSystem; - UErrorCode status = U_ZERO_ERROR; - const char *name, *tmpName; + numberingSystem = unumsys_open(localeId, &status); + if (U_FAILURE(status) != U_ZERO_ERROR) { + return 0; + } - numberingSystem = unumsys_open(localeId, &status); - if (U_FAILURE(status) != U_ZERO_ERROR) { - return 0; - } + name = unumsys_getName(numberingSystem); - name = unumsys_getName(numberingSystem); + tmpName = uloc_toUnicodeLocaleType(ICU_KEYWORD_NUMBERING_SYSTEM, name); + unumsys_close(numberingSystem); - tmpName = uloc_toUnicodeLocaleType(ICU_KEYWORD_NUMBERING_SYSTEM, name); - unumsys_close(numberingSystem); + values[0] = strdup(tmpName); - values[0] = strdup(tmpName); + return 1; + } - return 1; -} + int getTimeZonesForLocale(char *localeId, const char **values) + { + UEnumeration *supported; + UErrorCode status = U_ZERO_ERROR; + char *region = nullptr; + const char *timeZone; + int regionLen, timeZoneLen, count = 0; -int getTimeZonesForLocale(char *localeId, const char **values) { - UEnumeration *supported; - UErrorCode status = U_ZERO_ERROR; - char *region = nullptr; - const char *timeZone; - int regionLen, timeZoneLen, count = 0; + region = (char *)malloc(sizeof(char) * ULOC_COUNTRY_CAPACITY); + regionLen = uloc_getCountry(localeId, region, ULOC_COUNTRY_CAPACITY, &status); - region = (char *)malloc(sizeof(char) * ULOC_COUNTRY_CAPACITY); - regionLen = uloc_getCountry(localeId, region, ULOC_COUNTRY_CAPACITY, &status); + if (U_FAILURE(status) != U_ZERO_ERROR) { + free(region); + return 0; + } - if (U_FAILURE(status) != U_ZERO_ERROR) { - free(region); - return 0; - } + // Return -1 if regionLen is 0 to indicate the locale identifier does not + // contain a region subtag. + if (regionLen == 0) { + free(region); + return -1; + } - // Return -1 if regionLen is 0 to indicate the locale identifier does not - // contain a region subtag. - if (regionLen == 0) { - free(region); - return -1; - } + supported = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, region, nullptr, &status); - supported = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, region, nullptr, &status); + while ((timeZone = uenum_next(supported, &timeZoneLen, &status)) != nullptr) { + if (U_FAILURE(status) != U_ZERO_ERROR) { + continue; + } - while ((timeZone = uenum_next(supported, &timeZoneLen, &status)) != nullptr) { - if (U_FAILURE(status) != U_ZERO_ERROR) { - continue; + values[count] = strdup(timeZone); + count++; } - values[count] = strdup(timeZone); - count++; - } - - uenum_close(supported); - free(region); + uenum_close(supported); + free(region); - return count; -} + return count; + } } // namespace diff --git a/src/ecma402/numbering_system.c b/src/ecma402/numbering_system.c index 1c8ea13..e1d91b3 100644 --- a/src/ecma402/numbering_system.c +++ b/src/ecma402/numbering_system.c @@ -21,7 +21,8 @@ #include #include -int ecma402_availableCanonicalNumberingSystems(const char **values) { +int ecma402_availableCanonicalNumberingSystems(const char **values) +{ UEnumeration *enumeration = NULL; UErrorCode status = U_ZERO_ERROR; const char *identifier; @@ -49,6 +50,7 @@ int ecma402_availableCanonicalNumberingSystems(const char **values) { return ecma402_sortAndRemoveDuplicates((char **)values, valuesCount, ecma402_strToLower); } -int ecma402_numberingSystemsOfLocale(ecma402_locale *locale, const char **values) { +int ecma402_numberingSystemsOfLocale(ecma402_locale *locale, const char **values) +{ return ecma402_keywordsOfLocale(locale, ICU_KEYWORD_NUMBERING_SYSTEM, values); } diff --git a/src/ecma402/time_zone.c b/src/ecma402/time_zone.c index 7878df2..e0623d9 100644 --- a/src/ecma402/time_zone.c +++ b/src/ecma402/time_zone.c @@ -24,7 +24,8 @@ static const char *canonicalizeTimeZoneName(const char *timeZoneIdentifier); -int ecma402_availableCanonicalTimeZones(const char **values) { +int ecma402_availableCanonicalTimeZones(const char **values) +{ UEnumeration *enumeration = NULL; UErrorCode status = U_ZERO_ERROR; const char *identifier; @@ -52,11 +53,13 @@ int ecma402_availableCanonicalTimeZones(const char **values) { return ecma402_sortAndRemoveDuplicates((char **)values, valuesCount, NULL); } -int ecma402_timeZonesOfLocale(ecma402_locale *locale, const char **values) { +int ecma402_timeZonesOfLocale(ecma402_locale *locale, const char **values) +{ return ecma402_keywordsOfLocale(locale, ICU_KEYWORD_TIME_ZONE, values); } -static const char *canonicalizeTimeZoneName(const char *timeZoneIdentifier) { +static const char *canonicalizeTimeZoneName(const char *timeZoneIdentifier) +{ if (strcasecmp(timeZoneIdentifier, "Etc/UTC") == 0 || strcasecmp(timeZoneIdentifier, "Etc/GMT") == 0 || strcasecmp(timeZoneIdentifier, "GMT") == 0) { return TIME_ZONE_UTC; diff --git a/src/ecma402/unit.cpp b/src/ecma402/unit.cpp index 404911d..98fde6c 100644 --- a/src/ecma402/unit.cpp +++ b/src/ecma402/unit.cpp @@ -68,7 +68,8 @@ #define SANCTIONED_UNIT_YARD "yard" #define SANCTIONED_UNIT_YEAR "year" -int ecma402_availableCanonicalUnits(const char **values) { +int ecma402_availableCanonicalUnits(const char **values) +{ icu::StringEnumeration *availableTypes; icu::MeasureUnit measureUnits[TYPES_CAPACITY]; UErrorCode status = U_ZERO_ERROR; @@ -107,7 +108,8 @@ int ecma402_availableCanonicalUnits(const char **values) { return ecma402_sortAndRemoveDuplicates((char **)values, valuesCount, ecma402_strToLower); } -bool ecma402_isSanctionedSingleUnitIdentifier(const char *unitIdentifier) { +bool ecma402_isSanctionedSingleUnitIdentifier(const char *unitIdentifier) +{ return strcasecmp(SANCTIONED_UNIT_ACRE, unitIdentifier) == 0 || strcasecmp(SANCTIONED_UNIT_BIT, unitIdentifier) == 0 || strcasecmp(SANCTIONED_UNIT_BYTE, unitIdentifier) == 0 || @@ -155,7 +157,8 @@ bool ecma402_isSanctionedSingleUnitIdentifier(const char *unitIdentifier) { strcasecmp(SANCTIONED_UNIT_YEAR, unitIdentifier) == 0; } -bool ecma402_isWellFormedUnitIdentifier(const char *unitIdentifier) { +bool ecma402_isWellFormedUnitIdentifier(const char *unitIdentifier) +{ if (ecma402_isSanctionedSingleUnitIdentifier(unitIdentifier)) { return true; } diff --git a/src/ecma402/unit.h b/src/ecma402/unit.h index e02780c..8af3bd8 100644 --- a/src/ecma402/unit.h +++ b/src/ecma402/unit.h @@ -20,7 +20,7 @@ #ifdef __cplusplus extern "C" { #else -#include +# include #endif /** diff --git a/src/ecma402/util.cpp b/src/ecma402/util.cpp index 9f2c3a9..7ad7125 100644 --- a/src/ecma402/util.cpp +++ b/src/ecma402/util.cpp @@ -16,14 +16,15 @@ #include namespace { -int compareStrings(const void *left, const void *right); -int removeEmpty(char **array, int length); -void sort(char **array, int length); -void strArrayWalk(char **array, int length, char *(*callback)(char *)); -unsigned char toAsciiLowerUnchecked(unsigned char character); + int compareStrings(const void *left, const void *right); + int removeEmpty(char **array, int length); + void sort(char **array, int length); + void strArrayWalk(char **array, int length, char *(*callback)(char *)); + unsigned char toAsciiLowerUnchecked(unsigned char character); } // namespace -int ecma402_removeDuplicates(char **array, int length, char *(*callback)(char *)) { +int ecma402_removeDuplicates(char **array, int length, char *(*callback)(char *)) +{ int i, j, k; int const originalLength = length; @@ -52,7 +53,8 @@ int ecma402_removeDuplicates(char **array, int length, char *(*callback)(char *) return length; } -int ecma402_sortAndRemoveDuplicates(char **array, int length, char *(*callback)(char *)) { +int ecma402_sortAndRemoveDuplicates(char **array, int length, char *(*callback)(char *)) +{ if (callback != nullptr) { strArrayWalk(array, length, (*callback)); } @@ -63,7 +65,8 @@ int ecma402_sortAndRemoveDuplicates(char **array, int length, char *(*callback)( return ecma402_removeDuplicates(array, length, nullptr); } -char *ecma402_strToLower(char *string) { +char *ecma402_strToLower(char *string) +{ for (char *p = string; *p != 0; p++) { *p = ecma402::util::toAsciiLower(*p); } @@ -71,7 +74,8 @@ char *ecma402_strToLower(char *string) { return string; } -char *ecma402_strToUpper(char *string) { +char *ecma402_strToUpper(char *string) +{ for (char *p = string; *p != 0; p++) { *p = ecma402::util::toAsciiUpper(*p); } @@ -79,31 +83,38 @@ char *ecma402_strToUpper(char *string) { return string; } -bool ecma402::util::isAscii(unsigned char character) { +bool ecma402::util::isAscii(unsigned char character) +{ return (character & ~0x7F) == 0; } -bool ecma402::util::isAsciiAlnum(unsigned char character) { +bool ecma402::util::isAsciiAlnum(unsigned char character) +{ return isAsciiDigit(character) || isAsciiAlpha(character); } -bool ecma402::util::isAsciiAlpha(unsigned char character) { +bool ecma402::util::isAsciiAlpha(unsigned char character) +{ return isAsciiLower(toAsciiLowerUnchecked(character)); } -bool ecma402::util::isAsciiDigit(unsigned char character) { +bool ecma402::util::isAsciiDigit(unsigned char character) +{ return character >= '0' && character <= '9'; } -bool ecma402::util::isAsciiLower(unsigned char character) { +bool ecma402::util::isAsciiLower(unsigned char character) +{ return character >= 'a' && character <= 'z'; } -bool ecma402::util::isAsciiUpper(unsigned char character) { +bool ecma402::util::isAsciiUpper(unsigned char character) +{ return character >= 'A' && character <= 'Z'; } -std::vector ecma402::util::split(const std::string &string, const std::string &delimiter) { +std::vector ecma402::util::split(const std::string &string, const std::string &delimiter) +{ size_t startPosition = 0, endPosition, delimiterLength = delimiter.length(); std::string token; std::vector result; @@ -119,55 +130,62 @@ std::vector ecma402::util::split(const std::string &string, const s return result; } -unsigned char ecma402::util::toAsciiLower(unsigned char character) { +unsigned char ecma402::util::toAsciiLower(unsigned char character) +{ return character | (static_cast(isAsciiUpper(character)) << 5); } -unsigned char ecma402::util::toAsciiUpper(unsigned char character) { +unsigned char ecma402::util::toAsciiUpper(unsigned char character) +{ return character & ~(static_cast(isAsciiLower(character)) << 5); } namespace { -int compareStrings(const void *left, const void *right) { - return strcmp(*(const char **)left, *(const char **)right); -} + int compareStrings(const void *left, const void *right) + { + return strcmp(*(const char **)left, *(const char **)right); + } -int removeEmpty(char **array, int length) { - int i, j; - int const originalLength = length; + int removeEmpty(char **array, int length) + { + int i, j; + int const originalLength = length; - for (i = 0; i < length; i++) { - if (strcmp(array[i], "") == 0) { - for (j = i; j < length - 1; j++) { - array[j] = array[j + 1]; + for (i = 0; i < length; i++) { + if (strcmp(array[i], "") == 0) { + for (j = i; j < length - 1; j++) { + array[j] = array[j + 1]; + } + length--; + i--; } - length--; - i--; } - } - if ((originalLength - length) > 0) { - for (i = length; i < originalLength; i++) { - array[i] = nullptr; + if ((originalLength - length) > 0) { + for (i = length; i < originalLength; i++) { + array[i] = nullptr; + } } - } - return length; -} + return length; + } -void sort(char **array, int length) { - qsort(array, length, sizeof(const char *), compareStrings); -} + void sort(char **array, int length) + { + qsort(array, length, sizeof(const char *), compareStrings); + } -void strArrayWalk(char **array, int length, char *(*callback)(char *)) { - for (int i = 0; i < length; i++) { - array[i] = (*callback)(array[i]); + void strArrayWalk(char **array, int length, char *(*callback)(char *)) + { + for (int i = 0; i < length; i++) { + array[i] = (*callback)(array[i]); + } } -} -unsigned char toAsciiLowerUnchecked(unsigned char character) { - return character | 0x20; -} + unsigned char toAsciiLowerUnchecked(unsigned char character) + { + return character | 0x20; + } } // namespace diff --git a/src/ecma402/util.h b/src/ecma402/util.h index 2b096c5..9f0e529 100644 --- a/src/ecma402/util.h +++ b/src/ecma402/util.h @@ -70,58 +70,58 @@ char *ecma402_strToUpper(char *string); #ifdef __cplusplus -#include -#include +# include +# include namespace ecma402 { -namespace util { - -/** - * Returns true if character is an ASCII character. - */ -bool isAscii(unsigned char character); - -/** - * Returns true if character is an ASCII alphanumeric value. - */ -bool isAsciiAlnum(unsigned char character); - -/** - * Returns true if character is an ASCII alphabet value. - */ -bool isAsciiAlpha(unsigned char character); - -/** - * Returns true if character is an ASCII numeric digit value. - */ -bool isAsciiDigit(unsigned char character); - -/** - * Returns true if character is an ASCII lowercase alphabet value. - */ -bool isAsciiLower(unsigned char character); - -/** - * Returns true if character is an ASCII uppercase alphabet value. - */ -bool isAsciiUpper(unsigned char character); - -/** - * Split a string by delimiter into a vector of strings. - */ -std::vector split(const std::string &string, const std::string &delimiter); - -/** - * Returns the ASCII lowercase variant of the character, if possible. - */ -unsigned char toAsciiLower(unsigned char character); - -/** - * Returns the ASCII uppercase variant of the character, if possible. - */ -unsigned char toAsciiUpper(unsigned char character); - -} // namespace util + namespace util { + + /** + * Returns true if character is an ASCII character. + */ + bool isAscii(unsigned char character); + + /** + * Returns true if character is an ASCII alphanumeric value. + */ + bool isAsciiAlnum(unsigned char character); + + /** + * Returns true if character is an ASCII alphabet value. + */ + bool isAsciiAlpha(unsigned char character); + + /** + * Returns true if character is an ASCII numeric digit value. + */ + bool isAsciiDigit(unsigned char character); + + /** + * Returns true if character is an ASCII lowercase alphabet value. + */ + bool isAsciiLower(unsigned char character); + + /** + * Returns true if character is an ASCII uppercase alphabet value. + */ + bool isAsciiUpper(unsigned char character); + + /** + * Split a string by delimiter into a vector of strings. + */ + std::vector split(const std::string &string, const std::string &delimiter); + + /** + * Returns the ASCII lowercase variant of the character, if possible. + */ + unsigned char toAsciiLower(unsigned char character); + + /** + * Returns the ASCII uppercase variant of the character, if possible. + */ + unsigned char toAsciiUpper(unsigned char character); + + } // namespace util } // namespace ecma402 #endif diff --git a/src/php/classes/category.c b/src/php/classes/category.c index 066cfb6..abc8380 100644 --- a/src/php/classes/category.c +++ b/src/php/classes/category.c @@ -16,7 +16,8 @@ zend_class_entry *ecma_ce_IntlCategory = NULL; -PHP_MINIT_FUNCTION(ecma_intl_category) { +PHP_MINIT_FUNCTION(ecma_intl_category) +{ ecma_ce_IntlCategory = register_class_Ecma_Intl_Category(); return SUCCESS; diff --git a/src/php/classes/intl.c b/src/php/classes/intl.c index e4c8915..4e25ac8 100644 --- a/src/php/classes/intl.c +++ b/src/php/classes/intl.c @@ -23,17 +23,20 @@ zend_class_entry *ecma_ce_Intl = NULL; -PHP_MINIT_FUNCTION(ecma_intl) { +PHP_MINIT_FUNCTION(ecma_intl) +{ ecma_ce_Intl = register_class_Ecma_Intl(); return SUCCESS; } -PHP_METHOD(Ecma_Intl, __construct) { +PHP_METHOD(Ecma_Intl, __construct) +{ ZEND_PARSE_PARAMETERS_NONE(); } -PHP_METHOD(Ecma_Intl, getCanonicalLocales) { +PHP_METHOD(Ecma_Intl, getCanonicalLocales) +{ zval *localesArg, *loopItem; HashTable *localesHt; const char **locales; @@ -128,7 +131,8 @@ PHP_METHOD(Ecma_Intl, getCanonicalLocales) { } } -PHP_METHOD(Ecma_Intl, supportedValuesOf) { +PHP_METHOD(Ecma_Intl, supportedValuesOf) +{ zend_object *categoryCase; zval *categoryZval; const char **values = NULL; diff --git a/src/php/classes/locale.c b/src/php/classes/locale.c index 39c2c9e..f32271d 100644 --- a/src/php/classes/locale.c +++ b/src/php/classes/locale.c @@ -28,11 +28,11 @@ #include "php/classes/locale_arginfo.h" -#include #include #include #include #include +#include #define RETURN_PROPERTY(property) \ do { \ @@ -68,7 +68,8 @@ static void setWeekInfo(zend_object *object, ecma402_locale *locale); static UCalendarDaysOfWeek weekDayEcmaToIcu(ecma402_dayOfWeek day); static ecma402_dayOfWeek weekDayIcuToEcma(UCalendarDaysOfWeek day); -PHP_MINIT_FUNCTION(ecma_intl_locale) { +PHP_MINIT_FUNCTION(ecma_intl_locale) +{ ecma_ce_IntlLocale = register_class_Ecma_Intl_Locale(php_json_serializable_ce, zend_ce_stringable); ecma_ce_IntlLocale->create_object = ecma_createIntlLocale; #if PHP_VERSION_ID >= 80300 @@ -83,7 +84,8 @@ PHP_MINIT_FUNCTION(ecma_intl_locale) { return SUCCESS; } -zend_object *ecma_createIntlLocale(zend_class_entry *classEntry) { +zend_object *ecma_createIntlLocale(zend_class_entry *classEntry) +{ ecma_IntlLocale *intlLocale; intlLocale = zend_object_alloc(sizeof(struct ecma_IntlLocale), classEntry); @@ -99,7 +101,8 @@ zend_object *ecma_createIntlLocale(zend_class_entry *classEntry) { return &intlLocale->std; } -PHP_METHOD(Ecma_Intl_Locale, __construct) { +PHP_METHOD(Ecma_Intl_Locale, __construct) +{ ecma_IntlLocale *intlLocale; ecma402_locale *locale; zend_object *object, *options = NULL, *tagArgObj = NULL; @@ -168,7 +171,8 @@ PHP_METHOD(Ecma_Intl_Locale, __construct) { } } -PHP_METHOD(Ecma_Intl_Locale, toString) { +PHP_METHOD(Ecma_Intl_Locale, toString) +{ ecma_IntlLocale *intlLocale; ZEND_PARSE_PARAMETERS_NONE(); @@ -178,44 +182,54 @@ PHP_METHOD(Ecma_Intl_Locale, toString) { RETURN_STRING(intlLocale->locale->canonical); } -PHP_METHOD(Ecma_Intl_Locale, getCalendars) { +PHP_METHOD(Ecma_Intl_Locale, getCalendars) +{ RETURN_PROPERTY(calendars); } -PHP_METHOD(Ecma_Intl_Locale, getCollations) { +PHP_METHOD(Ecma_Intl_Locale, getCollations) +{ RETURN_PROPERTY(collations); } -PHP_METHOD(Ecma_Intl_Locale, getCurrencies) { +PHP_METHOD(Ecma_Intl_Locale, getCurrencies) +{ RETURN_PROPERTY(currencies); } -PHP_METHOD(Ecma_Intl_Locale, getHourCycles) { +PHP_METHOD(Ecma_Intl_Locale, getHourCycles) +{ RETURN_PROPERTY(hourCycles); } -PHP_METHOD(Ecma_Intl_Locale, getNumberingSystems) { +PHP_METHOD(Ecma_Intl_Locale, getNumberingSystems) +{ RETURN_PROPERTY(numberingSystems); } -PHP_METHOD(Ecma_Intl_Locale, getTextInfo) { +PHP_METHOD(Ecma_Intl_Locale, getTextInfo) +{ RETURN_PROPERTY(textInfo); } -PHP_METHOD(Ecma_Intl_Locale, getTimeZones) { +PHP_METHOD(Ecma_Intl_Locale, getTimeZones) +{ RETURN_PROPERTY(timeZones); } -PHP_METHOD(Ecma_Intl_Locale, getWeekInfo) { +PHP_METHOD(Ecma_Intl_Locale, getWeekInfo) +{ RETURN_PROPERTY(weekInfo); } -PHP_METHOD(Ecma_Intl_Locale, jsonSerialize) { +PHP_METHOD(Ecma_Intl_Locale, jsonSerialize) +{ ZEND_PARSE_PARAMETERS_NONE(); serializeObjectProperties(return_value, getThis(), true); } -PHP_METHOD(Ecma_Intl_Locale, maximize) { +PHP_METHOD(Ecma_Intl_Locale, maximize) +{ ecma_IntlLocale *intlLocale; zend_object *object; @@ -229,7 +243,8 @@ PHP_METHOD(Ecma_Intl_Locale, maximize) { maxOrMin(true, intlLocale, return_value); } -PHP_METHOD(Ecma_Intl_Locale, minimize) { +PHP_METHOD(Ecma_Intl_Locale, minimize) +{ ecma_IntlLocale *intlLocale; zend_object *object; @@ -243,7 +258,8 @@ PHP_METHOD(Ecma_Intl_Locale, minimize) { maxOrMin(false, intlLocale, return_value); } -static ecma402_locale *applyOptions(ecma402_locale *locale, zend_object *options) { +static ecma402_locale *applyOptions(ecma402_locale *locale, zend_object *options) +{ const char *calendar = getOption(options, "calendar"); const char *caseFirst = getOption(options, "caseFirst"); const char *collation = getOption(options, "collation"); @@ -259,13 +275,15 @@ static ecma402_locale *applyOptions(ecma402_locale *locale, zend_object *options numberingSystem, numeric, region, script); } -static void freeLocaleObj(zend_object *object) { +static void freeLocaleObj(zend_object *object) +{ ecma_IntlLocale *intlLocale = ecma_IntlLocaleFromObj(object); zend_object_std_dtor(&intlLocale->std); ecma402_freeLocale(intlLocale->locale); } -static const char *getOption(zend_object *options, const char *name) { +static const char *getOption(zend_object *options, const char *name) +{ zval *property, rv; property = zend_read_property(ecma_ce_IntlLocaleOptions, options, name, strlen(name), false, &rv); @@ -277,7 +295,8 @@ static const char *getOption(zend_object *options, const char *name) { return NULL; } -static int getOptionNumeric(zend_object *options) { +static int getOptionNumeric(zend_object *options) +{ zval *property, rv; property = zend_read_property(ecma_ce_IntlLocaleOptions, options, "numeric", strlen("numeric"), false, &rv); @@ -293,7 +312,8 @@ static int getOptionNumeric(zend_object *options) { return -1; } -static int getWeekendDays(UCalendar *calendar, int *weekendDays) { +static int getWeekendDays(UCalendar *calendar, int *weekendDays) +{ UErrorCode status = U_ZERO_ERROR; UCalendarWeekdayType dayType; int count = 0; @@ -307,22 +327,23 @@ static int getWeekendDays(UCalendar *calendar, int *weekendDays) { } switch (dayType) { - // UCAL_WEEKEND_CEASE is a day that starts as the weekend and transitions to - // a weekday. It means this is a weekend. - case UCAL_WEEKEND_CEASE: - case UCAL_WEEKEND: - weekendDays[count] = weekDay; - count++; - break; - default: - continue; + // UCAL_WEEKEND_CEASE is a day that starts as the weekend and transitions to + // a weekday. It means this is a weekend. + case UCAL_WEEKEND_CEASE: + case UCAL_WEEKEND: + weekendDays[count] = weekDay; + count++; + break; + default: + continue; } } return count; } -static void maxOrMin(bool doMaximize, ecma_IntlLocale *locale, zval *dest) { +static void maxOrMin(bool doMaximize, ecma_IntlLocale *locale, zval *dest) +{ ecma402_errorStatus *status; zval arg1; char *value; @@ -351,14 +372,16 @@ static void maxOrMin(bool doMaximize, ecma_IntlLocale *locale, zval *dest) { efree(value); } -static void returnProperty(zend_class_entry *ce, ecma_IntlLocale *this, const char *name, zval *returnValue) { +static void returnProperty(zend_class_entry *ce, ecma_IntlLocale *this, const char *name, zval *returnValue) +{ zval *property, rv; property = zend_read_property(ce, &this->std, name, strlen(name), false, &rv); ZVAL_COPY(returnValue, property); } static void setPropertyArray(zend_class_entry *ce, ecma_IntlLocale *this, const char *name, int capacity, - int (*callback)(ecma402_locale *, const char **)) { + int (*callback)(ecma402_locale *, const char **)) +{ const char **values; int valuesCount; zval propertyValue; @@ -383,7 +406,8 @@ static void setPropertyArray(zend_class_entry *ce, ecma_IntlLocale *this, const efree(values); } -static void setPropertyString(zend_class_entry *ce, ecma_IntlLocale *this, const char *name, const char *value) { +static void setPropertyString(zend_class_entry *ce, ecma_IntlLocale *this, const char *name, const char *value) +{ if (value == NULL) { zend_update_property_null(ce, &this->std, name, strlen(name)); } else { @@ -391,7 +415,8 @@ static void setPropertyString(zend_class_entry *ce, ecma_IntlLocale *this, const } } -static void setTextInfo(zend_object *object, ecma402_locale *locale) { +static void setTextInfo(zend_object *object, ecma402_locale *locale) +{ ULayoutType layout; UErrorCode status = U_ZERO_ERROR; zval direction, textInfo; @@ -418,7 +443,8 @@ static void setTextInfo(zend_object *object, ecma402_locale *locale) { zend_object_release(textInfoObj); } -static void setWeekInfo(zend_object *object, ecma402_locale *locale) { +static void setWeekInfo(zend_object *object, ecma402_locale *locale) +{ UErrorCode status = U_ZERO_ERROR; UCalendar *calendar; UCalendarDaysOfWeek icuFirstDay = UCAL_SUNDAY; @@ -466,40 +492,42 @@ static void setWeekInfo(zend_object *object, ecma402_locale *locale) { zval_ptr_dtor(&weekend); } -static UCalendarDaysOfWeek weekDayEcmaToIcu(ecma402_dayOfWeek day) { +static UCalendarDaysOfWeek weekDayEcmaToIcu(ecma402_dayOfWeek day) +{ switch (day) { - case ECMA402_MONDAY: - return UCAL_MONDAY; - case ECMA402_TUESDAY: - return UCAL_TUESDAY; - case ECMA402_WEDNESDAY: - return UCAL_WEDNESDAY; - case ECMA402_THURSDAY: - return UCAL_THURSDAY; - case ECMA402_FRIDAY: - return UCAL_FRIDAY; - case ECMA402_SATURDAY: - return UCAL_SATURDAY; - default: - return UCAL_SUNDAY; + case ECMA402_MONDAY: + return UCAL_MONDAY; + case ECMA402_TUESDAY: + return UCAL_TUESDAY; + case ECMA402_WEDNESDAY: + return UCAL_WEDNESDAY; + case ECMA402_THURSDAY: + return UCAL_THURSDAY; + case ECMA402_FRIDAY: + return UCAL_FRIDAY; + case ECMA402_SATURDAY: + return UCAL_SATURDAY; + default: + return UCAL_SUNDAY; } } -static ecma402_dayOfWeek weekDayIcuToEcma(UCalendarDaysOfWeek day) { +static ecma402_dayOfWeek weekDayIcuToEcma(UCalendarDaysOfWeek day) +{ switch (day) { - case UCAL_MONDAY: - return ECMA402_MONDAY; - case UCAL_TUESDAY: - return ECMA402_TUESDAY; - case UCAL_WEDNESDAY: - return ECMA402_WEDNESDAY; - case UCAL_THURSDAY: - return ECMA402_THURSDAY; - case UCAL_FRIDAY: - return ECMA402_FRIDAY; - case UCAL_SATURDAY: - return ECMA402_SATURDAY; - default: - return ECMA402_SUNDAY; + case UCAL_MONDAY: + return ECMA402_MONDAY; + case UCAL_TUESDAY: + return ECMA402_TUESDAY; + case UCAL_WEDNESDAY: + return ECMA402_WEDNESDAY; + case UCAL_THURSDAY: + return ECMA402_THURSDAY; + case UCAL_FRIDAY: + return ECMA402_FRIDAY; + case UCAL_SATURDAY: + return ECMA402_SATURDAY; + default: + return ECMA402_SUNDAY; } } diff --git a/src/php/classes/locale.h b/src/php/classes/locale.h index e454132..1af0357 100644 --- a/src/php/classes/locale.h +++ b/src/php/classes/locale.h @@ -22,7 +22,8 @@ typedef struct ecma_IntlLocale { zend_object std; } ecma_IntlLocale; -static inline ecma_IntlLocale *ecma_IntlLocaleFromObj(zend_object *obj) { +static inline ecma_IntlLocale *ecma_IntlLocaleFromObj(zend_object *obj) +{ return (ecma_IntlLocale *)((char *)(obj)-XtOffsetOf(ecma_IntlLocale, std)); } diff --git a/src/php/classes/locale_character_direction.c b/src/php/classes/locale_character_direction.c index 926cbbc..c25c21e 100644 --- a/src/php/classes/locale_character_direction.c +++ b/src/php/classes/locale_character_direction.c @@ -16,7 +16,8 @@ zend_class_entry *ecma_ce_IntlLocaleCharacterDirection = NULL; -PHP_MINIT_FUNCTION(ecma_intl_locale_characterdirection) { +PHP_MINIT_FUNCTION(ecma_intl_locale_characterdirection) +{ ecma_ce_IntlLocaleCharacterDirection = register_class_Ecma_Intl_Locale_CharacterDirection(); return SUCCESS; diff --git a/src/php/classes/locale_options.c b/src/php/classes/locale_options.c index 2a85ff9..991af64 100644 --- a/src/php/classes/locale_options.c +++ b/src/php/classes/locale_options.c @@ -16,8 +16,8 @@ #include "ecma402/language_tag.h" -#include #include +#include zend_class_entry *ecma_ce_IntlLocaleOptions = NULL; @@ -37,7 +37,8 @@ static bool setProperty(const char *name, zend_object *object, zend_string *valu static void setRegion(zend_object *object, zend_string *paramStr, zend_object *paramObj); static void setScript(zend_object *object, zend_string *paramStr, zend_object *paramObj); -PHP_MINIT_FUNCTION(ecma_intl_locale_options) { +PHP_MINIT_FUNCTION(ecma_intl_locale_options) +{ ecma_ce_IntlLocaleOptions = register_class_Ecma_Intl_Locale_Options(zend_ce_iterator, php_json_serializable_ce); ecma_ce_IntlLocaleOptions->create_object = ecma_createIntlOptions; #if PHP_VERSION_ID >= 80300 @@ -52,7 +53,8 @@ PHP_MINIT_FUNCTION(ecma_intl_locale_options) { return SUCCESS; } -PHP_METHOD(Ecma_Intl_Locale_Options, __construct) { +PHP_METHOD(Ecma_Intl_Locale_Options, __construct) +{ zend_string *calendar = NULL, *collation = NULL, *currency = NULL, *hourCycle = NULL, *language = NULL, *numberingSystem = NULL, *region = NULL, *script = NULL; zend_object *calendarObj = NULL, *collationObj = NULL, *currencyObj = NULL, *hourCycleObj = NULL, @@ -96,32 +98,38 @@ PHP_METHOD(Ecma_Intl_Locale_Options, __construct) { setScript(object, script, scriptObj); } -PHP_METHOD(Ecma_Intl_Locale_Options, jsonSerialize) { +PHP_METHOD(Ecma_Intl_Locale_Options, jsonSerialize) +{ ZEND_PARSE_PARAMETERS_NONE(); serializeObjectProperties(return_value, getThis(), false); } -PHP_METHOD(Ecma_Intl_Locale_Options, current) { +PHP_METHOD(Ecma_Intl_Locale_Options, current) +{ ZEND_PARSE_PARAMETERS_NONE(); ecma_getCurrentOptionValue(return_value, getThis(), false); } -PHP_METHOD(Ecma_Intl_Locale_Options, key) { +PHP_METHOD(Ecma_Intl_Locale_Options, key) +{ ZEND_PARSE_PARAMETERS_NONE(); ecma_getCurrentOptionKey(return_value, getThis(), false); } -PHP_METHOD(Ecma_Intl_Locale_Options, next) { +PHP_METHOD(Ecma_Intl_Locale_Options, next) +{ ZEND_PARSE_PARAMETERS_NONE(); ecma_nextOption(getThis(), false); } -PHP_METHOD(Ecma_Intl_Locale_Options, rewind) { +PHP_METHOD(Ecma_Intl_Locale_Options, rewind) +{ ZEND_PARSE_PARAMETERS_NONE(); ecma_rewindOptions(getThis()); } -PHP_METHOD(Ecma_Intl_Locale_Options, valid) { +PHP_METHOD(Ecma_Intl_Locale_Options, valid) +{ ZEND_PARSE_PARAMETERS_NONE(); if (!ecma_endOfOptions(getThis(), false)) { @@ -131,16 +139,19 @@ PHP_METHOD(Ecma_Intl_Locale_Options, valid) { RETURN_FALSE; } -static bool isCaseFirst(const char *value) { +static bool isCaseFirst(const char *value) +{ return strcmp(value, "upper") == 0 || strcmp(value, "lower") == 0 || strcmp(value, "false") == 0; } -static bool isHourCycle(const char *value) { +static bool isHourCycle(const char *value) +{ return strcmp(value, "h11") == 0 || strcmp(value, "h12") == 0 || strcmp(value, "h23") == 0 || strcmp(value, "h24") == 0; } -static bool isValidCaseFirstParam(zval *param) { +static bool isValidCaseFirstParam(zval *param) +{ if (param == NULL || isFalse(param) || isString(param) || isStringable(param)) { return true; } @@ -157,13 +168,15 @@ static bool isValidCaseFirstParam(zval *param) { return false; } -static void setCalendar(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setCalendar(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("calendar", object, paramStr, paramObj, ecma402_isUnicodeLocaleIdentifierType)) { zend_value_error("calendar is not a well-formed calendar value"); } } -static void setCaseFirst(zend_object *object, zval *param) { +static void setCaseFirst(zend_object *object, zval *param) +{ bool success = false; if (param == NULL) { @@ -183,37 +196,43 @@ static void setCaseFirst(zend_object *object, zval *param) { } } -static void setCollation(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setCollation(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("collation", object, paramStr, paramObj, ecma402_isUnicodeLocaleIdentifierType)) { zend_value_error("collation is not a well-formed collation value"); } } -static void setCurrency(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setCurrency(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("currency", object, paramStr, paramObj, ecma402_isUnicodeCurrencyType)) { zend_value_error("currency is not a well-formed currency value"); } } -static void setHourCycle(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setHourCycle(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("hourCycle", object, paramStr, paramObj, isHourCycle)) { zend_value_error("hourCycle must be \"h11\", \"h12\", \"h23\", or \"h24\""); } } -static void setLanguage(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setLanguage(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("language", object, paramStr, paramObj, ecma402_isUnicodeLanguageSubtag)) { zend_value_error("language is not a well-formed language value"); } } -static void setNumberingSystem(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setNumberingSystem(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("numberingSystem", object, paramStr, paramObj, ecma402_isUnicodeLocaleIdentifierType)) { zend_value_error("numberingSystem is not a well-formed numbering system value"); } } -static void setNumeric(zend_object *object, bool numeric, bool isNumericNull) { +static void setNumeric(zend_object *object, bool numeric, bool isNumericNull) +{ const char *name = "numeric"; const size_t length = strlen(name); zend_class_entry *ce = ecma_ce_IntlLocaleOptions; @@ -227,7 +246,8 @@ static void setNumeric(zend_object *object, bool numeric, bool isNumericNull) { } static bool setProperty(const char *name, zend_object *object, zend_string *valueStr, zend_object *valueObj, - bool (*validator)(const char *)) { + bool (*validator)(const char *)) +{ const char *value; const size_t length = strlen(name); zend_class_entry *ce = ecma_ce_IntlLocaleOptions; @@ -273,13 +293,15 @@ static bool setProperty(const char *name, zend_object *object, zend_string *valu return false; } -static void setRegion(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setRegion(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("region", object, paramStr, paramObj, ecma402_isUnicodeRegionSubtag)) { zend_value_error("region is not a well-formed region value"); } } -static void setScript(zend_object *object, zend_string *paramStr, zend_object *paramObj) { +static void setScript(zend_object *object, zend_string *paramStr, zend_object *paramObj) +{ if (!setProperty("script", object, paramStr, paramObj, ecma402_isUnicodeScriptSubtag)) { zend_value_error("script is not a well-formed script value"); } diff --git a/src/php/classes/locale_text_info.c b/src/php/classes/locale_text_info.c index 8d03e1a..8f30eb5 100644 --- a/src/php/classes/locale_text_info.c +++ b/src/php/classes/locale_text_info.c @@ -23,7 +23,8 @@ zend_object_handlers ecma_handlers_IntlLocaleTextInfo; static void freeLocaleTextInfoObj(zend_object *object); -PHP_MINIT_FUNCTION(ecma_intl_locale_textinfo) { +PHP_MINIT_FUNCTION(ecma_intl_locale_textinfo) +{ ecma_ce_IntlLocaleTextInfo = register_class_Ecma_Intl_Locale_TextInfo(php_json_serializable_ce); ecma_ce_IntlLocaleTextInfo->create_object = ecma_createIntlLocaleTextInfo; #if PHP_VERSION_ID >= 80300 @@ -38,7 +39,8 @@ PHP_MINIT_FUNCTION(ecma_intl_locale_textinfo) { return SUCCESS; } -zend_object *ecma_createIntlLocaleTextInfo(zend_class_entry *classEntry) { +zend_object *ecma_createIntlLocaleTextInfo(zend_class_entry *classEntry) +{ ecma_IntlLocaleTextInfo *intlLocaleTextInfo; intlLocaleTextInfo = zend_object_alloc(sizeof(struct ecma_IntlLocaleTextInfo), classEntry); @@ -53,7 +55,8 @@ zend_object *ecma_createIntlLocaleTextInfo(zend_class_entry *classEntry) { return &intlLocaleTextInfo->std; } -PHP_METHOD(Ecma_Intl_Locale_TextInfo, __construct) { +PHP_METHOD(Ecma_Intl_Locale_TextInfo, __construct) +{ zval *directionObj; zend_object *this; ecma_IntlLocaleTextInfo *intlLocaleTextInfo; @@ -68,7 +71,8 @@ PHP_METHOD(Ecma_Intl_Locale_TextInfo, __construct) { zend_update_property(ecma_ce_IntlLocaleTextInfo, this, "direction", strlen("direction"), directionObj); } -PHP_METHOD(Ecma_Intl_Locale_TextInfo, jsonSerialize) { +PHP_METHOD(Ecma_Intl_Locale_TextInfo, jsonSerialize) +{ ecma_IntlLocaleTextInfo *intlLocaleTextInfo; zend_object *this; zval direction, rv; @@ -84,7 +88,8 @@ PHP_METHOD(Ecma_Intl_Locale_TextInfo, jsonSerialize) { add_property_zval(return_value, "direction", &direction); } -static void freeLocaleTextInfoObj(zend_object *object) { +static void freeLocaleTextInfoObj(zend_object *object) +{ ecma_IntlLocaleTextInfo *intlLocaleTextInfo = ecma_IntlLocaleTextInfoFromObj(object); zend_object_std_dtor(&intlLocaleTextInfo->std); } diff --git a/src/php/classes/locale_text_info.h b/src/php/classes/locale_text_info.h index 8ed1d54..5c4d024 100644 --- a/src/php/classes/locale_text_info.h +++ b/src/php/classes/locale_text_info.h @@ -19,7 +19,8 @@ typedef struct ecma_IntlLocaleTextInfo { zend_object std; } ecma_IntlLocaleTextInfo; -static inline ecma_IntlLocaleTextInfo *ecma_IntlLocaleTextInfoFromObj(zend_object *obj) { +static inline ecma_IntlLocaleTextInfo *ecma_IntlLocaleTextInfoFromObj(zend_object *obj) +{ return (ecma_IntlLocaleTextInfo *)((char *)(obj)-XtOffsetOf(ecma_IntlLocaleTextInfo, std)); } diff --git a/src/php/classes/locale_week_day.c b/src/php/classes/locale_week_day.c index 894c409..7c2a82a 100644 --- a/src/php/classes/locale_week_day.c +++ b/src/php/classes/locale_week_day.c @@ -16,7 +16,8 @@ zend_class_entry *ecma_ce_IntlLocaleWeekDay = NULL; -PHP_MINIT_FUNCTION(ecma_intl_locale_weekday) { +PHP_MINIT_FUNCTION(ecma_intl_locale_weekday) +{ ecma_ce_IntlLocaleWeekDay = register_class_Ecma_Intl_Locale_WeekDay(); return SUCCESS; diff --git a/src/php/classes/locale_week_info.c b/src/php/classes/locale_week_info.c index a26c919..8ccf4b2 100644 --- a/src/php/classes/locale_week_info.c +++ b/src/php/classes/locale_week_info.c @@ -23,7 +23,8 @@ zend_object_handlers ecma_handlers_IntlLocaleWeekInfo; static void freeLocaleWeekInfoObj(zend_object *object); -PHP_MINIT_FUNCTION(ecma_intl_locale_weekinfo) { +PHP_MINIT_FUNCTION(ecma_intl_locale_weekinfo) +{ ecma_ce_IntlLocaleWeekInfo = register_class_Ecma_Intl_Locale_WeekInfo(php_json_serializable_ce); ecma_ce_IntlLocaleWeekInfo->create_object = ecma_createIntlLocaleWeekInfo; #if PHP_VERSION_ID >= 80300 @@ -37,7 +38,9 @@ PHP_MINIT_FUNCTION(ecma_intl_locale_weekinfo) { return SUCCESS; } -zend_object *ecma_createIntlLocaleWeekInfo(zend_class_entry *classEntry) { + +zend_object *ecma_createIntlLocaleWeekInfo(zend_class_entry *classEntry) +{ ecma_IntlLocaleWeekInfo *intlLocaleWeekInfo; intlLocaleWeekInfo = zend_object_alloc(sizeof(struct ecma_IntlLocaleWeekInfo), classEntry); @@ -52,7 +55,8 @@ zend_object *ecma_createIntlLocaleWeekInfo(zend_class_entry *classEntry) { return &intlLocaleWeekInfo->std; } -PHP_METHOD(Ecma_Intl_Locale_WeekInfo, __construct) { +PHP_METHOD(Ecma_Intl_Locale_WeekInfo, __construct) +{ zval *firstDay, *weekend, *weekendItem; zend_object *this; zend_long minimalDays; @@ -90,7 +94,8 @@ PHP_METHOD(Ecma_Intl_Locale_WeekInfo, __construct) { zend_update_property_long(ecma_ce_IntlLocaleWeekInfo, this, "minimalDays", strlen("minimalDays"), minimalDays); } -PHP_METHOD(Ecma_Intl_Locale_WeekInfo, jsonSerialize) { +PHP_METHOD(Ecma_Intl_Locale_WeekInfo, jsonSerialize) +{ ecma_IntlLocaleWeekInfo *intlLocaleWeekInfo; zend_object *this; zval firstDay, weekend, minimalDays, rv; @@ -111,7 +116,8 @@ PHP_METHOD(Ecma_Intl_Locale_WeekInfo, jsonSerialize) { add_property_zval(return_value, "minimalDays", &minimalDays); } -static void freeLocaleWeekInfoObj(zend_object *object) { +static void freeLocaleWeekInfoObj(zend_object *object) +{ ecma_IntlLocaleWeekInfo *intlLocaleWeekInfo = ecma_IntlLocaleWeekInfoFromObj(object); zend_object_std_dtor(&intlLocaleWeekInfo->std); } diff --git a/src/php/classes/locale_week_info.h b/src/php/classes/locale_week_info.h index 96521d1..6966964 100644 --- a/src/php/classes/locale_week_info.h +++ b/src/php/classes/locale_week_info.h @@ -19,7 +19,8 @@ typedef struct ecma_IntlLocaleWeekInfo { zend_object std; } ecma_IntlLocaleWeekInfo; -static inline ecma_IntlLocaleWeekInfo *ecma_IntlLocaleWeekInfoFromObj(zend_object *obj) { +static inline ecma_IntlLocaleWeekInfo *ecma_IntlLocaleWeekInfoFromObj(zend_object *obj) +{ return (ecma_IntlLocaleWeekInfo *)((char *)(obj)-XtOffsetOf(ecma_IntlLocaleWeekInfo, std)); } diff --git a/src/php/classes/options.c b/src/php/classes/options.c index 2579c08..7856b6b 100644 --- a/src/php/classes/options.c +++ b/src/php/classes/options.c @@ -14,7 +14,8 @@ zend_object_handlers ecma_handlers_IntlOptions; -zend_object *ecma_createIntlOptions(zend_class_entry *classEntry) { +zend_object *ecma_createIntlOptions(zend_class_entry *classEntry) +{ ecma_IntlOptions *intlOptions; intlOptions = zend_object_alloc(sizeof(struct ecma_IntlOptions), classEntry); @@ -29,7 +30,8 @@ zend_object *ecma_createIntlOptions(zend_class_entry *classEntry) { return &intlOptions->std; } -bool ecma_endOfOptions(zval *object, bool allowNull) { +bool ecma_endOfOptions(zval *object, bool allowNull) +{ HashTable *ht = HASH_OF(object); while (zend_hash_has_more_elements(ht) == SUCCESS) { @@ -50,12 +52,14 @@ bool ecma_endOfOptions(zval *object, bool allowNull) { return true; } -void ecma_freeIntlOptionsObj(zend_object *object) { +void ecma_freeIntlOptionsObj(zend_object *object) +{ ecma_IntlOptions *intlOptions = ecma_IntlOptionsFromObj(object); zend_object_std_dtor(&intlOptions->std); } -void ecma_getCurrentOptionKey(zval *rv, zval *object, bool allowNull) { +void ecma_getCurrentOptionKey(zval *rv, zval *object, bool allowNull) +{ HashTable *ht = HASH_OF(object); while (zend_hash_has_more_elements(ht) == SUCCESS) { @@ -75,7 +79,8 @@ void ecma_getCurrentOptionKey(zval *rv, zval *object, bool allowNull) { } } -void ecma_getCurrentOptionValue(zval *rv, zval *object, bool allowNull) { +void ecma_getCurrentOptionValue(zval *rv, zval *object, bool allowNull) +{ HashTable *ht = HASH_OF(object); while (zend_hash_has_more_elements(ht) == SUCCESS) { @@ -95,7 +100,8 @@ void ecma_getCurrentOptionValue(zval *rv, zval *object, bool allowNull) { } } -void ecma_nextOption(zval *object, bool allowNull) { +void ecma_nextOption(zval *object, bool allowNull) +{ HashTable *ht = HASH_OF(object); zend_hash_move_forward(ht); @@ -116,7 +122,8 @@ void ecma_nextOption(zval *object, bool allowNull) { } } -void ecma_rewindOptions(zval *object) { +void ecma_rewindOptions(zval *object) +{ HashTable *ht = HASH_OF(object); zend_hash_internal_pointer_reset(ht); } diff --git a/src/php/classes/options.h b/src/php/classes/options.h index f132ad8..dde304e 100644 --- a/src/php/classes/options.h +++ b/src/php/classes/options.h @@ -19,7 +19,8 @@ typedef struct ecma_IntlOptions { zend_object std; } ecma_IntlOptions; -static inline ecma_IntlOptions *ecma_IntlOptionsFromObj(zend_object *obj) { +static inline ecma_IntlOptions *ecma_IntlOptionsFromObj(zend_object *obj) +{ return (ecma_IntlOptions *)((char *)(obj)-XtOffsetOf(ecma_IntlOptions, std)); } diff --git a/src/php/ecma_intl.c b/src/php/ecma_intl.c index 8c743b3..ad7f707 100644 --- a/src/php/ecma_intl.c +++ b/src/php/ecma_intl.c @@ -11,7 +11,7 @@ */ #ifdef HAVE_CONFIG_H -#include "config.h" +# include "config.h" #endif #include "php/ecma_intl.h" @@ -40,13 +40,14 @@ zend_module_entry ecma_intl_module_entry = {STANDARD_MODULE_HEADER, STANDARD_MODULE_PROPERTIES}; #ifdef COMPILE_DL_ECMA_INTL -#ifdef ZTS +# ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() -#endif +# endif ZEND_GET_MODULE(ecma_intl) #endif -PHP_MINIT_FUNCTION(ecma_intl_all) { +PHP_MINIT_FUNCTION(ecma_intl_all) +{ PHP_MINIT(ecma_intl)(INIT_FUNC_ARGS_PASSTHRU); PHP_MINIT(ecma_intl_category)(INIT_FUNC_ARGS_PASSTHRU); PHP_MINIT(ecma_intl_locale)(INIT_FUNC_ARGS_PASSTHRU); @@ -59,7 +60,8 @@ PHP_MINIT_FUNCTION(ecma_intl_all) { return SUCCESS; } -PHP_RINIT_FUNCTION(ecma_intl) { +PHP_RINIT_FUNCTION(ecma_intl) +{ #if defined(ZTS) && defined(COMPILE_DL_ECMA_INTL) ZEND_TSRMLS_CACHE_UPDATE(); #endif @@ -67,7 +69,8 @@ PHP_RINIT_FUNCTION(ecma_intl) { return SUCCESS; } -PHP_MINFO_FUNCTION(ecma_intl) { +PHP_MINFO_FUNCTION(ecma_intl) +{ UErrorCode status = U_ZERO_ERROR; const char *timeZoneDataVersion = NULL; diff --git a/src/php/php_common.h b/src/php/php_common.h index 6caca77..591e004 100644 --- a/src/php/php_common.h +++ b/src/php/php_common.h @@ -17,37 +17,44 @@ #include -#include -#include #include #include +#include +#include -static inline bool isArray(zval *arg) { +static inline bool isArray(zval *arg) +{ return EXPECTED(Z_TYPE_P(arg) == IS_ARRAY); } -static inline bool isFalse(zval *arg) { +static inline bool isFalse(zval *arg) +{ return EXPECTED(Z_TYPE_P(arg) == IS_FALSE); } -static inline bool isIterable(zval *arg) { +static inline bool isIterable(zval *arg) +{ return EXPECTED(zend_is_iterable(arg)); } -static inline bool isNull(zval *arg) { +static inline bool isNull(zval *arg) +{ return EXPECTED(Z_TYPE_P(arg) == IS_NULL); } -static inline bool isString(zval *arg) { +static inline bool isString(zval *arg) +{ return EXPECTED(Z_TYPE_P(arg) == IS_STRING); } -static inline bool isStringable(zval *arg) { +static inline bool isStringable(zval *arg) +{ return EXPECTED(Z_TYPE_P(arg) == IS_OBJECT) && EXPECTED(zend_class_implements_interface(Z_OBJCE_P(arg), zend_ce_stringable)); } -static inline int iteratorToHashTable(zend_object_iterator *iter, void *dest) { +static inline int iteratorToHashTable(zend_object_iterator *iter, void *dest) +{ zval *value; HashTable *ht = (HashTable *)dest; @@ -63,7 +70,8 @@ static inline int iteratorToHashTable(zend_object_iterator *iter, void *dest) { return ZEND_HASH_APPLY_KEEP; } -static inline void serializeObjectProperties(zval *rv, zval *object, bool allowNull) { +static inline void serializeObjectProperties(zval *rv, zval *object, bool allowNull) +{ HashTable *ht; zend_string *key; zval *value;