LCOV - code coverage report
Current view: top level - libutf8 - libutf8.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 20 20
Test Date: 2025-06-22 07:49:47 Functions: 100.0 % 7 7
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright (c) 2000-2023  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              : /** \file
      22              :  * \brief The declarations of the UTF-8 library.
      23              :  *
      24              :  * This file is the declarations of the UTF-8 library which are just a few
      25              :  * functions used to convert a string from one format to another.
      26              :  */
      27              : 
      28              : // C++
      29              : //
      30              : #include    <string>
      31              : 
      32              : 
      33              : 
      34              : namespace libutf8
      35              : {
      36              : 
      37              : 
      38              : enum class bom_t
      39              : {
      40              :     BOM_NONE,
      41              :     BOM_UTF8,
      42              :     BOM_UTF16_LE,
      43              :     BOM_UTF16_BE,
      44              :     BOM_UTF32_LE,
      45              :     BOM_UTF32_BE
      46              : };
      47              : 
      48              : 
      49              : enum class surrogate_t
      50              : {
      51              :     SURROGATE_NO,
      52              :     SURROGATE_HIGH,
      53              :     SURROGATE_LOW
      54              : };
      55              : 
      56              : 
      57              : 
      58              : 
      59              : bool                is_valid_ascii(char c, bool ctrl = true);
      60              : bool                is_valid_ascii(char const * str, bool ctrl = true);
      61              : bool                is_valid_ascii(std::string const & str, bool ctrl = true);
      62              : bool                is_valid_utf8(char const * str);
      63              : bool                is_valid_utf8(std::string const & str);
      64              : bool                is_valid_utf16(std::u16string const & str);
      65              : bool                is_valid_unicode(char32_t const wc, bool ctrl = true);
      66              : bool                is_valid_unicode(char32_t const * str, bool ctrl = true);
      67              : bool                is_valid_unicode(std::u32string const & str, bool ctrl = true);
      68              : surrogate_t         is_surrogate(char32_t wc);
      69              : bom_t               start_with_bom(char const * str, size_t len);
      70              : std::string         to_u8string(std::u32string const & str);
      71              : std::string         to_u8string(std::u16string const & str);
      72              : std::string         to_u8string(std::wstring const & str);
      73              : std::string         to_u8string(wchar_t one, wchar_t two = L'\0');
      74              : std::string         to_u8string(char16_t one, char16_t two = u'\0');
      75              : std::string         to_u8string(char32_t const wc);
      76              : std::u16string      to_u16string(char32_t const wc);
      77              : std::u16string      to_u16string(std::string const & str);
      78              : std::u32string      to_u32string(std::string const & str);
      79              : std::size_t         u8length(std::string const & str);
      80              : ssize_t             u16length(std::u16string const & str);
      81              : int                 u8casecmp(std::string const & lhs, std::string const & rhs);
      82              : bool                make_u8string_valid(std::string & str, char32_t fix_char = U'?');
      83              : 
      84              : 
      85              : 
      86              : } // libutf8 namespace
      87              : 
      88              : 
      89            1 : inline std::string operator + (char32_t wc, std::string const & rhs)
      90              : {
      91            1 :     std::string v;
      92            1 :     v = libutf8::to_u8string(wc);
      93            2 :     return v + rhs;
      94            1 : }
      95              : 
      96              : 
      97            1 : inline std::string operator + (std::string const & lhs, char32_t wc)
      98              : {
      99            1 :     std::string v;
     100            1 :     v = libutf8::to_u8string(wc);
     101            2 :     return lhs + v;
     102            1 : }
     103              : 
     104              : 
     105      6683909 : inline std::string & operator += (std::string & lhs, char32_t wc)
     106              : {
     107      6683909 :     return lhs += libutf8::to_u8string(wc);
     108              : }
     109              : 
     110              : 
     111            2 : inline std::string & operator += (std::string & lhs, int c)
     112              : {
     113            2 :     return lhs += static_cast<char>(c);
     114              : }
     115              : 
     116              : 
     117         6204 : inline std::string & operator += (std::string & lhs, unsigned int c)
     118              : {
     119         6204 :     return lhs += static_cast<char>(c);
     120              : }
     121              : 
     122              : 
     123            2 : inline std::string & operator += (std::string & lhs, long c)
     124              : {
     125            2 :     return lhs += static_cast<char>(c);
     126              : }
     127              : 
     128              : 
     129            2 : inline std::string & operator += (std::string & lhs, unsigned long c)
     130              : {
     131            2 :     return lhs += static_cast<char>(c);
     132              : }
     133              : 
     134              : 
     135              : 
     136              : // vim: ts=4 sw=4 et
        

Generated by: LCOV version 2.0-1

Snap C++ | List of projects | List of versions