| OLD | NEW |
| 1 /* | 1 /* |
| 2 ****************************************************************************** | 2 ****************************************************************************** |
| 3 * Copyright (C) 2014, International Business Machines | 3 * Copyright (C) 2014-2015, International Business Machines |
| 4 * Corporation and others. All Rights Reserved. | 4 * Corporation and others. All Rights Reserved. |
| 5 ****************************************************************************** | 5 ****************************************************************************** |
| 6 * simplepatternformatter.cpp | 6 * simplepatternformatter.cpp |
| 7 */ | 7 */ |
| 8 #include "simplepatternformatter.h" | 8 #include "simplepatternformatter.h" |
| 9 #include "cstring.h" | 9 #include "cstring.h" |
| 10 #include "uassert.h" | 10 #include "uassert.h" |
| 11 | 11 |
| 12 U_NAMESPACE_BEGIN | 12 U_NAMESPACE_BEGIN |
| 13 | 13 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 SimplePatternFormatter::SimplePatternFormatter(const UnicodeString &pattern) : | 142 SimplePatternFormatter::SimplePatternFormatter(const UnicodeString &pattern) : |
| 143 noPlaceholders(), | 143 noPlaceholders(), |
| 144 placeholders(), | 144 placeholders(), |
| 145 placeholderSize(0), | 145 placeholderSize(0), |
| 146 placeholderCount(0), | 146 placeholderCount(0), |
| 147 firstPlaceholderReused(FALSE) { | 147 firstPlaceholderReused(FALSE) { |
| 148 UErrorCode status = U_ZERO_ERROR; | 148 UErrorCode status = U_ZERO_ERROR; |
| 149 compile(pattern, status); | 149 compile(pattern, status); |
| 150 } | 150 } |
| 151 | 151 |
| 152 SimplePatternFormatter::SimplePatternFormatter(const UnicodeString &pattern, |
| 153 int32_t min, int32_t max, |
| 154 UErrorCode &errorCode) |
| 155 : noPlaceholders(), |
| 156 placeholders(), |
| 157 placeholderSize(0), |
| 158 placeholderCount(0), |
| 159 firstPlaceholderReused(FALSE) { |
| 160 compileMinMaxPlaceholders(pattern, min, max, errorCode); |
| 161 } |
| 162 |
| 152 SimplePatternFormatter::SimplePatternFormatter( | 163 SimplePatternFormatter::SimplePatternFormatter( |
| 153 const SimplePatternFormatter &other) : | 164 const SimplePatternFormatter &other) : |
| 154 noPlaceholders(other.noPlaceholders), | 165 noPlaceholders(other.noPlaceholders), |
| 155 placeholders(), | 166 placeholders(), |
| 156 placeholderSize(0), | 167 placeholderSize(0), |
| 157 placeholderCount(other.placeholderCount), | 168 placeholderCount(other.placeholderCount), |
| 158 firstPlaceholderReused(other.firstPlaceholderReused) { | 169 firstPlaceholderReused(other.firstPlaceholderReused) { |
| 159 placeholderSize = ensureCapacity(other.placeholderSize); | 170 placeholderSize = ensureCapacity(other.placeholderSize); |
| 160 uprv_memcpy( | 171 uprv_memcpy( |
| 161 placeholders.getAlias(), | 172 placeholders.getAlias(), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 175 uprv_memcpy( | 186 uprv_memcpy( |
| 176 placeholders.getAlias(), | 187 placeholders.getAlias(), |
| 177 other.placeholders.getAlias(), | 188 other.placeholders.getAlias(), |
| 178 placeholderSize * sizeof(PlaceholderInfo)); | 189 placeholderSize * sizeof(PlaceholderInfo)); |
| 179 return *this; | 190 return *this; |
| 180 } | 191 } |
| 181 | 192 |
| 182 SimplePatternFormatter::~SimplePatternFormatter() { | 193 SimplePatternFormatter::~SimplePatternFormatter() { |
| 183 } | 194 } |
| 184 | 195 |
| 185 UBool SimplePatternFormatter::compile( | 196 UBool SimplePatternFormatter::compileMinMaxPlaceholders( |
| 186 const UnicodeString &pattern, UErrorCode &status) { | 197 const UnicodeString &pattern, |
| 198 int32_t min, int32_t max, |
| 199 UErrorCode &status) { |
| 187 if (U_FAILURE(status)) { | 200 if (U_FAILURE(status)) { |
| 188 return FALSE; | 201 return FALSE; |
| 189 } | 202 } |
| 190 const UChar *patternBuffer = pattern.getBuffer(); | 203 const UChar *patternBuffer = pattern.getBuffer(); |
| 191 int32_t patternLength = pattern.length(); | 204 int32_t patternLength = pattern.length(); |
| 192 UChar *buffer = noPlaceholders.getBuffer(patternLength); | 205 UChar *buffer = noPlaceholders.getBuffer(patternLength); |
| 193 int32_t len = 0; | 206 int32_t len = 0; |
| 194 placeholderSize = 0; | 207 placeholderSize = 0; |
| 195 placeholderCount = 0; | 208 placeholderCount = 0; |
| 196 SimplePatternFormatterCompileState state = INIT; | 209 SimplePatternFormatterCompileState state = INIT; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 217 buffer[len++] = 0x27; | 230 buffer[len++] = 0x27; |
| 218 buffer[len++] = ch; | 231 buffer[len++] = ch; |
| 219 } | 232 } |
| 220 state = INIT; | 233 state = INIT; |
| 221 break; | 234 break; |
| 222 case PLACEHOLDER: | 235 case PLACEHOLDER: |
| 223 if (ch >= 0x30 && ch <= 0x39) { | 236 if (ch >= 0x30 && ch <= 0x39) { |
| 224 idBuilder.add(ch); | 237 idBuilder.add(ch); |
| 225 } else if (ch == 0x7D && idBuilder.isValid()) { | 238 } else if (ch == 0x7D && idBuilder.isValid()) { |
| 226 if (!addPlaceholder(idBuilder.getId(), len)) { | 239 if (!addPlaceholder(idBuilder.getId(), len)) { |
| 240 noPlaceholders.releaseBuffer(0); |
| 227 status = U_MEMORY_ALLOCATION_ERROR; | 241 status = U_MEMORY_ALLOCATION_ERROR; |
| 228 return FALSE; | 242 return FALSE; |
| 229 } | 243 } |
| 230 state = INIT; | 244 state = INIT; |
| 231 } else { | 245 } else { |
| 232 buffer[len++] = 0x7B; | 246 buffer[len++] = 0x7B; |
| 233 idBuilder.appendTo(buffer, &len); | 247 idBuilder.appendTo(buffer, &len); |
| 234 buffer[len++] = ch; | 248 buffer[len++] = ch; |
| 235 state = INIT; | 249 state = INIT; |
| 236 } | 250 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 248 break; | 262 break; |
| 249 case PLACEHOLDER: | 263 case PLACEHOLDER: |
| 250 buffer[len++] = 0X7B; | 264 buffer[len++] = 0X7B; |
| 251 idBuilder.appendTo(buffer, &len); | 265 idBuilder.appendTo(buffer, &len); |
| 252 break; | 266 break; |
| 253 default: | 267 default: |
| 254 U_ASSERT(false); | 268 U_ASSERT(false); |
| 255 break; | 269 break; |
| 256 } | 270 } |
| 257 noPlaceholders.releaseBuffer(len); | 271 noPlaceholders.releaseBuffer(len); |
| 272 if (placeholderCount < min || max < placeholderCount) { |
| 273 status = U_ILLEGAL_ARGUMENT_ERROR; |
| 274 return FALSE; |
| 275 } |
| 258 return TRUE; | 276 return TRUE; |
| 259 } | 277 } |
| 260 | 278 |
| 261 UnicodeString& SimplePatternFormatter::format( | 279 UnicodeString& SimplePatternFormatter::format( |
| 262 const UnicodeString &arg0, | 280 const UnicodeString &arg0, |
| 263 UnicodeString &appendTo, | 281 UnicodeString &appendTo, |
| 264 UErrorCode &status) const { | 282 UErrorCode &status) const { |
| 265 const UnicodeString *params[] = {&arg0}; | 283 const UnicodeString *params[] = {&arg0}; |
| 266 return formatAndAppend( | 284 return formatAndAppend( |
| 267 params, | 285 params, |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 placeholderCount = id + 1; | 529 placeholderCount = id + 1; |
| 512 } | 530 } |
| 513 if (placeholderSize > 1 | 531 if (placeholderSize > 1 |
| 514 && placeholders[placeholderSize - 1].id == placeholders[0].id) { | 532 && placeholders[placeholderSize - 1].id == placeholders[0].id) { |
| 515 firstPlaceholderReused = TRUE; | 533 firstPlaceholderReused = TRUE; |
| 516 } | 534 } |
| 517 return TRUE; | 535 return TRUE; |
| 518 } | 536 } |
| 519 | 537 |
| 520 U_NAMESPACE_END | 538 U_NAMESPACE_END |
| OLD | NEW |