LCOV - code coverage report
Current view: top level - include/comphelper - string.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 39 50 78.0 %
Date: 2015-06-13 12:38:46 Functions: 16 20 80.0 %
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             : #ifndef INCLUDED_COMPHELPER_STRING_HXX
      21             : #define INCLUDED_COMPHELPER_STRING_HXX
      22             : 
      23             : #include <sal/config.h>
      24             : 
      25             : #include <cstddef>
      26             : #include <comphelper/comphelperdllapi.h>
      27             : #include <sal/types.h>
      28             : #include <rtl/strbuf.hxx>
      29             : #include <rtl/ustrbuf.hxx>
      30             : #include <com/sun/star/uno/Sequence.hxx>
      31             : 
      32             : #include <com/sun/star/uno/XComponentContext.hpp>
      33             : #include <com/sun/star/i18n/XCollator.hpp>
      34             : #include <com/sun/star/i18n/XBreakIterator.hpp>
      35             : 
      36             : // OUString helper functions that are not widespread or mature enough to
      37             : // go into the stable URE API:
      38             : namespace comphelper { namespace string {
      39             : 
      40             : /** Compare an OString to a single char
      41             : 
      42             :     @param rIn      The input OString
      43             :     @param c        The character to compare againsg
      44             : 
      45             :     @return         true if rIn has one char and its equal to c
      46             :  */
      47           4 : inline bool equals(const OString& rIn, sal_Char c)
      48           4 : { return rIn.getLength() == 1 && rIn[0] == c; }
      49             : 
      50             : /** Compare an OUString to a single char
      51             : 
      52             :     @param rIn      The input OUString
      53             :     @param c        The character to compare againsg
      54             : 
      55             :     @return         true if rIn has one char and its equal to c
      56             :  */
      57        3478 : inline bool equals(const OUString& rIn, sal_Unicode c)
      58        3478 : { return rIn.getLength() == 1 && rIn[0] == c; }
      59             : 
      60             : /** Removes all occurrences of a character from within the source string
      61             : 
      62             :     @deprecated  Use OString::replaceAll(OString(c), OString())
      63             :     instead.
      64             : 
      65             :     @param rIn      The input OString
      66             :     @param c        The character to be removed
      67             : 
      68             :     @return         The resulting OString
      69             :  */
      70         707 : inline OString remove(const OString &rIn,
      71             :     sal_Char c)
      72         707 : { return rIn.replaceAll(OString(c), OString()); }
      73             : 
      74             : /** Removes all occurrences of a character from within the source string
      75             : 
      76             :     @deprecated  Use
      77             :     OUString::replaceAll(OUString(c), OUString()) instead.
      78             : 
      79             :     @param rIn      The input OUString
      80             :     @param c        The character to be removed
      81             : 
      82             :     @return         The resulting OUString
      83             :  */
      84   171082555 : inline OUString remove(const OUString &rIn,
      85             :     sal_Unicode c)
      86   171082555 : { return rIn.replaceAll(OUString(c), OUString()); }
      87             : 
      88             : /** Removes all occurrences of a character from within the source string
      89             : 
      90             :     @param rIn      The input OUStringBuffer
      91             :     @param c        The character to be removed
      92             : 
      93             :     @return         The resulting OUStringBuffer
      94             :  */
      95        7366 : inline OUStringBuffer& remove(OUStringBuffer &rIn,
      96             :     sal_Unicode c)
      97             : {
      98        7366 :     sal_Int32 index = 0;
      99             :     while (true)
     100             :     {
     101        7366 :         if (index >= rIn.getLength())
     102         920 :             break;
     103        6446 :         index = rIn.indexOf(c, index);
     104        6446 :         if (index == -1)
     105        6446 :             break;
     106           0 :         rIn.remove(index, 1);
     107             :     }
     108           0 :     return rIn;
     109             : }
     110             : 
     111             : /** Strips occurrences of a character from the start of the source string
     112             : 
     113             :     @param rIn      The input OString
     114             :     @param c        The character to be stripped from the start
     115             : 
     116             :     @return         The resulting OString
     117             :  */
     118             : COMPHELPER_DLLPUBLIC OString stripStart(const OString &rIn,
     119             :     sal_Char c);
     120             : 
     121             : /** Strips occurrences of a character from the start of the source string
     122             : 
     123             :     @param rIn      The input OUString
     124             :     @param c        The character to be stripped from the start
     125             : 
     126             :     @return         The resulting OUString
     127             :  */
     128             : COMPHELPER_DLLPUBLIC OUString stripStart(const OUString &rIn,
     129             :     sal_Unicode c);
     130             : 
     131             : /** Strips occurrences of a character from the end of the source string
     132             : 
     133             :     @param rIn      The input OString
     134             :     @param c        The character to be stripped from the end
     135             : 
     136             :     @return         The resulting OString
     137             :  */
     138             : COMPHELPER_DLLPUBLIC OString stripEnd(const OString &rIn,
     139             :     sal_Char c);
     140             : 
     141             : /** Strips occurrences of a character from the end of the source string
     142             : 
     143             :     @param rIn      The input OUString
     144             :     @param c        The character to be stripped from the end
     145             : 
     146             :     @return         The resulting OUString
     147             :  */
     148             : COMPHELPER_DLLPUBLIC OUString stripEnd(const OUString &rIn,
     149             :     sal_Unicode c);
     150             : 
     151             : /** Strips occurrences of a character from the start and end of the source string
     152             : 
     153             :     @param rIn      The input OString
     154             :     @param c        The character to be stripped from the start and end
     155             : 
     156             :     @return         The resulting OString
     157             :  */
     158             : COMPHELPER_DLLPUBLIC OString strip(const OString &rIn,
     159             :     sal_Char c);
     160             : 
     161             : /** Strips occurrences of a character from the start and end of the source string
     162             : 
     163             :     @param rIn      The input OUString
     164             :     @param c        The character to be stripped from the start and end
     165             : 
     166             :     @return         The resulting OUString
     167             :  */
     168             : COMPHELPER_DLLPUBLIC OUString strip(const OUString &rIn,
     169             :     sal_Unicode c);
     170             : 
     171             : /** Returns number of tokens in an OUString
     172             : 
     173             :   @param    rIn     the input OString
     174             :   @param    cTok    the character which separate the tokens.
     175             :   @return   the number of tokens
     176             : */
     177             : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const OString &rIn, sal_Char cTok);
     178             : 
     179             : /** Returns number of tokens in an OUString
     180             : 
     181             :   @param    rIn     the input OUString
     182             :   @param    cTok    the character which separate the tokens.
     183             :   @return   the number of tokens
     184             : */
     185             : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const OUString &rIn, sal_Unicode cTok);
     186             : 
     187             : /** Reverse an OUString
     188             : 
     189             :   @param    rIn     the input OUString
     190             :   @return   the reversed input
     191             : */
     192             : COMPHELPER_DLLPUBLIC OUString reverseString(const OUString &rStr);
     193             : 
     194             : /** Reverse an OString
     195             : 
     196             :   @param    rIn     the input OString
     197             :   @return   the reversed input
     198             : */
     199             : COMPHELPER_DLLPUBLIC OString reverseString(const OString &rStr);
     200             : 
     201             : 
     202             : namespace detail
     203             : {
     204           0 :     template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
     205             :     {
     206           0 :         if (nLen < rBuffer.getLength())
     207           0 :             rBuffer.remove(nLen, rBuffer.getLength()-nLen);
     208           0 :         return rBuffer;
     209             :     }
     210             : }
     211             : 
     212             : /** Truncate a buffer to a given length.
     213             : 
     214             :     If the StringBuffer has more characters than nLength it will be truncated
     215             :     on the right to nLength characters.
     216             : 
     217             :     Has no effect if the StringBuffer is <= nLength
     218             : 
     219             :     @param rBuf   StringBuffer to operate on
     220             :     @param nLength   Length to truncate the buffer to
     221             : 
     222             :     @return         rBuf;
     223             :  */
     224             : inline OStringBuffer& truncateToLength(
     225             :     OStringBuffer& rBuffer, sal_Int32 nLength)
     226             : {
     227             :     return detail::truncateToLength(rBuffer, nLength);
     228             : }
     229             : 
     230           0 : inline OUStringBuffer& truncateToLength(
     231             :     OUStringBuffer& rBuffer, sal_Int32 nLength)
     232             : {
     233           0 :     return detail::truncateToLength(rBuffer, nLength);
     234             : }
     235             : 
     236             : namespace detail
     237             : {
     238       86412 :     template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
     239             :         U cFill = '\0')
     240             :     {
     241       86412 :         sal_Int32 nOrigLen = rBuffer.getLength();
     242       86412 :         if (nLen > nOrigLen)
     243             :         {
     244       85423 :             rBuffer.setLength(nLen);
     245      324976 :             for (sal_Int32 i = nOrigLen; i < nLen; ++i)
     246      239553 :                 rBuffer[i] = cFill;
     247             :         }
     248       86412 :         return rBuffer;
     249             :     }
     250             : }
     251             : 
     252             : /** Pad a buffer to a given length using a given char.
     253             : 
     254             :     If the StringBuffer has less characters than nLength it will be expanded on
     255             :     the right to nLength characters, with the expansion filled using cFill.
     256             : 
     257             :     Has no effect if the StringBuffer is >= nLength
     258             : 
     259             :     @param rBuf   StringBuffer to operate on
     260             :     @param nLength   Length to pad the buffer to
     261             :     @param cFill  character to fill expansion with
     262             : 
     263             :     @return         rBuf;
     264             :  */
     265         131 : inline OStringBuffer& padToLength(
     266             :     OStringBuffer& rBuffer, sal_Int32 nLength,
     267             :     sal_Char cFill = '\0')
     268             : {
     269         131 :     return detail::padToLength(rBuffer, nLength, cFill);
     270             : }
     271             : 
     272       86281 : inline OUStringBuffer& padToLength(
     273             :     OUStringBuffer& rBuffer, sal_Int32 nLength,
     274             :     sal_Unicode cFill = '\0')
     275             : {
     276       86281 :     return detail::padToLength(rBuffer, nLength, cFill);
     277             : }
     278             : 
     279             : /** Replace a token in a string
     280             :     @param rIn       OUString in which the token is to be replaced
     281             :     @param nToken    which nToken to replace
     282             :     @param cTok      token delimiter
     283             :     @param rNewToken replacement token
     284             : 
     285             :     @return original string with token nToken replaced by rNewToken
     286             :  */
     287             : COMPHELPER_DLLPUBLIC OUString setToken(const OUString& rIn, sal_Int32 nToken, sal_Unicode cTok,
     288             :     const OUString& rNewToken);
     289             : 
     290             : /** Find any of a list of code units in the string.
     291             :     @param rIn      OUString to search
     292             :     @param pChars   0-terminated array of sal_Unicode code units to search for
     293             :     @param nPos     start position
     294             : 
     295             :     @return position of first occurrence of any of the elements of pChars
     296             :             or -1 if none of the code units occur in the string
     297             :  */
     298             : COMPHELPER_DLLPUBLIC sal_Int32 indexOfAny(OUString const& rIn,
     299             :         sal_Unicode const*const pChars, sal_Int32 const nPos = 0);
     300             : 
     301             : /** Convert a sequence of strings to a single comma separated string.
     302             : 
     303             :     Note that no escaping of commas or anything fancy is done.
     304             : 
     305             :     @param i_rSeq   A list of strings to be concatenated.
     306             : 
     307             :     @return         A single string containing the concatenation of the given
     308             :                     list, interspersed with the string ", ".
     309             :  */
     310             : COMPHELPER_DLLPUBLIC OUString convertCommaSeparated(
     311             :     ::com::sun::star::uno::Sequence< OUString > const & i_rSeq);
     312             : 
     313             : /** Convert a decimal string to a number.
     314             : 
     315             :     The string must be base-10, no sign but can contain any
     316             :     codepoint listed in the "Number, Decimal Digit" Unicode
     317             :     category.
     318             : 
     319             :     No verification is made about the validity of the string,
     320             :     passing string not containing decimal digit code points
     321             :     gives unspecified results
     322             : 
     323             :     If your string is guaranteed to contain only ASCII digit
     324             :     use OUString::toInt32 instead.
     325             : 
     326             :     @param str  The string to convert containing only decimal
     327             :                 digit codepoints.
     328             : 
     329             :     @return     The value of the string as an int32.
     330             :  */
     331             : COMPHELPER_DLLPUBLIC sal_uInt32 decimalStringToNumber(
     332             :     OUString const & str );
     333             : 
     334             : /** Convert a single comma separated string to a sequence of strings.
     335             : 
     336             :     Note that no escaping of commas or anything fancy is done.
     337             : 
     338             :     @param i_rString    A string containing comma-separated words.
     339             : 
     340             :     @return         A sequence of strings resulting from splitting the given
     341             :                     string at ',' tokens and stripping whitespace.
     342             :  */
     343             : COMPHELPER_DLLPUBLIC ::com::sun::star::uno::Sequence< OUString >
     344             :     convertCommaSeparated( OUString const & i_rString );
     345             : 
     346             : /**
     347             :   Compares two strings using natural order.
     348             : 
     349             :   For non digit characters, the comparison use the same algorithm as
     350             :   rtl_str_compare. When a number is encountered during the comparison,
     351             :   natural order is used. Thus, Heading 10 will be considered as greater
     352             :   than Heading 2. Numerical comparison is done using decimal representation.
     353             : 
     354             :   Beware that "MyString 001" and "MyString 1" will be considered as equal
     355             :   since leading 0 are meaningless.
     356             : 
     357             :   @param    str         the object to be compared.
     358             :   @return   0 - if both strings are equal
     359             :             < 0 - if this string is less than the string argument
     360             :             > 0 - if this string is greater than the string argument
     361             : */
     362             : COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const OUString &rLHS, const OUString &rRHS,
     363             :     const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > &rCollator,
     364             :     const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > &rBI,
     365             :     const ::com::sun::star::lang::Locale &rLocale );
     366             : 
     367           7 : class COMPHELPER_DLLPUBLIC NaturalStringSorter
     368             : {
     369             : private:
     370             :     ::com::sun::star::lang::Locale m_aLocale;
     371             :     ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > m_xCollator;
     372             :     ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > m_xBI;
     373             : public:
     374             :     NaturalStringSorter(
     375             :         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > &rContext,
     376             :         const ::com::sun::star::lang::Locale &rLocale);
     377         462 :     sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
     378             :     {
     379         462 :         return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
     380             :     }
     381           0 :     const ::com::sun::star::lang::Locale& getLocale() const { return m_aLocale; }
     382             : };
     383             : 
     384             : /** Determine if an OString contains solely ASCII numeric digits
     385             : 
     386             :     @param rString  An OString
     387             : 
     388             :     @return         false if string contains any characters outside
     389             :                     the ASCII '0'-'9' range
     390             :                     true otherwise, including for empty string
     391             :  */
     392             : COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const OString &rString);
     393             : 
     394             : /** Determine if an OUString contains solely ASCII numeric digits
     395             : 
     396             :     @param rString  An OUString
     397             : 
     398             :     @return         false if string contains any characters outside
     399             :                     the ASCII '0'-'9' range
     400             :                     true otherwise, including for empty string
     401             :  */
     402             : COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const OUString &rString);
     403             : 
     404       50119 : inline bool isdigitAscii(sal_Unicode c)
     405             : {
     406       50119 :     return ((c >= '0') && (c <= '9'));
     407             : }
     408             : 
     409           0 : inline bool isxdigitAscii(sal_Unicode c)
     410             : {
     411           0 :     return isdigitAscii(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
     412             : }
     413             : 
     414      231572 : inline bool islowerAscii(sal_Unicode c)
     415             : {
     416      231572 :     return ((c >= 'a') && (c <= 'z'));
     417             : }
     418             : 
     419       61794 : inline bool isupperAscii(sal_Unicode c)
     420             : {
     421       61794 :     return ((c >= 'A') && (c <= 'Z'));
     422             : }
     423             : 
     424      231572 : inline bool isalphaAscii(sal_Unicode c)
     425             : {
     426      231572 :     return islowerAscii(c) || isupperAscii(c);
     427             : }
     428             : 
     429       42705 : inline bool isalnumAscii(sal_Unicode c)
     430             : {
     431       42705 :     return isalphaAscii(c) || isdigitAscii(c);
     432             : }
     433             : 
     434             : /** Compare two strings containing software version numbers
     435             : 
     436             :     Inspired by the GNU strverscmp(), but there is no guarantee that the exact
     437             :     same semantics are used, or that the semantics are stable between LibreOffice versions.
     438             : 
     439             :     @return -1, 0 or 1
     440             : */
     441             : COMPHELPER_DLLPUBLIC int compareVersionStrings(const OUString& a, const OUString& b);
     442             : 
     443             : } }
     444             : 
     445             : #endif
     446             : 
     447             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11