Unverified Commit 5c7d27c3 authored by Niels Lohmann's avatar Niels Lohmann Committed by GitHub

Merge pull request #1272 from antonioborondo/fix_warning

Fix warning C4127: conditional expression is constant
parents 9f18e170 b6fdad9a
...@@ -115,38 +115,11 @@ class input_buffer_adapter : public input_adapter_protocol ...@@ -115,38 +115,11 @@ class input_buffer_adapter : public input_adapter_protocol
const char* const limit; const char* const limit;
}; };
template<typename WideStringType> template<typename WideStringType, size_t T>
class wide_string_input_adapter : public input_adapter_protocol struct wide_string_input_helper
{ {
public: // UTF-32
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {} static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
std::char_traits<char>::int_type get_character() noexcept override
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
{
if (sizeof(typename WideStringType::value_type) == 2)
{
fill_buffer_utf16();
}
else
{
fill_buffer_utf32();
}
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index == 0);
}
// use buffer
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index < utf8_bytes_filled);
return utf8_bytes[utf8_bytes_index++];
}
private:
void fill_buffer_utf16()
{ {
utf8_bytes_index = 0; utf8_bytes_index = 0;
...@@ -160,7 +133,7 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -160,7 +133,7 @@ class wide_string_input_adapter : public input_adapter_protocol
// get the current character // get the current character
const int wc = static_cast<int>(str[current_wchar++]); const int wc = static_cast<int>(str[current_wchar++]);
// UTF-16 to UTF-8 encoding // UTF-32 to UTF-8 encoding
if (wc < 0x80) if (wc < 0x80)
{ {
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
...@@ -168,41 +141,40 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -168,41 +141,40 @@ class wide_string_input_adapter : public input_adapter_protocol
} }
else if (wc <= 0x7FF) else if (wc <= 0x7FF)
{ {
utf8_bytes[0] = 0xC0 | ((wc >> 6)); utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
utf8_bytes[1] = 0x80 | (wc & 0x3F); utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2; utf8_bytes_filled = 2;
} }
else if (0xD800 > wc or wc >= 0xE000) else if (wc <= 0xFFFF)
{ {
utf8_bytes[0] = 0xE0 | ((wc >> 12)); utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F); utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F); utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3; utf8_bytes_filled = 3;
} }
else else if (wc <= 0x10FFFF)
{
if (current_wchar < str.size())
{ {
const int wc2 = static_cast<int>(str[current_wchar++]); utf8_bytes[0] = 0xF0 | ((wc >> 18) & 0x07);
const int charcode = 0x10000 + (((wc & 0x3FF) << 10) | (wc2 & 0x3FF)); utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
utf8_bytes[0] = 0xf0 | (charcode >> 18); utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[1] = 0x80 | ((charcode >> 12) & 0x3F); utf8_bytes[3] = 0x80 | (wc & 0x3F);
utf8_bytes[2] = 0x80 | ((charcode >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (charcode & 0x3F);
utf8_bytes_filled = 4; utf8_bytes_filled = 4;
} }
else else
{ {
// unknown character // unknown character
++current_wchar;
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
utf8_bytes_filled = 1; utf8_bytes_filled = 1;
} }
} }
} }
} };
void fill_buffer_utf32() template<typename WideStringType>
struct wide_string_input_helper<WideStringType, 2>
{
// UTF-16
static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
{ {
utf8_bytes_index = 0; utf8_bytes_index = 0;
...@@ -216,7 +188,7 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -216,7 +188,7 @@ class wide_string_input_adapter : public input_adapter_protocol
// get the current character // get the current character
const int wc = static_cast<int>(str[current_wchar++]); const int wc = static_cast<int>(str[current_wchar++]);
// UTF-32 to UTF-8 encoding // UTF-16 to UTF-8 encoding
if (wc < 0x80) if (wc < 0x80)
{ {
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
...@@ -224,35 +196,71 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -224,35 +196,71 @@ class wide_string_input_adapter : public input_adapter_protocol
} }
else if (wc <= 0x7FF) else if (wc <= 0x7FF)
{ {
utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F); utf8_bytes[0] = 0xC0 | ((wc >> 6));
utf8_bytes[1] = 0x80 | (wc & 0x3F); utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2; utf8_bytes_filled = 2;
} }
else if (wc <= 0xFFFF) else if (0xD800 > wc or wc >= 0xE000)
{ {
utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F); utf8_bytes[0] = 0xE0 | ((wc >> 12));
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F); utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F); utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3; utf8_bytes_filled = 3;
} }
else if (wc <= 0x10FFFF) else
{ {
utf8_bytes[0] = 0xF0 | ((wc >> 18 ) & 0x07); if (current_wchar < str.size())
utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F); {
utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F); const int wc2 = static_cast<int>(str[current_wchar++]);
utf8_bytes[3] = 0x80 | (wc & 0x3F); const int charcode = 0x10000 + (((wc & 0x3FF) << 10) | (wc2 & 0x3FF));
utf8_bytes[0] = 0xf0 | (charcode >> 18);
utf8_bytes[1] = 0x80 | ((charcode >> 12) & 0x3F);
utf8_bytes[2] = 0x80 | ((charcode >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (charcode & 0x3F);
utf8_bytes_filled = 4; utf8_bytes_filled = 4;
} }
else else
{ {
// unknown character // unknown character
++current_wchar;
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
utf8_bytes_filled = 1; utf8_bytes_filled = 1;
} }
} }
} }
}
};
template<typename WideStringType>
class wide_string_input_adapter : public input_adapter_protocol
{
public:
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {}
std::char_traits<char>::int_type get_character() noexcept override
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
{
fill_buffer<sizeof(typename WideStringType::value_type)>();
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index == 0);
}
// use buffer
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index < utf8_bytes_filled);
return utf8_bytes[utf8_bytes_index++];
}
private: private:
template<size_t T>
void fill_buffer()
{
wide_string_input_helper<WideStringType, T>::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
}
/// the wstring to process /// the wstring to process
const WideStringType& str; const WideStringType& str;
......
...@@ -1993,38 +1993,11 @@ class input_buffer_adapter : public input_adapter_protocol ...@@ -1993,38 +1993,11 @@ class input_buffer_adapter : public input_adapter_protocol
const char* const limit; const char* const limit;
}; };
template<typename WideStringType> template<typename WideStringType, size_t T>
class wide_string_input_adapter : public input_adapter_protocol struct wide_string_input_helper
{ {
public: // UTF-32
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {} static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
std::char_traits<char>::int_type get_character() noexcept override
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
{
if (sizeof(typename WideStringType::value_type) == 2)
{
fill_buffer_utf16();
}
else
{
fill_buffer_utf32();
}
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index == 0);
}
// use buffer
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index < utf8_bytes_filled);
return utf8_bytes[utf8_bytes_index++];
}
private:
void fill_buffer_utf16()
{ {
utf8_bytes_index = 0; utf8_bytes_index = 0;
...@@ -2038,7 +2011,7 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -2038,7 +2011,7 @@ class wide_string_input_adapter : public input_adapter_protocol
// get the current character // get the current character
const int wc = static_cast<int>(str[current_wchar++]); const int wc = static_cast<int>(str[current_wchar++]);
// UTF-16 to UTF-8 encoding // UTF-32 to UTF-8 encoding
if (wc < 0x80) if (wc < 0x80)
{ {
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
...@@ -2046,41 +2019,40 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -2046,41 +2019,40 @@ class wide_string_input_adapter : public input_adapter_protocol
} }
else if (wc <= 0x7FF) else if (wc <= 0x7FF)
{ {
utf8_bytes[0] = 0xC0 | ((wc >> 6)); utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
utf8_bytes[1] = 0x80 | (wc & 0x3F); utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2; utf8_bytes_filled = 2;
} }
else if (0xD800 > wc or wc >= 0xE000) else if (wc <= 0xFFFF)
{ {
utf8_bytes[0] = 0xE0 | ((wc >> 12)); utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F); utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F); utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3; utf8_bytes_filled = 3;
} }
else else if (wc <= 0x10FFFF)
{
if (current_wchar < str.size())
{ {
const int wc2 = static_cast<int>(str[current_wchar++]); utf8_bytes[0] = 0xF0 | ((wc >> 18) & 0x07);
const int charcode = 0x10000 + (((wc & 0x3FF) << 10) | (wc2 & 0x3FF)); utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
utf8_bytes[0] = 0xf0 | (charcode >> 18); utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[1] = 0x80 | ((charcode >> 12) & 0x3F); utf8_bytes[3] = 0x80 | (wc & 0x3F);
utf8_bytes[2] = 0x80 | ((charcode >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (charcode & 0x3F);
utf8_bytes_filled = 4; utf8_bytes_filled = 4;
} }
else else
{ {
// unknown character // unknown character
++current_wchar;
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
utf8_bytes_filled = 1; utf8_bytes_filled = 1;
} }
} }
} }
} };
void fill_buffer_utf32() template<typename WideStringType>
struct wide_string_input_helper<WideStringType, 2>
{
// UTF-16
static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
{ {
utf8_bytes_index = 0; utf8_bytes_index = 0;
...@@ -2094,7 +2066,7 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -2094,7 +2066,7 @@ class wide_string_input_adapter : public input_adapter_protocol
// get the current character // get the current character
const int wc = static_cast<int>(str[current_wchar++]); const int wc = static_cast<int>(str[current_wchar++]);
// UTF-32 to UTF-8 encoding // UTF-16 to UTF-8 encoding
if (wc < 0x80) if (wc < 0x80)
{ {
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
...@@ -2102,35 +2074,71 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -2102,35 +2074,71 @@ class wide_string_input_adapter : public input_adapter_protocol
} }
else if (wc <= 0x7FF) else if (wc <= 0x7FF)
{ {
utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F); utf8_bytes[0] = 0xC0 | ((wc >> 6));
utf8_bytes[1] = 0x80 | (wc & 0x3F); utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2; utf8_bytes_filled = 2;
} }
else if (wc <= 0xFFFF) else if (0xD800 > wc or wc >= 0xE000)
{ {
utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F); utf8_bytes[0] = 0xE0 | ((wc >> 12));
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F); utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F); utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3; utf8_bytes_filled = 3;
} }
else if (wc <= 0x10FFFF) else
{ {
utf8_bytes[0] = 0xF0 | ((wc >> 18 ) & 0x07); if (current_wchar < str.size())
utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F); {
utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F); const int wc2 = static_cast<int>(str[current_wchar++]);
utf8_bytes[3] = 0x80 | (wc & 0x3F); const int charcode = 0x10000 + (((wc & 0x3FF) << 10) | (wc2 & 0x3FF));
utf8_bytes[0] = 0xf0 | (charcode >> 18);
utf8_bytes[1] = 0x80 | ((charcode >> 12) & 0x3F);
utf8_bytes[2] = 0x80 | ((charcode >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (charcode & 0x3F);
utf8_bytes_filled = 4; utf8_bytes_filled = 4;
} }
else else
{ {
// unknown character // unknown character
++current_wchar;
utf8_bytes[0] = wc; utf8_bytes[0] = wc;
utf8_bytes_filled = 1; utf8_bytes_filled = 1;
} }
} }
} }
}
};
template<typename WideStringType>
class wide_string_input_adapter : public input_adapter_protocol
{
public:
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {}
std::char_traits<char>::int_type get_character() noexcept override
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
{
fill_buffer<sizeof(typename WideStringType::value_type)>();
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index == 0);
}
// use buffer
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index < utf8_bytes_filled);
return utf8_bytes[utf8_bytes_index++];
}
private: private:
template<size_t T>
void fill_buffer()
{
wide_string_input_helper<WideStringType, T>::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
}
/// the wstring to process /// the wstring to process
const WideStringType& str; const WideStringType& str;
...@@ -2146,6 +2154,8 @@ class wide_string_input_adapter : public input_adapter_protocol ...@@ -2146,6 +2154,8 @@ class wide_string_input_adapter : public input_adapter_protocol
std::size_t utf8_bytes_filled = 0; std::size_t utf8_bytes_filled = 0;
}; };
class input_adapter class input_adapter
{ {
public: public:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment