summaryrefslogtreecommitdiffstats
path: root/include/unotools/charclass.hxx
blob: bc91aca63631e9b18a16c2464820958da818d5b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_UNOTOOLS_CHARCLASS_HXX
#define INCLUDED_UNOTOOLS_CHARCLASS_HXX

#include <unotools/unotoolsdllapi.h>
#include <i18nlangtag/languagetag.hxx>
#include <com/sun/star/i18n/DirectionProperty.hpp>
#include <com/sun/star/i18n/KCharacterType.hpp>
#include <com/sun/star/i18n/ParseResult.hpp>
#include <com/sun/star/i18n/UnicodeScript.hpp>
#include <com/sun/star/uno/Reference.hxx>

namespace com::sun::star::uno { class XComponentContext; }
namespace com::sun::star::i18n { class XCharacterClassification; }

inline constexpr sal_Int32 nCharClassAlphaType =
    css::i18n::KCharacterType::UPPER |
    css::i18n::KCharacterType::LOWER |
    css::i18n::KCharacterType::TITLE_CASE;

inline constexpr sal_Int32 nCharClassAlphaTypeMask =
    nCharClassAlphaType |
    css::i18n::KCharacterType::LETTER |     // Alpha is also always a LETTER
    css::i18n::KCharacterType::PRINTABLE |
    css::i18n::KCharacterType::BASE_FORM;

inline constexpr sal_Int32 nCharClassLetterType =
    nCharClassAlphaType |
    css::i18n::KCharacterType::LETTER;

inline constexpr sal_Int32 nCharClassLetterTypeMask =
    nCharClassAlphaTypeMask |
    css::i18n::KCharacterType::LETTER;

inline constexpr sal_Int32 nCharClassNumericType =
    css::i18n::KCharacterType::DIGIT;

inline constexpr sal_Int32 nCharClassNumericTypeMask =
    nCharClassNumericType |
    css::i18n::KCharacterType::PRINTABLE |
    css::i18n::KCharacterType::BASE_FORM;

inline constexpr sal_Int32 nCharClassBaseType =
    css::i18n::KCharacterType::BASE_FORM;

class UNOTOOLS_DLLPUBLIC CharClass
{
    LanguageTag                 maLanguageTag;
    css::uno::Reference< css::i18n::XCharacterClassification >    xCC;

    CharClass(const CharClass&) = delete;
    CharClass& operator=(const CharClass&) = delete;

public:
    /// Preferred ctor with service manager specified
    CharClass(
        const css::uno::Reference< css::uno::XComponentContext > & rxContext,
        LanguageTag aLanguageTag );

    /// Deprecated ctor, tries to get a process service manager or to load the
    /// library directly.
    CharClass( LanguageTag aLanguageTag );

    ~CharClass();

    /// get current Locale
    const LanguageTag& getLanguageTag() const;

    /// isdigit() on ascii values of entire string
    static bool isAsciiNumeric( std::u16string_view rStr );

    /// isalpha() on ascii values of entire string
    static bool isAsciiAlpha( std::u16string_view rStr );

    /// whether type is pure numeric or not, e.g. return of getCharacterType()
    static bool isNumericType( sal_Int32 nType )
    {
        return ((nType & nCharClassNumericType) != 0) &&
            ((nType & ~nCharClassNumericTypeMask) == 0);
    }

    /// whether type is pure alphanumeric or not, e.g. return of getCharacterType()
    static bool isAlphaNumericType( sal_Int32 nType )
    {
        return ((nType & (nCharClassAlphaType |
            nCharClassNumericType)) != 0) &&
            ((nType & ~(nCharClassAlphaTypeMask |
            nCharClassNumericTypeMask)) == 0);
    }

    /// whether type is pure letter or not, e.g. return of getCharacterType()
    static bool isLetterType( sal_Int32 nType )
    {
        return ((nType & nCharClassLetterType) != 0) &&
            ((nType & ~nCharClassLetterTypeMask) == 0);
    }

    /// whether type is pure letternumeric or not, e.g. return of getCharacterType()
    static bool isLetterNumericType( sal_Int32 nType )
    {
        return ((nType & (nCharClassLetterType |
            nCharClassNumericType)) != 0) &&
            ((nType & ~(nCharClassLetterTypeMask |
            nCharClassNumericTypeMask)) == 0);
    }

    // Wrapper implementations of class CharacterClassification

    OUString uppercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
    OUString lowercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
    OUString titlecase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;

    OUString uppercase( const OUString& _rStr ) const
    {
        return uppercase(_rStr, 0, _rStr.getLength());
    }
    OUString lowercase( const OUString& _rStr ) const
    {
        return lowercase(_rStr, 0, _rStr.getLength());
    }
    OUString titlecase( const OUString& _rStr ) const
    {
        return titlecase(_rStr, 0, _rStr.getLength());
    }

    sal_Int16 getType( const OUString& rStr, sal_Int32 nPos ) const;
    css::i18n::DirectionProperty getCharacterDirection( const OUString& rStr, sal_Int32 nPos ) const;
    css::i18n::UnicodeScript getScript( const OUString& rStr, sal_Int32 nPos ) const;
    sal_Int32 getCharacterType( const OUString& rStr, sal_Int32 nPos ) const;

    css::i18n::ParseResult parseAnyToken(
                                    const OUString& rStr,
                                    sal_Int32 nPos,
                                    sal_Int32 nStartCharFlags,
                                    const OUString& userDefinedCharactersStart,
                                    sal_Int32 nContCharFlags,
                                    const OUString& userDefinedCharactersCont ) const;

    css::i18n::ParseResult parsePredefinedToken(
                                    sal_Int32 nTokenType,
                                    const OUString& rStr,
                                    sal_Int32 nPos,
                                    sal_Int32 nStartCharFlags,
                                    const OUString& userDefinedCharactersStart,
                                    sal_Int32 nContCharFlags,
                                    const OUString& userDefinedCharactersCont ) const;

    // Functionality of class International methods

    bool isAlpha( const OUString& rStr, sal_Int32 nPos ) const;
    bool isLetter( const OUString& rStr, sal_Int32 nPos ) const;
    bool isDigit( const OUString& rStr, sal_Int32 nPos ) const;
    bool isAlphaNumeric( const OUString& rStr, sal_Int32 nPos ) const;
    bool isLetterNumeric( const OUString& rStr, sal_Int32 nPos ) const;
    bool isBase( const OUString& rStr, sal_Int32 nPos ) const;
    bool isUpper( const OUString& rStr, sal_Int32 nPos ) const;
    bool isLetter( const OUString& rStr ) const;
    bool isNumeric( const OUString& rStr ) const;
    bool isLetterNumeric( const OUString& rStr ) const;

    bool isUpper( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;

private:

    const css::lang::Locale &  getMyLocale() const;
};

#endif // INCLUDED_UNOTOOLS_CHARCLASS_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */