Buildfix for the current refactoring.

The build is fine but some functions are missing their implementation.
This is an intermediate commit.
This commit is contained in:
King_DuckZ 2014-06-30 10:44:48 +02:00
parent 1f805eb858
commit 9713f6125c
6 changed files with 547 additions and 449 deletions

View file

@ -1,4 +1,5 @@
// Copyright 2006 Nemanja Trifunovic // Copyright 2006 Nemanja Trifunovic
// Copyright 2014 Michele Santullo
/* /*
Permission is hereby granted, free of charge, to any person or organization Permission is hereby granted, free of charge, to any person or organization
@ -28,283 +29,282 @@ DEALINGS IN THE SOFTWARE.
#ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #ifndef UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
#define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 #define UTF8_FOR_CPP_CORE_H_2675DCD0_9480_4c0c_B92A_CC14C027B731
#include "error_policies.hpp"
#include "global.hpp"
#include <iterator> #include <iterator>
namespace utf8 #if defined(__GNUC__)
{ # define pure_function __attribute__((pure))
// The typedefs for 8-bit, 16-bit and 32-bit unsigned integers #else
// You may need to change them to match your system. # error "Unknown compiler - if your compiler doesn't support pure functions just declare pure_function as an empty macro for your specific compiler"
// These typedefs have the same names as ones from cstdint, or boost/cstdint #endif
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
// Helper code - not intended to be directly called by the library users. May be changed at any time namespace utf8 {
namespace internal // Helper code - not intended to be directly called by the library users. May be changed at any time
{ namespace internal {
// Unicode constants template<typename octet_type> bool is_trail(octet_type oc) pure_function;
// Leading (high) surrogates: 0xd800 - 0xdbff template<typename octet_type> uint8_t mask8(octet_type oc) pure_function;
// Trailing (low) surrogates: 0xdc00 - 0xdfff template<typename u16_type> inline uint16_t mask16(u16_type oc) pure_function;
const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10);
const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
// Maximum valid value for a Unicode code point template<typename octet_type>
const uint32_t CODE_POINT_MAX = 0x0010ffffu; bool is_trail(octet_type oc) {
return ((utf8::internal::mask8(oc) >> 6) == 0x2);
}
template<typename octet_type> template<typename octet_type>
inline uint8_t mask8(octet_type oc) inline uint8_t mask8(octet_type oc) {
{ return static_cast<uint8_t>(0xff & oc);
return static_cast<uint8_t>(0xff & oc); }
} template<>
template<typename u16_type> inline uint8_ mask8<uint8_t>(uint8_t oc) {
inline uint16_t mask16(u16_type oc) return oc;
{ }
return static_cast<uint16_t>(0xffff & oc);
}
template<typename octet_type>
inline bool is_trail(octet_type oc)
{
return ((utf8::internal::mask8(oc) >> 6) == 0x2);
}
template <typename u16> template<typename u16_type>
inline bool is_lead_surrogate(u16 cp) uint16_t mask16(u16_type oc) {
{ return static_cast<uint16_t>(0xffff & oc);
return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX); }
}
template <typename u16> /// Helper for get_sequence_x
inline bool is_trail_surrogate(u16 cp) template <typename octet_iterator>
{ class SequenceReader {
return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); public:
} enum SequenceErrors {
SequenceError_None,
SequenceError_BadLength,
SequenceError_BadByte2,
SequenceError_BadByte3,
SequenceError_BadByte4,
SequenceError_NothingRead
};
template <typename u16> typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
inline bool is_surrogate(u16 cp) typedef typename std::iterator_traits<octet_iterator>::value_type octet_value_type;
{
return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
}
template <typename u32> SequenceReader ( void ) :
inline bool is_code_point_valid(u32 cp) seq_error(SequenceError_NothingRead)
{ {
return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp)); }
}
template <typename octet_iterator> utf8::code_point get_sequence(octet_iterator& it, octet_difference_type seq_len) {
inline typename std::iterator_traits<octet_iterator>::difference_type switch (seq_len) {
sequence_length(octet_iterator lead_it) case 1: return get_sequence_1(it);
{ case 2: return get_sequence_2(it);
uint8_t lead = utf8::internal::mask8(*lead_it); case 3: return get_sequence_3(it);
if (lead < 0x80) case 4: return get_sequence_4(it);
return 1; //this shouldn't really happen, this is an internal function
else if ((lead >> 5) == 0x6) //and a correct length should be passed always
return 2; default:
else if ((lead >> 4) == 0xe) seq_error = SequenceError_BadLength;
return 3; return cp;
else if ((lead >> 3) == 0x1e) }
return 4; }
else
return 0;
}
template <typename octet_difference_type> /// get_sequence_x functions decode utf-8 sequences of the length x
inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length) utf8::code_point get_sequence_1(const octet_iterator& it) {
{ seq_error = SequenceError_None;
if (cp < 0x80) { return (cp = utf8::internal::mask8(*it));
if (length != 1) }
return true;
}
else if (cp < 0x800) {
if (length != 2)
return true;
}
else if (cp < 0x10000) {
if (length != 3)
return true;
}
return false; utf8::code_point get_sequence_2(octet_iterator& it) {
} cp = utf8::internal::mask8(*it);
enum utf_error {UTF8_OK, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT}; ++it;
if (not utf8::internal::is_trail(*it)) {
faulty_part = *it;
seq_error = SequenceError_BadByte2;
return cp;
}
/// Helper for get_sequence_x seq_error = SequenceError_None;
template <typename octet_iterator> cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
utf_error increase_safely(octet_iterator& it) return cp;
{ }
if (!utf8::internal::is_trail(*it))
return INCOMPLETE_SEQUENCE;
return UTF8_OK; utf8::code_point get_sequence_3(octet_iterator& it) {
} cp = utf8::internal::mask8(*it);
#define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT) {utf_error ret = increase_safely(IT); if (ret != UTF8_OK) return ret;} ++it;
if (not utf8::internal::is_trail(*it)) {
seq_error = SequenceError_BadByte2;
faulty_part = *it;
return cp;
}
/// get_sequence_x functions decode utf-8 sequences of the length x cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
template <typename octet_iterator>
void get_sequence_1(octet_iterator& it, uint32_t& code_point)
{
code_point = utf8::internal::mask8(*it);
}
template <typename octet_iterator> ++it;
utf_error get_sequence_2(octet_iterator& it, uint32_t& code_point) if (not utf8::internal::is_trail(*it)) {
{ faulty_part = *it;
code_point = utf8::internal::mask8(*it); seq_error = SequenceError_BadByte3;
return cp;
}
seq_error = SequenceError_None;
cp += *it & 0x3f;
return cp;
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it) utf8::code_point get_sequence_4(octet_iterator& it) {
utf8::code_point cp = utf8::internal::mask8(*it);
code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f); ++it;
if (not utf8::internal::is_trail(*it)) {
seq_error = SequenceError_BadByte2;
faulty_part = *it;
return cp;
}
return UTF8_OK; cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
}
template <typename octet_iterator> ++it;
utf_error get_sequence_3(octet_iterator& it, uint32_t& code_point) if (not utf8::internal::is_trail(*it)) {
{ faulty_part = *it;
code_point = utf8::internal::mask8(*it); seq_error = SequenceError_BadByte3;
return cp;
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it) cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); ++it;
if (not utf8::internal::is_trail(*it)) {
faulty_part = *it;
seq_error = SequenceError_BadByte4;
return cp;
}
seq_error = SequenceError_None;
cp += *it & 0x3f;
return cp;
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it) bool has_error ( void ) const { return seq_error != SequenceError_None; }
SequenceErrors error ( void ) const { return seq_error; }
utf8::code_point code ( void ) const { return cp; }
octet_value_type faulty ( void ) const { return faulty_part; }
uint8_t part ( void ) const {
if (SequenceError_BadByte2 == seq_error or SequenceError_BadByte3 == seq_error or SequenceError_BadByte4 == seq_error)
return static_cast<uint8_t>(seq_error - SequenceError_BadByte2) + 2;
else
return 0;
}
code_point += (*it) & 0x3f; private:
utf8::code_point cp;
SequenceErrors seq_error;
octet_value_type faulty_part;
};
return UTF8_OK; // Unicode constants
} enum {
// Leading (high) surrogates: 0xd800 - 0xdbff
// Trailing (low) surrogates: 0xdc00 - 0xdfff
LEAD_SURROGATE_MIN = 0xd800u,
LEAD_SURROGATE_MAX = 0xdbffu,
TRAIL_SURROGATE_MIN = 0xdc00u,
TRAIL_SURROGATE_MAX = 0xdfffu,
LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10),
SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN,
template <typename octet_iterator> // Maximum valid value for a Unicode code point
utf_error get_sequence_4(octet_iterator& it, uint32_t& code_point) CODE_POINT_MAX = 0x0010ffffu
{ };
code_point = utf8::internal::mask8(*it);
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it) template <typename u16>
inline bool is_lead_surrogate(u16 cp) {
return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
}
code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); template <typename u16>
inline bool is_trail_surrogate(u16 cp) {
return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it) template <typename u16>
inline bool is_surrogate(u16 cp) {
return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
}
code_point += (utf8::internal::mask8(*it) << 6) & 0xfff; template <typename u32>
inline bool is_code_point_valid(u32 cp) {
return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it) template <typename difference_type, typename octet_type>
inline difference_type sequence_length(octet_type lead_char) {
const uint8_t lead = utf8::internal::mask8(lead_char);
if (lead < 0x80)
return 1;
else if ((lead >> 5) == 0x6)
return 2;
else if ((lead >> 4) == 0xe)
return 3;
else if ((lead >> 3) == 0x1e)
return 4;
else
return 0;
}
code_point += (*it) & 0x3f; template <typename octet_difference_type>
inline bool is_overlong_sequence(utf8::code_point cp, octet_difference_type length) {
if (cp < 0x80) {
if (length != 1)
return true;
}
else if (cp < 0x800) {
if (length != 2)
return true;
}
else if (cp < 0x10000) {
if (length != 3)
return true;
}
return UTF8_OK; return false;
} }
} // namespace internal
#undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR // Byte order mark
const uint8_t bom[] = {0xef, 0xbb, 0xbf};
template <typename octet_iterator> template <typename octet_iterator>
utf_error validate_next(octet_iterator& it, uint32_t& code_point) octet_iterator find_invalid(octet_iterator start, octet_iterator end)
{ {
// Save the original value of it so we can go back in case of failure //octet_iterator result = start;
// Of course, it does not make much sense with i.e. stream iterators //while (result != end) {
octet_iterator original_it = it; // utf8::internal::utf_error err_code = utf8::internal::validate_next(result);
// if (err_code != internal::UTF8_OK)
// return result;
//}
//return result;
//TODO: implement
return start;
}
uint32_t cp = 0; template <typename octet_iterator>
// Determine the sequence length based on the lead octet inline bool is_valid(octet_iterator start, octet_iterator end)
typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type; {
const octet_difference_type length = utf8::internal::sequence_length(it); return (utf8::find_invalid(start, end) == end);
}
// Get trail octets and calculate the code point template <typename octet_iterator>
utf_error err = UTF8_OK; inline bool starts_with_bom (octet_iterator it, octet_iterator end)
switch (length) { {
case 0: return (
return INVALID_LEAD; ((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
case 1: ((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
utf8::internal::get_sequence_1(it, cp); ((it != end) && (utf8::internal::mask8(*it)) == bom[2])
break; );
case 2: }
err = utf8::internal::get_sequence_2(it, cp);
break;
case 3:
err = utf8::internal::get_sequence_3(it, cp);
break;
case 4:
err = utf8::internal::get_sequence_4(it, cp);
break;
}
if (err == UTF8_OK) { //Deprecated in release 2.3
// Decoding succeeded. Now, security checks... template <typename octet_iterator>
if (utf8::internal::is_code_point_valid(cp)) { inline bool is_bom (octet_iterator it)
if (!utf8::internal::is_overlong_sequence(cp, length)){ {
// Passed! Return here. return (
code_point = cp; (utf8::internal::mask8(*it++)) == bom[0] &&
++it; (utf8::internal::mask8(*it++)) == bom[1] &&
return UTF8_OK; (utf8::internal::mask8(*it)) == bom[2]
} );
else }
err = OVERLONG_SEQUENCE;
}
else
err = INVALID_CODE_POINT;
}
// Failure branch - restore the original value of the iterator
it = original_it;
return err;
}
template <typename octet_iterator>
inline utf_error validate_next(octet_iterator& it) {
uint32_t ignored;
return utf8::internal::validate_next(it, ignored);
}
} // namespace internal
/// The library API - functions intended to be called by the users
// Byte order mark
const uint8_t bom[] = {0xef, 0xbb, 0xbf};
template <typename octet_iterator>
octet_iterator find_invalid(octet_iterator start, octet_iterator end)
{
octet_iterator result = start;
while (result != end) {
utf8::internal::utf_error err_code = utf8::internal::validate_next(result);
if (err_code != internal::UTF8_OK)
return result;
}
return result;
}
template <typename octet_iterator>
inline bool is_valid(octet_iterator start, octet_iterator end)
{
return (utf8::find_invalid(start, end) == end);
}
template <typename octet_iterator>
inline bool starts_with_bom (octet_iterator it, octet_iterator end)
{
return (
((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
((it != end) && (utf8::internal::mask8(*it)) == bom[2])
);
}
//Deprecated in release 2.3
template <typename octet_iterator>
inline bool is_bom (octet_iterator it)
{
return (
(utf8::internal::mask8(*it++)) == bom[0] &&
(utf8::internal::mask8(*it++)) == bom[1] &&
(utf8::internal::mask8(*it)) == bom[2]
);
}
} // namespace utf8 } // namespace utf8
#endif // header guard #endif // header guard

View file

@ -0,0 +1,87 @@
// Copyright 2014 Michele Santullo
/*
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 idF5D925E65E0C4C2A983DAC728A7D5B77
#define idF5D925E65E0C4C2A983DAC728A7D5B77
#include "exception.hpp"
#include "global.hpp"
#include <cassert>
namespace utf8 {
/// Error policy to protect against out-of-bounds iterators.
/// Error policy to protect agains invalid utf sequences
template <typename C>
struct utf_policy_replace {
C operator() ( C value ) const {
return static_cast<C>('?');
}
};
template <typename C>
struct utf_policy_throw : private utf_policy_replace<C> {
enum { is_safe = 1 };
uint8_t operator() ( utf8::code_point cp, C value, utf8::ErrorTypes ty, uint8_t faulty_pos ) const {
throw utf8::exception<C>(cp, ty, value, faulty_pos);
return utf_policy_replace<C>::operator()(value);
}
};
template <typename C>
struct utf_policy_assert : private utf_policy_replace<C> {
#if defined(NDEBUG)
enum { is_safe = 0 };
#else
enum { is_safe = 1 };
#endif
C operator() ( C value ) const {
assert(false);
return utf_policy_replace<C>::operator()(value);
}
};
template <typename C>
struct utf_policy_ignore {
enum { is_safe = 0 };
C operator() ( C value ) const {
return value;
}
};
template <typename I>
struct utf_policy_default : public utf_policy_throw<typename std::iterator_traits<I>::value_type> {
};
//namespace internal {
// template <typename octet_iterator>
// class utf_validation {
// public:
// void operator() ( octet_iterator it ) const;
//} //namespace internal
} //namespace utf8
#endif

View file

@ -1,4 +1,5 @@
// Copyright 2006 Nemanja Trifunovic // Copyright 2006 Nemanja Trifunovic
// Copyright 2014 Michele Santullo
/* /*
Permission is hereby granted, free of charge, to any person or organization Permission is hereby granted, free of charge, to any person or organization
@ -28,40 +29,78 @@ DEALINGS IN THE SOFTWARE.
#define id71B1E0983F3D4F7BAD0C091C4569AB37 #define id71B1E0983F3D4F7BAD0C091C4569AB37
#include <stdexcept> #include <stdexcept>
#include "global.hpp"
namespace utf8 { namespace utf8 {
// Base for the exceptions that may be thrown from the library namespace internal {
class exception : public ::std::exception { template <uint64_t V>
}; class GetMinBitSizeClass {
private:
GetMinBitSizeClass ( void ); //Not implemented
template <uint64_t Val, bool End=(Val==0)>
struct CalcImpl {
enum { Result = 0 };
};
template <uint64_t Val>
struct CalcImpl<Val, false> {
enum { Result = 1 + CalcImpl<(Val>>1)>::Result };
};
public:
enum { Result = CalcImpl<V, 0>::Result };
};
} //namespace internal
// Exceptions that may be thrown from the library functions. // Exceptions that may be thrown from the library functions.
class invalid_code_point : public exception { enum ErrorTypes {
uint32_t cp; ErrorType_InvalidLead,
public: ErrorType_IncompleteSequence,
invalid_code_point(uint32_t cp) : cp(cp) {} ErrorType_OverlongSequence,
virtual const char* what() const throw() { return "Invalid code point"; } ErrorType_InvalidCodePoint
uint32_t code_point() const {return cp;}
}; };
class invalid_utf8 : public exception { template <typename C>
uint8_t u8; class exception : public ::std::exception {
public: public:
invalid_utf8 (uint8_t u) : u8(u) {} enum UtfCategories {
virtual const char* what() const throw() { return "Invalid UTF-8"; } UtfCategory_8 = 1,
uint8_t utf8_octet() const {return u8;} UtfCategory_16,
}; UtfCategory_32
};
class invalid_utf16 : public exception { exception ( utf8::code_point codep, ErrorTypes err_type, C value, uint8_t faulty_pos ) :
uint16_t u16; cp(codep),
public: utf_category(static_cast<UtfCategories>(internal::GetMinBitSizeClass<sizeof(C)>::Result)),
invalid_utf16 (uint16_t u) : u16(u) {} error_type(err_type),
virtual const char* what() const throw() { return "Invalid UTF-16"; } faulty_value(value),
uint16_t utf16_word() const {return u16;} faulty_part(faulty_pos)
}; {
static_assert(
static_cast<UtfCategories>(internal::GetMinBitSizeClass<sizeof(C)>::Result) == UtfCategory_8 or
static_cast<UtfCategories>(internal::GetMinBitSizeClass<sizeof(C)>::Result) == UtfCategory_16 or
static_cast<UtfCategories>(internal::GetMinBitSizeClass<sizeof(C)>::Result) == UtfCategory_32, "Invalid size for template parameter");
}
virtual const char* what() const noexcept {
switch (error_type) {
case ErrorType_IncompleteSequence:
switch (utf_category) {
case UtfCategory_32: return "Invalid code point";
case UtfCategory_8: return "Invalid UTF-8";
case UtfCategory_16: return "Invalid UTF-16";
default: return "Error in unknown sequence type";
}
case ErrorType_InvalidLead: return "Invalid lead";
case ErrorType_OverlongSequence: return "Overlong sequence";
case ErrorType_InvalidCodePoint: return "Invalid codepoint";
default: return "Unknown error";
}
}
class not_enough_room : public exception { const utf8::code_point cp;
public: const UtfCategories utf_category;
virtual const char* what() const throw() { return "Not enough space"; } const ErrorTypes error_type;
const C faulty_value;
const uint8_t faulty_part;
}; };
} //namespace utf8 } //namespace utf8

View file

@ -31,98 +31,117 @@ DEALINGS IN THE SOFTWARE.
#include "core.hpp" #include "core.hpp"
#include "exception.hpp" #include "exception.hpp"
#include "error_policies.hpp"
#include "global.hpp"
#include <iterator>
namespace utf8 namespace utf8
{ {
/// 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>
octet_iterator append(uint32_t cp, octet_iterator result) octet_iterator append(utf8::code_point cp, octet_iterator result)
{ {
if (!utf8::internal::is_code_point_valid(cp)) //TODO: implement
throw invalid_code_point(cp); //if (!utf8::internal::is_code_point_valid(cp))
// throw invalid_code_point(cp);
if (cp < 0x80) // one octet //if (cp < 0x80) // one octet
*(result++) = static_cast<uint8_t>(cp); // *(result++) = static_cast<uint8_t>(cp);
else if (cp < 0x800) { // two octets //else if (cp < 0x800) { // two octets
*(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0); // *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); // *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} //}
else if (cp < 0x10000) { // three octets //else if (cp < 0x10000) { // three octets
*(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0); // *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80); // *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); // *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} //}
else { // four octets //else { // four octets
*(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0); // *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
*(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80); // *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80); // *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); // *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} //}
return result; return result;
} }
template <typename octet_iterator, typename output_iterator> template <typename octet_iterator, typename output_iterator>
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement) output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, utf8::code_point replacement)
{ {
while (start != end) { //TODO: implement
octet_iterator sequence_start = start; //while (start != end) {
internal::utf_error err_code = utf8::internal::validate_next(start); // octet_iterator sequence_start = start;
switch (err_code) { // internal::utf_error err_code = utf8::internal::validate_next(start);
case internal::UTF8_OK : // switch (err_code) {
for (octet_iterator it = sequence_start; it != start; ++it) // case internal::UTF8_OK :
*out++ = *it; // for (octet_iterator it = sequence_start; it != start; ++it)
break; // *out++ = *it;
case internal::INVALID_LEAD: // break;
out = utf8::append (replacement, out); // case internal::INVALID_LEAD:
++start; // out = utf8::append (replacement, out);
break; // ++start;
case internal::INCOMPLETE_SEQUENCE: // break;
case internal::OVERLONG_SEQUENCE: // case internal::INCOMPLETE_SEQUENCE:
case internal::INVALID_CODE_POINT: // case internal::OVERLONG_SEQUENCE:
out = utf8::append (replacement, out); // case internal::INVALID_CODE_POINT:
++start; // out = utf8::append (replacement, out);
// just one replacement mark for the sequence // ++start;
while (start != end && utf8::internal::is_trail(*start)) // // just one replacement mark for the sequence
++start; // while (start != end && utf8::internal::is_trail(*start))
break; // ++start;
} // break;
} // }
//}
return out; return out;
} }
template <typename octet_iterator, typename output_iterator> template <typename octet_iterator, typename output_iterator>
inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out) inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
{ {
static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd); static const utf8::code_point replacement_marker = utf8::internal::mask16(0xfffd);
return utf8::replace_invalid(start, end, out, replacement_marker); return utf8::replace_invalid(start, end, out, replacement_marker);
} }
template <typename octet_iterator> template <typename octet_iterator, typename invalid_utf_policy=utf_policy_default<octet_iterator> >
uint32_t next(octet_iterator& it) utf8::code_point next(octet_iterator& it) {
{ typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
uint32_t cp = 0; typedef typename std::iterator_traits<octet_iterator>::value_type octet_value_type;
internal::utf_error err_code = utf8::internal::validate_next(it, cp);
switch (err_code) { // Determine the sequence length based on the lead octet
case internal::UTF8_OK : const octet_value_type lead_char(*it);
break; const octet_difference_type length = utf8::internal::sequence_length<octet_difference_type>(lead_char);
case internal::INVALID_LEAD :
case internal::INCOMPLETE_SEQUENCE : // Get trail octets and calculate the code point
case internal::OVERLONG_SEQUENCE : utf8::internal::SequenceReader<octet_iterator> seq;
throw invalid_utf8(*it); const utf8::code_point cp = seq.get_sequence(it, length);
case internal::INVALID_CODE_POINT : if (seq.has_error()) {
throw invalid_code_point(cp); return invalid_utf_policy()(seq.code(), seq.faulty(), utf8::ErrorType_InvalidCodePoint, seq.part());
}
// Decoding succeeded. Now, security checks...
if (utf8::internal::is_code_point_valid(cp)) {
if (not utf8::internal::is_overlong_sequence(cp, length)){
// Passed! Return here.
++it;
return cp;
}
else {
return invalid_utf_policy()(cp, 0, utf8::ErrorType_OverlongSequence, 0);
}
}
else {
return invalid_utf_policy()(cp, 0, utf8::ErrorType_InvalidCodePoint, 0);
} }
return cp;
} }
template <typename octet_iterator> template <typename octet_iterator>
uint32_t peek_next(octet_iterator it) utf8::code_point peek_next(octet_iterator it)
{ {
return utf8::next(it); return utf8::next(it);
} }
template <typename octet_iterator> template <typename octet_iterator>
uint32_t prior(octet_iterator& it) utf8::code_point prior(octet_iterator& it)
{ {
octet_iterator end = it; octet_iterator end = it;
// Go back until we hit either a lead octet or start // Go back until we hit either a lead octet or start
@ -130,11 +149,12 @@ namespace utf8
return utf8::peek_next(it); return utf8::peek_next(it);
} }
template <typename octet_iterator, typename distance_type> template <typename octet_iterator, typename distance_type, typename error_policy=utf_policy_default<octet_iterator> >
void advance (octet_iterator& it, distance_type n) code_point advance (octet_iterator& it, distance_type n) {
{ code_point ret = InvalidCodePoint;
for (distance_type i = 0; i < n; ++i) for (distance_type i = 0; i < n; ++i)
utf8::next(it); ret = utf8::next(it);
return ret;
} }
template <typename octet_iterator> template <typename octet_iterator>
@ -150,27 +170,28 @@ namespace utf8
template <typename u16bit_iterator, typename octet_iterator> template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result) octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
{ {
while (start != end) { //TODO: implement
uint32_t cp = utf8::internal::mask16(*start++); //while (start != end) {
// Take care of surrogate pairs first // utf8::code_point cp = utf8::internal::mask16(*start++);
if (utf8::internal::is_lead_surrogate(cp)) { // // Take care of surrogate pairs first
if (start != end) { // if (utf8::internal::is_lead_surrogate(cp)) {
uint32_t trail_surrogate = utf8::internal::mask16(*start++); // if (start != end) {
if (utf8::internal::is_trail_surrogate(trail_surrogate)) // utf8::code_point trail_surrogate = utf8::internal::mask16(*start++);
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; // if (utf8::internal::is_trail_surrogate(trail_surrogate))
else // cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
throw invalid_utf16(static_cast<uint16_t>(trail_surrogate)); // else
} // throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
else // }
throw invalid_utf16(static_cast<uint16_t>(cp)); // else
// throw invalid_utf16(static_cast<uint16_t>(cp));
} // }
// Lone trail surrogate // // Lone trail surrogate
else if (utf8::internal::is_trail_surrogate(cp)) // else if (utf8::internal::is_trail_surrogate(cp))
throw invalid_utf16(static_cast<uint16_t>(cp)); // throw invalid_utf16(static_cast<uint16_t>(cp));
result = utf8::append(cp, result); // result = utf8::append(cp, result);
} //}
return result; return result;
} }
@ -178,7 +199,7 @@ namespace utf8
u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result) u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
{ {
while (start != end) { while (start != end) {
uint32_t cp = utf8::next(start); utf8::code_point cp = utf8::next(start);
if (cp > 0xffff) { //make a surrogate pair if (cp > 0xffff) { //make a surrogate pair
*result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET); *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
*result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);

40
src/utf8/global.hpp Normal file
View file

@ -0,0 +1,40 @@
// Copyright 2014 Michele Santullo
/*
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 idACBCC4F2EA7148C2B3F22FBCA60E3F21
#define idACBCC4F2EA7148C2B3F22FBCA60E3F21
#include <cstdint>
#include <ciso646>
namespace utf8 {
typedef uint32_t code_point;
enum {
InvalidCodePoint = static_cast<code_point>(-1)
};
} //namespace utf8
#endif

View file

@ -31,102 +31,13 @@ DEALINGS IN THE SOFTWARE.
#include <iterator> #include <iterator>
namespace utf8 { namespace utf8 {
// Error policies for the iterator class
template <typename I>
class range_policy_throw {
public:
range_policy_throw ( const range_policy_throw& ) = delete;
range_policy_throw ( void ) = delete;
range_policy_throw ( range_policy_throw&& ) = delete;
range_policy_throw& operator= ( const range_policy_throw& ) = delete;
range_policy_throw ( const I& range_start, const I& range_end ) :
m_range_start(range_start),
m_range_end(range_end)
{
}
void operator() ( const I& it ) const {
if (it < m_range_start || it > m_range_end)
throw std::out_of_range("Invalid utf-8 iterator position");
}
void operator() ( const I& range_start, const I& range_end ) {
if (m_range_start != range_start || m_range_end != range_end)
throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
}
private:
I m_range_start;
I m_range_end;
};
// template <typename I>
// class range_policy_assert {
// public:
// range_policy_assert (
// 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
// }
// };
template <typename C>
struct utf_policy_replace {
C operator() ( C value ) const {
return static_cast<C>('?');
}
};
template <typename C> struct utf_policy_throw;
template <> struct utf_policy_throw<uint8_t> : private utf_policy_replace<uint8_t> {
char operator() ( uint8_t value ) const {
throw utf8::invalid_utf8(value);
return utf_policy_replace<uint8_t>::operator()(value);
}
};
template <> struct utf_policy_throw<uint16_t> : private utf_policy_replace<uint16_t> {
char operator() ( uint16_t value ) const {
throw utf8::invalid_utf16(value);
return utf_policy_replace<uint16_t>::operator()(value);
}
};
template <> struct utf_policy_throw<uint32_t> : private utf_policy_replace<uint32_t> {
char operator() ( uint32_t value ) const {
throw utf8::invalid_code_point(value);
return utf_policy_replace<uint32_t>::operator()(value);
}
};
template <typename C>
struct utf_policy_assert : private utf_policy_replace<C> {
char operator() ( uint8_t value ) const {
assert(false);
return utf_policy_replace<C>::operator()(value);
}
};
/// The iterator class /// The iterator class
template < template <
typename octet_iterator, typename octet_iterator,
typename utf_error_policy=utf_policy_throw<typename std::iterator_traits<octet_iterator>::value_type> typename utf_error_policy=utf_policy_default<octet_iterator>
> >
class iterator : public std::iterator <typename std::iterator_traits<octet_iterator>::iterator_category, uint32_t> { class iterator : public std::iterator <typename std::iterator_traits<octet_iterator>::iterator_category, typename std::iterator_traits<octet_iterator>::value_type, typename std::iterator_traits<octet_iterator>::difference_type, typename std::iterator_traits<octet_iterator>::pointer, typename std::iterator_traits<octet_iterator>::reference> {
public: public:
typedef typename std::iterator_traits<octet_iterator>::difference_type difference_type; typedef typename std::iterator_traits<octet_iterator>::difference_type difference_type;
@ -144,43 +55,43 @@ namespace utf8 {
~iterator ( void ) noexcept(noexcept(std::declval<octet_iterator>().~octet_iterator())) { } ~iterator ( void ) noexcept(noexcept(std::declval<octet_iterator>().~octet_iterator())) { }
octet_iterator base () const { return m_it; } octet_iterator base () const { return m_it; }
uint32_t operator* () const { const uint32_t& operator* () const {
octet_iterator temp = m_it; return m_codepoint;
return utf8::next<utf_error_policy>(temp);
} }
bool operator== (const iterator& rhs) const { bool operator== (const iterator& rhs) const {
return (m_it == rhs.m_it); return m_codepoint == rhs.m_codepoint && m_it == rhs.m_it;
} }
bool operator!= (const iterator& rhs) const { bool operator!= (const iterator& rhs) const {
return !(operator== (rhs)); return !(operator== (rhs));
} }
iterator& operator++ () { iterator& operator++ () {
utf8::next(m_it); m_codepoint = utf8::next<utf_error_policy>(m_it);
return *this; return *this;
} }
iterator operator++ (int) { iterator operator++ (int) {
iterator temp = *this; iterator temp = *this;
utf8::next(m_it); m_codepoint = utf8::next<utf_error_policy>(m_it);
return temp; return temp;
} }
iterator& operator-- () { iterator& operator-- () {
utf8::prior(m_it); m_codepoint = utf8::prior<utf_error_policy>(m_it);
return *this; return *this;
} }
iterator operator-- (int) { iterator operator-- (int) {
iterator temp = *this; iterator temp = *this;
utf8::prior(m_it); m_codepoint = utf8::prior<utf_error_policy>(m_it);
return temp; return temp;
} }
difference_type operator- (const iterator& rhs) const { difference_type operator- (const iterator& rhs) const {
return m_it - rhs.m_it; return m_it - rhs.m_it;
} }
iterator& operator+= (difference_type inc) { iterator& operator+= (difference_type inc) {
utf8::advance(m_it, inc); m_codepoint = utf8::advance<octet_iterator, difference_type, utf_error_policy>(m_it, inc);
} }
public: public:
octet_iterator m_it; octet_iterator m_it;
uint32_t m_codepoint;
}; // class iterator }; // class iterator
} // namespace utf8 } // namespace utf8