ICU-11857 Add CStr class for easier UnicodeString to char * conversions.

X-SVN-Rev: 38054
This commit is contained in:
Andy Heninger 2015-10-09 17:53:44 +00:00
parent 21720c5627
commit 0464cceedf
8 changed files with 117 additions and 2 deletions

View file

@ -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 \

View file

@ -439,6 +439,7 @@
<ClCompile Include="chariter.cpp" />
<ClCompile Include="charstr.cpp" />
<ClCompile Include="cstring.c" />
<ClCompile Include="cstr.cpp" />
<ClCompile Include="cwchar.c" />
<ClCompile Include="messagepattern.cpp" />
<ClCompile Include="schriter.cpp" />
@ -1449,6 +1450,7 @@
</CustomBuild>
<ClInclude Include="charstr.h" />
<ClInclude Include="cstring.h" />
<ClInclude Include="cstr.h" />
<ClInclude Include="cwchar.h" />
<CustomBuild Include="unicode\messagepattern.h">
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">copy "%(FullPath)" ..\..\include\unicode

View file

@ -463,6 +463,9 @@
<ClCompile Include="cstring.c">
<Filter>strings</Filter>
</ClCompile>
<ClCompile Include="cstr.cpp">
<Filter>strings</Filter>
</ClCompile>
<ClCompile Include="cwchar.c">
<Filter>strings</Filter>
</ClCompile>
@ -829,6 +832,9 @@
<ClInclude Include="cstring.h">
<Filter>strings</Filter>
</ClInclude>
<ClInclude Include="cstr.h">
<Filter>strings</Filter>
</ClInclude>
<ClInclude Include="cwchar.h">
<Filter>strings</Filter>
</ClInclude>

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)());
}
}

View file

@ -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