ICU-5830 Improve reading of UnicodeStrings with operator>>

X-SVN-Rev: 22579
This commit is contained in:
George Rhoten 2007-08-30 23:13:07 +00:00
parent ffc55edf36
commit ca7379f1a3
2 changed files with 46 additions and 6 deletions
icu4c/source

View file

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (C) 2001-2006, International Business Machines
* Copyright (C) 2001-2007, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* FILE NAME : ustream.cpp
@ -88,18 +88,25 @@ operator>>(STD_ISTREAM& stream, UnicodeString& str)
char ch;
UChar ch32;
UBool intialWhitespace = TRUE;
UBool continueReading = TRUE;
/* We need to consume one byte at a time to see what is considered whitespace. */
while (!stream.eof()) {
while (continueReading) {
ch = stream.get();
sLimit = &ch + 1;
errorCode = U_ZERO_ERROR;
if (stream.eof()) {
// The EOF is only set after the get() of an unavailable byte.
stream.clear(STD_NAMESPACE ios_base::eofbit);
continueReading = FALSE;
}
sLimit = &ch + (int)continueReading;
us = uBuffer;
s = &ch;
ucnv_toUnicode(converter, &us, uLimit, &s, sLimit, 0, FALSE, &errorCode);
errorCode = U_ZERO_ERROR;
ucnv_toUnicode(converter, &us, uLimit, &s, sLimit, 0, !continueReading, &errorCode);
if(U_FAILURE(errorCode)) {
/* Something really bad happened */
return stream;
stream.setstate(STD_NAMESPACE ios_base::failbit);
goto STOP_READING;
}
/* Was the character consumed? */
if (us != uBuffer) {

View file

@ -28,9 +28,11 @@
// <strstream> is deprecated on some platforms, and the compiler complains very loudly if you use it.
#include <strstream>
#endif
#include <fstream>
using namespace std;
#elif U_IOSTREAM_SOURCE >= 198506
#include <strstream.h>
#include <fstream.h>
#endif
#include <string.h>
@ -127,9 +129,40 @@ static void U_CALLCONV TestStream(void)
log_info("U_IOSTREAM_SOURCE is disabled\n");
#endif
}
static void U_CALLCONV TestStreamEOF(void)
{
UnicodeString dest;
fstream fs(STANDARD_TEST_FILE, fstream::in | fstream::out | fstream::trunc);
#ifdef USE_SSTREAM
stringstream ss;
#else
strstream ss;
#endif
fs << "EXAMPLE";
fs.seekg(0);
ss << "EXAMPLE";
if (!(fs >> dest)) {
log_err("Reading of file did not return expected status result\n");
}
if (dest != "EXAMPLE") {
log_err("Reading of file did not return expected string\n");
}
if (!(ss >> dest)) {
log_err("Reading of string did not return expected status result\n");
}
if (dest != "EXAMPLE") {
log_err("Reading of string did not return expected string\n");
}
fs.close();
}
U_CDECL_END
U_CFUNC void addStreamTests(TestNode** root) {
addTest(root, &TestStream, "stream/TestStream");
addTest(root, &TestStreamEOF, "stream/TestStreamEOF");
}