2016-06-15 18:58:17 +00:00
|
|
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2010-01-19 19:25:25 +00:00
|
|
|
/********************************************************************
|
2010-10-20 02:36:31 +00:00
|
|
|
* COPYRIGHT:
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (c) 1997-2012, International Business Machines Corporation and
|
|
|
|
* others. All Rights Reserved.
|
|
|
|
* Copyright (C) 2010 , Yahoo! Inc.
|
2010-01-19 19:25:25 +00:00
|
|
|
********************************************************************
|
|
|
|
*
|
|
|
|
* File SELFMT.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 11/11/09 kirtig Finished first cut of implementation.
|
2010-10-20 02:36:31 +00:00
|
|
|
* 11/16/09 kirtig Improved version
|
2010-01-19 19:25:25 +00:00
|
|
|
********************************************************************/
|
|
|
|
|
2012-06-28 23:28:00 +00:00
|
|
|
#include "utypeinfo.h" // for 'typeid' to work
|
2010-05-19 17:29:33 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/messagepattern.h"
|
|
|
|
#include "unicode/rbnf.h"
|
|
|
|
#include "unicode/selfmt.h"
|
2010-01-19 19:25:25 +00:00
|
|
|
#include "unicode/uchar.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/ucnv_err.h"
|
2010-01-19 19:25:25 +00:00
|
|
|
#include "unicode/umsg.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/utypes.h"
|
2010-01-19 19:25:25 +00:00
|
|
|
#include "cmemory.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "messageimpl.h"
|
|
|
|
#include "patternprops.h"
|
|
|
|
#include "selfmtimpl.h"
|
2010-01-19 19:25:25 +00:00
|
|
|
#include "uassert.h"
|
|
|
|
#include "ustrfmt.h"
|
2011-04-25 20:47:32 +00:00
|
|
|
#include "util.h"
|
2010-01-19 19:25:25 +00:00
|
|
|
#include "uvector.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SelectFormat)
|
|
|
|
|
|
|
|
static const UChar SELECT_KEYWORD_OTHER[] = {LOW_O, LOW_T, LOW_H, LOW_E, LOW_R, 0};
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
SelectFormat::SelectFormat(const UnicodeString& pat,
|
|
|
|
UErrorCode& status) : msgPattern(status) {
|
2010-01-19 19:25:25 +00:00
|
|
|
applyPattern(pat, status);
|
|
|
|
}
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
SelectFormat::SelectFormat(const SelectFormat& other) : Format(other),
|
|
|
|
msgPattern(other.msgPattern) {
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SelectFormat::~SelectFormat() {
|
2010-10-20 02:36:31 +00:00
|
|
|
}
|
2010-01-19 19:25:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
SelectFormat::applyPattern(const UnicodeString& newPattern, UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
2010-10-20 02:36:31 +00:00
|
|
|
}
|
2010-01-19 19:25:25 +00:00
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern.parseSelectStyle(newPattern, NULL, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
msgPattern.clear();
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SelectFormat::format(const Formattable& obj,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (obj.getType() == Formattable::kString) {
|
|
|
|
return format(obj.getString(status), appendTo, pos, status);
|
|
|
|
} else {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2010-01-19 19:25:25 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2010-02-01 23:32:57 +00:00
|
|
|
SelectFormat::format(const UnicodeString& keyword,
|
2010-10-20 02:36:31 +00:00
|
|
|
UnicodeString& appendTo,
|
2010-07-12 18:03:29 +00:00
|
|
|
FieldPosition& /*pos */,
|
2010-01-19 19:25:25 +00:00
|
|
|
UErrorCode& status) const {
|
2011-04-25 20:47:32 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2010-01-19 19:25:25 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// Check for the validity of the keyword
|
|
|
|
if (!PatternProps::isIdentifier(keyword.getBuffer(), keyword.length())) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR; // Invalid formatting argument.
|
|
|
|
}
|
|
|
|
if (msgPattern.countParts() == 0) {
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
2010-01-19 19:25:25 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t msgStart = findSubMessage(msgPattern, 0, keyword, status);
|
|
|
|
if (!MessageImpl::jdkAposMode(msgPattern)) {
|
|
|
|
int32_t patternStart = msgPattern.getPart(msgStart).getLimit();
|
|
|
|
int32_t msgLimit = msgPattern.getLimitPartIndex(msgStart);
|
|
|
|
appendTo.append(msgPattern.getPatternString(),
|
|
|
|
patternStart,
|
|
|
|
msgPattern.getPatternIndex(msgLimit) - patternStart);
|
|
|
|
return appendTo;
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// JDK compatibility mode: Remove SKIP_SYNTAX.
|
|
|
|
return MessageImpl::appendSubMessageWithoutSkipSyntax(msgPattern, msgStart, appendTo);
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
SelectFormat::toPattern(UnicodeString& appendTo) {
|
2011-04-25 20:47:32 +00:00
|
|
|
if (0 == msgPattern.countParts()) {
|
|
|
|
appendTo.setToBogus();
|
|
|
|
} else {
|
|
|
|
appendTo.append(msgPattern.getPatternString());
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
return appendTo;
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-25 20:47:32 +00:00
|
|
|
int32_t SelectFormat::findSubMessage(const MessagePattern& pattern, int32_t partIndex,
|
|
|
|
const UnicodeString& keyword, UErrorCode& ec) {
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return 0;
|
2010-01-30 00:11:31 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
UnicodeString other(FALSE, SELECT_KEYWORD_OTHER, 5);
|
|
|
|
int32_t count = pattern.countParts();
|
|
|
|
int32_t msgStart=0;
|
|
|
|
// Iterate over (ARG_SELECTOR, message) pairs until ARG_LIMIT or end of select-only pattern.
|
|
|
|
do {
|
|
|
|
const MessagePattern::Part& part=pattern.getPart(partIndex++);
|
|
|
|
const UMessagePatternPartType type=part.getType();
|
|
|
|
if(type==UMSGPAT_PART_TYPE_ARG_LIMIT) {
|
|
|
|
break;
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
// part is an ARG_SELECTOR followed by a message
|
|
|
|
if(pattern.partSubstringMatches(part, keyword)) {
|
|
|
|
// keyword matches
|
|
|
|
return partIndex;
|
|
|
|
} else if(msgStart==0 && pattern.partSubstringMatches(part, other)) {
|
|
|
|
msgStart=partIndex;
|
|
|
|
}
|
|
|
|
partIndex=pattern.getLimitPartIndex(partIndex);
|
|
|
|
} while(++partIndex<count);
|
|
|
|
return msgStart;
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Format* SelectFormat::clone() const
|
|
|
|
{
|
|
|
|
return new SelectFormat(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectFormat&
|
|
|
|
SelectFormat::operator=(const SelectFormat& other) {
|
|
|
|
if (this != &other) {
|
2011-04-25 20:47:32 +00:00
|
|
|
msgPattern = other.msgPattern;
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
SelectFormat::operator==(const Format& other) const {
|
2011-04-25 20:47:32 +00:00
|
|
|
if (this == &other) {
|
2010-02-01 23:32:57 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2011-04-25 20:47:32 +00:00
|
|
|
if (!Format::operator==(other)) {
|
2010-01-19 19:25:25 +00:00
|
|
|
return FALSE;
|
2011-04-25 20:47:32 +00:00
|
|
|
}
|
|
|
|
const SelectFormat& o = (const SelectFormat&)other;
|
|
|
|
return msgPattern == o.msgPattern;
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
SelectFormat::operator!=(const Format& other) const {
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectFormat::parseObject(const UnicodeString& /*source*/,
|
|
|
|
Formattable& /*result*/,
|
2010-02-01 23:32:57 +00:00
|
|
|
ParsePosition& pos) const
|
2010-01-19 19:25:25 +00:00
|
|
|
{
|
2011-04-25 20:47:32 +00:00
|
|
|
// Parsing not supported.
|
2010-02-01 23:32:57 +00:00
|
|
|
pos.setErrorIndex(pos.getIndex());
|
2010-01-19 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
|
|
|
//eof
|