From 0464cceedfe6dbe9f62e6b489cba378848df0867 Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Fri, 9 Oct 2015 17:53:44 +0000 Subject: [PATCH] ICU-11857 Add CStr class for easier UnicodeString to char * conversions. X-SVN-Rev: 38054 --- icu4c/source/common/Makefile.in | 2 +- icu4c/source/common/common.vcxproj | 2 + icu4c/source/common/common.vcxproj.filters | 6 +++ icu4c/source/common/cstr.cpp | 34 +++++++++++++ icu4c/source/common/cstr.h | 55 +++++++++++++++++++++ icu4c/source/test/depstest/dependencies.txt | 6 +++ icu4c/source/test/intltest/strtest.cpp | 11 +++++ icu4c/source/test/intltest/strtest.h | 3 +- 8 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 icu4c/source/common/cstr.cpp create mode 100644 icu4c/source/common/cstr.h diff --git a/icu4c/source/common/Makefile.in b/icu4c/source/common/Makefile.in index 6dd4c4d5047..30b4c6c89d6 100644 --- a/icu4c/source/common/Makefile.in +++ b/icu4c/source/common/Makefile.in @@ -78,7 +78,7 @@ endif LIBS = $(LIBICUDT) $(DEFAULT_LIBS) OBJECTS = errorcode.o putil.o umath.o utypes.o uinvchar.o umutex.o ucln_cmn.o \ -uinit.o uobject.o cmemory.o charstr.o \ +uinit.o uobject.o cmemory.o charstr.o cstr.o \ udata.o ucmndata.o udatamem.o umapfile.o udataswp.o ucol_swp.o utrace.o \ uhash.o uhash_us.o uenum.o ustrenum.o uvector.o ustack.o uvectr32.o uvectr64.o \ ucnv.o ucnv_bld.o ucnv_cnv.o ucnv_io.o ucnv_cb.o ucnv_err.o ucnvlat1.o \ diff --git a/icu4c/source/common/common.vcxproj b/icu4c/source/common/common.vcxproj index 24bf4de5b27..0517cae1767 100644 --- a/icu4c/source/common/common.vcxproj +++ b/icu4c/source/common/common.vcxproj @@ -439,6 +439,7 @@ + @@ -1449,6 +1450,7 @@ + copy "%(FullPath)" ..\..\include\unicode diff --git a/icu4c/source/common/common.vcxproj.filters b/icu4c/source/common/common.vcxproj.filters index 01b0625e64e..2d4810b9000 100644 --- a/icu4c/source/common/common.vcxproj.filters +++ b/icu4c/source/common/common.vcxproj.filters @@ -463,6 +463,9 @@ strings + + strings + strings @@ -829,6 +832,9 @@ strings + + strings + strings diff --git a/icu4c/source/common/cstr.cpp b/icu4c/source/common/cstr.cpp new file mode 100644 index 00000000000..c34b212dc27 --- /dev/null +++ b/icu4c/source/common/cstr.cpp @@ -0,0 +1,34 @@ +/* +******************************************************************************* +* Copyright (C) 2015, International Business Machines +* Corporation and others. All Rights Reserved. +******************************************************************************* +* file name: charstr.cpp +*/ +#include "unicode/utypes.h" +#include "unicode/unistr.h" + +#include "charstr.h" +#include "cstr.h" + +U_NAMESPACE_BEGIN + +CStr::CStr(const UnicodeString &in) { + UErrorCode status = U_ZERO_ERROR; + int32_t length = in.extract(0, in.length(), NULL, (uint32_t)0); + int32_t resultCapacity = 0; + char *buf = s.getAppendBuffer(length, length, resultCapacity, status); + if (U_SUCCESS(status)) { + in.extract(0, in.length(), buf, resultCapacity); + s.append(buf, length, status); + } +} + +CStr::~CStr() { +} + +const char * CStr::operator ()() const { + return s.data(); +} + +U_NAMESPACE_END diff --git a/icu4c/source/common/cstr.h b/icu4c/source/common/cstr.h new file mode 100644 index 00000000000..7c64aee8917 --- /dev/null +++ b/icu4c/source/common/cstr.h @@ -0,0 +1,55 @@ +/* +****************************************************************************** +* +* Copyright (C) 2015, International Business Machines +* Corporation and others. All Rights Reserved. +* +****************************************************************************** +* +* File: cstr.h +*/ + +#ifndef CSTR_H +#define CSTR_H + +#include "unicode/unistr.h" +#include "unicode/uobject.h" +#include "unicode/utypes.h" + +#include "charstr.h" + +/** + * ICU-internal class CStr, a small helper class to facilitate passing UnicodStrings + * to functions needing (const char *) strings, such as printf(). + * + * It is intended primarily for use in debugging or in tests. Uses platform + * default code page conversion, which will do the best job possible, + * but may be lossy, depending on the platform. + * + * Example Usage: + * UnicodeString s = whatever; + * printf("%s", CStr(s)()); + * + * The explicit call to the CStr() constructor creates a temporary object. + * Operator () on the temporary object returns a (const char *) pointer. + * The lifetime of the (const char *) data is that of the temporary object, + * which works well when passing it as a parameter to another function, such as printf. + */ + +U_NAMESPACE_BEGIN + +class U_COMMON_API CStr : public UMemory { + public: + CStr(const UnicodeString &in); + ~CStr(); + const char * operator ()() const; + + private: + CharString s; + CStr(const CStr &other); // Forbid copying of this class. + CStr &operator =(const CStr &other); // Forbid assignment. +}; + +U_NAMESPACE_END + +#endif diff --git a/icu4c/source/test/depstest/dependencies.txt b/icu4c/source/test/depstest/dependencies.txt index d77c82851d8..1f554a43903 100644 --- a/icu4c/source/test/depstest/dependencies.txt +++ b/icu4c/source/test/depstest/dependencies.txt @@ -140,6 +140,7 @@ library: common idna2003 stringprep stringenumeration unistr_props unistr_case unistr_case_locale unistr_titlecase_brkiter unistr_cnv + cstr uniset_core uniset_props uniset_closure usetiter uset uset_props uiter ucasemap ucasemap_titlecase_brkiter script_runs @@ -350,6 +351,11 @@ group: unistr_cnv deps conversion +group: cstr + cstr.o + deps + unistr_cnv + group: uscript uscript.o # uscript_getCode() accepts a locale ID and loads its script code data deps diff --git a/icu4c/source/test/intltest/strtest.cpp b/icu4c/source/test/intltest/strtest.cpp index 5075fa5fc8e..0ec61eba8fd 100644 --- a/icu4c/source/test/intltest/strtest.cpp +++ b/icu4c/source/test/intltest/strtest.cpp @@ -21,6 +21,7 @@ #include "unicode/unistr.h" #include "unicode/ustring.h" #include "charstr.h" +#include "cstr.h" #include "intltest.h" #include "strtest.h" @@ -168,6 +169,7 @@ void StringTest::runIndexedTest(int32_t index, UBool exec, const char *&name, ch TESTCASE_AUTO(TestCheckedArrayByteSink); TESTCASE_AUTO(TestStringByteSink); TESTCASE_AUTO(TestCharString); + TESTCASE_AUTO(TestCStr); TESTCASE_AUTO_END; } @@ -532,3 +534,12 @@ StringTest::TestCharString() { errln("%s:%d expected length() = 0, got %d", __FILE__, __LINE__, chStr.length()); } } + +void +StringTest::TestCStr() { + const char *cs = "This is a test string."; + UnicodeString us(cs); + if (0 != strcmp(CStr(us)(), cs)) { + errln("%s:%d CStr(s)() failed. Expected \"%s\", got \"%s\"", __FILE__, __LINE__, cs, CStr(us)()); + } +} diff --git a/icu4c/source/test/intltest/strtest.h b/icu4c/source/test/intltest/strtest.h index c90d1afa138..9589a728e05 100644 --- a/icu4c/source/test/intltest/strtest.h +++ b/icu4c/source/test/intltest/strtest.h @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2011, International Business Machines Corporation and + * Copyright (c) 1997-2015, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /* file name: strtest.h @@ -45,6 +45,7 @@ private: void TestStringByteSink(); void TestSTLCompatibility(); void TestCharString(); + void TestCStr(); }; #endif -- 2.40.0