Logo Search packages:      
Sourcecode: icu version File versions

void UnicodeString::toUTF8 ( ByteSink sink  )  const

Convert the UnicodeString to UTF-8 and write the result to a ByteSink. This is called by toUTF8String(). Unpaired surrogates are replaced with U+FFFD. Calls u_strToUTF8WithSub().

Parameters:
sink A ByteSink to which the UTF-8 version of the string is written. ICU 4.2
See also:
toUTF8String

Definition at line 825 of file unistr.cpp.

References ByteSink::Append(), FALSE, ByteSink::GetAppendBuffer(), getBuffer(), length(), NULL, TRUE, U_BUFFER_OVERFLOW_ERROR, U_MEMORY_ALLOCATION_ERROR, U_SUCCESS, and U_ZERO_ERROR.

Referenced by extract().

                                          {
  int32_t length16 = length();
  if(length16 != 0) {
    char stackBuffer[1024];
    int32_t capacity = (int32_t)sizeof(stackBuffer);
    UBool utf8IsOwned = FALSE;
    char *utf8 = sink.GetAppendBuffer(length16 < capacity ? length16 : capacity,
                                      3*length16,
                                      stackBuffer, capacity,
                                      &capacity);
    int32_t length8 = 0;
    UErrorCode errorCode = U_ZERO_ERROR;
    u_strToUTF8WithSub(utf8, capacity, &length8,
                       getBuffer(), length16,
                       0xFFFD,  // Standard substitution character.
                       NULL,    // Don't care about number of substitutions.
                       &errorCode);
    if(errorCode == U_BUFFER_OVERFLOW_ERROR) {
      utf8 = (char *)uprv_malloc(length8);
      if(utf8 != NULL) {
        utf8IsOwned = TRUE;
        errorCode = U_ZERO_ERROR;
        u_strToUTF8WithSub(utf8, length8, &length8,
                           getBuffer(), length16,
                           0xFFFD,  // Standard substitution character.
                           NULL,    // Don't care about number of substitutions.
                           &errorCode);
      } else {
        errorCode = U_MEMORY_ALLOCATION_ERROR;
      }
    }
    if(U_SUCCESS(errorCode)) {
      sink.Append(utf8, length8);
    }
    if(utf8IsOwned) {
      uprv_free(utf8);
    }
  }
}


Generated by  Doxygen 1.6.0   Back to index