Skip to content

Commit

Permalink
pulled out repeated UTF-16 character generation code
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Mar 9, 2022
1 parent c45d90b commit 967bcf3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class simplecpp::TokenList::Stream {
// character is non-ASCII character then replace it with 0xff
if (isUtf16) {
const unsigned char ch2 = static_cast<unsigned char>(get());
const int ch16 = (bom == 0xfeff) ? (ch<<8 | ch2) : (ch2<<8 | ch);
const int ch16 = makeUtf16Char(ch, ch2);
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));
}

Expand All @@ -242,7 +242,7 @@ class simplecpp::TokenList::Stream {
else if (isUtf16) {
const int c1 = get();
const int c2 = get();
const int ch16 = (bom == 0xfeff) ? (c1<<8 | c2) : (c2<<8 | c1);
const int ch16 = makeUtf16Char(c1, c2);
if (ch16 != '\n') {
unget();
unget();
Expand All @@ -263,7 +263,7 @@ class simplecpp::TokenList::Stream {
(void)get();
const unsigned char ch2 = static_cast<unsigned char>(peek());
unget();
const int ch16 = (bom == 0xfeff) ? (ch<<8 | ch2) : (ch2<<8 | ch);
const int ch16 = makeUtf16Char(ch, ch2);
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));
}

Expand All @@ -288,6 +288,11 @@ class simplecpp::TokenList::Stream {
}

private:
inline int makeUtf16Char(const unsigned char ch, const unsigned char ch2) const
{
return (bom == 0xfeff) ? (ch<<8 | ch2) : (ch2<<8 | ch);
}

unsigned short getAndSkipBOM()
{
const int ch1 = peek();
Expand Down

0 comments on commit 967bcf3

Please sign in to comment.