intl/icu/source/i18n/fpositer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/i18n/fpositer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 +******************************************************************************
     1.6 +* Copyright (C) 2009-2012, International Business Machines Corporation and
     1.7 +* others. All Rights Reserved.
     1.8 +******************************************************************************
     1.9 +*   Date        Name        Description
    1.10 +*   12/14/09    doug        Creation.
    1.11 +******************************************************************************
    1.12 +*/
    1.13 +
    1.14 +#include "unicode/utypes.h"
    1.15 +
    1.16 +#if !UCONFIG_NO_FORMATTING
    1.17 +
    1.18 +#include "unicode/fpositer.h"
    1.19 +#include "cmemory.h"
    1.20 +#include "uvectr32.h"
    1.21 +
    1.22 +U_NAMESPACE_BEGIN
    1.23 +
    1.24 +FieldPositionIterator::~FieldPositionIterator() {
    1.25 +  delete data;
    1.26 +  data = NULL;
    1.27 +  pos = -1;
    1.28 +}
    1.29 +
    1.30 +FieldPositionIterator::FieldPositionIterator()
    1.31 +    : data(NULL), pos(-1) {
    1.32 +}
    1.33 +
    1.34 +FieldPositionIterator::FieldPositionIterator(const FieldPositionIterator &rhs)
    1.35 +  : UObject(rhs), data(NULL), pos(rhs.pos) {
    1.36 +
    1.37 +  if (rhs.data) {
    1.38 +    UErrorCode status = U_ZERO_ERROR;
    1.39 +    data = new UVector32(status);
    1.40 +    data->assign(*rhs.data, status);
    1.41 +    if (status != U_ZERO_ERROR) {
    1.42 +      delete data;
    1.43 +      data = NULL;
    1.44 +      pos = -1;
    1.45 +    }
    1.46 +  }
    1.47 +}
    1.48 +
    1.49 +UBool FieldPositionIterator::operator==(const FieldPositionIterator &rhs) const {
    1.50 +  if (&rhs == this) {
    1.51 +    return TRUE;
    1.52 +  }
    1.53 +  if (pos != rhs.pos) {
    1.54 +    return FALSE;
    1.55 +  }
    1.56 +  if (!data) {
    1.57 +    return rhs.data == NULL;
    1.58 +  }
    1.59 +  return rhs.data ? data->operator==(*rhs.data) : FALSE;
    1.60 +}
    1.61 +
    1.62 +void FieldPositionIterator::setData(UVector32 *adopt, UErrorCode& status) {
    1.63 +  // Verify that adopt has valid data, and update status if it doesn't.
    1.64 +  if (U_SUCCESS(status)) {
    1.65 +    if (adopt) {
    1.66 +      if ((adopt->size() % 3) != 0) {
    1.67 +        status = U_ILLEGAL_ARGUMENT_ERROR;
    1.68 +      } else {
    1.69 +        for (int i = 1; i < adopt->size(); i += 3) {
    1.70 +          if (adopt->elementAti(i) >= adopt->elementAti(i+1)) {
    1.71 +            status = U_ILLEGAL_ARGUMENT_ERROR;
    1.72 +            break;
    1.73 +          }
    1.74 +        }
    1.75 +      }
    1.76 +    }
    1.77 +  }
    1.78 +
    1.79 +  // We own the data, even if status is in error, so we need to delete it now
    1.80 +  // if we're not keeping track of it.
    1.81 +  if (!U_SUCCESS(status)) {
    1.82 +    delete adopt;
    1.83 +    return;
    1.84 +  }
    1.85 +
    1.86 +  delete data;
    1.87 +  data = adopt;
    1.88 +  pos = adopt == NULL ? -1 : 0;
    1.89 +}
    1.90 +
    1.91 +UBool FieldPositionIterator::next(FieldPosition& fp) {
    1.92 +  if (pos == -1) {
    1.93 +    return FALSE;
    1.94 +  }
    1.95 +
    1.96 +  fp.setField(data->elementAti(pos++));
    1.97 +  fp.setBeginIndex(data->elementAti(pos++));
    1.98 +  fp.setEndIndex(data->elementAti(pos++));
    1.99 +
   1.100 +  if (pos == data->size()) {
   1.101 +    pos = -1;
   1.102 +  }
   1.103 +
   1.104 +  return TRUE;
   1.105 +}
   1.106 +
   1.107 +U_NAMESPACE_END
   1.108 +
   1.109 +#endif /* #if !UCONFIG_NO_FORMATTING */
   1.110 +

mercurial