Bump version to 4.3-4
[LibreOffice.git] / i18npool / source / ordinalsuffix / ordinalsuffix.cxx
blobdafc783ab2b91a95359b03a96321d6fc24c37765
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/XComponentContext.hpp>
21 #include <boost/scoped_ptr.hpp>
22 #include <i18nlangtag/languagetag.hxx>
23 #include <i18nlangtag/languagetagicu.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <string.h>
26 #include "ordinalsuffix.hxx"
28 #include <unicode/rbnf.h>
29 #include <unicode/normlzr.h>
31 using namespace ::com::sun::star::i18n;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star;
35 namespace com { namespace sun { namespace star { namespace i18n {
38 OrdinalSuffix::OrdinalSuffix()
42 OrdinalSuffix::~OrdinalSuffix()
46 namespace
48 OUString mungeUnicodeStringToOUString(const icu::UnicodeString &rIn, UErrorCode &rCode)
50 // Apply NFKC normalization to get normal letters
51 icu::UnicodeString normalized;
52 icu::Normalizer::normalize(rIn, UNORM_NFKC, 0, normalized, rCode);
53 // Convert the normalized UnicodeString to OUString
54 OUString sRet = (U_SUCCESS(rCode))
55 ? OUString(reinterpret_cast<const sal_Unicode *>(normalized.getBuffer()), normalized.length())
56 : OUString();
57 // replace any minus signs with hyphen-minus so that negative numbers
58 // from the simple number formatter and heavy-duty pattern formatter
59 // agree as to their negative number sign
60 return sRet.replace(0x2212, '-');
65 * For this method to properly return the ordinal suffix for other locales
66 * than english ones, ICU 4.2+ has to be used.
68 uno::Sequence< OUString > SAL_CALL OrdinalSuffix::getOrdinalSuffix( sal_Int32 nNumber,
69 const lang::Locale &rLocale ) throw( RuntimeException, std::exception )
71 uno::Sequence< OUString > retValue;
73 // Get the value from ICU
74 UErrorCode nCode = U_ZERO_ERROR;
75 const icu::Locale aIcuLocale( LanguageTagIcu::getIcuLocale( LanguageTag( rLocale)));
77 icu::RuleBasedNumberFormat formatter(icu::URBNF_ORDINAL, aIcuLocale, nCode);
78 if (!U_SUCCESS(nCode))
79 return retValue;
81 boost::scoped_ptr<NumberFormat> xNumberFormat(icu::NumberFormat::createInstance(aIcuLocale, nCode));
82 if (!U_SUCCESS(nCode))
83 return retValue;
85 icu::UnicodeString sFormatWithNoOrdinal;
86 icu::Formattable ftmNumber((int32_t)nNumber);
87 icu::FieldPosition icuPosA;
88 xNumberFormat->format(ftmNumber, sFormatWithNoOrdinal, icuPosA, nCode);
89 if (!U_SUCCESS(nCode))
90 return retValue;
92 OUString sValueWithNoOrdinal = mungeUnicodeStringToOUString(sFormatWithNoOrdinal, nCode);
93 if (!U_SUCCESS(nCode))
94 return retValue;
96 int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
97 for (int32_t i = 0; i < nRuleSets; ++i)
99 icu::UnicodeString ruleSet = formatter.getRuleSetName(i);
101 // format the string
102 icu::UnicodeString sFormatWithOrdinal;
103 icu::FieldPosition icuPosB;
104 formatter.format((int32_t)nNumber, ruleSet, sFormatWithOrdinal, icuPosB, nCode);
106 if (!U_SUCCESS(nCode))
107 continue;
109 OUString sValueWithOrdinal = mungeUnicodeStringToOUString(sFormatWithOrdinal, nCode);
110 if (!U_SUCCESS(nCode))
111 continue;
113 // fdo#54486 lets make sure that the ordinal format and the non-ordinal
114 // format match at the start, so that the expectation can be verified
115 // that there is some trailing "ordinal suffix" which can be extracted
116 bool bSimpleOrdinalSuffix = sValueWithOrdinal.startsWith(sValueWithNoOrdinal);
118 SAL_WARN_IF(!bSimpleOrdinalSuffix, "i18npool", "ordinal " <<
119 sValueWithOrdinal << " didn't start with expected " <<
120 sValueWithNoOrdinal << " prefix");
122 if (!bSimpleOrdinalSuffix)
123 continue;
125 // Remove the number to get the prefix
126 sal_Int32 len = sValueWithNoOrdinal.getLength();
128 sal_Int32 newLength = retValue.getLength() + 1;
129 retValue.realloc( newLength );
130 retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len );
133 return retValue;
136 const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
138 OUString SAL_CALL OrdinalSuffix::getImplementationName(void) throw( RuntimeException, std::exception )
140 return OUString::createFromAscii(cOrdinalSuffix);
143 sal_Bool SAL_CALL OrdinalSuffix::supportsService( const OUString& rServiceName) throw( RuntimeException, std::exception )
145 return cppu::supportsService(this, rServiceName);
148 Sequence< OUString > SAL_CALL OrdinalSuffix::getSupportedServiceNames(void) throw( RuntimeException, std::exception )
150 Sequence< OUString > aRet(1);
151 aRet[0] = OUString::createFromAscii(cOrdinalSuffix);
152 return aRet;
155 } } } }
157 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
158 com_sun_star_i18n_OrdinalSuffix_get_implementation(
159 css::uno::XComponentContext *,
160 css::uno::Sequence<css::uno::Any> const &)
162 return cppu::acquire(new css::i18n::OrdinalSuffix());
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */