2014-01-29 05:08:55 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
2016-01-07 00:20:53 +00:00
|
|
|
* Copyright (C) 2014-2016, International Business Machines
|
2014-01-29 05:08:55 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
******************************************************************************
|
2014-01-29 22:57:06 +00:00
|
|
|
* simplepatternformatter.cpp
|
2014-01-29 05:08:55 +00:00
|
|
|
*/
|
2016-01-07 00:20:53 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/unistr.h"
|
2014-01-29 22:57:06 +00:00
|
|
|
#include "simplepatternformatter.h"
|
2014-01-29 05:08:55 +00:00
|
|
|
#include "uassert.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Argument numbers must be smaller than this limit.
|
|
|
|
* Text segment lengths are offset by this much.
|
|
|
|
* This is currently the only unused char value in compiled patterns,
|
|
|
|
* except it is the maximum value of the first unit (max arg +1).
|
|
|
|
*/
|
|
|
|
const int32_t ARG_NUM_LIMIT = 0x100;
|
|
|
|
/**
|
|
|
|
* Initial and maximum char/UChar value set for a text segment.
|
|
|
|
* Segment length char values are from ARG_NUM_LIMIT+1 to this value here.
|
|
|
|
* Normally 0xffff, but can be as small as ARG_NUM_LIMIT+1 for testing.
|
|
|
|
*/
|
|
|
|
const UChar SEGMENT_LENGTH_PLACEHOLDER_CHAR = 0xffff;
|
|
|
|
/**
|
|
|
|
* Maximum length of a text segment. Longer segments are split into shorter ones.
|
|
|
|
*/
|
|
|
|
const int32_t MAX_SEGMENT_LENGTH = SEGMENT_LENGTH_PLACEHOLDER_CHAR - ARG_NUM_LIMIT;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
APOS = 0x27,
|
|
|
|
DIGIT_ZERO = 0x30,
|
|
|
|
DIGIT_ONE = 0x31,
|
|
|
|
DIGIT_NINE = 0x39,
|
|
|
|
OPEN_BRACE = 0x7b,
|
|
|
|
CLOSE_BRACE = 0x7d
|
2014-12-05 20:52:28 +00:00
|
|
|
};
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
inline UBool isInvalidArray(const void *array, int32_t length) {
|
|
|
|
return (length < 0 || (array == NULL && length != 0));
|
2014-12-05 20:52:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
} // namespace
|
2014-01-29 05:08:55 +00:00
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter &SimplePatternFormatter::operator=(
|
|
|
|
const SimplePatternFormatter& other) {
|
2014-01-29 05:08:55 +00:00
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
compiledPattern = other.compiledPattern;
|
2014-01-29 05:08:55 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
SimplePatternFormatter::~SimplePatternFormatter() {}
|
2014-01-29 05:08:55 +00:00
|
|
|
|
2015-12-16 04:41:33 +00:00
|
|
|
UBool SimplePatternFormatter::compileMinMaxPlaceholders(
|
|
|
|
const UnicodeString &pattern,
|
|
|
|
int32_t min, int32_t max,
|
2016-01-07 00:20:53 +00:00
|
|
|
UErrorCode &errorCode) {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
2014-01-29 05:08:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
// Parse consistent with MessagePattern, but
|
|
|
|
// - support only simple numbered arguments
|
|
|
|
// - build a simple binary structure into the result string
|
2014-01-29 05:08:55 +00:00
|
|
|
const UChar *patternBuffer = pattern.getBuffer();
|
|
|
|
int32_t patternLength = pattern.length();
|
2016-01-07 00:20:53 +00:00
|
|
|
// Reserve the first char for the number of arguments.
|
|
|
|
compiledPattern.setTo((UChar)0);
|
|
|
|
int32_t textLength = 0;
|
|
|
|
int32_t maxArg = -1;
|
|
|
|
UBool inQuote = FALSE;
|
|
|
|
for (int32_t i = 0; i < patternLength;) {
|
|
|
|
UChar c = patternBuffer[i++];
|
|
|
|
if (c == APOS) {
|
|
|
|
if (i < patternLength && (c = patternBuffer[i]) == APOS) {
|
|
|
|
// double apostrophe, skip the second one
|
|
|
|
++i;
|
|
|
|
} else if (inQuote) {
|
|
|
|
// skip the quote-ending apostrophe
|
|
|
|
inQuote = FALSE;
|
|
|
|
continue;
|
|
|
|
} else if (c == OPEN_BRACE || c == CLOSE_BRACE) {
|
|
|
|
// Skip the quote-starting apostrophe, find the end of the quoted literal text.
|
|
|
|
++i;
|
|
|
|
inQuote = TRUE;
|
2014-01-29 05:08:55 +00:00
|
|
|
} else {
|
2016-01-07 00:20:53 +00:00
|
|
|
// The apostrophe is part of literal text.
|
|
|
|
c = APOS;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
} else if (!inQuote && c == OPEN_BRACE) {
|
|
|
|
if (textLength > 0) {
|
|
|
|
compiledPattern.setCharAt(compiledPattern.length() - textLength - 1,
|
|
|
|
(UChar)(ARG_NUM_LIMIT + textLength));
|
|
|
|
textLength = 0;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
int32_t argNumber;
|
|
|
|
if ((i + 1) < patternLength &&
|
|
|
|
0 <= (argNumber = patternBuffer[i] - DIGIT_ZERO) && argNumber <= 9 &&
|
|
|
|
patternBuffer[i + 1] == CLOSE_BRACE) {
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
// Multi-digit argument number (no leading zero) or syntax error.
|
|
|
|
// MessagePattern permits PatternProps.skipWhiteSpace(pattern, index)
|
|
|
|
// around the number, but this class does not.
|
|
|
|
argNumber = -1;
|
|
|
|
if (i < patternLength && DIGIT_ONE <= (c = patternBuffer[i++]) && c <= DIGIT_NINE) {
|
|
|
|
argNumber = c - DIGIT_ZERO;
|
|
|
|
while (i < patternLength &&
|
|
|
|
DIGIT_ZERO <= (c = patternBuffer[i++]) && c <= DIGIT_NINE) {
|
|
|
|
argNumber = argNumber * 10 + (c - DIGIT_ZERO);
|
|
|
|
if (argNumber >= ARG_NUM_LIMIT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argNumber < 0 || c != CLOSE_BRACE) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
2014-01-29 05:08:55 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
if (argNumber > maxArg) {
|
|
|
|
maxArg = argNumber;
|
|
|
|
}
|
|
|
|
compiledPattern.append((UChar)argNumber);
|
|
|
|
continue;
|
|
|
|
} // else: c is part of literal text
|
|
|
|
// Append c and track the literal-text segment length.
|
|
|
|
if (textLength == 0) {
|
|
|
|
// Reserve a char for the length of a new text segment, preset the maximum length.
|
|
|
|
compiledPattern.append(SEGMENT_LENGTH_PLACEHOLDER_CHAR);
|
|
|
|
}
|
|
|
|
compiledPattern.append(c);
|
|
|
|
if (++textLength == MAX_SEGMENT_LENGTH) {
|
|
|
|
textLength = 0;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
if (textLength > 0) {
|
|
|
|
compiledPattern.setCharAt(compiledPattern.length() - textLength - 1,
|
|
|
|
(UChar)(ARG_NUM_LIMIT + textLength));
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
int32_t argCount = maxArg + 1;
|
|
|
|
if (argCount < min || max < argCount) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
2015-12-16 04:41:33 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
compiledPattern.setCharAt(0, (UChar)argCount);
|
2014-01-29 05:08:55 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:57:06 +00:00
|
|
|
UnicodeString& SimplePatternFormatter::format(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString &value0,
|
|
|
|
UnicodeString &appendTo, UErrorCode &errorCode) const {
|
|
|
|
const UnicodeString *values[] = { &value0 };
|
|
|
|
return formatAndAppend(values, 1, appendTo, NULL, 0, errorCode);
|
2014-01-29 22:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& SimplePatternFormatter::format(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString &value0,
|
|
|
|
const UnicodeString &value1,
|
|
|
|
UnicodeString &appendTo, UErrorCode &errorCode) const {
|
|
|
|
const UnicodeString *values[] = { &value0, &value1 };
|
|
|
|
return formatAndAppend(values, 2, appendTo, NULL, 0, errorCode);
|
2014-01-29 22:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& SimplePatternFormatter::format(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString &value0,
|
|
|
|
const UnicodeString &value1,
|
|
|
|
const UnicodeString &value2,
|
|
|
|
UnicodeString &appendTo, UErrorCode &errorCode) const {
|
|
|
|
const UnicodeString *values[] = { &value0, &value1, &value2 };
|
|
|
|
return formatAndAppend(values, 3, appendTo, NULL, 0, errorCode);
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 20:52:28 +00:00
|
|
|
UnicodeString& SimplePatternFormatter::formatAndAppend(
|
2016-01-07 00:20:53 +00:00
|
|
|
const UnicodeString *const *values, int32_t valuesLength,
|
2014-01-29 05:08:55 +00:00
|
|
|
UnicodeString &appendTo,
|
2016-01-07 00:20:53 +00:00
|
|
|
int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
2014-12-05 20:52:28 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
if (isInvalidArray(values, valuesLength) || isInvalidArray(offsets, offsetsLength) ||
|
|
|
|
valuesLength < getPlaceholderCount()) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
2014-01-29 05:08:55 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
return format(compiledPattern.getBuffer(), compiledPattern.length(), values,
|
|
|
|
appendTo, NULL, TRUE,
|
|
|
|
offsets, offsetsLength, errorCode);
|
2014-12-05 20:52:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
UnicodeString &SimplePatternFormatter::formatAndReplace(
|
|
|
|
const UnicodeString *const *values, int32_t valuesLength,
|
2014-12-05 20:52:28 +00:00
|
|
|
UnicodeString &result,
|
2016-01-07 00:20:53 +00:00
|
|
|
int32_t *offsets, int32_t offsetsLength, UErrorCode &errorCode) const {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
2014-12-05 20:52:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
if (isInvalidArray(values, valuesLength) || isInvalidArray(offsets, offsetsLength)) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
2014-12-05 20:52:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
const UChar *cp = compiledPattern.getBuffer();
|
|
|
|
int32_t cpLength = compiledPattern.length();
|
|
|
|
if (valuesLength < getPlaceholderCount(cp, cpLength)) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
2014-12-05 20:52:28 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
// If the pattern starts with an argument whose value is the same object
|
|
|
|
// as the result, then we keep the result contents and append to it.
|
|
|
|
// Otherwise we replace its contents.
|
|
|
|
int32_t firstArg = -1;
|
|
|
|
// If any non-initial argument value is the same object as the result,
|
|
|
|
// then we first copy its contents and use that instead while formatting.
|
|
|
|
UnicodeString resultCopy;
|
|
|
|
if (getPlaceholderCount(cp, cpLength) > 0) {
|
|
|
|
for (int32_t i = 1; i < cpLength;) {
|
|
|
|
int32_t n = cp[i++];
|
|
|
|
if (n < ARG_NUM_LIMIT) {
|
|
|
|
if (values[n] == &result) {
|
|
|
|
if (i == 2) {
|
|
|
|
firstArg = n;
|
|
|
|
} else if (resultCopy.isEmpty() && !result.isEmpty()) {
|
|
|
|
resultCopy = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i += n - ARG_NUM_LIMIT;
|
|
|
|
}
|
2014-12-05 20:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
if (firstArg < 0) {
|
|
|
|
result.remove();
|
2014-12-05 20:52:28 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
return format(cp, cpLength, values,
|
|
|
|
result, &resultCopy, FALSE,
|
|
|
|
offsets, offsetsLength, errorCode);
|
2014-12-05 20:52:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
UnicodeString SimplePatternFormatter::getTextWithNoPlaceholders(
|
|
|
|
const UChar *compiledPattern, int32_t compiledPatternLength) {
|
|
|
|
int32_t capacity = compiledPatternLength - 1 -
|
|
|
|
getPlaceholderCount(compiledPattern, compiledPatternLength);
|
|
|
|
UnicodeString sb(capacity, 0, 0); // Java: StringBuilder
|
|
|
|
for (int32_t i = 1; i < compiledPatternLength;) {
|
|
|
|
int32_t segmentLength = compiledPattern[i++] - ARG_NUM_LIMIT;
|
|
|
|
if (segmentLength > 0) {
|
|
|
|
sb.append(compiledPattern + i, segmentLength);
|
|
|
|
i += segmentLength;
|
2014-12-05 20:52:28 +00:00
|
|
|
}
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
return sb;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 00:20:53 +00:00
|
|
|
UnicodeString &SimplePatternFormatter::format(
|
|
|
|
const UChar *compiledPattern, int32_t compiledPatternLength,
|
|
|
|
const UnicodeString *const *values,
|
|
|
|
UnicodeString &result, const UnicodeString *resultCopy, UBool forbidResultAsValue,
|
|
|
|
int32_t *offsets, int32_t offsetsLength,
|
|
|
|
UErrorCode &errorCode) {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return result;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
for (int32_t i = 0; i < offsetsLength; i++) {
|
|
|
|
offsets[i] = -1;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
for (int32_t i = 1; i < compiledPatternLength;) {
|
|
|
|
int32_t n = compiledPattern[i++];
|
|
|
|
if (n < ARG_NUM_LIMIT) {
|
|
|
|
const UnicodeString *value = values[n];
|
|
|
|
if (value == NULL) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (value == &result) {
|
|
|
|
if (forbidResultAsValue) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (i == 2) {
|
|
|
|
// We are appending to result which is also the first value object.
|
|
|
|
if (n < offsetsLength) {
|
|
|
|
offsets[n] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (n < offsetsLength) {
|
|
|
|
offsets[n] = result.length();
|
|
|
|
}
|
|
|
|
result.append(*resultCopy);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (n < offsetsLength) {
|
|
|
|
offsets[n] = result.length();
|
|
|
|
}
|
|
|
|
result.append(*value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int32_t length = n - ARG_NUM_LIMIT;
|
|
|
|
result.append(compiledPattern + i, length);
|
|
|
|
i += length;
|
|
|
|
}
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
2016-01-07 00:20:53 +00:00
|
|
|
return result;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|