LCOV - code coverage report
Current view: top level - i18npool/source/ordinalsuffix - ordinalsuffix.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 51 60 85.0 %
Date: 2015-06-13 12:38:46 Functions: 8 9 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      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 <sal/log.hxx>
      25             : #include <cppuhelper/supportsservice.hxx>
      26             : #include <string.h>
      27             : #include "ordinalsuffix.hxx"
      28             : 
      29             : #include <unicode/rbnf.h>
      30             : #include <unicode/normlzr.h>
      31             : 
      32             : using namespace ::com::sun::star::i18n;
      33             : using namespace ::com::sun::star::uno;
      34             : using namespace ::com::sun::star;
      35             : 
      36             : namespace com { namespace sun { namespace star { namespace i18n {
      37             : 
      38             : 
      39           4 : OrdinalSuffixService::OrdinalSuffixService()
      40             : {
      41           4 : }
      42             : 
      43           6 : OrdinalSuffixService::~OrdinalSuffixService()
      44             : {
      45           6 : }
      46             : 
      47             : namespace
      48             : {
      49          18 :     OUString mungeUnicodeStringToOUString(const icu::UnicodeString &rIn, UErrorCode &rCode)
      50             :     {
      51             :         // Apply NFKC normalization to get normal letters
      52          18 :         icu::UnicodeString normalized;
      53          18 :         icu::Normalizer::normalize(rIn, UNORM_NFKC, 0, normalized, rCode);
      54             :         // Convert the normalized UnicodeString to OUString
      55          18 :         OUString sRet = (U_SUCCESS(rCode))
      56          18 :             ? OUString(reinterpret_cast<const sal_Unicode *>(normalized.getBuffer()), normalized.length())
      57          54 :             : OUString();
      58             :         // replace any minus signs with hyphen-minus so that negative numbers
      59             :         // from the simple number formatter and heavy-duty pattern formatter
      60             :         // agree as to their negative number sign
      61          36 :         return sRet.replace(0x2212, '-');
      62             :     }
      63             : }
      64             : 
      65             : /*
      66             :  * For this method to properly return the ordinal suffix for other locales
      67             :  * than english ones, ICU 4.2+ has to be used.
      68             :  */
      69           7 : uno::Sequence< OUString > SAL_CALL OrdinalSuffixService::getOrdinalSuffix( sal_Int32 nNumber,
      70             :         const lang::Locale &rLocale ) throw( RuntimeException, std::exception )
      71             : {
      72           7 :     uno::Sequence< OUString > retValue;
      73             : 
      74             :     // Get the value from ICU
      75           7 :     UErrorCode nCode = U_ZERO_ERROR;
      76          14 :     const icu::Locale aIcuLocale( LanguageTagIcu::getIcuLocale( LanguageTag( rLocale)));
      77             : 
      78          14 :     icu::RuleBasedNumberFormat formatter(icu::URBNF_ORDINAL, aIcuLocale, nCode);
      79           7 :     if (!U_SUCCESS(nCode))
      80           0 :         return retValue;
      81             : 
      82          14 :     boost::scoped_ptr<NumberFormat> xNumberFormat(icu::NumberFormat::createInstance(aIcuLocale, nCode));
      83           7 :     if (!U_SUCCESS(nCode))
      84           0 :         return retValue;
      85             : 
      86          14 :     icu::UnicodeString sFormatWithNoOrdinal;
      87          14 :     icu::Formattable ftmNumber((int32_t)nNumber);
      88          14 :     icu::FieldPosition icuPosA;
      89           7 :     xNumberFormat->format(ftmNumber, sFormatWithNoOrdinal, icuPosA, nCode);
      90           7 :     if (!U_SUCCESS(nCode))
      91           0 :         return retValue;
      92             : 
      93          14 :     OUString sValueWithNoOrdinal = mungeUnicodeStringToOUString(sFormatWithNoOrdinal, nCode);
      94           7 :     if (!U_SUCCESS(nCode))
      95           0 :         return retValue;
      96             : 
      97           7 :     int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
      98          18 :     for (int32_t i = 0; i < nRuleSets; ++i)
      99             :     {
     100          11 :         icu::UnicodeString ruleSet = formatter.getRuleSetName(i);
     101             : 
     102             :         // format the string
     103          22 :         icu::UnicodeString sFormatWithOrdinal;
     104          22 :         icu::FieldPosition icuPosB;
     105          11 :         formatter.format((int32_t)nNumber, ruleSet, sFormatWithOrdinal, icuPosB, nCode);
     106             : 
     107          11 :         if (!U_SUCCESS(nCode))
     108           0 :             continue;
     109             : 
     110          22 :         OUString sValueWithOrdinal = mungeUnicodeStringToOUString(sFormatWithOrdinal, nCode);
     111          11 :         if (!U_SUCCESS(nCode))
     112           0 :             continue;
     113             : 
     114             :         // fdo#54486 lets make sure that the ordinal format and the non-ordinal
     115             :         // format match at the start, so that the expectation can be verified
     116             :         // that there is some trailing "ordinal suffix" which can be extracted
     117          11 :         bool bSimpleOrdinalSuffix = sValueWithOrdinal.startsWith(sValueWithNoOrdinal);
     118             : 
     119             :         SAL_WARN_IF(!bSimpleOrdinalSuffix, "i18npool", "ordinal " <<
     120             :             sValueWithOrdinal << " didn't start with expected " <<
     121             :             sValueWithNoOrdinal << " prefix");
     122             : 
     123          11 :         if (!bSimpleOrdinalSuffix)
     124           0 :             continue;
     125             : 
     126             :         // Remove the number to get the prefix
     127          11 :         sal_Int32 len = sValueWithNoOrdinal.getLength();
     128             : 
     129          11 :         sal_Int32 newLength = retValue.getLength() + 1;
     130          11 :         retValue.realloc( newLength );
     131          11 :         retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len );
     132          11 :     }
     133             : 
     134           7 :     return retValue;
     135             : }
     136             : 
     137             : const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
     138             : 
     139           1 : OUString SAL_CALL OrdinalSuffixService::getImplementationName() throw( RuntimeException, std::exception )
     140             : {
     141           1 :     return OUString(cOrdinalSuffix);
     142             : }
     143             : 
     144           0 : sal_Bool SAL_CALL OrdinalSuffixService::supportsService( const OUString& rServiceName) throw( RuntimeException, std::exception )
     145             : {
     146           0 :     return cppu::supportsService(this, rServiceName);
     147             : }
     148             : 
     149           1 : Sequence< OUString > SAL_CALL OrdinalSuffixService::getSupportedServiceNames() throw( RuntimeException, std::exception )
     150             : {
     151           1 :     Sequence< OUString > aRet(1);
     152           1 :     aRet[0] = cOrdinalSuffix;
     153           1 :     return aRet;
     154             : }
     155             : 
     156             : } } } }
     157             : 
     158             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     159           4 : com_sun_star_i18n_OrdinalSuffix_get_implementation(
     160             :     css::uno::XComponentContext *,
     161             :     css::uno::Sequence<css::uno::Any> const &)
     162             : {
     163           4 :     return cppu::acquire(new css::i18n::OrdinalSuffixService());
     164             : }
     165             : 
     166             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11