]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/cstr.cpp
ICU-62108.0.1.tar.gz
[apple/icu.git] / icuSources / common / cstr.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
2ca993e8
A
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"
f3c0d7a5 11#include "unicode/putil.h"
2ca993e8
A
12#include "unicode/unistr.h"
13
2ca993e8
A
14#include "cstr.h"
15
f3c0d7a5
A
16#include "charstr.h"
17#include "uinvchar.h"
18
2ca993e8
A
19U_NAMESPACE_BEGIN
20
21CStr::CStr(const UnicodeString &in) {
22 UErrorCode status = U_ZERO_ERROR;
f3c0d7a5
A
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));
2ca993e8
A
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 }
f3c0d7a5
A
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
2ca993e8
A
45}
46
47CStr::~CStr() {
48}
49
50const char * CStr::operator ()() const {
51 return s.data();
52}
53
54U_NAMESPACE_END