LCOV - code coverage report
Current view: top level - libutf8 - caseinsensitivestring.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 103 103 100.0 %
Date: 2022-07-31 10:17:08 Functions: 45 45 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2013-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/libutf8
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
      19             : #pragma once
      20             : 
      21             : 
      22             : // self
      23             : //
      24             : #include    <libutf8/libutf8.h>
      25             : 
      26             : // C++
      27             : //
      28             : #include    <string>
      29             : 
      30             : 
      31             : 
      32             : namespace libutf8
      33             : {
      34             : 
      35             : 
      36             : 
      37             : /** \brief Case insensitive string.
      38             :  *
      39             :  * This class is an overload of the string template which allows you to
      40             :  * create case insensitive strings as far as the comparison operators
      41             :  * are concerned. All the other functions still work the same way.
      42             :  *
      43             :  * This is particularly useful if you manage an std::map<> with a string as
      44             :  * the key, string which should not be case sensitive.
      45             :  *
      46             :  * The comparisons are done using the libutf8::u8casecmp() function.
      47             :  *
      48             :  * \sa u8casecmp()
      49             :  */
      50             : template<
      51             :     class _CharT,
      52             :     class _Traits = std::char_traits<_CharT>,
      53             :     class _Alloc = std::allocator<_CharT>
      54             : >
      55          32 : class case_insensitive_basic_string
      56             :     : public std::basic_string<_CharT, _Traits, _Alloc>
      57             : {
      58             : public:
      59             :     typedef typename std::basic_string<_CharT, _Traits, _Alloc>::size_type      size_type;
      60             : 
      61           1 :     case_insensitive_basic_string() noexcept(std::is_nothrow_default_constructible<_Alloc>::value)
      62           1 :         : std::basic_string<_CharT, _Traits, _Alloc>()
      63             :     {
      64           1 :     }
      65             : 
      66           1 :     explicit case_insensitive_basic_string(_Alloc const & __a)
      67           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__a)
      68             :     {
      69           1 :     }
      70             : 
      71           1 :     case_insensitive_basic_string(size_type __n, _CharT __c, _Alloc const & __a = _Alloc())
      72           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__n, __c, __a)
      73             :     {
      74           1 :     }
      75             : 
      76             :     // the following are for C++17 and over
      77             :     // (and then the next two constructors will not set __n)
      78             :     //
      79             :     //case_insensitive_basic_string(std::basic_string<_CharT, _Traits, _Alloc> const & __str, size_type __pos, _Alloc const & __a = _Alloc())
      80             :     //    : std::basic_string<_CharT, _Traits, _Alloc>(__str, __pos, __a)
      81             :     //{
      82             :     //}
      83             :     //
      84             :     //case_insensitive_basic_string(case_insensitive_basic_string const & __str, size_type __pos, _Alloc const & __a = _Alloc())
      85             :     //    : std::basic_string<_CharT, _Traits, _Alloc>(static_cast<std::basic_string<_CharT, _Traits, _Alloc> const &>(__str), __pos, __a)
      86             :     //{
      87             :     //}
      88             : 
      89           2 :     case_insensitive_basic_string(std::basic_string<_CharT, _Traits, _Alloc> const & __str, size_type __pos, size_type __n = std::basic_string<_CharT, _Traits, _Alloc>::npos, _Alloc const & __a = _Alloc())
      90           2 :         : std::basic_string<_CharT, _Traits, _Alloc>(__str, __pos, __n, __a)
      91             :     {
      92           2 :     }
      93             : 
      94           2 :     case_insensitive_basic_string(case_insensitive_basic_string const & __str, size_type __pos, size_type __n = std::basic_string<_CharT, _Traits, _Alloc>::npos, _Alloc const & __a = _Alloc())
      95           2 :         : std::basic_string<_CharT, _Traits, _Alloc>(__str, __pos, __n, __a)
      96             :     {
      97           2 :     }
      98             : 
      99           1 :     case_insensitive_basic_string(_CharT const * __d, size_type __n, _Alloc const & __a = _Alloc())
     100           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__d, __n, __a)
     101             :     {
     102           1 :     }
     103             : 
     104          17 :     case_insensitive_basic_string(_CharT const * __d, _Alloc const & __a = _Alloc())
     105          17 :         : std::basic_string<_CharT, _Traits, _Alloc>(__d, __a)
     106             :     {
     107          17 :     }
     108             : 
     109             :     template<class _InputIterator>
     110           1 :     case_insensitive_basic_string(_InputIterator __beg, _InputIterator __end, _Alloc const & __a = _Alloc())
     111           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__beg, __end, __a)
     112             :     {
     113           1 :     }
     114             : 
     115           1 :     case_insensitive_basic_string(std::basic_string<_CharT, _Traits, _Alloc> const & __str)
     116           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__str)
     117             :     {
     118           1 :     }
     119             : 
     120           1 :     case_insensitive_basic_string(case_insensitive_basic_string const & __str)
     121           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__str)
     122             :     {
     123           1 :     }
     124             : 
     125           1 :     case_insensitive_basic_string(std::basic_string<_CharT, _Traits, _Alloc> && __str) noexcept
     126           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__str)
     127             :     {
     128           1 :     }
     129             : 
     130             :     case_insensitive_basic_string(case_insensitive_basic_string && __str) noexcept
     131             :         : std::basic_string<_CharT, _Traits, _Alloc>(__str)
     132             :     {
     133             :     }
     134             : 
     135           1 :     case_insensitive_basic_string(std::basic_string<_CharT, _Traits, _Alloc> && __str, _Alloc const & __a)
     136           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__str, __a)
     137             :     {
     138           1 :     }
     139             : 
     140           1 :     case_insensitive_basic_string(case_insensitive_basic_string && __str, _Alloc const & __a)
     141           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__str, __a)
     142             :     {
     143           1 :     }
     144             : 
     145           1 :     case_insensitive_basic_string(std::initializer_list<_CharT> __l, _Alloc const & __a = _Alloc())
     146           1 :         : std::basic_string<_CharT, _Traits, _Alloc>(__l, __a)
     147             :     {
     148           1 :     }
     149             : 
     150             : 
     151           3 :     friend bool operator == (case_insensitive_basic_string const & lhs, std::basic_string<_CharT, _Traits, _Alloc> const & rhs)
     152             :     {
     153           3 :         return libutf8::u8casecmp(lhs, rhs) == 0;
     154             :     }
     155             : 
     156           2 :     friend bool operator == (case_insensitive_basic_string const & lhs, case_insensitive_basic_string const & rhs)
     157             :     {
     158           2 :         return libutf8::u8casecmp(lhs, rhs) == 0;
     159             :     }
     160             : 
     161           3 :     friend bool operator == (std::basic_string<_CharT, _Traits, _Alloc> const & lhs, case_insensitive_basic_string const & rhs)
     162             :     {
     163           3 :         return libutf8::u8casecmp(lhs, rhs) == 0;
     164             :     }
     165             : 
     166          16 :     friend bool operator == (case_insensitive_basic_string const & lhs, _CharT const * rhs)
     167             :     {
     168          16 :         return libutf8::u8casecmp(lhs, rhs) == 0;
     169             :     }
     170             : 
     171           1 :     friend bool operator == (_CharT const * lhs, case_insensitive_basic_string const & rhs)
     172             :     {
     173           1 :         return libutf8::u8casecmp(lhs, rhs) == 0;
     174             :     }
     175             : 
     176           1 :     friend bool operator != (case_insensitive_basic_string const & lhs, std::basic_string<_CharT, _Traits, _Alloc> const & rhs)
     177             :     {
     178           1 :         return libutf8::u8casecmp(lhs, rhs) != 0;
     179             :     }
     180             : 
     181           2 :     friend bool operator != (case_insensitive_basic_string const & lhs, case_insensitive_basic_string const & rhs)
     182             :     {
     183           2 :         return libutf8::u8casecmp(lhs, rhs) != 0;
     184             :     }
     185             : 
     186           1 :     friend bool operator != (std::basic_string<_CharT, _Traits, _Alloc> const & lhs, case_insensitive_basic_string const & rhs)
     187             :     {
     188           1 :         return libutf8::u8casecmp(lhs, rhs) != 0;
     189             :     }
     190             : 
     191           1 :     friend bool operator != (case_insensitive_basic_string const & lhs, _CharT const * rhs)
     192             :     {
     193           1 :         return libutf8::u8casecmp(lhs, rhs) != 0;
     194             :     }
     195             : 
     196           1 :     friend bool operator != (_CharT const * lhs, case_insensitive_basic_string const & rhs)
     197             :     {
     198           1 :         return libutf8::u8casecmp(lhs, rhs) != 0;
     199             :     }
     200             : 
     201           1 :     friend bool operator < (case_insensitive_basic_string const & lhs, std::basic_string<_CharT, _Traits, _Alloc> const & rhs)
     202             :     {
     203           1 :         return libutf8::u8casecmp(lhs, rhs) < 0;
     204             :     }
     205             : 
     206           2 :     friend bool operator < (case_insensitive_basic_string const & lhs, case_insensitive_basic_string const & rhs)
     207             :     {
     208           2 :         return libutf8::u8casecmp(lhs, rhs) < 0;
     209             :     }
     210             : 
     211           1 :     friend bool operator < (std::basic_string<_CharT, _Traits, _Alloc> const & lhs, case_insensitive_basic_string const & rhs)
     212             :     {
     213           1 :         return libutf8::u8casecmp(lhs, rhs) < 0;
     214             :     }
     215             : 
     216           1 :     friend bool operator < (case_insensitive_basic_string const & lhs, _CharT const * rhs)
     217             :     {
     218           1 :         return libutf8::u8casecmp(lhs, rhs) < 0;
     219             :     }
     220             : 
     221           1 :     friend bool operator < (_CharT const * lhs, case_insensitive_basic_string const & rhs)
     222             :     {
     223           1 :         return libutf8::u8casecmp(lhs, rhs) < 0;
     224             :     }
     225             : 
     226           1 :     friend bool operator <= (case_insensitive_basic_string const & lhs, std::basic_string<_CharT, _Traits, _Alloc> const & rhs)
     227             :     {
     228           1 :         return libutf8::u8casecmp(lhs, rhs) <= 0;
     229             :     }
     230             : 
     231           2 :     friend bool operator <= (case_insensitive_basic_string const & lhs, case_insensitive_basic_string const & rhs)
     232             :     {
     233           2 :         return libutf8::u8casecmp(lhs, rhs) <= 0;
     234             :     }
     235             : 
     236           1 :     friend bool operator <= (std::basic_string<_CharT, _Traits, _Alloc> const & lhs, case_insensitive_basic_string const & rhs)
     237             :     {
     238           1 :         return libutf8::u8casecmp(lhs, rhs) <= 0;
     239             :     }
     240             : 
     241           1 :     friend bool operator <= (case_insensitive_basic_string const & lhs, _CharT const * rhs)
     242             :     {
     243           1 :         return libutf8::u8casecmp(lhs, rhs) <= 0;
     244             :     }
     245             : 
     246           1 :     friend bool operator <= (_CharT const * lhs, case_insensitive_basic_string const & rhs)
     247             :     {
     248           1 :         return libutf8::u8casecmp(lhs, rhs) <= 0;
     249             :     }
     250             : 
     251           1 :     friend bool operator > (case_insensitive_basic_string const & lhs, std::basic_string<_CharT, _Traits, _Alloc> const & rhs)
     252             :     {
     253           1 :         return libutf8::u8casecmp(lhs, rhs) > 0;
     254             :     }
     255             : 
     256           2 :     friend bool operator > (case_insensitive_basic_string const & lhs, case_insensitive_basic_string const & rhs)
     257             :     {
     258           2 :         return libutf8::u8casecmp(lhs, rhs) > 0;
     259             :     }
     260             : 
     261           1 :     friend bool operator > (std::basic_string<_CharT, _Traits, _Alloc> const & lhs, case_insensitive_basic_string const & rhs)
     262             :     {
     263           1 :         return libutf8::u8casecmp(lhs, rhs) > 0;
     264             :     }
     265             : 
     266           1 :     friend bool operator > (case_insensitive_basic_string const & lhs, _CharT const * rhs)
     267             :     {
     268           1 :         return libutf8::u8casecmp(lhs, rhs) > 0;
     269             :     }
     270             : 
     271           1 :     friend bool operator > (_CharT const * lhs, case_insensitive_basic_string const & rhs)
     272             :     {
     273           1 :         return libutf8::u8casecmp(lhs, rhs) > 0;
     274             :     }
     275             : 
     276           1 :     friend bool operator >= (case_insensitive_basic_string const & lhs, std::basic_string<_CharT, _Traits, _Alloc> const & rhs)
     277             :     {
     278           1 :         return libutf8::u8casecmp(lhs, rhs) >= 0;
     279             :     }
     280             : 
     281           2 :     friend bool operator >= (case_insensitive_basic_string const & lhs, case_insensitive_basic_string const & rhs)
     282             :     {
     283           2 :         return libutf8::u8casecmp(lhs, rhs) >= 0;
     284             :     }
     285             : 
     286           1 :     friend bool operator >= (std::basic_string<_CharT, _Traits, _Alloc> const & lhs, case_insensitive_basic_string const & rhs)
     287             :     {
     288           1 :         return libutf8::u8casecmp(lhs, rhs) >= 0;
     289             :     }
     290             : 
     291           1 :     friend bool operator >= (case_insensitive_basic_string const & lhs, _CharT const * rhs)
     292             :     {
     293           1 :         return libutf8::u8casecmp(lhs, rhs) >= 0;
     294             :     }
     295             : 
     296           1 :     friend bool operator >= (_CharT const * lhs, case_insensitive_basic_string const & rhs)
     297             :     {
     298           1 :         return libutf8::u8casecmp(lhs, rhs) >= 0;
     299             :     }
     300             : };
     301             : 
     302             : 
     303             : typedef case_insensitive_basic_string<char>         case_insensitive_string;
     304             : 
     305             : // TODO add support for other types
     306             : //typedef case_insensitive_basic_string<wchar_t>      case_insensitive_wstring;
     307             : //typedef case_insensitive_basic_string<char16_t>     case_insensitive_u16string;
     308             : //typedef case_insensitive_basic_string<char32_t>     case_insensitive_u32string;
     309             : 
     310             : 
     311             : }
     312             : // libutf8 namespace
     313             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13