Skip to content

Commit a5ce56a

Browse files
committed
Avoid type casts and work on full unsigneds instead
1 parent 8187bdd commit a5ce56a

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/util/unicode.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ std::vector<std::string> narrow_argv(int argc, const wchar_t **argv_wide)
167167
}
168168

169169
/// A helper function for dealing with different UTF16 endians
170-
/// \par parameters: A 16-bit integer
171-
/// \return A 16-bit integer with bytes swapped
172-
uint16_t do_swap_bytes(uint16_t x)
170+
/// \par x Integer the lower two bytes of which are to be swapped
171+
/// \return The lower 16 bits of the integer with those bytes swapped
172+
unsigned do_swap_bytes(unsigned x)
173173
{
174-
uint16_t b1=x & 0xFF;
175-
uint16_t b2=x & 0xFF00;
174+
const unsigned b1 = x & 0xFFu;
175+
const unsigned b2 = x & 0xFF00u;
176176
return (b1 << 8) | (b2 >> 8);
177177
}
178178

@@ -197,10 +197,10 @@ void utf16_append_code(unsigned int code, bool swap_bytes, std::wstring &result)
197197
// encode the code in UTF16, possibly swapping bytes.
198198
code=code-0x10000;
199199
unsigned int i1=((code>>10) & 0x3ff) | 0xD800;
200-
unsigned int a1=(swap_bytes)?do_swap_bytes(static_cast<uint16_t>(i1)):i1;
200+
const unsigned int a1 = swap_bytes ? do_swap_bytes(i1) : i1;
201201
result+=static_cast<wchar_t>(a1);
202202
unsigned int i2=(code & 0x3ff) | 0xDC00;
203-
unsigned int a2=(swap_bytes)?do_swap_bytes(static_cast<uint16_t>(i2)):i2;
203+
const unsigned int a2 = swap_bytes ? do_swap_bytes(i2) : i2;
204204
result+=static_cast<wchar_t>(a2);
205205
}
206206
}

0 commit comments

Comments
 (0)