diff --git a/icu4c/source/common/charstr.cpp b/icu4c/source/common/charstr.cpp index 76723d9790c..846a074feb6 100644 --- a/icu4c/source/common/charstr.cpp +++ b/icu4c/source/common/charstr.cpp @@ -1,6 +1,6 @@ /* ******************************************************************************* -* Copyright (C) 2010-2011, International Business Machines +* Copyright (C) 2010-2015, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* * file name: charstr.cpp @@ -16,6 +16,7 @@ #include "charstr.h" #include "cmemory.h" #include "cstring.h" +#include "uinvchar.h" U_NAMESPACE_BEGIN @@ -101,6 +102,13 @@ char *CharString::getAppendBuffer(int32_t minCapacity, } CharString &CharString::appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode) { + if(U_FAILURE(errorCode)) { + return *this; + } + if (!uprv_isInvariantUString(s.getBuffer(), s.length())) { + errorCode = U_INVARIANT_CONVERSION_ERROR; + return *this; + } if(ensureCapacity(len+s.length()+1, 0, errorCode)) { len+=s.extract(0, 0x7fffffff, buffer.getAlias()+len, buffer.getCapacity()-len, US_INV); } diff --git a/icu4c/source/test/intltest/strtest.cpp b/icu4c/source/test/intltest/strtest.cpp index 2081454d5fe..5075fa5fc8e 100644 --- a/icu4c/source/test/intltest/strtest.cpp +++ b/icu4c/source/test/intltest/strtest.cpp @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2012, International Business Machines Corporation and + * Copyright (c) 1997-2015, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /* file name: strtest.cpp @@ -521,4 +521,14 @@ StringTest::TestCharString() { if (0 != strcmp(expected, chStr.data()) || (int32_t)strlen(expected) != chStr.length()) { errln("CharString.getAppendBuffer().append(**) failed."); } + + UErrorCode ec = U_ZERO_ERROR; + chStr.clear(); + chStr.appendInvariantChars(UnicodeString("The '@' character is not invariant."), ec); + if (ec != U_INVARIANT_CONVERSION_ERROR) { + errln("%s:%d expected U_INVARIANT_CONVERSION_ERROR, got %s", __FILE__, __LINE__, u_errorName(ec)); + } + if (chStr.length() != 0) { + errln("%s:%d expected length() = 0, got %d", __FILE__, __LINE__, chStr.length()); + } }