1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <cppuhelper/supportsservice.hxx>
21 #include <chaptercollator.hxx>
22 #include <com/sun/star/i18n/KCharacterType.hpp>
23 #include <com/sun/star/i18n/ParseResult.hpp>
24 #include <com/sun/star/i18n/CharacterClassification.hpp>
26 using namespace ::com::sun::star::lang
;
27 using namespace ::com::sun::star::uno
;
28 using namespace ::com::sun::star::i18n
;
32 ChapterCollator::ChapterCollator( const Reference
< XComponentContext
>& rxContext
) : CollatorImpl(rxContext
)
34 cclass
= CharacterClassification::create( rxContext
);
37 ChapterCollator::~ChapterCollator()
42 ChapterCollator::compareString( const OUString
& s1
, const OUString
& s2
)
44 return compareSubstring(s1
, 0, s1
.getLength(), s2
, 0, s2
.getLength());
47 #define DIGIT KCharacterType::DIGIT
50 ChapterCollator::compareSubstring( const OUString
& str1
, sal_Int32 off1
, sal_Int32 len1
,
51 const OUString
& str2
, sal_Int32 off2
, sal_Int32 len2
)
53 if( len1
<= 1 || len2
<= 1 || ! cclass
.is() )
54 return CollatorImpl::compareSubstring( str1
, off1
, len1
, str2
, off2
, len2
);
57 for (i1
= len1
; i1
&& (cclass
->getCharacterType(str1
, off1
+i1
-1, nLocale
) & DIGIT
); i1
--) ;
58 for (i2
= len2
; i2
&& (cclass
->getCharacterType(str2
, off2
+i2
-1, nLocale
) & DIGIT
); i2
--) ;
60 sal_Int32 ans
= CollatorImpl::compareSubstring(str1
, off1
, i1
, str2
, off2
, i2
);
64 const OUString
aAddAllowed("?");
65 ParseResult res1
, res2
;
66 // since parseAnyToken does not take length as parameter, we have to copy
67 // it to a temp. string.
68 OUString s1
= str1
.copy(off1
+i1
, len1
-i1
), s2
= str2
.copy(off2
+i2
, len2
-i2
);
69 res1
= cclass
->parseAnyToken( s1
, 0, nLocale
, DIGIT
, aAddAllowed
, DIGIT
, aAddAllowed
);
70 res2
= cclass
->parseAnyToken( s2
, 0, nLocale
, DIGIT
, aAddAllowed
, DIGIT
, aAddAllowed
);
72 return res1
.Value
== res2
.Value
? 0 : res1
.Value
> res2
.Value
? 1 : -1;
75 const sal_Char cChapCollator
[] = "com.sun.star.i18n.ChapterCollator";
78 ChapterCollator::getImplementationName()
80 return OUString(cChapCollator
);
84 ChapterCollator::supportsService(const OUString
& rServiceName
)
86 return cppu::supportsService(this, rServiceName
);
89 Sequence
< OUString
> SAL_CALL
90 ChapterCollator::getSupportedServiceNames()
92 Sequence
< OUString
> aRet
{ cChapCollator
};
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */