317 |
317 |
318 QString QGbkCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const |
318 QString QGbkCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const |
319 { |
319 { |
320 uchar buf[2]; |
320 uchar buf[2]; |
321 int nbuf = 0; |
321 int nbuf = 0; |
322 QChar replacement = QChar::ReplacementCharacter; |
322 ushort replacement = QChar::ReplacementCharacter; |
323 if (state) { |
323 if (state) { |
324 if (state->flags & ConvertInvalidToNull) |
324 if (state->flags & ConvertInvalidToNull) |
325 replacement = QChar::Null; |
325 replacement = QChar::Null; |
326 nbuf = state->remainingChars; |
326 nbuf = state->remainingChars; |
327 buf[0] = state->state_data[0]; |
327 buf[0] = state->state_data[0]; |
328 buf[1] = state->state_data[1]; |
328 buf[1] = state->state_data[1]; |
329 } |
329 } |
330 int invalid = 0; |
330 int invalid = 0; |
331 |
331 |
332 QString result; |
332 QString result; |
|
333 result.resize(len); |
|
334 int unicodeLen = 0; |
|
335 ushort *const resultData = reinterpret_cast<ushort*>(result.data()); |
333 |
336 |
334 //qDebug("QGbkDecoder::toUnicode(const char* chars = \"%s\", int len = %d)", chars, len); |
337 //qDebug("QGbkDecoder::toUnicode(const char* chars = \"%s\", int len = %d)", chars, len); |
335 for (int i=0; i<len; i++) { |
338 for (int i=0; i<len; i++) { |
336 uchar ch = chars[i]; |
339 uchar ch = chars[i]; |
337 switch (nbuf) { |
340 switch (nbuf) { |
338 case 0: |
341 case 0: |
339 if (ch < 0x80) { |
342 if (ch < 0x80) { |
340 // ASCII |
343 // ASCII |
341 result += QLatin1Char(ch); |
344 resultData[unicodeLen] = ch; |
|
345 ++unicodeLen; |
342 } else if (Is1stByte(ch)) { |
346 } else if (Is1stByte(ch)) { |
343 // GBK 1st byte? |
347 // GBK 1st byte? |
344 buf[0] = ch; |
348 buf[0] = ch; |
345 nbuf = 1; |
349 nbuf = 1; |
346 } else { |
350 } else { |
347 // Invalid |
351 // Invalid |
348 result += replacement; |
352 resultData[unicodeLen] = replacement; |
|
353 ++unicodeLen; |
349 ++invalid; |
354 ++invalid; |
350 } |
355 } |
351 break; |
356 break; |
352 case 1: |
357 case 1: |
353 // GBK 2nd byte |
358 // GBK 2nd byte |
354 if (Is2ndByteIn2Bytes(ch)) { |
359 if (Is2ndByteIn2Bytes(ch)) { |
355 buf[1] = ch; |
360 buf[1] = ch; |
356 int clen = 2; |
361 int clen = 2; |
357 uint u = qt_Gb18030ToUnicode(buf, clen); |
362 uint u = qt_Gb18030ToUnicode(buf, clen); |
358 if (clen == 2) { |
363 if (clen == 2) { |
359 result += qValidChar(u); |
364 resultData[unicodeLen] = qValidChar(static_cast<ushort>(u)); |
|
365 ++unicodeLen; |
360 } else { |
366 } else { |
361 result += replacement; |
367 resultData[unicodeLen] = replacement; |
|
368 ++unicodeLen; |
362 ++invalid; |
369 ++invalid; |
363 } |
370 } |
364 nbuf = 0; |
371 nbuf = 0; |
365 } else { |
372 } else { |
366 // Error |
373 // Error |
367 result += replacement; |
374 resultData[unicodeLen] = replacement; |
|
375 ++unicodeLen; |
368 ++invalid; |
376 ++invalid; |
369 nbuf = 0; |
377 nbuf = 0; |
370 } |
378 } |
371 break; |
379 break; |
372 } |
380 } |
373 } |
381 } |
|
382 result.resize(unicodeLen); |
374 |
383 |
375 if (state) { |
384 if (state) { |
376 state->remainingChars = nbuf; |
385 state->remainingChars = nbuf; |
377 state->state_data[0] = buf[0]; |
386 state->state_data[0] = buf[0]; |
378 state->state_data[1] = buf[1]; |
387 state->state_data[1] = buf[1]; |