Compare commits
5 commits
Author | SHA1 | Date | |
---|---|---|---|
73746e3821 | |||
1ed5b3bc9f | |||
e5aa85b2d7 | |||
73bd06b5d6 | |||
a377c642ff |
6 changed files with 145 additions and 184 deletions
23
LICENSE
Normal file
23
LICENSE
Normal file
|
@ -0,0 +1,23 @@
|
|||
Boost Software License - Version 1.0 - August 17th, 2003
|
||||
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
this license (the "Software") to use, reproduce, display, distribute,
|
||||
execute, and transmit the Software, and to prepare derivative works of the
|
||||
Software, and to permit third-parties to whom the Software is furnished to
|
||||
do so, all subject to the following:
|
||||
|
||||
The copyright notices in the Software and this entire statement, including
|
||||
the above license grant, this restriction and the following disclaimer,
|
||||
must be included in all copies of the Software, in whole or in part, and
|
||||
all derivative works of the Software, unless such copies or derivative
|
||||
works are solely in the form of machine-executable object code generated by
|
||||
a source language processor.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
119
src/utf8.h
119
src/utf8.h
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2006-2013 Nemanja Trifunovic
|
||||
// Copyright 2006 Nemanja Trifunovic
|
||||
|
||||
/*
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
|
@ -28,120 +28,7 @@ DEALINGS IN THE SOFTWARE.
|
|||
#ifndef UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731
|
||||
#define UTF8_FOR_CPP_2675DCD0_9480_4c0c_B92A_CC14C027B731
|
||||
|
||||
// By default, utf8 cpp requires C++ Standard Library strings and exceptions
|
||||
// The following macros can be used to change the default behavior
|
||||
|
||||
// #define UTF_CPP_NO_STD_STRING
|
||||
// #define UTF_CPP_NO_EXCEPTIONS
|
||||
|
||||
#ifndef UTF_CPP_NO_EXCEPTIONS
|
||||
#include <stdexcept>
|
||||
|
||||
#ifndef UTF_CPP_NO_STD_STRING
|
||||
#include <string>
|
||||
#include <iterator>
|
||||
|
||||
#endif // #ifndef UTF_CPP_NO_STD_STRING
|
||||
#endif // #ifndef UTF_CPP_NO_EXCEPTIONS
|
||||
|
||||
namespace utf8
|
||||
{
|
||||
|
||||
// Error codes - used internally and if exceptions disabled
|
||||
enum class utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD,
|
||||
INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
|
||||
|
||||
#ifndef UTF_CPP_NO_EXCEPTIONS
|
||||
// Base for the exceptions that may be thrown from the library
|
||||
class exception : public ::std::exception {
|
||||
};
|
||||
// Exceptions that may be thrown from the library functions.
|
||||
|
||||
class invalid_code_point : public exception {
|
||||
char32_t cp;
|
||||
public:
|
||||
invalid_code_point(char32_t cp) : cp(cp) {}
|
||||
virtual const char* what() const noexcept { return "Invalid code point"; }
|
||||
char32_t code_point() const {return cp;}
|
||||
};
|
||||
|
||||
#endif // #ifndef UTF_CPP_NO_EXCEPTIONS
|
||||
|
||||
// Helper code - not intended to be directly called by the library users. May be changed at any time
|
||||
namespace internal
|
||||
{
|
||||
// Unicode constants
|
||||
// Leading (high) surrogates: 0xd800 - 0xdbff
|
||||
// Trailing (low) surrogates: 0xdc00 - 0xdfff
|
||||
const char32_t LEAD_SURROGATE_MIN = 0x0000d800;
|
||||
const char32_t LEAD_SURROGATE_MAX = 0x0000dbff;
|
||||
const char32_t TRAIL_SURROGATE_MIN = 0x0000dc00;
|
||||
const char32_t TRAIL_SURROGATE_MAX = 0x0000dfff;
|
||||
|
||||
// Maximum valid value for a Unicode code point
|
||||
const char32_t CODE_POINT_MAX = 0x0010ffff;
|
||||
|
||||
|
||||
inline bool is_surrogate(char32_t cp)
|
||||
{
|
||||
return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
|
||||
}
|
||||
|
||||
inline bool is_code_point_valid(char32_t cp)
|
||||
{
|
||||
return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
/// The library API - functions intended to be called by the users
|
||||
|
||||
template <typename octet_iterator>
|
||||
octet_iterator append(char32_t cp, octet_iterator result, utf_error& error)
|
||||
{
|
||||
if (!utf8::internal::is_code_point_valid(cp)) {
|
||||
error = utf8::utf_error::INVALID_CODE_POINT;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (cp < 0x80) // one octet
|
||||
*(result++) = static_cast<char>(cp);
|
||||
else if (cp < 0x800) { // two octets
|
||||
*(result++) = static_cast<char>((cp >> 6) | 0xc0);
|
||||
*(result++) = static_cast<char>((cp & 0x3f) | 0x80);
|
||||
}
|
||||
else if (cp < 0x10000) { // three octets
|
||||
*(result++) = static_cast<char>((cp >> 12) | 0xe0);
|
||||
*(result++) = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
|
||||
*(result++) = static_cast<char>((cp & 0x3f) | 0x80);
|
||||
}
|
||||
else { // four octets
|
||||
*(result++) = static_cast<char>((cp >> 18) | 0xf0);
|
||||
*(result++) = static_cast<char>(((cp >> 12) & 0x3f) | 0x80);
|
||||
*(result++) = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
|
||||
*(result++) = static_cast<char>((cp & 0x3f) | 0x80);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef UTF_CPP_NO_EXCEPTIONS
|
||||
template <typename octet_iterator>
|
||||
octet_iterator append(char32_t cp, octet_iterator result)
|
||||
{
|
||||
utf8::utf_error err {utf8::utf_error::UTF8_OK};
|
||||
utf8::append(cp, result, err);
|
||||
if (err != utf8::utf_error::UTF8_OK)
|
||||
throw utf8::invalid_code_point(cp);
|
||||
return result;
|
||||
}
|
||||
#ifndef UTF_CPP_NO_STD_STRING
|
||||
inline void append(char32_t cp, std::string& str)
|
||||
{
|
||||
utf8::append(cp, std::back_inserter(str));
|
||||
}
|
||||
#endif // #ifndef UTF_CPP_NO_STD_STRING
|
||||
#endif // #ifndef UTF_CPP_NO_EXCEPTIONS
|
||||
|
||||
} // namespace utf8
|
||||
#include "utf8/checked.h"
|
||||
#include "utf8/unchecked.h"
|
||||
|
||||
#endif // header guard
|
||||
|
||||
|
|
|
@ -29,44 +29,12 @@ DEALINGS IN THE SOFTWARE.
|
|||
#define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
|
||||
|
||||
#include "core.h"
|
||||
#include <stdexcept>
|
||||
#include "exception.h"
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
namespace utf8
|
||||
{
|
||||
// Base for the exceptions that may be thrown from the library
|
||||
class exception : public ::std::exception {
|
||||
};
|
||||
|
||||
// Exceptions that may be thrown from the library functions.
|
||||
class invalid_code_point : public exception {
|
||||
uint32_t cp;
|
||||
public:
|
||||
invalid_code_point(uint32_t cp) : cp(cp) {}
|
||||
virtual const char* what() const throw() { return "Invalid code point"; }
|
||||
uint32_t code_point() const {return cp;}
|
||||
};
|
||||
|
||||
class invalid_utf8 : public exception {
|
||||
uint8_t u8;
|
||||
public:
|
||||
invalid_utf8 (uint8_t u) : u8(u) {}
|
||||
virtual const char* what() const throw() { return "Invalid UTF-8"; }
|
||||
uint8_t utf8_octet() const {return u8;}
|
||||
};
|
||||
|
||||
class invalid_utf16 : public exception {
|
||||
uint16_t u16;
|
||||
public:
|
||||
invalid_utf16 (uint16_t u) : u16(u) {}
|
||||
virtual const char* what() const throw() { return "Invalid UTF-16"; }
|
||||
uint16_t utf16_word() const {return u16;}
|
||||
};
|
||||
|
||||
class not_enough_room : public exception {
|
||||
public:
|
||||
virtual const char* what() const throw() { return "Not enough space"; }
|
||||
};
|
||||
|
||||
/// The library API - functions intended to be called by the users
|
||||
|
||||
template <typename octet_iterator>
|
||||
|
@ -263,9 +231,53 @@ namespace utf8
|
|||
return result;
|
||||
}
|
||||
|
||||
// Error policies for the iterator class
|
||||
template <typename I>
|
||||
class ErrorPolicyThrow {
|
||||
public:
|
||||
static void check_in_range(const I& it, const I& range_start, const I& range_end)
|
||||
{
|
||||
if (it < range_start || it > range_end)
|
||||
throw std::out_of_range("Invalid utf-8 iterator position");
|
||||
}
|
||||
static void check_same_range(const I& range_start_a, const I& range_start_b, const I& range_end_a, const I& range_end_b)
|
||||
{
|
||||
if (range_start_a != range_start_b || range_end_a != range_end_b)
|
||||
throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
|
||||
}
|
||||
};
|
||||
template <typename I>
|
||||
class ErrorPolicyAssert {
|
||||
public:
|
||||
static void check_in_range(const I& it, const I& range_start, const I& range_end)
|
||||
{
|
||||
#if defined(NDEBUG)
|
||||
(void)it;
|
||||
(void)range_start;
|
||||
(void)range_end;
|
||||
#else
|
||||
assert(it >= range_start && it <= range_end);
|
||||
#endif
|
||||
}
|
||||
static void check_same_range(const I& range_start_a, const I& range_start_b, const I& range_end_a, const I& range_end_b)
|
||||
{
|
||||
#if defined(NDEBUG)
|
||||
(void)range_start_a;
|
||||
(void)range_start_b;
|
||||
(void)range_end_a;
|
||||
(void)range_end_b;
|
||||
#else
|
||||
assert(range_start_a == range_start_b && range_end_a == range_end_b);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
// The iterator class
|
||||
template <typename octet_iterator>
|
||||
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
|
||||
template <
|
||||
typename octet_iterator,
|
||||
typename error_policy=ErrorPolicyThrow<octet_iterator>
|
||||
>
|
||||
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t, std::ptrdiff_t, uint32_t*, uint32_t> {
|
||||
octet_iterator it;
|
||||
octet_iterator range_start;
|
||||
octet_iterator range_end;
|
||||
|
@ -276,8 +288,7 @@ namespace utf8
|
|||
const octet_iterator& range_end) :
|
||||
it(octet_it), range_start(range_start), range_end(range_end)
|
||||
{
|
||||
if (it < range_start || it > range_end)
|
||||
throw std::out_of_range("Invalid utf-8 iterator position");
|
||||
error_policy::check_in_range(it, range_start, range_end);
|
||||
}
|
||||
// the default "big three" are OK
|
||||
octet_iterator base () const { return it; }
|
||||
|
@ -288,8 +299,7 @@ namespace utf8
|
|||
}
|
||||
bool operator == (const iterator& rhs) const
|
||||
{
|
||||
if (range_start != rhs.range_start || range_end != rhs.range_end)
|
||||
throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
|
||||
error_policy::check_same_range(range_start, rhs.range_start, range_end, rhs.range_end);
|
||||
return (it == rhs.it);
|
||||
}
|
||||
bool operator != (const iterator& rhs) const
|
||||
|
@ -323,5 +333,3 @@ namespace utf8
|
|||
} // namespace utf8
|
||||
|
||||
#endif //header guard
|
||||
|
||||
|
||||
|
|
68
src/utf8/exception.h
Normal file
68
src/utf8/exception.h
Normal file
|
@ -0,0 +1,68 @@
|
|||
// Copyright 2006 Nemanja Trifunovic
|
||||
|
||||
/*
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
this license (the "Software") to use, reproduce, display, distribute,
|
||||
execute, and transmit the Software, and to prepare derivative works of the
|
||||
Software, and to permit third-parties to whom the Software is furnished to
|
||||
do so, all subject to the following:
|
||||
|
||||
The copyright notices in the Software and this entire statement, including
|
||||
the above license grant, this restriction and the following disclaimer,
|
||||
must be included in all copies of the Software, in whole or in part, and
|
||||
all derivative works of the Software, unless such copies or derivative
|
||||
works are solely in the form of machine-executable object code generated by
|
||||
a source language processor.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef id71B1E0983F3D4F7BAD0C091C4569AB37
|
||||
#define id71B1E0983F3D4F7BAD0C091C4569AB37
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace utf8 {
|
||||
// Base for the exceptions that may be thrown from the library
|
||||
class exception : public ::std::exception {
|
||||
};
|
||||
|
||||
// Exceptions that may be thrown from the library functions.
|
||||
class invalid_code_point : public exception {
|
||||
uint32_t cp;
|
||||
public:
|
||||
invalid_code_point(uint32_t cp) : cp(cp) {}
|
||||
virtual const char* what() const throw() { return "Invalid code point"; }
|
||||
uint32_t code_point() const {return cp;}
|
||||
};
|
||||
|
||||
class invalid_utf8 : public exception {
|
||||
uint8_t u8;
|
||||
public:
|
||||
invalid_utf8 (uint8_t u) : u8(u) {}
|
||||
virtual const char* what() const throw() { return "Invalid UTF-8"; }
|
||||
uint8_t utf8_octet() const {return u8;}
|
||||
};
|
||||
|
||||
class invalid_utf16 : public exception {
|
||||
uint16_t u16;
|
||||
public:
|
||||
invalid_utf16 (uint16_t u) : u16(u) {}
|
||||
virtual const char* what() const throw() { return "Invalid UTF-16"; }
|
||||
uint16_t utf16_word() const {return u16;}
|
||||
};
|
||||
|
||||
class not_enough_room : public exception {
|
||||
public:
|
||||
virtual const char* what() const throw() { return "Not enough space"; }
|
||||
};
|
||||
} //namespace utf8
|
||||
|
||||
#endif
|
|
@ -1,6 +0,0 @@
|
|||
CC = g++
|
||||
CFLAGS = -g -Wall --std=c++11
|
||||
|
||||
smoketest: unit.cpp ../src/utf8.h
|
||||
$(CC) $(CFLAGS) unit.cpp -ounit -lboost_unit_test_framework
|
||||
./unit
|
|
@ -1,19 +0,0 @@
|
|||
#define BOOST_TEST_DYN_LINK
|
||||
#define BOOST_TEST_MODULE UTF8_CPP_UNIT
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "../src/utf8.h"
|
||||
using namespace std;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(append)
|
||||
{
|
||||
string s;
|
||||
BOOST_CHECK_NO_THROW (utf8::append(U'\U00000448', s));
|
||||
BOOST_CHECK (s.length() == 2 && s[0] == '\xd1' && s[1] == '\x88');
|
||||
|
||||
s.erase();
|
||||
BOOST_CHECK_NO_THROW(utf8::append(U'\U000065e5', s));
|
||||
BOOST_CHECK (s.length() == 3 && s[0] == '\xe6' && s[1] == '\x97' && s[2] == '\xa5');
|
||||
}
|
||||
|
||||
|
Loading…
Reference in a new issue