Compare commits
5 commits
Author | SHA1 | Date | |
---|---|---|---|
73746e3821 | |||
1ed5b3bc9f | |||
e5aa85b2d7 | |||
73bd06b5d6 | |||
a377c642ff |
6 changed files with 142 additions and 43 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.
|
|
@ -29,44 +29,12 @@ DEALINGS IN THE SOFTWARE.
|
||||||
#define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
|
#define UTF8_FOR_CPP_CHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
|
||||||
|
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include <stdexcept>
|
#include "exception.h"
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
namespace utf8
|
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
|
/// The library API - functions intended to be called by the users
|
||||||
|
|
||||||
template <typename octet_iterator>
|
template <typename octet_iterator>
|
||||||
|
@ -263,9 +231,53 @@ namespace utf8
|
||||||
return result;
|
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
|
// The iterator class
|
||||||
template <typename octet_iterator>
|
template <
|
||||||
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
|
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 it;
|
||||||
octet_iterator range_start;
|
octet_iterator range_start;
|
||||||
octet_iterator range_end;
|
octet_iterator range_end;
|
||||||
|
@ -276,8 +288,7 @@ namespace utf8
|
||||||
const octet_iterator& range_end) :
|
const octet_iterator& range_end) :
|
||||||
it(octet_it), range_start(range_start), range_end(range_end)
|
it(octet_it), range_start(range_start), range_end(range_end)
|
||||||
{
|
{
|
||||||
if (it < range_start || it > range_end)
|
error_policy::check_in_range(it, range_start, range_end);
|
||||||
throw std::out_of_range("Invalid utf-8 iterator position");
|
|
||||||
}
|
}
|
||||||
// the default "big three" are OK
|
// the default "big three" are OK
|
||||||
octet_iterator base () const { return it; }
|
octet_iterator base () const { return it; }
|
||||||
|
@ -288,8 +299,7 @@ namespace utf8
|
||||||
}
|
}
|
||||||
bool operator == (const iterator& rhs) const
|
bool operator == (const iterator& rhs) const
|
||||||
{
|
{
|
||||||
if (range_start != rhs.range_start || range_end != rhs.range_end)
|
error_policy::check_same_range(range_start, rhs.range_start, range_end, rhs.range_end);
|
||||||
throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
|
|
||||||
return (it == rhs.it);
|
return (it == rhs.it);
|
||||||
}
|
}
|
||||||
bool operator != (const iterator& rhs) const
|
bool operator != (const iterator& rhs) const
|
||||||
|
@ -323,5 +333,3 @@ namespace utf8
|
||||||
} // namespace utf8
|
} // namespace utf8
|
||||||
|
|
||||||
#endif //header guard
|
#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
|
Loading…
Reference in a new issue