1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *   Copyright (C) 2015-2016, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 *******************************************************************************
8 *   file name:  charstr.cpp
9 */
10 #include "unicode/utypes.h"
11 #include "unicode/putil.h"
12 #include "unicode/unistr.h"
13 
14 #include "cstr.h"
15 
16 #include "charstr.h"
17 #include "uinvchar.h"
18 
19 U_NAMESPACE_BEGIN
20 
CStr(const UnicodeString & in)21 CStr::CStr(const UnicodeString &in) {
22     UErrorCode status = U_ZERO_ERROR;
23 #if !UCONFIG_NO_CONVERSION || U_CHARSET_IS_UTF8
24     int32_t length = in.extract(0, in.length(), static_cast<char *>(NULL), static_cast<uint32_t>(0));
25     int32_t resultCapacity = 0;
26     char *buf = s.getAppendBuffer(length, length, resultCapacity, status);
27     if (U_SUCCESS(status)) {
28         in.extract(0, in.length(), buf, resultCapacity);
29         s.append(buf, length, status);
30     }
31 #else
32     // No conversion available. Convert any invariant characters; substitute '?' for the rest.
33     // Note: can't just call u_UCharsToChars() or CharString.appendInvariantChars() on the
34     //       whole string because they require that the entire input be invariant.
35     char buf[2];
36     for (int i=0; i<in.length(); i = in.moveIndex32(i, 1)) {
37         if (uprv_isInvariantUString(in.getBuffer()+i, 1)) {
38             u_UCharsToChars(in.getBuffer()+i, buf, 1);
39         } else {
40             buf[0] = '?';
41         }
42         s.append(buf, 1, status);
43     }
44 #endif
45 }
46 
~CStr()47 CStr::~CStr() {
48 }
49 
operator ()() const50 const char * CStr::operator ()() const {
51     return s.data();
52 }
53 
54 U_NAMESPACE_END
55