1313#include < vector>
1414
1515#if defined(__APPLE__) || defined(__linux__)
16+
1617// Constants for character encoding
1718const char * kOdbcEncoding = " utf-16-le" ; // ODBC uses UTF-16LE for SQLWCHAR
1819const size_t kUcsLength = 2 ; // SQLWCHAR is 2 bytes on all platforms
@@ -27,8 +28,7 @@ std::wstring SQLWCHARToWString(const SQLWCHAR* sqlwStr, size_t length = SQL_NTS)
2728 // Lambda to calculate string length using pointer arithmetic
2829 auto calculateLength = [](const SQLWCHAR* str) -> size_t {
2930 const SQLWCHAR* p = str;
30- while (*p)
31- ++p;
31+ while (*p) ++p;
3232 return p - str;
3333 };
3434
@@ -42,20 +42,22 @@ std::wstring SQLWCHARToWString(const SQLWCHAR* sqlwStr, size_t length = SQL_NTS)
4242
4343 // Lambda to check if character is in Basic Multilingual Plane
4444 auto isBMP = [](uint16_t ch) { return ch < 0xD800 || ch > 0xDFFF ; };
45-
45+
4646 // Lambda to decode surrogate pair into code point
4747 auto decodeSurrogatePair = [](uint16_t high, uint16_t low) -> uint32_t {
48- return 0x10000 + (static_cast <uint32_t >(high & 0x3FF ) << 10 ) + (low & 0x3FF );
48+ return 0x10000 +
49+ (static_cast <uint32_t >(high & 0x3FF ) << 10 ) +
50+ (low & 0x3FF );
4951 };
5052
5153 // Convert UTF-16 to UTF-32 directly without intermediate buffer
5254 std::wstring result;
5355 result.reserve (length); // Reserve assuming most chars are BMP
54-
56+
5557 size_t i = 0 ;
5658 while (i < length) {
5759 uint16_t utf16Char = static_cast <uint16_t >(sqlwStr[i]);
58-
60+
5961 // Fast path: BMP character (most common - ~99% of strings)
6062 if (isBMP (utf16Char)) {
6163 result.push_back (static_cast <wchar_t >(utf16Char));
@@ -75,7 +77,8 @@ std::wstring SQLWCHARToWString(const SQLWCHAR* sqlwStr, size_t length = SQL_NTS)
7577 // Invalid surrogate - push as-is
7678 result.push_back (static_cast <wchar_t >(utf16Char));
7779 ++i;
78- } else { // Low surrogate without high - invalid but push as-is
80+ }
81+ else { // Low surrogate without high - invalid but push as-is
7982 result.push_back (static_cast <wchar_t >(utf16Char));
8083 ++i;
8184 }
@@ -100,21 +103,21 @@ std::vector<SQLWCHAR> WStringToSQLWCHAR(const std::wstring& str) {
100103 // Convert wstring (UTF-32) to UTF-16
101104 std::vector<SQLWCHAR> result;
102105 result.reserve (str.size () + 1 ); // Most chars are BMP, so reserve exact size
103-
106+
104107 for (wchar_t wc : str) {
105108 uint32_t codePoint = static_cast <uint32_t >(wc);
106-
109+
107110 // Fast path: BMP character (most common - ~99% of strings)
108111 if (codePoint <= 0xFFFF ) {
109112 result.push_back (static_cast <SQLWCHAR>(codePoint));
110- }
113+ }
111114 // Encode as surrogate pair for characters outside BMP
112115 else if (codePoint <= 0x10FFFF ) {
113116 encodeSurrogatePair (result, codePoint);
114117 }
115118 // Invalid code points silently skipped
116119 }
117-
120+
118121 result.push_back (0 ); // Null terminator
119122 return result;
120123}
0 commit comments