Skip to content

Commit

Permalink
Merge pull request #1272 from antonioborondo/fix_warning
Browse files Browse the repository at this point in the history
Fix warning C4127: conditional expression is constant
  • Loading branch information
nlohmann authored Oct 4, 2018
2 parents 9f18e17 + b6fdad9 commit 5c7d27c
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 130 deletions.
138 changes: 73 additions & 65 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,38 +115,66 @@ class input_buffer_adapter : public input_adapter_protocol
const char* const limit;
};

template<typename WideStringType>
class wide_string_input_adapter : public input_adapter_protocol
template<typename WideStringType, size_t T>
struct wide_string_input_helper
{
public:
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {}

std::char_traits<char>::int_type get_character() noexcept override
// UTF-32
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)
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
utf8_bytes_index = 0;

if (current_wchar == str.size())
{
utf8_bytes[0] = std::char_traits<char>::eof();
utf8_bytes_filled = 1;
}
else
{
if (sizeof(typename WideStringType::value_type) == 2)
// get the current character
const int wc = static_cast<int>(str[current_wchar++]);

// UTF-32 to UTF-8 encoding
if (wc < 0x80)
{
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}
else if (wc <= 0x7FF)
{
utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2;
}
else if (wc <= 0xFFFF)
{
fill_buffer_utf16();
utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3;
}
else if (wc <= 0x10FFFF)
{
utf8_bytes[0] = 0xF0 | ((wc >> 18) & 0x07);
utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 4;
}
else
{
fill_buffer_utf32();
// unknown character
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}

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()
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;

Expand Down Expand Up @@ -201,58 +229,38 @@ class wide_string_input_adapter : public input_adapter_protocol
}
}
}
};

void fill_buffer_utf32()
{
utf8_bytes_index = 0;
template<typename WideStringType>
class wide_string_input_adapter : public input_adapter_protocol
{
public:
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {}

if (current_wchar == str.size())
{
utf8_bytes[0] = std::char_traits<char>::eof();
utf8_bytes_filled = 1;
}
else
std::char_traits<char>::int_type get_character() noexcept override
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
{
// get the current character
const int wc = static_cast<int>(str[current_wchar++]);
fill_buffer<sizeof(typename WideStringType::value_type)>();

// UTF-32 to UTF-8 encoding
if (wc < 0x80)
{
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}
else if (wc <= 0x7FF)
{
utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2;
}
else if (wc <= 0xFFFF)
{
utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3;
}
else if (wc <= 0x10FFFF)
{
utf8_bytes[0] = 0xF0 | ((wc >> 18 ) & 0x07);
utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 4;
}
else
{
// unknown character
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}
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:
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
const WideStringType& str;

Expand Down
140 changes: 75 additions & 65 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1993,38 +1993,66 @@ class input_buffer_adapter : public input_adapter_protocol
const char* const limit;
};

template<typename WideStringType>
class wide_string_input_adapter : public input_adapter_protocol
template<typename WideStringType, size_t T>
struct wide_string_input_helper
{
public:
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {}

std::char_traits<char>::int_type get_character() noexcept override
// UTF-32
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)
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
utf8_bytes_index = 0;

if (current_wchar == str.size())
{
utf8_bytes[0] = std::char_traits<char>::eof();
utf8_bytes_filled = 1;
}
else
{
if (sizeof(typename WideStringType::value_type) == 2)
// get the current character
const int wc = static_cast<int>(str[current_wchar++]);

// UTF-32 to UTF-8 encoding
if (wc < 0x80)
{
fill_buffer_utf16();
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}
else if (wc <= 0x7FF)
{
utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2;
}
else if (wc <= 0xFFFF)
{
utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3;
}
else if (wc <= 0x10FFFF)
{
utf8_bytes[0] = 0xF0 | ((wc >> 18) & 0x07);
utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 4;
}
else
{
fill_buffer_utf32();
// unknown character
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}

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()
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;

Expand Down Expand Up @@ -2079,58 +2107,38 @@ class wide_string_input_adapter : public input_adapter_protocol
}
}
}
};

void fill_buffer_utf32()
{
utf8_bytes_index = 0;
template<typename WideStringType>
class wide_string_input_adapter : public input_adapter_protocol
{
public:
explicit wide_string_input_adapter(const WideStringType& w) : str(w) {}

if (current_wchar == str.size())
{
utf8_bytes[0] = std::char_traits<char>::eof();
utf8_bytes_filled = 1;
}
else
std::char_traits<char>::int_type get_character() noexcept override
{
// check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled)
{
// get the current character
const int wc = static_cast<int>(str[current_wchar++]);
fill_buffer<sizeof(typename WideStringType::value_type)>();

// UTF-32 to UTF-8 encoding
if (wc < 0x80)
{
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}
else if (wc <= 0x7FF)
{
utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
utf8_bytes[1] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 2;
}
else if (wc <= 0xFFFF)
{
utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[2] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 3;
}
else if (wc <= 0x10FFFF)
{
utf8_bytes[0] = 0xF0 | ((wc >> 18 ) & 0x07);
utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
utf8_bytes[3] = 0x80 | (wc & 0x3F);
utf8_bytes_filled = 4;
}
else
{
// unknown character
utf8_bytes[0] = wc;
utf8_bytes_filled = 1;
}
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:
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
const WideStringType& str;

Expand All @@ -2146,6 +2154,8 @@ class wide_string_input_adapter : public input_adapter_protocol
std::size_t utf8_bytes_filled = 0;
};



class input_adapter
{
public:
Expand Down

0 comments on commit 5c7d27c

Please sign in to comment.